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#define LOCKSTEP_INCREMENT 2048
9
10static bool GBASIOLockstepNodeInit(struct GBASIODriver* driver);
11static void GBASIOLockstepNodeDeinit(struct GBASIODriver* driver);
12static bool GBASIOLockstepNodeLoad(struct GBASIODriver* driver);
13static bool GBASIOLockstepNodeUnload(struct GBASIODriver* driver);
14static uint16_t GBASIOLockstepNodeWriteRegister(struct GBASIODriver* driver, uint32_t address, uint16_t value);
15static int32_t GBASIOLockstepNodeProcessEvents(struct GBASIODriver* driver, int32_t cycles);
16
17void GBASIOLockstepInit(struct GBASIOLockstep* lockstep) {
18 lockstep->players[0] = 0;
19 lockstep->players[1] = 0;
20 lockstep->players[2] = 0;
21 lockstep->players[3] = 0;
22 lockstep->attached = 0;
23 ConditionInit(&lockstep->barrier);
24 MutexInit(&lockstep->mutex);
25}
26
27void GBASIOLockstepDeinit(struct GBASIOLockstep* lockstep) {
28 ConditionDeinit(&lockstep->barrier);
29 MutexDeinit(&lockstep->mutex);
30}
31
32void GBASIOLockstepNodeCreate(struct GBASIOLockstepNode* node) {
33 node->d.init = GBASIOLockstepNodeInit;
34 node->d.deinit = GBASIOLockstepNodeDeinit;
35 node->d.load = GBASIOLockstepNodeLoad;
36 node->d.unload = GBASIOLockstepNodeUnload;
37 node->d.writeRegister = GBASIOLockstepNodeWriteRegister;
38 node->d.processEvents = GBASIOLockstepNodeProcessEvents;
39}
40
41bool GBASIOLockstepAttachNode(struct GBASIOLockstep* lockstep, struct GBASIOLockstepNode* node) {
42 if (lockstep->attached == MAX_GBAS) {
43 return false;
44 }
45 lockstep->players[lockstep->attached] = node;
46 ++lockstep->attached;
47 return true;
48}
49
50void GBASIOLockstepDetachNode(struct GBASIOLockstep* lockstep, struct GBASIOLockstepNode* node) {
51 if (lockstep->attached == 0) {
52 return;
53 }
54 int i;
55 for (i = 0; i < lockstep->attached; ++i) {
56 if (lockstep->players[i] != node) {
57 continue;
58 }
59 for (++i; i < lockstep->attached; ++i) {
60 lockstep->players[i - 1] = lockstep->players[i];
61 }
62 --lockstep->attached;
63 break;
64 }
65}
66
67bool GBASIOLockstepNodeInit(struct GBASIODriver* driver) {
68 UNUSED(driver);
69 return true;
70}
71
72void GBASIOLockstepNodeDeinit(struct GBASIODriver* driver) {
73 UNUSED(driver);
74}
75
76bool GBASIOLockstepNodeLoad(struct GBASIODriver* driver) {
77 UNUSED(driver);
78 return true;
79}
80
81bool GBASIOLockstepNodeUnload(struct GBASIODriver* driver) {
82 UNUSED(driver);
83 return true;
84}
85
86static uint16_t GBASIOLockstepNodeWriteRegister(struct GBASIODriver* driver, uint32_t address, uint16_t value) {
87 return value;
88}
89
90static int32_t GBASIOLockstepNodeProcessEvents(struct GBASIODriver* driver, int32_t cycles) {
91 return INT_MAX;
92}