all repos — mgba @ 303a7685a28b2ccbbc4c7bb6cc62a75fcdc0116c

mGBA Game Boy Advance Emulator

src/platform/python/mgba/core.py (view raw)

  1# Copyright (c) 2013-2016 Jeffrey Pfau
  2#
  3# This Source Code Form is subject to the terms of the Mozilla Public
  4# License, v. 2.0. If a copy of the MPL was not distributed with this
  5# file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6from ._pylib import ffi, lib
  7from . import tile
  8from cached_property import cached_property
  9
 10def find(path):
 11    core = lib.mCoreFind(path.encode('UTF-8'))
 12    if core == ffi.NULL:
 13        return None
 14    return Core(core)
 15
 16def findVF(vf):
 17    core = lib.mCoreFindVF(vf.handle)
 18    if core == ffi.NULL:
 19        return None
 20    return Core(core)
 21
 22def loadPath(path):
 23    core = find(path)
 24    if not core or not core.loadFile(path):
 25        return None
 26    return core
 27
 28def loadVF(vf):
 29    core = findVF(vf)
 30    if not core or not core.loadROM(vf):
 31        return None
 32    return core
 33
 34class Core:
 35    def __init__(self, native):
 36        self._core = ffi.gc(native, native.deinit)
 37        success = bool(self._core.init(self._core))
 38        if not success:
 39            raise RuntimeError("Failed to initialize core")
 40
 41        if hasattr(self, 'PLATFORM_GBA') and self.platform() == self.PLATFORM_GBA:
 42            self.cpu = ARMCore(self._core.cpu)
 43            self.board = GBA(self._core.board)
 44        if hasattr(self, 'PLATFORM_GB') and self.platform() == self.PLATFORM_GB:
 45            self.cpu = LR35902Core(self._core.cpu)
 46            self.board = GB(self._core.board)
 47
 48    @cached_property
 49    def tiles(self):
 50        return tile.TileView(self)
 51
 52    def _deinit(self):
 53        self._core.deinit(self._core)
 54
 55    def loadFile(self, path):
 56        return bool(lib.mCoreLoadFile(self._core, path.encode('UTF-8')))
 57
 58    def isROM(self, vf):
 59        return bool(self._core.isROM(vf.handle))
 60
 61    def loadROM(self, vf):
 62        return bool(self._core.loadROM(self._core, vf.handle))
 63
 64    def autoloadSave(self):
 65        return bool(lib.mCoreAutoloadSave(self._core))
 66
 67    def autoloadPatch(self):
 68        return bool(lib.mCoreAutoloadPatch(self._core))
 69
 70    def platform(self):
 71        return self._core.platform(self._core)
 72
 73    def desiredVideoDimensions(self):
 74        width = ffi.new("unsigned*")
 75        height = ffi.new("unsigned*")
 76        self._core.desiredVideoDimensions(self._core, width, height)
 77        return width[0], height[0]
 78
 79    def setVideoBuffer(self, image):
 80        self._core.setVideoBuffer(self._core, image.buffer, image.stride)
 81
 82    def reset(self):
 83        self._core.reset(self._core)
 84
 85    def runFrame(self):
 86        self._core.runFrame(self._core)
 87
 88    def runLoop(self):
 89        self._core.runLoop(self._core)
 90
 91    def step(self):
 92        self._core.step(self._core)
 93
 94    def frameCounter(self):
 95        return self._core.frameCounter(self._core)
 96
 97    def frameCycles(self):
 98        return self._core.frameCycles(self._core)
 99
100    def frequency(self):
101        return self._core.frequency(self._core)
102
103    def getGameTitle(self):
104        title = ffi.new("char[16]")
105        self._core.getGameTitle(self._core, title)
106        return ffi.string(title, 16).decode("ascii")
107
108    def getGameCode(self):
109        code = ffi.new("char[12]")
110        self._core.getGameCode(self._core, code)
111        return ffi.string(code, 12).decode("ascii")
112
113if hasattr(lib, 'PLATFORM_GBA'):
114    from .gba import GBA
115    from .arm import ARMCore
116    Core.PLATFORM_GBA = lib.PLATFORM_GBA
117
118if hasattr(lib, 'PLATFORM_GB'):
119    from .gb import GB
120    from .lr35902 import LR35902Core
121    Core.PLATFORM_GB = lib.PLATFORM_GB