gb_test2

annotate test.asm @ 3:d7c6e1165028

added input handling
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 10 Jan 2019 06:05:21 +0200
parents a8b7297e7e2c
children 216bdbc75cf4
rev   line source
nuclear@0 1 ; vi:ft=rgbasm:
nuclear@0 2 include "hw.inc"
nuclear@0 3
nuclear@0 4 xoffs_center equ 4
nuclear@0 5 yoffs_center equ 12
nuclear@0 6
nuclear@3 7 frame equ $ff80
nuclear@3 8 bnstate equ $ff81
nuclear@3 9 bnxor equ $ff82
nuclear@3 10 pause equ $ff83
nuclear@3 11
nuclear@3 12 BN_A equ $01
nuclear@3 13 BN_B equ $02
nuclear@3 14 BN_SELECT equ $04
nuclear@3 15 BN_START equ $08
nuclear@3 16 BN_RIGHT equ $10
nuclear@3 17 BN_LEFT equ $20
nuclear@3 18 BN_UP equ $40
nuclear@3 19 BN_DOWN equ $80
nuclear@0 20
nuclear@0 21 section "hdr", ROM0[$100]
nuclear@0 22 nop
nuclear@0 23 jp main
nuclear@0 24
nuclear@0 25 rept $150 - $104
nuclear@0 26 db 0
nuclear@0 27 endr
nuclear@0 28
nuclear@0 29 section "text", ROM0
nuclear@0 30
nuclear@0 31 main:
nuclear@0 32 call init
nuclear@0 33
nuclear@0 34 xor a, a
nuclear@3 35 ldh [frame], a
nuclear@3 36 ldh [bnstate], a
nuclear@3 37 ldh [bnxor], a
nuclear@3 38 ldh [pause], a
nuclear@3 39
nuclear@0 40 .mainloop:
nuclear@0 41 ldh a, [REG_LY]
nuclear@0 42 cp a, 144
nuclear@0 43 jr c, .wait_hsync
nuclear@0 44
nuclear@3 45 ; we're in vsync, increment frame counter, handle input,
nuclear@3 46 ; and wait for the next frame
nuclear@3 47 call read_input
nuclear@3 48
nuclear@3 49 ; swap palette if A is pressed
nuclear@3 50 ldh a, [bnxor]
nuclear@3 51 and a, BN_A
nuclear@3 52 jr z, .skip_akey ; skip if A haven't changed state since last frame
nuclear@3 53 ldh a, [bnstate]
nuclear@3 54 and a, BN_A
nuclear@3 55 jr z, .skip_akey ; skip if A is not pressed
nuclear@3 56 ldh a, [REG_BGP]
nuclear@3 57 cpl
nuclear@3 58 ldh [REG_BGP], a
nuclear@3 59 .skip_akey:
nuclear@3 60 ; toggle pause if start is pressed
nuclear@3 61 ldh a, [bnxor]
nuclear@3 62 and a, BN_START
nuclear@3 63 jr z, .skip_startkey ; skip if start haven't changed state since last frame
nuclear@3 64 ldh a, [bnstate]
nuclear@3 65 and a, BN_START
nuclear@3 66 jr z, .skip_startkey ; skip if start is not pressed
nuclear@3 67 ldh a, [pause]
nuclear@3 68 cpl
nuclear@3 69 ldh [pause], a
nuclear@3 70 .skip_startkey:
nuclear@3 71
nuclear@3 72 ; increment frame if we're not paused
nuclear@3 73 ldh a, [pause]
nuclear@3 74 bit 0, a
nuclear@3 75 jr nz, .skip_frameinc
nuclear@3 76 ldh a, [frame]
nuclear@0 77 inc a
nuclear@3 78 ldh [frame], a
nuclear@3 79 .skip_frameinc:
nuclear@3 80
nuclear@3 81
nuclear@0 82 .wait_newframe:
nuclear@0 83 ldh a, [REG_LY]
nuclear@0 84 cp a, 0
nuclear@0 85 jr nz, .wait_newframe
nuclear@0 86
nuclear@0 87 ; scanline code
nuclear@0 88 .wait_hsync:
nuclear@0 89 ldh a, [REG_STAT]
nuclear@0 90 and a, STAT_MODE_MASK
nuclear@0 91 jr nz, .wait_hsync
nuclear@0 92
nuclear@3 93 ldh a, [frame]
nuclear@0 94 ld d, a
nuclear@0 95
nuclear@0 96 xor a, a
nuclear@0 97 ld b, a
nuclear@1 98
nuclear@0 99 ldh a, [REG_LY]
nuclear@1 100 add a, d ; add frame number
nuclear@0 101 ld c, a
nuclear@0 102
nuclear@0 103 ld hl, sintab
nuclear@1 104 add hl, bc ; hl now points to the sine value
nuclear@0 105
nuclear@1 106 ld a, [hl]
nuclear@1 107
nuclear@1 108 ; add a half-octave sine
nuclear@1 109 ld e, a ; save first sine to e
nuclear@1 110 sla d
nuclear@1 111 ld a, [REG_LY]
nuclear@1 112 sla a
nuclear@1 113 add a, d
nuclear@1 114 ld c, a
nuclear@1 115 srl d
nuclear@1 116
nuclear@1 117 ld hl, sintab
nuclear@1 118 add hl, bc
nuclear@1 119 ld a, [hl]
nuclear@1 120 sra a
nuclear@1 121 add a, e ; add previously saved sine
nuclear@1 122
nuclear@1 123 add a, yoffs_center
nuclear@0 124 ldh [REG_SCY], a
nuclear@0 125
nuclear@1 126 ; do something for SCX too
nuclear@1 127 ld a, d
nuclear@1 128 sla a
nuclear@1 129 ld d, a
nuclear@1 130
nuclear@1 131 ldh a, [REG_LY]
nuclear@1 132 add a, 32
nuclear@1 133 add a, d ; add frame number
nuclear@1 134 ld c, a
nuclear@1 135
nuclear@1 136 ld hl, sintab
nuclear@1 137 add hl, bc
nuclear@1 138
nuclear@1 139 ld a, [hl]
nuclear@1 140 sra a
nuclear@1 141 add a, xoffs_center
nuclear@1 142 ldh [REG_SCX], a
nuclear@1 143
nuclear@0 144 ; done, wait until we're out of hsync
nuclear@0 145 .wait_endhsync:
nuclear@0 146 ldh a, [REG_STAT]
nuclear@0 147 and a, STAT_MODE_MASK
nuclear@0 148 jr z, .wait_endhsync
nuclear@0 149
nuclear@3 150 jp .mainloop
nuclear@0 151
nuclear@0 152
nuclear@0 153 di
nuclear@0 154 .end: halt
nuclear@0 155 nop
nuclear@0 156 jp .end
nuclear@0 157
nuclear@0 158 init:
nuclear@0 159 call wait_vsync
nuclear@0 160 xor a, a
nuclear@0 161 ldh [REG_LCDC], a
nuclear@0 162
nuclear@2 163 ; setup palette
nuclear@0 164 ld a, $1b
nuclear@0 165 ldh [REG_BGP], a
nuclear@0 166
nuclear@0 167 ; copy tiles
nuclear@0 168 ld hl, $8000
nuclear@0 169 ld de, tiles
nuclear@0 170 ld bc, tiles_end - tiles
nuclear@0 171 .copytiles:
nuclear@0 172 ld a, [de]
nuclear@0 173 ld [hl+], a
nuclear@0 174 inc de
nuclear@0 175 dec bc
nuclear@0 176 ld a, b
nuclear@0 177 or c
nuclear@0 178 jp nz, .copytiles
nuclear@0 179
nuclear@0 180 ; copy tilemap
nuclear@0 181 ld hl, $9800
nuclear@0 182 ld de, tilemap
nuclear@0 183 ld b, 21
nuclear@0 184 .copymap:
nuclear@0 185 ld c, 21
nuclear@0 186 .copymaprow:
nuclear@0 187 ld a, [de]
nuclear@0 188 inc de
nuclear@0 189 ld [hl+], a
nuclear@0 190 dec c
nuclear@0 191 jr nz, .copymaprow
nuclear@0 192
nuclear@0 193 push bc
nuclear@0 194 ld bc, 11
nuclear@0 195 add hl, bc
nuclear@0 196 pop bc
nuclear@0 197
nuclear@0 198 dec b
nuclear@0 199 jr nz, .copymap
nuclear@0 200
nuclear@0 201 ; center viewport
nuclear@0 202 ld a, yoffs_center
nuclear@0 203 ldh [REG_SCY], a
nuclear@0 204 ld a, xoffs_center
nuclear@0 205 ldh [REG_SCX], a
nuclear@0 206
nuclear@0 207 ; configure LCD
nuclear@0 208 ld a, LCDC_DISPON | LCDC_CHAR_8000 | LCDC_BGON
nuclear@0 209 ldh [REG_LCDC], a
nuclear@0 210 ret
nuclear@0 211
nuclear@0 212 wait_vsync:
nuclear@0 213 ldh a, [REG_LY]
nuclear@0 214 cp a, 144
nuclear@0 215 jr c, wait_vsync
nuclear@0 216 ret
nuclear@0 217
nuclear@3 218 read_input:
nuclear@3 219 ; read D-pad
nuclear@3 220 ld a, P1_DPAD
nuclear@3 221 ld [REG_P1], a
nuclear@3 222 ld a, [REG_P1]
nuclear@3 223 ld a, [REG_P1]
nuclear@3 224 cpl
nuclear@3 225 and a, $0f
nuclear@3 226 swap a
nuclear@3 227 ld b, a
nuclear@3 228 ; read buttons
nuclear@3 229 ld a, P1_BUTTONS
nuclear@3 230 ld [REG_P1], a
nuclear@3 231 ld a, [REG_P1]
nuclear@3 232 ld a, [REG_P1]
nuclear@3 233 ld a, [REG_P1]
nuclear@3 234 ld a, [REG_P1]
nuclear@3 235 ld a, [REG_P1]
nuclear@3 236 ld a, [REG_P1]
nuclear@3 237 cpl
nuclear@3 238 and a, $0f
nuclear@3 239 or a, b
nuclear@3 240 ld b, a
nuclear@3 241 ; reset port
nuclear@3 242 ld a, P1_DPAD | P1_BUTTONS
nuclear@3 243 ldh [REG_P1], a
nuclear@3 244 ; calculate differences and save state variables
nuclear@3 245 ldh a, [bnstate]
nuclear@3 246 xor a, b
nuclear@3 247 ldh [bnxor], a
nuclear@3 248 ld a, b
nuclear@3 249 ldh [bnstate], a
nuclear@3 250 ret
nuclear@3 251
nuclear@0 252 section "data", ROM0, align[8]
nuclear@0 253 sintab:
nuclear@0 254 include "sin.inc"
nuclear@0 255
nuclear@0 256 tiles:
nuclear@0 257 incbin "logo.tiles"
nuclear@0 258 tiles_end:
nuclear@0 259 tilemap:
nuclear@0 260 incbin "logo.tilemap"
nuclear@0 261 tilemap_end: