-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathgbtest.mfk
250 lines (224 loc) · 5.62 KB
/
gbtest.mfk
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// compile with -t gb_small
import gb_hardware
import gb_joy
import stdio
array oam_buffer[$a0] align(256)
sbyte sprite_direction
byte old_a
void on_vblank(){
flush_putchar_buffer()
sprite_dma(oam_buffer.addr.hi)
oam_buffer[1] += sprite_direction
react_to_input()
if oam_buffer[1] == 160 { sprite_direction = -1 }
if oam_buffer[1] == 8 { sprite_direction = 1 }
}
void on_lcdc(){
}
void on_timer(){
}
void on_serial(){
}
void on_joypad(){
}
void react_to_input() {
read_joy()
if old_a == 0 && input_a != 0 { // A button
sprite_direction = 0 - sprite_direction
low_c()
}
old_a = input_a
if input_dy < 0 { // Up button
if oam_buffer[0] > 16 { oam_buffer[0] -= 1}
}
if input_dy > 0 { // Down button
if oam_buffer[0] < 152 { oam_buffer[0] += 1}
}
}
void low_c() {
}
void high_c() {
}
void main() {
byte i
for i,0,paralleluntil,$a0 {
oam_buffer[i] = 0
}
reg_obj0_palette = 0q2222
load_charset()
init_putchar()
reg_lcd_ctrl = lcd_tile_8000 | lcd_bg_on | lcd_obj_on | lcd_win_off | lcd_map_9800
enable_lcd()
enable_irq()
putstrz("HELLO WORLD"z)
new_line()
sprite_direction = 1
oam_buffer[0] = 50
oam_buffer[1] = 8
oam_buffer[2] = '@'
oam_buffer[3] = 0
while(true){}
}
void load_charset() {
pointer s
pointer d
s = charset.addr
d = $8200
while s < charset.addr + $300 {
d[0] = s[0]
d += 1
d[0] = s[0]
d += 1
s += 1
}
}
const array charset = [
$00,$00,$00,$00,$00,$00,$00,$00,
$18,$18,$18,$18,$00,$00,$18,$00,
$66,$66,$66,$00,$00,$00,$00,$00,
$66,$66,$FF,$66,$FF,$66,$66,$00,
$18,$3E,$60,$3C,$06,$7C,$18,$00,
$62,$66,$0C,$18,$30,$66,$46,$00,
$3C,$66,$3C,$38,$67,$66,$3F,$00,
$06,$0C,$18,$00,$00,$00,$00,$00,
$0C,$18,$30,$30,$30,$18,$0C,$00,
$30,$18,$0C,$0C,$0C,$18,$30,$00,
$00,$66,$3C,$FF,$3C,$66,$00,$00,
$00,$18,$18,$7E,$18,$18,$00,$00,
$00,$00,$00,$00,$00,$18,$18,$30,
$00,$00,$00,$7E,$00,$00,$00,$00,
$00,$00,$00,$00,$00,$18,$18,$00,
$00,$03,$06,$0C,$18,$30,$60,$00,
$3C,$66,$6E,$76,$66,$66,$3C,$00,
$18,$18,$38,$18,$18,$18,$7E,$00,
$3C,$66,$06,$0C,$30,$60,$7E,$00,
$3C,$66,$06,$1C,$06,$66,$3C,$00,
$06,$0E,$1E,$66,$7F,$06,$06,$00,
$7E,$60,$7C,$06,$06,$66,$3C,$00,
$3C,$66,$60,$7C,$66,$66,$3C,$00,
$7E,$66,$0C,$18,$18,$18,$18,$00,
$3C,$66,$66,$3C,$66,$66,$3C,$00,
$3C,$66,$66,$3E,$06,$66,$3C,$00,
$00,$00,$18,$00,$00,$18,$00,$00,
$00,$00,$18,$00,$00,$18,$18,$30,
$0E,$18,$30,$60,$30,$18,$0E,$00,
$00,$00,$7E,$00,$7E,$00,$00,$00,
$70,$18,$0C,$06,$0C,$18,$70,$00,
$3C,$66,$06,$0C,$18,$00,$18,$00,
$3C,$66,$6E,$6E,$60,$62,$3C,$00,
$18,$3C,$66,$7E,$66,$66,$66,$00,
$7C,$66,$66,$7C,$66,$66,$7C,$00,
$3C,$66,$60,$60,$60,$66,$3C,$00,
$78,$6C,$66,$66,$66,$6C,$78,$00,
$7E,$60,$60,$78,$60,$60,$7E,$00,
$7E,$60,$60,$78,$60,$60,$60,$00,
$3C,$66,$60,$6E,$66,$66,$3C,$00,
$66,$66,$66,$7E,$66,$66,$66,$00,
$3C,$18,$18,$18,$18,$18,$3C,$00,
$1E,$0C,$0C,$0C,$0C,$6C,$38,$00,
$66,$6C,$78,$70,$78,$6C,$66,$00,
$60,$60,$60,$60,$60,$60,$7E,$00,
$63,$77,$7F,$6B,$63,$63,$63,$00,
$66,$76,$7E,$7E,$6E,$66,$66,$00,
$3C,$66,$66,$66,$66,$66,$3C,$00,
$7C,$66,$66,$7C,$60,$60,$60,$00,
$3C,$66,$66,$66,$66,$3C,$0E,$00,
$7C,$66,$66,$7C,$78,$6C,$66,$00,
$3C,$66,$60,$3C,$06,$66,$3C,$00,
$7E,$18,$18,$18,$18,$18,$18,$00,
$66,$66,$66,$66,$66,$66,$3C,$00,
$66,$66,$66,$66,$66,$3C,$18,$00,
$63,$63,$63,$6B,$7F,$77,$63,$00,
$66,$66,$3C,$18,$3C,$66,$66,$00,
$66,$66,$66,$3C,$18,$18,$18,$00,
$7E,$06,$0C,$18,$30,$60,$7E,$00,
$3C,$30,$30,$30,$30,$30,$3C,$00,
$0C,$12,$30,$7C,$30,$62,$FC,$00,
$3C,$0C,$0C,$0C,$0C,$0C,$3C,$00,
$00,$18,$3C,$7E,$18,$18,$18,$18,
$00,$10,$30,$7F,$7F,$30,$10,$00,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
]
// the following is an experimental implementation of text output for Game Boy
// if it matures, it might get mainlined into the standard distribution
array putchar_buffer[256]
byte putchar_buffer_start
byte putchar_buffer_size
byte putchar_column
byte putchar_row
volatile byte putchar_lock
void init_putchar() {
putchar_buffer_start = 0
putchar_buffer_size = 0
putchar_column = 0
putchar_row = 0
putchar_lock = 0
}
asm void putchar(byte e) {
ld a,(putchar_buffer_size)
cp $ff
jr nz, __putchar_do
halt
jr putchar
__putchar_do:
ld b,a
ld a,1
ld (putchar_lock),a
ld a,(putchar_buffer_start)
add a,b
ld c,a
ld b,0
ld hl,putchar_buffer.addr
add hl,bc
ld (hl),e
ld hl,putchar_buffer_size.addr
inc (hl)
xor a
ld (putchar_lock),a
ret
}
// TODO: ???
noinline pointer calculate_cursor() {
pointer p
p = putchar_column
p <<= 5
p += putchar_row
p += $9800
return p
}
void new_line() {
putchar(13)
putchar(10)
}
void flush_putchar_buffer() {
pointer dest
pointer src
if putchar_lock != 0 { return }
if putchar_buffer_size == 0 { return }
src = putchar_buffer.addr + putchar_buffer_start
dest = calculate_cursor()
byte i
byte c
for i,0,paralleluntil,putchar_buffer_size {
c = src[0]
src += 1
if c == 13 {
putchar_column = 0
dest.lo &= $E0
} else if c == 10 {
putchar_row += 1
dest += 32
} else {
dest[0] = c
dest += 1
putchar_column += 1
if putchar_column == 20 {
putchar_column = 0
putchar_row += 1
dest.lo &= $E0
dest += 32
}
}
}
putchar_buffer_start += putchar_buffer_size
putchar_buffer_size = 0
}