all repos — mgba @ ceea51b55ea2f112c49b4ad22e6d70b08ad430e7

mGBA Game Boy Advance Emulator

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

  1/* Copyright (c) 2013-2016 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 "gb/gb.h"
  9#include "gb/io.h"
 10
 11#define LOCKSTEP_INCREMENT 500
 12
 13static bool GBSIOLockstepNodeInit(struct GBSIODriver* driver);
 14static void GBSIOLockstepNodeDeinit(struct GBSIODriver* driver);
 15static void GBSIOLockstepNodeWriteSB(struct GBSIODriver* driver, uint8_t value);
 16static uint8_t GBSIOLockstepNodeWriteSC(struct GBSIODriver* driver, uint8_t value);
 17static void _GBSIOLockstepNodeProcessEvents(struct mTiming* timing, void* driver, uint32_t cyclesLate);
 18
 19void GBSIOLockstepInit(struct GBSIOLockstep* lockstep) {
 20	mLockstepInit(&lockstep->d);
 21	lockstep->players[0] = NULL;
 22	lockstep->players[1] = NULL;
 23	lockstep->pendingSB[0] = 0xFF;
 24	lockstep->pendingSB[1] = 0xFF;
 25}
 26
 27void GBSIOLockstepNodeCreate(struct GBSIOLockstepNode* node) {
 28	node->d.init = GBSIOLockstepNodeInit;
 29	node->d.deinit = GBSIOLockstepNodeDeinit;
 30	node->d.writeSB = GBSIOLockstepNodeWriteSB;
 31	node->d.writeSC = GBSIOLockstepNodeWriteSC;
 32}
 33
 34bool GBSIOLockstepAttachNode(struct GBSIOLockstep* lockstep, struct GBSIOLockstepNode* node) {
 35	if (lockstep->d.attached == MAX_GBS) {
 36		return false;
 37	}
 38	lockstep->players[lockstep->d.attached] = node;
 39	node->p = lockstep;
 40	node->id = lockstep->d.attached;
 41	++lockstep->d.attached;
 42	return true;
 43}
 44
 45void GBSIOLockstepDetachNode(struct GBSIOLockstep* lockstep, struct GBSIOLockstepNode* node) {
 46	if (lockstep->d.attached == 0) {
 47		return;
 48	}
 49	int i;
 50	for (i = 0; i < lockstep->d.attached; ++i) {
 51		if (lockstep->players[i] != node) {
 52			continue;
 53		}
 54		for (++i; i < lockstep->d.attached; ++i) {
 55			lockstep->players[i - 1] = lockstep->players[i];
 56			lockstep->players[i - 1]->id = i - 1;
 57		}
 58		--lockstep->d.attached;
 59		break;
 60	}
 61}
 62
 63bool GBSIOLockstepNodeInit(struct GBSIODriver* driver) {
 64	struct GBSIOLockstepNode* node = (struct GBSIOLockstepNode*) driver;
 65	mLOG(GB_SIO, DEBUG, "Lockstep %i: Node init", node->id);
 66	node->event.context = node;
 67	node->event.name = "GB SIO Lockstep";
 68	node->event.callback = _GBSIOLockstepNodeProcessEvents;
 69	node->event.priority = 0x80;
 70
 71	node->nextEvent = 0;
 72	node->eventDiff = 0;
 73	mTimingSchedule(&driver->p->p->timing, &node->event, 0);
 74#ifndef NDEBUG
 75	node->phase = node->p->d.transferActive;
 76	node->transferId = node->p->d.transferId;
 77#endif
 78	return true;
 79}
 80
 81void GBSIOLockstepNodeDeinit(struct GBSIODriver* driver) {
 82	struct GBSIOLockstepNode* node = (struct GBSIOLockstepNode*) driver;
 83	node->p->d.unload(&node->p->d, node->id);
 84	mTimingDeschedule(&driver->p->p->timing, &node->event);
 85}
 86
 87static void _finishTransfer(struct GBSIOLockstepNode* node) {
 88	if (node->transferFinished) {
 89		return;
 90	}
 91	struct GBSIO* sio = node->d.p;
 92	sio->pendingSB = node->p->pendingSB[!node->id];
 93	node->transferFinished = true;
 94#ifndef NDEBUG
 95	++node->transferId;
 96#endif
 97}
 98
 99
100static int32_t _masterUpdate(struct GBSIOLockstepNode* node) {
101	bool needsToWait = false;
102	int i;
103	switch (node->p->d.transferActive) {
104	case TRANSFER_IDLE:
105		// If the master hasn't initiated a transfer, it can keep going.
106		node->nextEvent += LOCKSTEP_INCREMENT;
107		break;
108	case TRANSFER_STARTING:
109		// Start the transfer, but wait for the other GBs to catch up
110		node->transferFinished = false;
111		needsToWait = true;
112		ATOMIC_STORE(node->p->d.transferActive, TRANSFER_STARTED);
113		node->nextEvent += 128;
114		break;
115	case TRANSFER_STARTED:
116		// All the other GBs have caught up and are sleeping, we can all continue now
117		node->nextEvent += 128;
118		ATOMIC_STORE(node->p->d.transferActive, TRANSFER_FINISHING);
119		break;
120	case TRANSFER_FINISHING:
121		// Finish the transfer
122		// We need to make sure the other GBs catch up so they don't get behind
123		node->nextEvent += LOCKSTEP_INCREMENT - 256; // Split the cycles to avoid waiting too long
124#ifndef NDEBUG
125		ATOMIC_ADD(node->p->d.transferId, 1);
126#endif
127		needsToWait = true;
128		ATOMIC_STORE(node->p->d.transferActive, TRANSFER_FINISHED);
129		break;
130	case TRANSFER_FINISHED:
131		// Everything's settled. We're done.
132		_finishTransfer(node);
133		node->nextEvent += LOCKSTEP_INCREMENT;
134		ATOMIC_STORE(node->p->d.transferActive, TRANSFER_IDLE);
135		break;
136	}
137	int mask = 0;
138	for (i = 1; i < node->p->d.attached; ++i) {
139		mask |= 1 << i;
140	}
141	if (mask) {
142		if (needsToWait) {
143			if (!node->p->d.wait(&node->p->d, mask)) {
144				abort();
145			}
146		} else {
147			node->p->d.signal(&node->p->d, mask);
148		}
149	}
150	// Tell the other GBs they can continue up to where we were
151	node->p->d.addCycles(&node->p->d, 0, node->eventDiff);
152#ifndef NDEBUG
153	node->phase = node->p->d.transferActive;
154#endif
155	if (needsToWait) {
156		return 0;
157	}
158	return node->nextEvent;
159}
160
161static uint32_t _slaveUpdate(struct GBSIOLockstepNode* node) {
162	bool signal = false;
163	switch (node->p->d.transferActive) {
164	case TRANSFER_IDLE:
165		node->p->d.addCycles(&node->p->d, node->id, LOCKSTEP_INCREMENT);
166		break;
167	case TRANSFER_STARTING:
168	case TRANSFER_FINISHING:
169		break;
170	case TRANSFER_STARTED:
171		node->transferFinished = false;
172		signal = true;
173		break;
174	case TRANSFER_FINISHED:
175		_finishTransfer(node);
176		signal = true;
177		break;
178	}
179#ifndef NDEBUG
180	node->phase = node->p->d.transferActive;
181#endif
182	if (signal) {
183		node->p->d.signal(&node->p->d, 1 << node->id);
184	}
185	return 0;
186}
187
188static void _GBSIOLockstepNodeProcessEvents(struct mTiming* timing, void* user, uint32_t cyclesLate) {
189	struct GBSIOLockstepNode* node = user;
190	if (node->p->d.attached < 2) {
191		return;
192	}
193	int32_t cycles = 0;
194	node->nextEvent -= cyclesLate;
195	if (node->nextEvent <= 0) {
196		if (!node->id) {
197			cycles = _masterUpdate(node);
198		} else {
199			cycles = _slaveUpdate(node);
200			cycles += node->p->d.useCycles(&node->p->d, node->id, node->eventDiff);
201		}
202		node->eventDiff = 0;
203	} else {
204		cycles = node->nextEvent;
205	}
206	if (cycles > 0) {
207		node->nextEvent = 0;
208		node->eventDiff += cycles;
209		mTimingDeschedule(timing, &node->event);
210		mTimingSchedule(timing, &node->event, cycles);
211	} else {
212		node->d.p->p->earlyExit = true;
213		mTimingSchedule(timing, &node->event, cyclesLate + 1);
214	}
215}
216
217static void GBSIOLockstepNodeWriteSB(struct GBSIODriver* driver, uint8_t value) {
218	struct GBSIOLockstepNode* node = (struct GBSIOLockstepNode*) driver;
219	node->p->pendingSB[node->id] = value;
220}
221
222static uint8_t GBSIOLockstepNodeWriteSC(struct GBSIODriver* driver, uint8_t value) {
223	struct GBSIOLockstepNode* node = (struct GBSIOLockstepNode*) driver;
224	if (!node->id && (value & 0x81) == 0x81) {
225		node->p->d.transferActive = TRANSFER_STARTING;
226		node->p->d.transferCycles = GBSIOCyclesPerTransfer[(value >> 1) & 1];
227		mTimingDeschedule(&driver->p->p->timing, &node->event);
228		mTimingSchedule(&driver->p->p->timing, &node->event, 0);
229	}
230	return value;
231}