diff --git a/src/platform/python/mgba/core.py b/src/platform/python/mgba/core.py index 8bf339846..9dbb80415 100644 --- a/src/platform/python/mgba/core.py +++ b/src/platform/python/mgba/core.py @@ -11,13 +11,13 @@ def find(path): core = lib.mCoreFind(path.encode('UTF-8')) if core == ffi.NULL: return None - return Core(core) + return Core._init(core) def findVF(vf): core = lib.mCoreFindVF(vf.handle) if core == ffi.NULL: return None - return Core(core) + return Core._init(core) def loadPath(path): core = find(path) @@ -31,24 +31,26 @@ def loadVF(vf): return None return core -class Core: +class Core(object): def __init__(self, native): - self._core = ffi.gc(native, native.deinit) - success = bool(self._core.init(self._core)) - 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) + self._core = native @cached_property def tiles(self): return tile.TileView(self) + @classmethod + def _init(cls, native): + core = ffi.gc(native, native.deinit) + success = bool(core.init(core)) + if not success: + raise RuntimeError("Failed to initialize core") + if hasattr(cls, 'PLATFORM_GBA') and core.platform(core) == cls.PLATFORM_GBA: + return GBA(core) + if hasattr(cls, 'PLATFORM_GB') and core.platform(core) == cls.PLATFORM_GB: + return GB(core) + return Core(core) + def _deinit(self): self._core.deinit(self._core) @@ -112,7 +114,6 @@ class Core: if hasattr(lib, 'PLATFORM_GBA'): from .gba import GBA - from .arm import ARMCore Core.PLATFORM_GBA = lib.PLATFORM_GBA if hasattr(lib, 'PLATFORM_GB'): diff --git a/src/platform/python/mgba/gb.py b/src/platform/python/mgba/gb.py index 0b5f0f79d..bb34d4dad 100644 --- a/src/platform/python/mgba/gb.py +++ b/src/platform/python/mgba/gb.py @@ -4,10 +4,13 @@ # 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/. from ._pylib import ffi, lib +from . import core, lr35902 class GB: def __init__(self, native): - self._native = ffi.cast("struct GB*", native) + super(GB, self).__init__(native) + self._native = ffi.cast("struct GB*", native.board) + self.cpu = lr35902.LR35902Core(self._core.cpu) def _initTileCache(self, cache): lib.GBVideoTileCacheInit(cache) diff --git a/src/platform/python/mgba/gba.py b/src/platform/python/mgba/gba.py index 4a66222d9..9ed401709 100644 --- a/src/platform/python/mgba/gba.py +++ b/src/platform/python/mgba/gba.py @@ -4,10 +4,13 @@ # 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/. from ._pylib import ffi, lib +from . import core, arm -class GBA: +class GBA(core.Core): def __init__(self, native): - self._native = ffi.cast("struct GBA*", native) + super(GBA, self).__init__(native) + self._native = ffi.cast("struct GBA*", native.board) + self.cpu = arm.ARMCore(self._core.cpu) def _initTileCache(self, cache): lib.GBAVideoTileCacheInit(cache) diff --git a/src/platform/python/mgba/tile.py b/src/platform/python/mgba/tile.py index 1728e8927..e8fe07271 100644 --- a/src/platform/python/mgba/tile.py +++ b/src/platform/python/mgba/tile.py @@ -23,8 +23,8 @@ class Tile: class TileView: def __init__(self, core): self.core = core - self.cache = ffi.gc(ffi.new("struct mTileCache*"), core.board._deinitTileCache) - core.board._initTileCache(self.cache) + self.cache = ffi.gc(ffi.new("struct mTileCache*"), core._deinitTileCache) + core._initTileCache(self.cache) lib.mTileCacheSetPalette(self.cache, 0) def getTile(self, tile, palette):