gb_test2

view test.asm @ 6:6a7eb418caec

even better chessboard
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 12 Jan 2019 00:05:14 +0200
parents be6f719279b6
children
line source
1 ; vi:ft=rgbasm:
2 ; -------- build options ----------
3 BUILD_CHESS equ 1
4 BUILD_LOGO equ 2
6 BUILD = BUILD_LOGO
7 ; ---------------------------------
9 include "hw.inc"
11 xoffs_center equ 4
12 yoffs_center equ 12
14 frame equ $ff80
15 bnstate equ $ff81
16 bnxor equ $ff82
17 pause equ $ff83
19 BN_A equ $01
20 BN_B equ $02
21 BN_SELECT equ $04
22 BN_START equ $08
23 BN_RIGHT equ $10
24 BN_LEFT equ $20
25 BN_UP equ $40
26 BN_DOWN equ $80
28 section "hdr", ROM0[$100]
29 nop
30 jp main
32 rept $150 - $104
33 db 0
34 endr
36 section "text", ROM0
38 main:
39 call init
41 xor a, a
42 ldh [frame], a
43 ldh [bnstate], a
44 ldh [bnxor], a
45 ldh [pause], a
47 .mainloop:
48 ldh a, [REG_LY]
49 cp a, 144
50 jr c, .wait_hsync
52 ; we're in vsync, increment frame counter, handle input,
53 ; and wait for the next frame
54 call read_input
56 ; swap palette if A is pressed
57 ldh a, [bnxor]
58 and a, BN_A
59 jr z, .skip_akey ; skip if A haven't changed state since last frame
60 ldh a, [bnstate]
61 and a, BN_A
62 jr z, .skip_akey ; skip if A is not pressed
63 ldh a, [REG_BGP]
64 cpl
65 ldh [REG_BGP], a
66 .skip_akey:
67 ; toggle pause if start is pressed
68 ldh a, [bnxor]
69 and a, BN_START
70 jr z, .skip_startkey ; skip if start haven't changed state since last frame
71 ldh a, [bnstate]
72 and a, BN_START
73 jr z, .skip_startkey ; skip if start is not pressed
74 ldh a, [pause]
75 cpl
76 ldh [pause], a
77 .skip_startkey:
79 ; increment frame if we're not paused
80 ldh a, [pause]
81 bit 0, a
82 jr nz, .skip_frameinc
83 ldh a, [frame]
84 inc a
85 ldh [frame], a
86 .skip_frameinc:
89 .wait_newframe:
90 ldh a, [REG_LY]
91 cp a, 0
92 jr nz, .wait_newframe
94 ; scanline code
95 .wait_hsync:
96 ldh a, [REG_STAT]
97 and a, STAT_MODE_MASK
98 jr nz, .wait_hsync
100 ldh a, [frame]
101 ld d, a
103 xor a, a
104 ld b, a
106 ldh a, [REG_LY]
107 add a, d ; add frame number
108 ld c, a
110 ld hl, sintab
111 add hl, bc ; hl now points to the sine value
113 ld a, [hl]
115 ; add a half-octave sine
116 ld e, a ; save first sine to e
117 sla d
118 ld a, [REG_LY]
119 sla a
120 add a, d
121 ld c, a
122 srl d
124 ld hl, sintab
125 add hl, bc
126 ld a, [hl]
127 sra a
128 add a, e ; add previously saved sine
130 add a, yoffs_center
131 ldh [REG_SCY], a
133 ; do something for SCX too
134 ld a, d
135 sla a
136 ld d, a
138 ldh a, [REG_LY]
139 add a, 32
140 add a, d ; add frame number
141 ld c, a
143 ld hl, sintab
144 add hl, bc
146 ld a, [hl]
147 sra a
148 add a, xoffs_center
149 ldh [REG_SCX], a
151 ; done, wait until we're out of hsync
152 .wait_endhsync:
153 ldh a, [REG_STAT]
154 and a, STAT_MODE_MASK
155 jr z, .wait_endhsync
157 jp .mainloop
160 di
161 .end: halt
162 nop
163 jp .end
165 init:
166 call wait_vsync
167 xor a, a
168 ldh [REG_LCDC], a
170 ; setup palette
171 ld a, $1b
172 ldh [REG_BGP], a
174 ; copy tiles
175 ld hl, $8000
176 ld de, tiles
177 ld bc, tiles_end - tiles
178 .copytiles:
179 ld a, [de]
180 ld [hl+], a
181 inc de
182 dec bc
183 ld a, b
184 or c
185 jp nz, .copytiles
187 IF BUILD == BUILD_LOGO
188 ; copy logo tilemap
189 ld hl, $9800
190 ld de, tilemap
191 ld b, 21
192 .copymap:
193 ld c, 21
194 .copymaprow:
195 ld a, [de]
196 inc de
197 ld [hl+], a
198 dec c
199 jr nz, .copymaprow
201 push bc
202 ld bc, 11
203 add hl, bc
204 pop bc
206 dec b
207 jr nz, .copymap
208 ELSE
209 ; generate chessboard tilemap
210 ld hl, $9800
211 ld b, 32
212 .fillscr:
213 ld c, 32
214 .fillrow:
215 ld a, b
216 srl a
217 ld d, a
218 ld a, c
219 srl a
220 add a, d
221 and a, 1
222 ld [hl+], a
223 ld a, d
224 dec c
225 jr nz, .fillrow
227 dec b
228 jr nz, .fillscr
229 ENDC
231 ; center viewport
232 ld a, yoffs_center
233 ldh [REG_SCY], a
234 ld a, xoffs_center
235 ldh [REG_SCX], a
237 ; configure LCD
238 ld a, LCDC_DISPON | LCDC_CHAR_8000 | LCDC_BGON
239 ldh [REG_LCDC], a
240 ret
242 wait_vsync:
243 ldh a, [REG_LY]
244 cp a, 144
245 jr c, wait_vsync
246 ret
248 read_input:
249 ; read D-pad
250 ld a, P1_DPAD
251 ld [REG_P1], a
252 ld a, [REG_P1]
253 ld a, [REG_P1]
254 cpl
255 and a, $0f
256 swap a
257 ld b, a
258 ; read buttons
259 ld a, P1_BUTTONS
260 ld [REG_P1], a
261 ld a, [REG_P1]
262 ld a, [REG_P1]
263 ld a, [REG_P1]
264 ld a, [REG_P1]
265 ld a, [REG_P1]
266 ld a, [REG_P1]
267 cpl
268 and a, $0f
269 or a, b
270 ld b, a
271 ; reset port
272 ld a, P1_DPAD | P1_BUTTONS
273 ldh [REG_P1], a
274 ; calculate differences and save state variables
275 ldh a, [bnstate]
276 xor a, b
277 ldh [bnxor], a
278 ld a, b
279 ldh [bnstate], a
280 ret
282 section "data", ROM0, align[8]
283 sintab:
284 include "sin.inc"
286 IF BUILD == BUILD_LOGO
287 tiles:
288 incbin "logo.tiles"
289 tiles_end:
290 tilemap:
291 incbin "logo.tilemap"
292 tilemap_end:
294 ELSE
295 ; chessboard tiles
296 tiles:
297 db $ff,$00
298 db $81,$00
299 db $81,$00
300 db $81,$00
301 db $81,$00
302 db $81,$00
303 db $81,$00
304 db $ff,$00
306 db $00,$ff
307 db $7e,$ff
308 db $7e,$ff
309 db $7e,$ff
310 db $7e,$ff
311 db $7e,$ff
312 db $7e,$ff
313 db $00,$ff
314 tiles_end:
315 ENDC