src/platform/python/mgba/core.py (view raw)
1from _pylib import ffi, lib
2
3def find(path):
4 core = lib.mCoreFind(path.encode('UTF-8'))
5 if core == ffi.NULL:
6 return None
7 return Core(core)
8
9def loadPath(path):
10 core = find(path)
11 if not core or not core.loadFile(path):
12 return None
13 return core
14
15class Core:
16 def __init__(self, native):
17 self._core = ffi.gc(native, self._deinit)
18 success = bool(self._core.init(self._core))
19 if not success:
20 raise RuntimeError("Failed to initialize core")
21
22 if hasattr(self, 'PLATFORM_GBA') and self.platform() == self.PLATFORM_GBA:
23 self.cpu = ARMCore(self._core.cpu)
24 self.board = GBA(self._core.board)
25 if hasattr(self, 'PLATFORM_GB') and self.platform() == self.PLATFORM_GB:
26 self.cpu = LR35902Core(self._core.cpu)
27 self.board = GB(self._core.board)
28
29 def _deinit(self):
30 self._core.deinit(self._core)
31
32 def loadFile(self, path):
33 return bool(lib.mCoreLoadFile(self._core, path.encode('UTF-8')))
34
35 def autoloadSave(self):
36 return bool(lib.mCoreAutoloadSave(self._core))
37
38 def autoloadPatch(self):
39 return bool(lib.mCoreAutoloadPatch(self._core))
40
41 def platform(self):
42 return self._core.platform(self._core)
43
44 def desiredVideoDimensions(self):
45 width = ffi.new("unsigned*")
46 height = ffi.new("unsigned*")
47 self._core.desiredVideoDimensions(self._core, width, height)
48 return width[0], height[0]
49
50 def reset(self):
51 self._core.reset(self._core)
52
53 def runFrame(self):
54 self._core.runFrame(self._core)
55
56 def runLoop(self):
57 self._core.runLoop(self._core)
58
59 def step(self):
60 self._core.step(self._core)
61
62 def frameCounter(self):
63 return self._core.frameCounter(self._core)
64
65 def frameCycles(self):
66 return self._core.frameCycles(self._core)
67
68 def frequency(self):
69 return self._core.frequency(self._core)
70
71 def getGameTitle(self):
72 title = ffi.new("char[16]")
73 self._core.getGameTitle(self._core, title)
74 return ffi.string(title, 16).decode("ascii")
75
76 def getGameCode(self):
77 code = ffi.new("char[12]")
78 self._core.getGameCode(self._core, code)
79 return ffi.string(code, 12).decode("ascii")
80
81if hasattr(lib, 'PLATFORM_GBA'):
82 from .gba import GBA
83 from .arm import ARMCore
84 Core.PLATFORM_GBA = lib.PLATFORM_GBA
85
86if hasattr(lib, 'PLATFORM_GB'):
87 from .gb import GB
88 from .lr35902 import LR35902Core
89 Core.PLATFORM_GB = lib.PLATFORM_GB