Python: Clean up Core interface so boards inherit Core
Jeffrey Pfau jeffrey@endrift.com
Tue, 18 Oct 2016 15:07:14 -0700
4 files changed,
27 insertions(+),
20 deletions(-)
M
src/platform/python/mgba/core.py
→
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,23 +31,25 @@ if not core or not core.loadROM(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 @@ return ffi.string(code, 12).decode("ascii")
if hasattr(lib, 'PLATFORM_GBA'): from .gba import GBA - from .arm import ARMCore Core.PLATFORM_GBA = lib.PLATFORM_GBA if hasattr(lib, 'PLATFORM_GB'):
M
src/platform/python/mgba/gb.py
→
src/platform/python/mgba/gb.py
@@ -4,10 +4,13 @@ # 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/. 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)
M
src/platform/python/mgba/gba.py
→
src/platform/python/mgba/gba.py
@@ -4,10 +4,13 @@ # 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/. 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)
M
src/platform/python/mgba/tile.py
→
src/platform/python/mgba/tile.py
@@ -23,8 +23,8 @@
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):