all repos — mgba @ adc45c9bdc2b20d1a439a9a21945a870f85adb15

mGBA Game Boy Advance Emulator

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
 9from .tile import Sprite
10
11class GBA(Core):
12    def __init__(self, native):
13        super(GBA, self).__init__(native)
14        self._native = ffi.cast("struct GBA*", native.board)
15        self.sprites = GBAObjs(self)
16        self.cpu = ARMCore(self._core.cpu)
17
18    def _initTileCache(self, cache):
19        lib.GBAVideoTileCacheInit(cache)
20        lib.GBAVideoTileCacheAssociate(cache, ffi.addressof(self._native.video))
21
22    def _deinitTileCache(self, cache):
23        self._native.video.renderer.cache = ffi.NULL
24        lib.mTileCacheDeinit(cache)
25
26class GBASprite(Sprite):
27    TILE_BASE = 0x800, 0x400
28    PALETTE_BASE = 0x10, 1
29
30    def __init__(self, obj):
31        self._a = obj.a
32        self._b = obj.b
33        self._c = obj.c
34        self.x = self._b & 0x1FF
35        self.y = self._a & 0xFF
36        self._shape = self._a >> 14
37        self._size = self._b >> 14
38        self._256Color = bool(self._a & 0x2000)
39        self.width, self.height = lib.GBAVideoObjSizes[self._shape * 4 + self._size]
40        self.tile = self._c & 0x3FF
41        if self._256Color:
42            self.paletteId = 0
43            self.tile >>= 1
44        else:
45            self.paletteId = self._c >> 12
46
47class GBAObjs:
48    def __init__(self, core):
49        self._core = core
50        self._obj = core._native.video.oam.obj
51
52    def __len__(self):
53        return 128
54
55    def __getitem__(self, index):
56        if index >= len(self):
57            raise IndexError()
58        sprite = GBASprite(self._obj[index])
59        map1D = bool(self._core._native.memory.io[0] & 0x40)
60        sprite.constitute(self._core.tiles, 0 if map1D else 0x20, int(sprite._256Color))
61        return sprite