diff --git a/src/platform/python/arm.py b/src/platform/python/arm.py index 29d27e932..8692523d6 100644 --- a/src/platform/python/arm.py +++ b/src/platform/python/arm.py @@ -1,16 +1,23 @@ from _pylib import ffi, lib class _ARMRegisters: - def __init__(self, cpu): - self._cpu = cpu + def __init__(self, cpu): + self._cpu = cpu - def __getitem__(self, r): - if r > lib.ARM_PC: - raise IndexError("Register out of range") - return int(self._cpu._native.gprs[r]) + def __getitem__(self, r): + if r > lib.ARM_PC: + raise IndexError("Register out of range") + return self._cpu._native.gprs[r] + + def __setitem__(self, r, value): + if r >= lib.ARM_PC: + raise IndexError("Register out of range") + self._cpu._native.gprs[r] = value class ARMCore: def __init__(self, native): - self._native = ffi.cast("struct ARMCore*", native) - self.gprs = _ARMRegisters(self) + self._native = ffi.cast("struct ARMCore*", native) + self.gprs = _ARMRegisters(self) + self.cpsr = self._native.cpsr + self.spsr = self._native.spsr diff --git a/src/platform/python/gb.py b/src/platform/python/gb.py index edcb72d5c..8184ad30e 100644 --- a/src/platform/python/gb.py +++ b/src/platform/python/gb.py @@ -2,4 +2,4 @@ from _pylib import ffi, lib class GB: def __init__(self, native): - self._native = ffi.cast("struct GB*", native) + self._native = ffi.cast("struct GB*", native) diff --git a/src/platform/python/gba.py b/src/platform/python/gba.py index 825754836..709e85b2d 100644 --- a/src/platform/python/gba.py +++ b/src/platform/python/gba.py @@ -2,4 +2,4 @@ from _pylib import ffi, lib class GBA: def __init__(self, native): - self._native = ffi.cast("struct GBA*", native) + self._native = ffi.cast("struct GBA*", native) diff --git a/src/platform/python/lr35902.py b/src/platform/python/lr35902.py index eaef019cc..8272e37a5 100644 --- a/src/platform/python/lr35902.py +++ b/src/platform/python/lr35902.py @@ -2,4 +2,49 @@ from _pylib import ffi, lib class LR35902Core: def __init__(self, native): - self._native = ffi.cast("struct LR35902*", native) + self._native = ffi.cast("struct LR35902Core*", native) + + def __getattr__(self, key): + if key == 'a': + return self._native.a + if key == 'b': + return self._native.b + if key == 'c': + return self._native.c + if key == 'd': + return self._native.d + if key == 'e': + return self._native.e + if key == 'f': + return self._native.f + if key == 'h': + return self._native.h + if key == 'l': + return self._native.l + if key == 'sp': + return self._native.sp + if key == 'pc': + return self._native.pc + raise AttributeError() + + def __setattr__(self, key, value): + if key == 'a': + self._native.a = value & 0xF0 + if key == 'b': + self._native.b = value + if key == 'c': + self._native.c = value + if key == 'd': + self._native.d = value + if key == 'e': + self._native.e = value + if key == 'f': + self._native.f = value + if key == 'h': + self._native.h = value + if key == 'l': + self._native.l = value + if key == 'sp': + self._native.sp = value + else: + self.__dict__[key] = value \ No newline at end of file diff --git a/src/platform/python/mCore.py b/src/platform/python/mCore.py index 9086a63ff..2339dd91a 100644 --- a/src/platform/python/mCore.py +++ b/src/platform/python/mCore.py @@ -6,20 +6,25 @@ def find(path): return None return mCore(core) +def loadPath(path): + core = find(path) + if not core or not core.loadFile(path): + return None + return core + class mCore: def __init__(self, native): self._core = ffi.gc(native, self._deinit) - - def init(self): success = bool(self._core.init(self._core)) - if success: - if hasattr(self, 'PLATFORM_GBA') and self.platform() == self.PLATFORM_GBA: - self.cpu = ARMCore(self._core.cpu) - self.board = GBA(self._core.board) - if hasattr(self, 'PLATFORM_GB') and self.platform() == self.PLATFORM_GB: - self.cpu = LR35902Core(self._core.cpu) - self.board = GB(self._core.board) - return success + if not success: + raise RuntimeError("Failed to initialize core") + + if hasattr(self, 'PLATFORM_GBA') and self.platform() == self.PLATFORM_GBA: + self.cpu = ARMCore(self._core.cpu) + self.board = GBA(self._core.board) + if hasattr(self, 'PLATFORM_GB') and self.platform() == self.PLATFORM_GB: + self.cpu = LR35902Core(self._core.cpu) + self.board = GB(self._core.board) def _deinit(self): self._core.deinit(self._core) @@ -54,6 +59,25 @@ class mCore: def step(self): self._core.step(self._core) + def frameCounter(self): + return self._core.frameCounter(self._core) + + def frameCycles(self): + return self._core.frameCycles(self._core) + + def frequency(self): + return self._core.frequency(self._core) + + def getGameTitle(self): + title = ffi.new("char[16]") + self._core.getGameTitle(self._core, title) + return ffi.string(title, 16).decode("ascii") + + def getGameCode(self): + code = ffi.new("char[12]") + self._core.getGameCode(self._core, code) + return ffi.string(code, 12).decode("ascii") + if hasattr(lib, 'PLATFORM_GBA'): from .gba import GBA from .arm import ARMCore