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 <mgba/internal/gb/sio/lockstep.h>
7
8#include <mgba/internal/gb/gb.h>
9#include <mgba/internal/gb/io.h>
10
11#define LOCKSTEP_INCREMENT 512
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 lockstep->players[0] = NULL;
21 lockstep->players[1] = NULL;
22 lockstep->pendingSB[0] = 0xFF;
23 lockstep->pendingSB[1] = 0xFF;
24 lockstep->masterClaimed = false;
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 if (GBRegisterSCIsEnable(sio->p->memory.io[REG_SC])) {
94 sio->remainingBits = 8;
95 mTimingDeschedule(&sio->p->timing, &sio->event);
96 mTimingSchedule(&sio->p->timing, &sio->event, 0);
97 }
98 node->transferFinished = true;
99#ifndef NDEBUG
100 ++node->transferId;
101#endif
102}
103
104
105static int32_t _masterUpdate(struct GBSIOLockstepNode* node) {
106 bool needsToWait = false;
107 int i;
108 switch (node->p->d.transferActive) {
109 case TRANSFER_IDLE:
110 // If the master hasn't initiated a transfer, it can keep going.
111 node->nextEvent += LOCKSTEP_INCREMENT;
112 break;
113 case TRANSFER_STARTING:
114 // Start the transfer, but wait for the other GBs to catch up
115 node->transferFinished = false;
116 needsToWait = true;
117 ATOMIC_STORE(node->p->d.transferActive, TRANSFER_STARTED);
118 node->nextEvent += 4;
119 break;
120 case TRANSFER_STARTED:
121 // All the other GBs have caught up and are sleeping, we can all continue now
122 node->nextEvent += 4;
123 ATOMIC_STORE(node->p->d.transferActive, TRANSFER_FINISHING);
124 break;
125 case TRANSFER_FINISHING:
126 // Finish the transfer
127 // We need to make sure the other GBs catch up so they don't get behind
128 node->nextEvent += node->d.p->period - 8; // Split the cycles to avoid waiting too long
129#ifndef NDEBUG
130 ATOMIC_ADD(node->p->d.transferId, 1);
131#endif
132 needsToWait = true;
133 ATOMIC_STORE(node->p->d.transferActive, TRANSFER_FINISHED);
134 break;
135 case TRANSFER_FINISHED:
136 // Everything's settled. We're done.
137 _finishTransfer(node);
138 ATOMIC_STORE(node->p->masterClaimed, false);
139 node->nextEvent += LOCKSTEP_INCREMENT;
140 ATOMIC_STORE(node->p->d.transferActive, TRANSFER_IDLE);
141 break;
142 }
143 int mask = 0;
144 for (i = 1; i < node->p->d.attached; ++i) {
145 mask |= 1 << i;
146 }
147 if (mask) {
148 if (needsToWait) {
149 if (!node->p->d.wait(&node->p->d, mask)) {
150 abort();
151 }
152 } else {
153 node->p->d.signal(&node->p->d, mask);
154 }
155 }
156 // Tell the other GBs they can continue up to where we were
157 node->p->d.addCycles(&node->p->d, 0, node->eventDiff);
158#ifndef NDEBUG
159 node->phase = node->p->d.transferActive;
160#endif
161 if (needsToWait) {
162 return 0;
163 }
164 return node->nextEvent;
165}
166
167static uint32_t _slaveUpdate(struct GBSIOLockstepNode* node) {
168 bool signal = false;
169 switch (node->p->d.transferActive) {
170 case TRANSFER_IDLE:
171 node->p->d.addCycles(&node->p->d, node->id, LOCKSTEP_INCREMENT);
172 break;
173 case TRANSFER_STARTING:
174 case TRANSFER_FINISHING:
175 break;
176 case TRANSFER_STARTED:
177 node->transferFinished = false;
178 signal = true;
179 break;
180 case TRANSFER_FINISHED:
181 _finishTransfer(node);
182 signal = true;
183 break;
184 }
185#ifndef NDEBUG
186 node->phase = node->p->d.transferActive;
187#endif
188 if (signal) {
189 node->p->d.signal(&node->p->d, 1 << node->id);
190 }
191 return 0;
192}
193
194static void _GBSIOLockstepNodeProcessEvents(struct mTiming* timing, void* user, uint32_t cyclesLate) {
195 struct GBSIOLockstepNode* node = user;
196 if (node->p->d.attached < 2) {
197 return;
198 }
199 int32_t cycles = 0;
200 node->nextEvent -= cyclesLate;
201 if (node->nextEvent <= 0) {
202 if (!node->id) {
203 cycles = _masterUpdate(node);
204 } else {
205 cycles = _slaveUpdate(node);
206 cycles += node->p->d.useCycles(&node->p->d, node->id, node->eventDiff);
207 }
208 node->eventDiff = 0;
209 } else {
210 cycles = node->nextEvent;
211 }
212 if (cycles > 0) {
213 node->nextEvent = 0;
214 node->eventDiff += cycles;
215 mTimingDeschedule(timing, &node->event);
216 mTimingSchedule(timing, &node->event, cycles);
217 } else {
218 node->d.p->p->earlyExit = true;
219 mTimingSchedule(timing, &node->event, cyclesLate + 1);
220 }
221}
222
223static void GBSIOLockstepNodeWriteSB(struct GBSIODriver* driver, uint8_t value) {
224 struct GBSIOLockstepNode* node = (struct GBSIOLockstepNode*) driver;
225 node->p->pendingSB[node->id] = value;
226}
227
228static uint8_t GBSIOLockstepNodeWriteSC(struct GBSIODriver* driver, uint8_t value) {
229 struct GBSIOLockstepNode* node = (struct GBSIOLockstepNode*) driver;
230 if ((value & 0x81) == 0x81 && node->p->d.attached > 1) {
231 bool claimed = false;
232 if (ATOMIC_CMPXCHG(node->p->masterClaimed, claimed, true)) {
233 node->p->d.transferActive = TRANSFER_STARTING;
234 node->p->d.transferCycles = GBSIOCyclesPerTransfer[(value >> 1) & 1];
235 mTimingDeschedule(&driver->p->p->timing, &driver->p->event);
236 mTimingDeschedule(&driver->p->p->timing, &node->event);
237 mTimingSchedule(&driver->p->p->timing, &node->event, 0);
238 } else {
239 mLOG(GB_SIO, FATAL, "GBSIOLockstepNodeWriteSC() failed to write to masterClaimed\n");
240 }
241 }
242 return value;
243}