Python: Add GBA SIO
Vicki Pfau vi@endrift.com
Wed, 31 May 2017 17:59:16 -0700
6 files changed,
114 insertions(+),
4 deletions(-)
M
src/platform/python/_builder.h
→
src/platform/python/_builder.h
@@ -31,8 +31,9 @@ #include <mgba/core/core.h>
#include <mgba/core/tile-cache.h> #define PYEXPORT extern "Python+C" +#include "platform/python/log.h" +#include "platform/python/sio.h" #include "platform/python/vfs-py.h" -#include "platform/python/log.h" #undef PYEXPORT #ifdef USE_PNG
M
src/platform/python/_builder.py
→
src/platform/python/_builder.py
@@ -34,13 +34,14 @@ #include <mgba-util/vfs.h>
#define PYEXPORT #include "platform/python/log.h" +#include "platform/python/sio.h" #include "platform/python/vfs-py.h" #undef PYEXPORT """, include_dirs=[incdir, srcdir], extra_compile_args=cppflags, libraries=["mgba"], library_dirs=[bindir], - sources=[os.path.join(pydir, path) for path in ["vfs-py.c", "log.c"]]) + sources=[os.path.join(pydir, path) for path in ["vfs-py.c", "log.c", "sio.c"]]) preprocessed = subprocess.check_output(cpp + ["-fno-inline", "-P"] + cppflags + [os.path.join(pydir, "_builder.h")], universal_newlines=True)
M
src/platform/python/mgba/__init__.py
→
src/platform/python/mgba/__init__.py
@@ -6,10 +6,10 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/.
from ._pylib import ffi, lib def createCallback(structName, cbName, funcName=None): - funcName = funcName or "_py{}{}".format(structName, cbName.capitalize()) + funcName = funcName or "_py{}{}".format(structName, cbName[0].upper() + cbName[1:]) fullStruct = "struct {}*".format(structName) def cb(handle, *args): h = ffi.cast(fullStruct, handle) - getattr(ffi.from_handle(h.pyobj), cbName)(*args) + return getattr(ffi.from_handle(h.pyobj), cbName)(*args) return ffi.def_extern(name=funcName)(cb)
M
src/platform/python/mgba/gba.py
→
src/platform/python/mgba/gba.py
@@ -8,6 +8,7 @@ from .arm import ARMCore
from .core import Core, needsReset from .tile import Sprite from .memory import Memory +from . import createCallback class GBA(Core): KEY_A = lib.GBA_KEY_A@@ -21,6 +22,12 @@ KEY_RIGHT = lib.GBA_KEY_RIGHT
KEY_L = lib.GBA_KEY_L KEY_R = lib.GBA_KEY_R + SIO_NORMAL_8 = lib.SIO_NORMAL_8 + SIO_NORMAL_32 = lib.SIO_NORMAL_32 + SIO_MULTI = lib.SIO_MULTI + SIO_UART = lib.SIO_UART + SIO_GPIO = lib.SIO_GPIO + def __init__(self, native): super(GBA, self).__init__(native) self._native = ffi.cast("struct GBA*", native.board)@@ -39,6 +46,35 @@
def reset(self): super(GBA, self).reset() self.memory = GBAMemory(self._core, self._native.memory.romSize) + + def attachSIO(self, link, mode=lib.SIO_MULTI): + lib.GBASIOSetDriver(ffi.addressof(self._native.sio), link._native, mode) + +createCallback("GBASIOPythonDriver", "init") +createCallback("GBASIOPythonDriver", "deinit") +createCallback("GBASIOPythonDriver", "load") +createCallback("GBASIOPythonDriver", "unload") +createCallback("GBASIOPythonDriver", "writeRegister") + +class GBASIODriver(object): + def __init__(self): + self._handle = ffi.new_handle(self) + self._native = ffi.gc(lib.GBASIOPythonDriverCreate(self._handle), lib.free) + + def init(self): + return True + + def deinit(self): + pass + + def load(self): + return True + + def unload(self): + return True + + def writeRegister(self, address, value): + return value class GBAMemory(Memory): def __init__(self, core, romSize=lib.SIZE_CART0):
A
src/platform/python/sio.c
@@ -0,0 +1,49 @@
+/* Copyright (c) 2013-2017 Jeffrey Pfau + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include <mgba/gba/interface.h> + +#include "flags.h" + +#ifdef M_CORE_GBA + +#define CREATE_SHIM(NAME, RETURN) \ + RETURN _pyGBASIOPythonDriver ## NAME (void* driver); \ + static RETURN _pyGBASIOPythonDriver ## NAME ## Shim(struct GBASIODriver* driver) { \ + struct GBASIODriver* py = (struct GBASIODriver*) driver; \ + return _pyGBASIOPythonDriver ## NAME(py); \ + } + +#define CREATE_SHIM_ARGS(NAME, RETURN, TYPES, ...) \ + RETURN _pyGBASIOPythonDriver ## NAME TYPES; \ + static RETURN _pyGBASIOPythonDriver ## NAME ## Shim TYPES { \ + struct GBASIODriver* py = (struct GBASIODriver*) driver; \ + return _pyGBASIOPythonDriver ## NAME(py, __VA_ARGS__); \ + } + +struct GBASIOPythonDriver { + struct GBASIODriver d; + void* pyobj; +}; + +CREATE_SHIM(Init, bool); +CREATE_SHIM(Deinit, void); +CREATE_SHIM(Load, bool); +CREATE_SHIM(Unload, bool); +CREATE_SHIM_ARGS(WriteRegister, uint16_t, (struct GBASIODriver* driver, uint32_t address, uint16_t value), address, value); + +struct GBASIODriver* GBASIOPythonDriverCreate(void* pyobj) { + struct GBASIOPythonDriver* driver = malloc(sizeof(*driver)); + driver->d.init = _pyGBASIOPythonDriverInitShim; + driver->d.deinit = _pyGBASIOPythonDriverDeinitShim; + driver->d.load = _pyGBASIOPythonDriverLoadShim; + driver->d.unload = _pyGBASIOPythonDriverUnloadShim; + driver->d.writeRegister = _pyGBASIOPythonDriverWriteRegisterShim; + + driver->pyobj = pyobj; + return &driver->d; +} + +#endif
A
src/platform/python/sio.h
@@ -0,0 +1,23 @@
+/* Copyright (c) 2013-2017 Jeffrey Pfau + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#ifdef M_CORE_GBA + +#include <mgba/gba/interface.h> + +struct GBASIOPythonDriver { + struct GBASIODriver d; + void* pyobj; +}; + +struct GBASIODriver* GBASIOPythonDriverCreate(void* pyobj); + +PYEXPORT bool _pyGBASIOPythonDriverInit(void* driver); +PYEXPORT void _pyGBASIOPythonDriverDeinit(void* driver); +PYEXPORT bool _pyGBASIOPythonDriverLoad(void* driver); +PYEXPORT bool _pyGBASIOPythonDriverUnload(void* driver); +PYEXPORT uint16_t _pyGBASIOPythonDriverWriteRegister(void* driver, uint32_t address, uint16_t value); + +#endif