src/platform/python/sio.c (view raw)
1/* Copyright (c) 2013-2017 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 "flags.h"
7
8#define CREATE_SHIM(PLAT, NAME, RETURN) \
9 RETURN _py ## PLAT ## SIOPythonDriver ## NAME (void* driver); \
10 static RETURN _py ## PLAT ## SIOPythonDriver ## NAME ## Shim(struct PLAT ## SIODriver* driver) { \
11 struct PLAT ## SIODriver* py = (struct PLAT ## SIODriver*) driver; \
12 return _py ## PLAT ## SIOPythonDriver ## NAME(py); \
13 }
14
15#define CREATE_SHIM_ARGS(PLAT, NAME, RETURN, TYPES, ...) \
16 RETURN _py ## PLAT ## SIOPythonDriver ## NAME TYPES; \
17 static RETURN _py ## PLAT ## SIOPythonDriver ## NAME ## Shim TYPES { \
18 struct PLAT ## SIODriver* py = (struct PLAT ## SIODriver*) driver; \
19 return _py ## PLAT ## SIOPythonDriver ## NAME(py, __VA_ARGS__); \
20 }
21
22#ifdef M_CORE_GBA
23
24#include <mgba/gba/interface.h>
25
26struct GBASIOPythonDriver {
27 struct GBASIODriver d;
28 void* pyobj;
29};
30
31CREATE_SHIM(GBA, Init, bool);
32CREATE_SHIM(GBA, Deinit, void);
33CREATE_SHIM(GBA, Load, bool);
34CREATE_SHIM(GBA, Unload, bool);
35CREATE_SHIM_ARGS(GBA, WriteRegister, uint16_t, (struct GBASIODriver* driver, uint32_t address, uint16_t value), address, value);
36
37struct GBASIODriver* GBASIOPythonDriverCreate(void* pyobj) {
38 struct GBASIOPythonDriver* driver = malloc(sizeof(*driver));
39 driver->d.init = _pyGBASIOPythonDriverInitShim;
40 driver->d.deinit = _pyGBASIOPythonDriverDeinitShim;
41 driver->d.load = _pyGBASIOPythonDriverLoadShim;
42 driver->d.unload = _pyGBASIOPythonDriverUnloadShim;
43 driver->d.writeRegister = _pyGBASIOPythonDriverWriteRegisterShim;
44
45 driver->pyobj = pyobj;
46 return &driver->d;
47}
48
49struct GBASIODriver* GBASIOJOYPythonDriverCreate(void* pyobj) {
50 struct GBASIOPythonDriver* driver = malloc(sizeof(*driver));
51 GBASIOJOYCreate(&driver->d);
52 driver->d.init = _pyGBASIOPythonDriverInitShim;
53 driver->d.deinit = _pyGBASIOPythonDriverDeinitShim;
54 driver->d.load = _pyGBASIOPythonDriverLoadShim;
55 driver->d.unload = _pyGBASIOPythonDriverUnloadShim;
56
57 driver->pyobj = pyobj;
58 return &driver->d;
59}
60
61#endif
62
63#ifdef M_CORE_GB
64
65#include <mgba/gb/interface.h>
66
67struct GBSIOPythonDriver {
68 struct GBSIODriver d;
69 void* pyobj;
70};
71
72CREATE_SHIM(GB, Init, bool);
73CREATE_SHIM(GB, Deinit, void);
74CREATE_SHIM_ARGS(GB, WriteSB, void, (struct GBSIODriver* driver, uint8_t value), value);
75CREATE_SHIM_ARGS(GB, WriteSC, uint8_t, (struct GBSIODriver* driver, uint8_t value), value);
76
77struct GBSIODriver* GBSIOPythonDriverCreate(void* pyobj) {
78 struct GBSIOPythonDriver* driver = malloc(sizeof(*driver));
79 driver->d.init = _pyGBSIOPythonDriverInitShim;
80 driver->d.deinit = _pyGBSIOPythonDriverDeinitShim;
81 driver->d.writeSB = _pyGBSIOPythonDriverWriteSBShim;
82 driver->d.writeSC = _pyGBSIOPythonDriverWriteSCShim;
83
84 driver->pyobj = pyobj;
85 return &driver->d;
86}
87
88#endif