all repos — mgba @ ba2a31c3f2ff5ef6fcc44570dfda7b4911704b25

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 _initCache(self, cache):
 31        lib.GBVideoCacheInit(cache)
 32        lib.GBVideoCacheAssociate(cache, ffi.addressof(self._native.video))
 33
 34    def _deinitCache(self, cache):
 35        self._native.video.renderer.cache = ffi.NULL
 36        lib.mCacheSetDeinit(cache)
 37
 38    def reset(self):
 39        super(GB, self).reset()
 40        self.memory = GBMemory(self._core)
 41
 42    def attachSIO(self, link):
 43        lib.GBSIOSetDriver(ffi.addressof(self._native.sio), link._native)
 44
 45createCallback("GBSIOPythonDriver", "init")
 46createCallback("GBSIOPythonDriver", "deinit")
 47createCallback("GBSIOPythonDriver", "writeSB")
 48createCallback("GBSIOPythonDriver", "writeSC")
 49
 50class GBSIODriver(object):
 51    def __init__(self):
 52        self._handle = ffi.new_handle(self)
 53        self._native = ffi.gc(lib.GBSIOPythonDriverCreate(self._handle), lib.free)
 54
 55    def init(self):
 56        return True
 57
 58    def deinit(self):
 59        pass
 60
 61    def writeSB(self, value):
 62        pass
 63
 64    def writeSC(self, value):
 65        return value
 66
 67class GBSIOSimpleDriver(GBSIODriver):
 68    def __init__(self, period=0x100):
 69        super(GBSIOSimpleDriver, self).__init__()
 70        self.rx = 0x00
 71        self._period = period
 72
 73    def init(self):
 74        self._native.p.period = self._period
 75        return True
 76
 77    def writeSB(self, value):
 78        self.rx = value
 79
 80    def writeSC(self, value):
 81        self._native.p.period = self._period
 82        if value & 0x80:
 83            lib.mTimingDeschedule(ffi.addressof(self._native.p.p.timing), ffi.addressof(self._native.p.event))
 84            lib.mTimingSchedule(ffi.addressof(self._native.p.p.timing), ffi.addressof(self._native.p.event), self._native.p.period)
 85        return value
 86
 87    def isReady(self):
 88        return not self._native.p.remainingBits
 89
 90    @property
 91    def tx(self):
 92        self._native.p.pendingSB
 93
 94    @property
 95    def period(self):
 96        return self._native.p.period
 97
 98    @tx.setter
 99    def tx(self, newTx):
100        self._native.p.pendingSB = newTx
101        self._native.p.remainingBits = 8
102
103    @period.setter
104    def period(self, newPeriod):
105        self._period = newPeriod
106        if self._native.p:
107            self._native.p.period = newPeriod
108
109class GBMemory(Memory):
110    def __init__(self, core):
111        super(GBMemory, self).__init__(core, 0x10000)
112
113        self.cart = Memory(core, lib.GB_SIZE_CART_BANK0 * 2, lib.GB_BASE_CART_BANK0)
114        self.vram = Memory(core, lib.GB_SIZE_VRAM, lib.GB_BASE_VRAM)
115        self.sram = Memory(core, lib.GB_SIZE_EXTERNAL_RAM, lib.GB_REGION_EXTERNAL_RAM)
116        self.iwram = Memory(core, lib.GB_SIZE_WORKING_RAM_BANK0, lib.GB_BASE_WORKING_RAM_BANK0)
117        self.oam = Memory(core, lib.GB_SIZE_OAM, lib.GB_BASE_OAM)
118        self.io = Memory(core, lib.GB_SIZE_IO, lib.GB_BASE_IO)
119        self.hram = Memory(core, lib.GB_SIZE_HRAM, lib.GB_BASE_HRAM)
120
121class GBSprite(Sprite):
122    PALETTE_BASE = 8,
123
124    def __init__(self, obj, core):
125        self.x = obj.x
126        self.y = obj.y
127        self.tile = obj.tile
128        self._attr = obj.attr
129        self.width = 8
130        lcdc = core._native.memory.io[0x40]
131        self.height = 16 if lcdc & 4 else 8
132        if core._native.model >= lib.GB_MODEL_CGB:
133            if self._attr & 8:
134                self.tile += 512
135            self.paletteId = self._attr & 7
136        else:
137            self.paletteId = (self._attr >> 4) & 1
138        self.paletteId += 8
139
140
141class GBObjs:
142    def __init__(self, core):
143        self._core = core
144        self._obj = core._native.video.oam.obj
145
146    def __len__(self):
147        return 40
148
149    def __getitem__(self, index):
150        if index >= len(self):
151            raise IndexError()
152        sprite = GBSprite(self._obj[index], self._core)
153        sprite.constitute(self._core.tiles[0], 0)
154        return sprite