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