-
Notifications
You must be signed in to change notification settings - Fork 2
/
board-gameboy-advance.go
161 lines (128 loc) · 3.41 KB
/
board-gameboy-advance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//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<<gba.DISPCNT_BGMODE_Pos |
gba.DISPCNT_SCREENDISPLAY_BG2_ENABLE<<gba.DISPCNT_SCREENDISPLAY_BG2_Pos)
return gbaDisplay{}
}
func (d mainDisplay) MaxBrightness() int {
return 0
}
func (d mainDisplay) SetBrightness(level int) {
// The display doesn't have a backlight.
}
func (d mainDisplay) WaitForVBlank(time.Duration) {
// Wait until the VBlank flag is set.
// TODO: sleep until the next VBlank instead of busy waiting.
// (See VBlankIntrWait)
for gba.DISP.DISPSTAT.Get()&(1<<gba.DISPSTAT_VBLANK_Pos) == 0 {
}
}
func (d mainDisplay) ConfigureTouch() TouchInput {
return noTouch{}
}
type gbaDisplay struct{}
var displayFrameBuffer = (*[160 * 240]volatile.Register16)(unsafe.Pointer(uintptr(gba.MEM_VRAM)))
var errOutOfBounds = errors.New("rectangle coordinates outside display area")
const (
displayWidth = 240
displayHeight = 160
)
func (d gbaDisplay) Size() (x, y int16) {
return displayWidth, displayHeight
}
func (d gbaDisplay) Display() error {
// Nothing to do here.
return nil
}
func (d gbaDisplay) DrawBitmap(x, y int16, buf pixel.Image[pixel.RGB555]) error {
width, height := buf.Size()
if x < 0 || y < 0 || int(x)+width > 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<<index) == 0 {
// The button state change was from 1 to 0, so it was released.
e |= keyReleased
}
// This button event was read, so mark it as such.
// By toggling the bit, the bit will be set to the value that is currently
// in b.state.
b.previousState ^= (1 << index)
return e
}