gb_test2

view test.asm @ 5:be6f719279b6

better chessboard
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 10 Jan 2019 08:02:38 +0200
parents 216bdbc75cf4
children 6a7eb418caec
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 add a, c
217 and a, 1
219 ld [hl+], a
220 dec c
221 jr nz, .fillrow
223 dec b
224 jr nz, .fillscr
225 ENDC
227 ; center viewport
228 ld a, yoffs_center
229 ldh [REG_SCY], a
230 ld a, xoffs_center
231 ldh [REG_SCX], a
233 ; configure LCD
234 ld a, LCDC_DISPON | LCDC_CHAR_8000 | LCDC_BGON
235 ldh [REG_LCDC], a
236 ret
238 wait_vsync:
239 ldh a, [REG_LY]
240 cp a, 144
241 jr c, wait_vsync
242 ret
244 read_input:
245 ; read D-pad
246 ld a, P1_DPAD
247 ld [REG_P1], a
248 ld a, [REG_P1]
249 ld a, [REG_P1]
250 cpl
251 and a, $0f
252 swap a
253 ld b, a
254 ; read buttons
255 ld a, P1_BUTTONS
256 ld [REG_P1], a
257 ld a, [REG_P1]
258 ld a, [REG_P1]
259 ld a, [REG_P1]
260 ld a, [REG_P1]
261 ld a, [REG_P1]
262 ld a, [REG_P1]
263 cpl
264 and a, $0f
265 or a, b
266 ld b, a
267 ; reset port
268 ld a, P1_DPAD | P1_BUTTONS
269 ldh [REG_P1], a
270 ; calculate differences and save state variables
271 ldh a, [bnstate]
272 xor a, b
273 ldh [bnxor], a
274 ld a, b
275 ldh [bnstate], a
276 ret
278 section "data", ROM0, align[8]
279 sintab:
280 include "sin.inc"
282 IF BUILD == BUILD_LOGO
283 tiles:
284 incbin "logo.tiles"
285 tiles_end:
286 tilemap:
287 incbin "logo.tilemap"
288 tilemap_end:
290 ELSE
291 ; chessboard tiles
292 tiles:
293 db $ff,$00
294 db $81,$00
295 db $81,$00
296 db $81,$00
297 db $81,$00
298 db $81,$00
299 db $81,$00
300 db $ff,$00
302 db $00,$ff
303 db $7e,$ff
304 db $7e,$ff
305 db $7e,$ff
306 db $7e,$ff
307 db $7e,$ff
308 db $7e,$ff
309 db $00,$ff
310 tiles_end:
311 ENDC