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 findVF(vf):
10 core = lib.mCoreFindVF(vf.handle())
11 if core == ffi.NULL:
12 return None
13 return Core(core)
14
15def loadPath(path):
16 core = find(path)
17 if not core or not core.loadFile(path):
18 return None
19 return core
20
21def loadVF(vf):
22 core = findVF(vf)
23 if not core or not core.loadROM(vf):
24 return None
25 return core
26
27class Core:
28 def __init__(self, native):
29 self._core = ffi.gc(native, self._deinit)
30 success = bool(self._core.init(self._core))
31 if not success:
32 raise RuntimeError("Failed to initialize core")
33
34 if hasattr(self, 'PLATFORM_GBA') and self.platform() == self.PLATFORM_GBA:
35 self.cpu = ARMCore(self._core.cpu)
36 self.board = GBA(self._core.board)
37 if hasattr(self, 'PLATFORM_GB') and self.platform() == self.PLATFORM_GB:
38 self.cpu = LR35902Core(self._core.cpu)
39 self.board = GB(self._core.board)
40
41 def _deinit(self):
42 self._core.deinit(self._core)
43
44 def loadFile(self, path):
45 return bool(lib.mCoreLoadFile(self._core, path.encode('UTF-8')))
46
47 def isROM(self, vf):
48 return bool(self._core.isROM(vf.handle()))
49
50 def loadROM(self, vf):
51 return bool(self._core.loadROM(self._core, vf.handle()))
52
53 def autoloadSave(self):
54 return bool(lib.mCoreAutoloadSave(self._core))
55
56 def autoloadPatch(self):
57 return bool(lib.mCoreAutoloadPatch(self._core))
58
59 def platform(self):
60 return self._core.platform(self._core)
61
62 def desiredVideoDimensions(self):
63 width = ffi.new("unsigned*")
64 height = ffi.new("unsigned*")
65 self._core.desiredVideoDimensions(self._core, width, height)
66 return width[0], height[0]
67
68 def reset(self):
69 self._core.reset(self._core)
70
71 def runFrame(self):
72 self._core.runFrame(self._core)
73
74 def runLoop(self):
75 self._core.runLoop(self._core)
76
77 def step(self):
78 self._core.step(self._core)
79
80 def frameCounter(self):
81 return self._core.frameCounter(self._core)
82
83 def frameCycles(self):
84 return self._core.frameCycles(self._core)
85
86 def frequency(self):
87 return self._core.frequency(self._core)
88
89 def getGameTitle(self):
90 title = ffi.new("char[16]")
91 self._core.getGameTitle(self._core, title)
92 return ffi.string(title, 16).decode("ascii")
93
94 def getGameCode(self):
95 code = ffi.new("char[12]")
96 self._core.getGameCode(self._core, code)
97 return ffi.string(code, 12).decode("ascii")
98
99if hasattr(lib, 'PLATFORM_GBA'):
100 from .gba import GBA
101 from .arm import ARMCore
102 Core.PLATFORM_GBA = lib.PLATFORM_GBA
103
104if hasattr(lib, 'PLATFORM_GB'):
105 from .gb import GB
106 from .lr35902 import LR35902Core
107 Core.PLATFORM_GB = lib.PLATFORM_GB