src/platform/python/mgba/gb.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 .lr35902 import LR35902Core
8from .core import Core, needsReset
9from .memory import Memory
10from .tile import Sprite
11
12class GB(Core):
13 KEY_A = lib.GBA_KEY_A
14 KEY_B = lib.GBA_KEY_B
15 KEY_SELECT = lib.GBA_KEY_SELECT
16 KEY_START = lib.GBA_KEY_START
17 KEY_DOWN = lib.GBA_KEY_DOWN
18 KEY_UP = lib.GBA_KEY_UP
19 KEY_LEFT = lib.GBA_KEY_LEFT
20 KEY_RIGHT = lib.GBA_KEY_RIGHT
21
22 def __init__(self, native):
23 super(GB, self).__init__(native)
24 self._native = ffi.cast("struct GB*", native.board)
25 self.sprites = GBObjs(self)
26 self.cpu = LR35902Core(self._core.cpu)
27
28 @needsReset
29 def _initTileCache(self, cache):
30 lib.GBVideoTileCacheInit(cache)
31 lib.GBVideoTileCacheAssociate(cache, ffi.addressof(self._native.video))
32
33 def _deinitTileCache(self, cache):
34 self._native.video.renderer.cache = ffi.NULL
35 lib.mTileCacheDeinit(cache)
36
37class GBMemory(Memory):
38 def __init__(self, core):
39 super(GBMemory, self).__init__(core, 0x10000)
40
41 self.cart = Memory(core, lib.GB_SIZE_CART_BANK0 * 2, lib.GB_BASE_CART_BANK0)
42 self.vram = Memory(core, lib.GB_SIZE_VRAM, lib.GB_BASE_VRAM)
43 self.sram = Memory(core, lib.GB_SIZE_EXTERNAL_RAM, lib.GB_REGION_EXTERNAL_RAM)
44 self.iwram = Memory(core, lib.GB_SIZE_WORKING_RAM_BANK0, lib.GB_BASE_WORKING_RAM_BANK0)
45 self.oam = Memory(core, lib.GB_SIZE_OAM, lib.GB_BASE_OAM)
46 self.io = Memory(core, lib.GB_SIZE_IO, lib.GB_BASE_IO)
47 self.hram = Memory(core, lib.GB_SIZE_HRAM, lib.GB_BASE_HRAM)
48
49class GBSprite(Sprite):
50 PALETTE_BASE = 8,
51
52 def __init__(self, obj, core):
53 self.x = obj.x
54 self.y = obj.y
55 self.tile = obj.tile
56 self._attr = obj.attr
57 self.width = 8
58 lcdc = core._native.memory.io[0x40]
59 self.height = 16 if lcdc & 4 else 8
60 if core._native.model >= lib.GB_MODEL_CGB:
61 if self._attr & 8:
62 self.tile += 512
63 self.paletteId = self._attr & 7
64 else:
65 self.paletteId = (self._attr >> 4) & 1
66
67
68class GBObjs:
69 def __init__(self, core):
70 self._core = core
71 self._obj = core._native.video.oam.obj
72
73 def __len__(self):
74 return 40
75
76 def __getitem__(self, index):
77 if index >= len(self):
78 raise IndexError()
79 sprite = GBSprite(self._obj[index], self._core)
80 sprite.constitute(self._core.tiles, 0, 0)
81 return sprite