//go:build gameboyadvance package board import ( "device/gba" "errors" "math/bits" "runtime/volatile" "time" "unsafe" "tinygo.org/x/drivers" "tinygo.org/x/drivers/pixel" ) const ( Name = "gameboy-advance" ) var ( Power = dummyBattery{state: UnknownBattery} Sensors = baseSensors{} Display = mainDisplay{} Buttons = &gbaButtons{} ) type mainDisplay struct{} func (d mainDisplay) PPI() int { return 99 } func (d mainDisplay) Configure() Displayer[pixel.RGB555] { // Use video mode 3 (in BG2, a 16bpp bitmap in VRAM) and Enable BG2. gba.DISP.DISPCNT.Set(gba.DISPCNT_BGMODE_3< displayWidth || int(y)+height > displayHeight { return errOutOfBounds } // TODO: try to do a 4-byte memcpy if possible. That should significantly // speed up the copying of this image. for bufY := 0; bufY < int(height); bufY++ { for bufX := 0; bufX < int(width); bufX++ { val := buf.Get(bufX, bufY) displayFrameBuffer[(int(y)+bufY)*240+int(x)+bufX].Set(uint16(val)) } } return nil } func (d gbaDisplay) Sleep(sleepEnabled bool) error { return nil // nothign to do here } var errNoRotation = errors.New("error: SetRotation isn't supported") func (d gbaDisplay) Rotation() drivers.Rotation { return drivers.Rotation0 } func (d gbaDisplay) SetRotation(rotation drivers.Rotation) error { return errNoRotation } type gbaButtons struct { state uint16 previousState uint16 } func (b *gbaButtons) Configure() { // nothing to configure } func (b *gbaButtons) ReadInput() { b.state = gba.KEY.INPUT.Get() ^ 0x3ff } var codes = [16]Key{ KeyA, KeyB, KeySelect, KeyStart, KeyRight, KeyLeft, KeyUp, KeyDown, KeyR, KeyL, } func (b *gbaButtons) NextEvent() KeyEvent { // The xor between the previous state and the current state is the buttons // that changed. change := b.state ^ b.previousState if change == 0 { return NoKeyEvent } // Find the index of the button with the lowest index that changed state. index := bits.TrailingZeros32(uint32(change)) e := KeyEvent(codes[index]) if b.state&(1<