all repos — mgba @ 871c21fb6cf9041b4ac7c7f7074e0b2eb77484fe

mGBA Game Boy Advance Emulator

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
49#endif
50
51#ifdef M_CORE_GB
52
53#include <mgba/gb/interface.h>
54
55struct GBSIOPythonDriver {
56    struct GBSIODriver d;
57    void* pyobj;
58};
59
60CREATE_SHIM(GB, Init, bool);
61CREATE_SHIM(GB, Deinit, void);
62CREATE_SHIM_ARGS(GB, WriteSB, void, (struct GBSIODriver* driver, uint8_t value), value);
63CREATE_SHIM_ARGS(GB, WriteSC, uint8_t, (struct GBSIODriver* driver, uint8_t value), value);
64
65struct GBSIODriver* GBSIOPythonDriverCreate(void* pyobj) {
66	struct GBSIOPythonDriver* driver = malloc(sizeof(*driver));
67	driver->d.init = _pyGBSIOPythonDriverInitShim;
68	driver->d.deinit = _pyGBSIOPythonDriverDeinitShim;
69	driver->d.writeSB = _pyGBSIOPythonDriverWriteSBShim;
70	driver->d.writeSC = _pyGBSIOPythonDriverWriteSCShim;
71
72	driver->pyobj = pyobj;
73	return &driver->d;
74}
75
76#endif