Python: Add GBA SIO

This commit is contained in:
Vicki Pfau 2017-05-31 17:59:16 -07:00
parent fe2854db79
commit efd8c4b466
6 changed files with 114 additions and 4 deletions

View File

@ -31,8 +31,9 @@ void free(void*);
#include <mgba/core/tile-cache.h>
#define PYEXPORT extern "Python+C"
#include "platform/python/vfs-py.h"
#include "platform/python/log.h"
#include "platform/python/sio.h"
#include "platform/python/vfs-py.h"
#undef PYEXPORT
#ifdef USE_PNG

View File

@ -34,13 +34,14 @@ ffi.set_source("mgba._pylib", """
#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)

View File

@ -6,10 +6,10 @@
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)

View File

@ -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 @@ class GBA(Core):
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)
@ -40,6 +47,35 @@ class GBA(Core):
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):
super(GBAMemory, self).__init__(core, 0x100000000)

49
src/platform/python/sio.c Normal file
View File

@ -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

23
src/platform/python/sio.h Normal file
View File

@ -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