Python: Clean up Core interface so boards inherit Core

This commit is contained in:
Jeffrey Pfau 2016-10-18 15:07:14 -07:00
parent 303a7685a2
commit a9ccb0fdd7
4 changed files with 27 additions and 20 deletions

View File

@ -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'):

View File

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

View File

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

View File

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