all repos — mgba @ 471bbf1da5ace6f817c666e8520dfbb17442f688

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._init(core)
 15
 16def findVF(vf):
 17    core = lib.mCoreFindVF(vf.handle)
 18    if core == ffi.NULL:
 19        return None
 20    return Core._init(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(object):
 35    def __init__(self, native):
 36        self._core = native
 37
 38    @cached_property
 39    def tiles(self):
 40        return tile.TileView(self)
 41
 42    @classmethod
 43    def _init(cls, native):
 44        core = ffi.gc(native, native.deinit)
 45        success = bool(core.init(core))
 46        if not success:
 47            raise RuntimeError("Failed to initialize core")
 48        if hasattr(cls, 'PLATFORM_GBA') and core.platform(core) == cls.PLATFORM_GBA:
 49            return GBA(core)
 50        if hasattr(cls, 'PLATFORM_GB') and core.platform(core) == cls.PLATFORM_GB:
 51            return GB(core)
 52        return Core(core)
 53
 54    def _deinit(self):
 55        self._core.deinit(self._core)
 56
 57    def loadFile(self, path):
 58        return bool(lib.mCoreLoadFile(self._core, path.encode('UTF-8')))
 59
 60    def isROM(self, vf):
 61        return bool(self._core.isROM(vf.handle))
 62
 63    def loadROM(self, vf):
 64        return bool(self._core.loadROM(self._core, vf.handle))
 65
 66    def autoloadSave(self):
 67        return bool(lib.mCoreAutoloadSave(self._core))
 68
 69    def autoloadPatch(self):
 70        return bool(lib.mCoreAutoloadPatch(self._core))
 71
 72    def platform(self):
 73        return self._core.platform(self._core)
 74
 75    def desiredVideoDimensions(self):
 76        width = ffi.new("unsigned*")
 77        height = ffi.new("unsigned*")
 78        self._core.desiredVideoDimensions(self._core, width, height)
 79        return width[0], height[0]
 80
 81    def setVideoBuffer(self, image):
 82        self._core.setVideoBuffer(self._core, image.buffer, image.stride)
 83
 84    def reset(self):
 85        self._core.reset(self._core)
 86
 87    def runFrame(self):
 88        self._core.runFrame(self._core)
 89
 90    def runLoop(self):
 91        self._core.runLoop(self._core)
 92
 93    def step(self):
 94        self._core.step(self._core)
 95
 96    def frameCounter(self):
 97        return self._core.frameCounter(self._core)
 98
 99    def frameCycles(self):
100        return self._core.frameCycles(self._core)
101
102    def frequency(self):
103        return self._core.frequency(self._core)
104
105    def getGameTitle(self):
106        title = ffi.new("char[16]")
107        self._core.getGameTitle(self._core, title)
108        return ffi.string(title, 16).decode("ascii")
109
110    def getGameCode(self):
111        code = ffi.new("char[12]")
112        self._core.getGameCode(self._core, code)
113        return ffi.string(code, 12).decode("ascii")
114
115if hasattr(lib, 'PLATFORM_GBA'):
116    from .gba import GBA
117    Core.PLATFORM_GBA = lib.PLATFORM_GBA
118
119if hasattr(lib, 'PLATFORM_GB'):
120    from .gb import GB
121    from .lr35902 import LR35902Core
122    Core.PLATFORM_GB = lib.PLATFORM_GB