all repos — mgba @ ce647c8613f53d30a8cece2f380def41fc041ffa

mGBA Game Boy Advance Emulator

src/gba/sio/lockstep.c (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "lockstep.h"
  7
  8#include "gba/gba.h"
  9#include "gba/io.h"
 10
 11#define LOCKSTEP_INCREMENT 2048
 12
 13static bool GBASIOLockstepNodeInit(struct GBASIODriver* driver);
 14static void GBASIOLockstepNodeDeinit(struct GBASIODriver* driver);
 15static bool GBASIOLockstepNodeLoad(struct GBASIODriver* driver);
 16static bool GBASIOLockstepNodeUnload(struct GBASIODriver* driver);
 17static uint16_t GBASIOLockstepNodeWriteRegister(struct GBASIODriver* driver, uint32_t address, uint16_t value);
 18static int32_t GBASIOLockstepNodeProcessEvents(struct GBASIODriver* driver, int32_t cycles);
 19
 20void GBASIOLockstepInit(struct GBASIOLockstep* lockstep) {
 21	lockstep->players[0] = 0;
 22	lockstep->players[1] = 0;
 23	lockstep->players[2] = 0;
 24	lockstep->players[3] = 0;
 25	lockstep->multiRecv[0] = 0xFFFF;
 26	lockstep->multiRecv[1] = 0xFFFF;
 27	lockstep->multiRecv[2] = 0xFFFF;
 28	lockstep->multiRecv[3] = 0xFFFF;
 29	lockstep->attached = 0;
 30	lockstep->loaded = 0;
 31	lockstep->transferActive = false;
 32	lockstep->waiting = 0;
 33	lockstep->nextEvent = LOCKSTEP_INCREMENT;
 34	ConditionInit(&lockstep->barrier);
 35	MutexInit(&lockstep->mutex);
 36}
 37
 38void GBASIOLockstepDeinit(struct GBASIOLockstep* lockstep) {
 39	ConditionDeinit(&lockstep->barrier);
 40	MutexDeinit(&lockstep->mutex);
 41}
 42
 43void GBASIOLockstepNodeCreate(struct GBASIOLockstepNode* node) {
 44	node->d.init = GBASIOLockstepNodeInit;
 45	node->d.deinit = GBASIOLockstepNodeDeinit;
 46	node->d.load = GBASIOLockstepNodeLoad;
 47	node->d.unload = GBASIOLockstepNodeUnload;
 48	node->d.writeRegister = GBASIOLockstepNodeWriteRegister;
 49	node->d.processEvents = GBASIOLockstepNodeProcessEvents;
 50}
 51
 52bool GBASIOLockstepAttachNode(struct GBASIOLockstep* lockstep, struct GBASIOLockstepNode* node) {
 53	if (lockstep->attached == MAX_GBAS) {
 54		return false;
 55	}
 56	lockstep->players[lockstep->attached] = node;
 57	node->p = lockstep;
 58	node->id = lockstep->attached;
 59	++lockstep->attached;
 60	return true;
 61}
 62
 63void GBASIOLockstepDetachNode(struct GBASIOLockstep* lockstep, struct GBASIOLockstepNode* node) {
 64	if (lockstep->attached == 0) {
 65		return;
 66	}
 67	int i;
 68	for (i = 0; i < lockstep->attached; ++i) {
 69		if (lockstep->players[i] != node) {
 70			continue;
 71		}
 72		for (++i; i < lockstep->attached; ++i) {
 73			lockstep->players[i - 1] = lockstep->players[i];
 74			lockstep->players[i - 1]->id = i - 1;
 75		}
 76		--lockstep->attached;
 77		break;
 78	}
 79}
 80
 81bool GBASIOLockstepNodeInit(struct GBASIODriver* driver) {
 82	struct GBASIOLockstepNode* node = (struct GBASIOLockstepNode*) driver;
 83	node->nextEvent = LOCKSTEP_INCREMENT;
 84	node->d.p->multiplayerControl.slave = node->id > 0;
 85	return true;
 86}
 87
 88void GBASIOLockstepNodeDeinit(struct GBASIODriver* driver) {
 89	UNUSED(driver);
 90}
 91
 92bool GBASIOLockstepNodeLoad(struct GBASIODriver* driver) {
 93	struct GBASIOLockstepNode* node = (struct GBASIOLockstepNode*) driver;
 94	node->state = LOCKSTEP_IDLE;
 95	MutexLock(&node->p->mutex);
 96	++node->p->loaded;
 97	node->d.p->rcnt |= 3;
 98	if (node->id) {
 99		node->d.p->rcnt |= 4;
100		node->d.p->multiplayerControl.slave = 1;
101	}
102	MutexUnlock(&node->p->mutex);
103	return true;
104}
105
106bool GBASIOLockstepNodeUnload(struct GBASIODriver* driver) {
107	struct GBASIOLockstepNode* node = (struct GBASIOLockstepNode*) driver;
108	MutexLock(&node->p->mutex);
109	--node->p->loaded;
110	ConditionWake(&node->p->barrier);
111	MutexUnlock(&node->p->mutex);
112	return true;
113}
114
115static uint16_t GBASIOLockstepNodeWriteRegister(struct GBASIODriver* driver, uint32_t address, uint16_t value) {
116	struct GBASIOLockstepNode* node = (struct GBASIOLockstepNode*) driver;
117	if (address == REG_SIOCNT) {
118		if (value & 0x0080) {
119			value &= ~0x0080;
120			if (!node->id) {
121				MutexLock(&node->p->mutex);
122				node->p->transferActive = true;
123				node->p->transferCycles = GBASIOCyclesPerTransfer[node->d.p->multiplayerControl.baud][node->p->attached - 1];
124				MutexUnlock(&node->p->mutex);
125			}
126		}
127		value &= 0xFF03;
128		value |= driver->p->siocnt & 0x007C;
129	}
130	return value;
131}
132
133static int32_t GBASIOLockstepNodeProcessEvents(struct GBASIODriver* driver, int32_t cycles) {
134	struct GBASIOLockstepNode* node = (struct GBASIOLockstepNode*) driver;
135	node->nextEvent -= cycles;
136	while (node->nextEvent <= 0) {
137		MutexLock(&node->p->mutex);
138		++node->p->waiting;
139		if (node->p->waiting < node->p->loaded) {
140			ConditionWait(&node->p->barrier, &node->p->mutex);
141		} else {
142			if (node->p->transferActive) {
143				node->p->transferCycles -= node->p->nextEvent;
144				if (node->p->transferCycles > 0) {
145					if (node->p->transferCycles < LOCKSTEP_INCREMENT) {
146						node->p->nextEvent = node->p->transferCycles;
147					}
148				} else {
149					node->p->nextEvent = LOCKSTEP_INCREMENT;
150					node->p->transferActive = false;
151					int i;
152					for (i = 0; i < node->p->attached; ++i) {
153						node->p->multiRecv[i] = node->p->players[i]->multiSend;
154						node->p->players[i]->state = LOCKSTEP_FINISHED;
155					}
156					for (; i < MAX_GBAS; ++i) {
157						node->p->multiRecv[i] = 0xFFFF;
158					}
159				}
160			}
161			node->p->waiting = 0;
162			ConditionWake(&node->p->barrier);
163		}
164		if (node->state == LOCKSTEP_FINISHED) {
165			node->d.p->p->memory.io[REG_SIOMULTI0 >> 1] = node->p->multiRecv[0];
166			node->d.p->p->memory.io[REG_SIOMULTI1 >> 1] = node->p->multiRecv[1];
167			node->d.p->p->memory.io[REG_SIOMULTI2 >> 1] = node->p->multiRecv[2];
168			node->d.p->p->memory.io[REG_SIOMULTI3 >> 1] = node->p->multiRecv[3];
169			node->d.p->rcnt |= 1;
170			node->state = LOCKSTEP_IDLE;
171			if (node->d.p->multiplayerControl.irq) {
172				GBARaiseIRQ(node->d.p->p, IRQ_SIO);
173			}
174			node->d.p->multiplayerControl.id = node->id;
175			node->d.p->multiplayerControl.busy = 0;
176		} else if (node->state == LOCKSTEP_IDLE && node->p->transferActive) {
177			node->state = LOCKSTEP_STARTED;
178			node->d.p->p->memory.io[REG_SIOMULTI0 >> 1] = 0xFFFF;
179			node->d.p->p->memory.io[REG_SIOMULTI1 >> 1] = 0xFFFF;
180			node->d.p->p->memory.io[REG_SIOMULTI2 >> 1] = 0xFFFF;
181			node->d.p->p->memory.io[REG_SIOMULTI3 >> 1] = 0xFFFF;
182			node->d.p->rcnt &= ~1;
183			node->multiSend = node->d.p->p->memory.io[REG_SIOMLT_SEND >> 1];
184			if (node->id) {
185				node->d.p->multiplayerControl.busy = 1;
186			}
187		}
188		node->d.p->multiplayerControl.ready = node->p->loaded == node->p->attached;
189		node->nextEvent += node->p->nextEvent;
190		MutexUnlock(&node->p->mutex);
191	}
192	return node->nextEvent;
193}