all repos — mgba @ f775af67f352e08d55891c1031b0f3a75285da4f

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	}
101	MutexUnlock(&node->p->mutex);
102	return true;
103}
104
105bool GBASIOLockstepNodeUnload(struct GBASIODriver* driver) {
106	struct GBASIOLockstepNode* node = (struct GBASIOLockstepNode*) driver;
107	MutexLock(&node->p->mutex);
108	--node->p->loaded;
109	ConditionWake(&node->p->barrier);
110	MutexUnlock(&node->p->mutex);
111	return true;
112}
113
114static uint16_t GBASIOLockstepNodeWriteRegister(struct GBASIODriver* driver, uint32_t address, uint16_t value) {
115	struct GBASIOLockstepNode* node = (struct GBASIOLockstepNode*) driver;
116	if (address == REG_SIOCNT) {
117		if (value & 0x0080) {
118			value &= ~0x0080;
119			if (!node->id) {
120				MutexLock(&node->p->mutex);
121				node->p->transferActive = true;
122				node->p->transferCycles = GBASIOCyclesPerTransfer[node->d.p->multiplayerControl.baud][node->p->attached - 1];
123				MutexUnlock(&node->p->mutex);
124			}
125		}
126		value &= 0xFF03;
127		value |= driver->p->siocnt & 0x0078;
128	}
129	return value;
130}
131
132static int32_t GBASIOLockstepNodeProcessEvents(struct GBASIODriver* driver, int32_t cycles) {
133	struct GBASIOLockstepNode* node = (struct GBASIOLockstepNode*) driver;
134	node->nextEvent -= cycles;
135	while (node->nextEvent <= 0) {
136		MutexLock(&node->p->mutex);
137		++node->p->waiting;
138		if (node->p->waiting < node->p->loaded) {
139			ConditionWait(&node->p->barrier, &node->p->mutex);
140		} else {
141			if (node->p->transferActive) {
142				node->p->transferCycles -= node->p->nextEvent;
143				if (node->p->transferCycles > 0) {
144					if (node->p->transferCycles < LOCKSTEP_INCREMENT) {
145						node->p->nextEvent = node->p->transferCycles;
146					}
147				} else {
148					node->p->nextEvent = LOCKSTEP_INCREMENT;
149					node->p->transferActive = false;
150					int i;
151					for (i = 0; i < node->p->attached; ++i) {
152						node->p->multiRecv[i] = node->p->players[i]->multiSend;
153						node->p->players[i]->state = LOCKSTEP_FINISHED;
154					}
155					for (; i < MAX_GBAS; ++i) {
156						node->p->multiRecv[i] = 0xFFFF;
157					}
158				}
159			}
160			ConditionWake(&node->p->barrier);
161		}
162		--node->p->waiting;
163		if (node->state == LOCKSTEP_FINISHED) {
164			node->d.p->p->memory.io[REG_SIOMULTI0 >> 1] = node->p->multiRecv[0];
165			node->d.p->p->memory.io[REG_SIOMULTI1 >> 1] = node->p->multiRecv[1];
166			node->d.p->p->memory.io[REG_SIOMULTI2 >> 1] = node->p->multiRecv[2];
167			node->d.p->p->memory.io[REG_SIOMULTI3 >> 1] = node->p->multiRecv[3];
168			node->d.p->rcnt |= 1;
169			node->state = LOCKSTEP_IDLE;
170			if (node->d.p->multiplayerControl.irq) {
171				GBARaiseIRQ(node->d.p->p, IRQ_SIO);
172			}
173			node->d.p->multiplayerControl.id = node->id;
174			node->d.p->multiplayerControl.busy = 0;
175		} else if (node->state == LOCKSTEP_IDLE && node->p->transferActive) {
176			node->state = LOCKSTEP_STARTED;
177			node->d.p->p->memory.io[REG_SIOMULTI0 >> 1] = 0xFFFF;
178			node->d.p->p->memory.io[REG_SIOMULTI1 >> 1] = 0xFFFF;
179			node->d.p->p->memory.io[REG_SIOMULTI2 >> 1] = 0xFFFF;
180			node->d.p->p->memory.io[REG_SIOMULTI3 >> 1] = 0xFFFF;
181			node->d.p->rcnt &= ~1;
182			node->multiSend = node->d.p->p->memory.io[REG_SIOMLT_SEND >> 1];
183			if (node->id) {
184				node->d.p->multiplayerControl.busy = 1;
185			}
186		}
187		node->d.p->multiplayerControl.ready = node->p->loaded == node->p->attached;
188		node->nextEvent += node->p->nextEvent;
189		MutexUnlock(&node->p->mutex);
190	}
191	return node->nextEvent;
192}