all repos — mgba @ a0b794364f55cff7d62281fe525ce1a2edd96d41

mGBA Game Boy Advance Emulator

Python: More basic framework
Jeffrey Pfau jeffrey@endrift.com
Tue, 11 Oct 2016 17:38:52 -0700
commit

a0b794364f55cff7d62281fe525ce1a2edd96d41

parent

d53497cb32309ef83b530986bc110e03ef2d1ebd

M src/platform/python/arm.pysrc/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
M src/platform/python/gb.pysrc/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)
M src/platform/python/gba.pysrc/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)
M src/platform/python/lr35902.pysrc/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
M src/platform/python/mCore.pysrc/platform/python/mCore.py

@@ -6,20 +6,25 @@ if core == ffi.NULL:

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)

@@ -53,6 +58,25 @@ self._core.runLoop(self._core)

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