all repos — mgba @ 362c572009ac74f34eda03b4b4b7b5f93faa1daf

mGBA Game Boy Advance Emulator

src/platform/python/mgba/gb.py (view raw)

  1# Copyright (c) 2013-2017 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
 11from . import createCallback
 12
 13class GB(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
 23    def __init__(self, native):
 24        super(GB, self).__init__(native)
 25        self._native = ffi.cast("struct GB*", native.board)
 26        self.sprites = GBObjs(self)
 27        self.cpu = LR35902Core(self._core.cpu)
 28
 29    @needsReset
 30    def _initTileCache(self, cache):
 31        lib.GBVideoTileCacheInit(cache)
 32        lib.GBVideoTileCacheAssociate(cache, ffi.addressof(self._native.video))
 33
 34    def _deinitTileCache(self, cache):
 35        self._native.video.renderer.cache = ffi.NULL
 36        lib.mTileCacheDeinit(cache)
 37
 38    def attachSIO(self, link):
 39        lib.GBSIOSetDriver(ffi.addressof(self._native.sio), link._native)
 40
 41createCallback("GBSIOPythonDriver", "init")
 42createCallback("GBSIOPythonDriver", "deinit")
 43createCallback("GBSIOPythonDriver", "writeSB")
 44createCallback("GBSIOPythonDriver", "writeSC")
 45
 46class GBSIODriver(object):
 47    def __init__(self):
 48        self._handle = ffi.new_handle(self)
 49        self._native = ffi.gc(lib.GBSIOPythonDriverCreate(self._handle), lib.free)
 50
 51    def init(self):
 52        return True
 53
 54    def deinit(self):
 55        pass
 56
 57    def writeSB(self, value):
 58        pass
 59
 60    def writeSC(self, value):
 61        return value
 62
 63class GBSIOSimpleDriver(GBSIODriver):
 64    def __init__(self):
 65        super(GBSIOSimpleDriver, self).__init__()
 66        self.tx = 0xFF
 67        self.rx = 0xFF
 68
 69    def writeSB(self, value):
 70        self.rx = value
 71
 72    def schedule(self, period=0x100, when=0):
 73        self._native.p.remainingBits = 8
 74        self._native.p.period = period
 75        self._native.p.pendingSB = self.tx
 76        self.tx = 0xFF
 77        lib.mTimingDeschedule(ffi.addressof(self._native.p.p.timing), ffi.addressof(self._native.p.event))
 78        lib.mTimingSchedule(ffi.addressof(self._native.p.p.timing), ffi.addressof(self._native.p.event), when)
 79
 80class GBMemory(Memory):
 81    def __init__(self, core):
 82        super(GBMemory, self).__init__(core, 0x10000)
 83
 84        self.cart = Memory(core, lib.GB_SIZE_CART_BANK0 * 2, lib.GB_BASE_CART_BANK0)
 85        self.vram = Memory(core, lib.GB_SIZE_VRAM, lib.GB_BASE_VRAM)
 86        self.sram = Memory(core, lib.GB_SIZE_EXTERNAL_RAM, lib.GB_REGION_EXTERNAL_RAM)
 87        self.iwram = Memory(core, lib.GB_SIZE_WORKING_RAM_BANK0, lib.GB_BASE_WORKING_RAM_BANK0)
 88        self.oam = Memory(core, lib.GB_SIZE_OAM, lib.GB_BASE_OAM)
 89        self.io = Memory(core, lib.GB_SIZE_IO, lib.GB_BASE_IO)
 90        self.hram = Memory(core, lib.GB_SIZE_HRAM, lib.GB_BASE_HRAM)
 91
 92class GBSprite(Sprite):
 93    PALETTE_BASE = 8,
 94
 95    def __init__(self, obj, core):
 96        self.x = obj.x
 97        self.y = obj.y
 98        self.tile = obj.tile
 99        self._attr = obj.attr
100        self.width = 8
101        lcdc = core._native.memory.io[0x40]
102        self.height = 16 if lcdc & 4 else 8
103        if core._native.model >= lib.GB_MODEL_CGB:
104            if self._attr & 8:
105                self.tile += 512
106            self.paletteId = self._attr & 7
107        else:
108            self.paletteId = (self._attr >> 4) & 1
109
110
111class GBObjs:
112    def __init__(self, core):
113        self._core = core
114        self._obj = core._native.video.oam.obj
115
116    def __len__(self):
117        return 40
118
119    def __getitem__(self, index):
120        if index >= len(self):
121            raise IndexError()
122        sprite = GBSprite(self._obj[index], self._core)
123        sprite.constitute(self._core.tiles, 0, 0)
124        return sprite