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 setVideoBuffer(self, image):
74 self._core.setVideoBuffer(self._core, image.buffer, image.stride)
75
76 def reset(self):
77 self._core.reset(self._core)
78
79 def runFrame(self):
80 self._core.runFrame(self._core)
81
82 def runLoop(self):
83 self._core.runLoop(self._core)
84
85 def step(self):
86 self._core.step(self._core)
87
88 def frameCounter(self):
89 return self._core.frameCounter(self._core)
90
91 def frameCycles(self):
92 return self._core.frameCycles(self._core)
93
94 def frequency(self):
95 return self._core.frequency(self._core)
96
97 def getGameTitle(self):
98 title = ffi.new("char[16]")
99 self._core.getGameTitle(self._core, title)
100 return ffi.string(title, 16).decode("ascii")
101
102 def getGameCode(self):
103 code = ffi.new("char[12]")
104 self._core.getGameCode(self._core, code)
105 return ffi.string(code, 12).decode("ascii")
106
107if hasattr(lib, 'PLATFORM_GBA'):
108 from .gba import GBA
109 from .arm import ARMCore
110 Core.PLATFORM_GBA = lib.PLATFORM_GBA
111
112if hasattr(lib, 'PLATFORM_GB'):
113 from .gb import GB
114 from .lr35902 import LR35902Core
115 Core.PLATFORM_GB = lib.PLATFORM_GB