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, createCallback
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
34def needsReset(f):
35 def wrapper(self, *args, **kwargs):
36 if not self._wasReset:
37 raise RuntimeError("Core must be reset first")
38 return f(self, *args, **kwargs)
39 return wrapper
40
41def protected(f):
42 def wrapper(self, *args, **kwargs):
43 if self._protected:
44 raise RuntimeError("Core is protected")
45 return f(self, *args, **kwargs)
46 return wrapper
47
48@ffi.def_extern()
49def _mCorePythonCallbacksVideoFrameStarted(user):
50 context = ffi.from_handle(user)
51 context._videoFrameStarted()
52
53@ffi.def_extern()
54def _mCorePythonCallbacksVideoFrameEnded(user):
55 context = ffi.from_handle(user)
56 context._videoFrameEnded()
57
58@ffi.def_extern()
59def _mCorePythonCallbacksCoreCrashed(user):
60 context = ffi.from_handle(user)
61 context._coreCrashed()
62
63@ffi.def_extern()
64def _mCorePythonCallbacksSleep(user):
65 context = ffi.from_handle(user)
66 context._sleep()
67
68class CoreCallbacks(object):
69 def __init__(self):
70 self._handle = ffi.new_handle(self)
71 self.videoFrameStarted = []
72 self.videoFrameEnded = []
73 self.coreCrashed = []
74 self.sleep = []
75 self.context = lib.mCorePythonCallbackCreate(self._handle)
76
77 def _videoFrameStarted(self):
78 for cb in self.videoFrameStarted:
79 cb()
80
81 def _videoFrameEnded(self):
82 for cb in self.videoFrameEnded:
83 cb()
84
85 def _coreCrashed(self):
86 for cb in self.coreCrashed:
87 cb()
88
89 def _sleep(self):
90 for cb in self.sleep:
91 cb()
92
93class Core(object):
94 def __init__(self, native):
95 self._core = native
96 self._wasReset = False
97 self._protected = False
98 self._callbacks = CoreCallbacks()
99 self._core.addCoreCallbacks(self._core, self._callbacks.context)
100
101 @cached_property
102 def tiles(self):
103 return tile.TileView(self)
104
105 @classmethod
106 def _init(cls, native):
107 core = ffi.gc(native, native.deinit)
108 success = bool(core.init(core))
109 lib.mCoreInitConfig(core, ffi.NULL)
110 if not success:
111 raise RuntimeError("Failed to initialize core")
112 return cls._detect(core)
113
114 def _deinit(self):
115 self._core.deinit(self._core)
116
117 @classmethod
118 def _detect(cls, core):
119 if hasattr(cls, 'PLATFORM_GBA') and core.platform(core) == cls.PLATFORM_GBA:
120 return GBA(core)
121 if hasattr(cls, 'PLATFORM_GB') and core.platform(core) == cls.PLATFORM_GB:
122 return GB(core)
123 return Core(core)
124
125 def loadFile(self, path):
126 return bool(lib.mCoreLoadFile(self._core, path.encode('UTF-8')))
127
128 def isROM(self, vf):
129 return bool(self._core.isROM(vf.handle))
130
131 def loadROM(self, vf):
132 return bool(self._core.loadROM(self._core, vf.handle))
133
134 def loadSave(self, vf):
135 return bool(self._core.loadSave(self._core, vf.handle))
136
137 def loadTemporarySave(self, vf):
138 return bool(self._core.loadTemporarySave(self._core, vf.handle))
139
140 def loadPatch(self, vf):
141 return bool(self._core.loadPatch(self._core, vf.handle))
142
143 def autoloadSave(self):
144 return bool(lib.mCoreAutoloadSave(self._core))
145
146 def autoloadPatch(self):
147 return bool(lib.mCoreAutoloadPatch(self._core))
148
149 def platform(self):
150 return self._core.platform(self._core)
151
152 def desiredVideoDimensions(self):
153 width = ffi.new("unsigned*")
154 height = ffi.new("unsigned*")
155 self._core.desiredVideoDimensions(self._core, width, height)
156 return width[0], height[0]
157
158 def setVideoBuffer(self, image):
159 self._core.setVideoBuffer(self._core, image.buffer, image.stride)
160
161 def reset(self):
162 self._core.reset(self._core)
163 self._wasReset = True
164
165 @needsReset
166 @protected
167 def runFrame(self):
168 self._core.runFrame(self._core)
169
170 @needsReset
171 @protected
172 def runLoop(self):
173 self._core.runLoop(self._core)
174
175 @needsReset
176 def step(self):
177 self._core.step(self._core)
178
179 @staticmethod
180 def _keysToInt(*args, **kwargs):
181 keys = 0
182 if 'raw' in kwargs:
183 keys = kwargs['raw']
184 for key in args:
185 keys |= 1 << key
186 return keys
187
188 def setKeys(self, *args, **kwargs):
189 self._core.setKeys(self._core, self._keysToInt(*args, **kwargs))
190
191 def addKeys(self, *args, **kwargs):
192 self._core.addKeys(self._core, self._keysToInt(*args, **kwargs))
193
194 def clearKeys(self, *args, **kwargs):
195 self._core.clearKeys(self._core, self._keysToInt(*args, **kwargs))
196
197 @property
198 @needsReset
199 def frameCounter(self):
200 return self._core.frameCounter(self._core)
201
202 @property
203 def frameCycles(self):
204 return self._core.frameCycles(self._core)
205
206 @property
207 def frequency(self):
208 return self._core.frequency(self._core)
209
210 @property
211 def gameTitle(self):
212 title = ffi.new("char[16]")
213 self._core.getGameTitle(self._core, title)
214 return ffi.string(title, 16).decode("ascii")
215
216 @property
217 def gameCode(self):
218 code = ffi.new("char[12]")
219 self._core.getGameCode(self._core, code)
220 return ffi.string(code, 12).decode("ascii")
221
222 def addFrameCallback(self, cb):
223 self._callbacks.videoFrameEnded.append(cb)
224
225class ICoreOwner(object):
226 def claim(self):
227 raise NotImplementedError
228
229 def release(self):
230 raise NotImplementedError
231
232 def __enter__(self):
233 self.core = self.claim()
234 self.core._protected = True
235 return self.core
236
237 def __exit__(self, type, value, traceback):
238 self.core._protected = False
239 self.release()
240
241class IRunner(object):
242 def pause(self):
243 raise NotImplementedError
244
245 def unpause(self):
246 raise NotImplementedError
247
248 def useCore(self):
249 raise NotImplementedError
250
251 def isRunning(self):
252 raise NotImplementedError
253
254 def isPaused(self):
255 raise NotImplementedError
256
257if hasattr(lib, 'PLATFORM_GBA'):
258 from .gba import GBA
259 Core.PLATFORM_GBA = lib.PLATFORM_GBA
260
261if hasattr(lib, 'PLATFORM_GB'):
262 from .gb import GB
263 from .lr35902 import LR35902Core
264 Core.PLATFORM_GB = lib.PLATFORM_GB