src/platform/python/mgba/gba.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 .arm import ARMCore
8from .core import Core, needsReset
9from .tile import Sprite
10from .memory import Memory
11from . import createCallback
12
13class GBA(Core):
14 KEY_A = lib.GBA_KEY_A
15 KEY_B = lib.GBA_KEY_B
16 KEY_SELECT = lib.GBA_KEY_SELECT
17 KEY_START = lib.GBA_KEY_START
18 KEY_DOWN = lib.GBA_KEY_DOWN
19 KEY_UP = lib.GBA_KEY_UP
20 KEY_LEFT = lib.GBA_KEY_LEFT
21 KEY_RIGHT = lib.GBA_KEY_RIGHT
22 KEY_L = lib.GBA_KEY_L
23 KEY_R = lib.GBA_KEY_R
24
25 SIO_NORMAL_8 = lib.SIO_NORMAL_8
26 SIO_NORMAL_32 = lib.SIO_NORMAL_32
27 SIO_MULTI = lib.SIO_MULTI
28 SIO_UART = lib.SIO_UART
29 SIO_GPIO = lib.SIO_GPIO
30
31 def __init__(self, native):
32 super(GBA, self).__init__(native)
33 self._native = ffi.cast("struct GBA*", native.board)
34 self.sprites = GBAObjs(self)
35 self.cpu = ARMCore(self._core.cpu)
36
37 @needsReset
38 def _initCache(self, cache):
39 lib.GBAVideoCacheInit(cache)
40 lib.GBAVideoCacheAssociate(cache, ffi.addressof(self._native.video))
41
42 def _deinitCache(self, cache):
43 self._native.video.renderer.cache = ffi.NULL
44 lib.mCacheSetDeinit(cache)
45
46 def reset(self):
47 super(GBA, self).reset()
48 self.memory = GBAMemory(self._core, self._native.memory.romSize)
49
50 def attachSIO(self, link, mode=lib.SIO_MULTI):
51 lib.GBASIOSetDriver(ffi.addressof(self._native.sio), link._native, mode)
52
53createCallback("GBASIOPythonDriver", "init")
54createCallback("GBASIOPythonDriver", "deinit")
55createCallback("GBASIOPythonDriver", "load")
56createCallback("GBASIOPythonDriver", "unload")
57createCallback("GBASIOPythonDriver", "writeRegister")
58
59class GBASIODriver(object):
60 def __init__(self):
61 self._handle = ffi.new_handle(self)
62 self._native = ffi.gc(lib.GBASIOPythonDriverCreate(self._handle), lib.free)
63
64 def init(self):
65 return True
66
67 def deinit(self):
68 pass
69
70 def load(self):
71 return True
72
73 def unload(self):
74 return True
75
76 def writeRegister(self, address, value):
77 return value
78
79class GBAMemory(Memory):
80 def __init__(self, core, romSize=lib.SIZE_CART0):
81 super(GBAMemory, self).__init__(core, 0x100000000)
82
83 self.bios = Memory(core, lib.SIZE_BIOS, lib.BASE_BIOS)
84 self.wram = Memory(core, lib.SIZE_WORKING_RAM, lib.BASE_WORKING_RAM)
85 self.iwram = Memory(core, lib.SIZE_WORKING_IRAM, lib.BASE_WORKING_IRAM)
86 self.io = Memory(core, lib.SIZE_IO, lib.BASE_IO)
87 self.palette = Memory(core, lib.SIZE_PALETTE_RAM, lib.BASE_PALETTE_RAM)
88 self.vram = Memory(core, lib.SIZE_VRAM, lib.BASE_VRAM)
89 self.oam = Memory(core, lib.SIZE_OAM, lib.BASE_OAM)
90 self.cart0 = Memory(core, romSize, lib.BASE_CART0)
91 self.cart1 = Memory(core, romSize, lib.BASE_CART1)
92 self.cart2 = Memory(core, romSize, lib.BASE_CART2)
93 self.cart = self.cart0
94 self.rom = self.cart0
95 self.sram = Memory(core, lib.SIZE_CART_SRAM, lib.BASE_CART_SRAM)
96
97class GBASprite(Sprite):
98 TILE_BASE = 0x800, 0x400
99 PALETTE_BASE = 0x10, 1
100
101 def __init__(self, obj):
102 self._a = obj.a
103 self._b = obj.b
104 self._c = obj.c
105 self.x = self._b & 0x1FF
106 self.y = self._a & 0xFF
107 self._shape = self._a >> 14
108 self._size = self._b >> 14
109 self._256Color = bool(self._a & 0x2000)
110 self.width, self.height = lib.GBAVideoObjSizes[self._shape * 4 + self._size]
111 self.tile = self._c & 0x3FF
112 if self._256Color:
113 self.paletteId = 0
114 self.tile >>= 1
115 else:
116 self.paletteId = self._c >> 12
117
118class GBAObjs:
119 def __init__(self, core):
120 self._core = core
121 self._obj = core._native.video.oam.obj
122
123 def __len__(self):
124 return 128
125
126 def __getitem__(self, index):
127 if index >= len(self):
128 raise IndexError()
129 sprite = GBASprite(self._obj[index])
130 tiles = self._core.tiles[3 if sprite._256Color else 2]
131 map1D = bool(self._core._native.memory.io[0] & 0x40)
132 sprite.constitute(tiles, 0 if map1D else 0x20)
133 return sprite