all repos — mgba @ f7c935f217370c86078e3c5a7603f65cb87c092c

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