# HG changeset patch # User John Tsiombikas # Date 1547093121 -7200 # Node ID d7c6e1165028bf64afb2a0e4c3e4aeca99194496 # Parent a8b7297e7e2cdaa7037495b37c1059677b3be50f added input handling diff -r a8b7297e7e2c -r d7c6e1165028 hw.inc --- a/hw.inc Thu Jan 10 04:48:34 2019 +0200 +++ b/hw.inc Thu Jan 10 06:05:21 2019 +0200 @@ -1,4 +1,5 @@ ; vi:ft=rgbasm: +REG_P1 equ $ff00 REG_LCDC equ $ff40 REG_STAT equ $ff41 REG_SCY equ $ff42 @@ -11,6 +12,17 @@ REG_WY equ $ff4a REG_WX equ $ff4b +P1_RIGHT equ $01 +P1_A equ $01 +P1_LEFT equ $02 +P1_B equ $02 +P1_UP equ $04 +P1_SELECT equ $04 +P1_DOWN equ $08 +P1_START equ $08 +P1_DPAD equ $20 +P1_BUTTONS equ $10 + LCDC_BGON equ $01 LCDC_OBJON equ $02 LCDC_OBJ16 equ $04 diff -r a8b7297e7e2c -r d7c6e1165028 test.asm --- a/test.asm Thu Jan 10 04:48:34 2019 +0200 +++ b/test.asm Thu Jan 10 06:05:21 2019 +0200 @@ -4,7 +4,19 @@ xoffs_center equ 4 yoffs_center equ 12 -frame_ptr equ $ff80 +frame equ $ff80 +bnstate equ $ff81 +bnxor equ $ff82 +pause equ $ff83 + +BN_A equ $01 +BN_B equ $02 +BN_SELECT equ $04 +BN_START equ $08 +BN_RIGHT equ $10 +BN_LEFT equ $20 +BN_UP equ $40 +BN_DOWN equ $80 section "hdr", ROM0[$100] nop @@ -20,16 +32,53 @@ call init xor a, a - ldh [frame_ptr], a + ldh [frame], a + ldh [bnstate], a + ldh [bnxor], a + ldh [pause], a + .mainloop: ldh a, [REG_LY] cp a, 144 jr c, .wait_hsync - ; we're in vsync, increment frame counter and wait for the next frame - ldh a, [frame_ptr] + ; we're in vsync, increment frame counter, handle input, + ; and wait for the next frame + call read_input + + ; swap palette if A is pressed + ldh a, [bnxor] + and a, BN_A + jr z, .skip_akey ; skip if A haven't changed state since last frame + ldh a, [bnstate] + and a, BN_A + jr z, .skip_akey ; skip if A is not pressed + ldh a, [REG_BGP] + cpl + ldh [REG_BGP], a +.skip_akey: + ; toggle pause if start is pressed + ldh a, [bnxor] + and a, BN_START + jr z, .skip_startkey ; skip if start haven't changed state since last frame + ldh a, [bnstate] + and a, BN_START + jr z, .skip_startkey ; skip if start is not pressed + ldh a, [pause] + cpl + ldh [pause], a +.skip_startkey: + + ; increment frame if we're not paused + ldh a, [pause] + bit 0, a + jr nz, .skip_frameinc + ldh a, [frame] inc a - ldh [frame_ptr], a + ldh [frame], a +.skip_frameinc: + + .wait_newframe: ldh a, [REG_LY] cp a, 0 @@ -41,7 +90,7 @@ and a, STAT_MODE_MASK jr nz, .wait_hsync - ldh a, [frame_ptr] + ldh a, [frame] ld d, a xor a, a @@ -98,7 +147,7 @@ and a, STAT_MODE_MASK jr z, .wait_endhsync - jr .mainloop + jp .mainloop di @@ -166,6 +215,40 @@ jr c, wait_vsync ret +read_input: + ; read D-pad + ld a, P1_DPAD + ld [REG_P1], a + ld a, [REG_P1] + ld a, [REG_P1] + cpl + and a, $0f + swap a + ld b, a + ; read buttons + ld a, P1_BUTTONS + ld [REG_P1], a + ld a, [REG_P1] + ld a, [REG_P1] + ld a, [REG_P1] + ld a, [REG_P1] + ld a, [REG_P1] + ld a, [REG_P1] + cpl + and a, $0f + or a, b + ld b, a + ; reset port + ld a, P1_DPAD | P1_BUTTONS + ldh [REG_P1], a + ; calculate differences and save state variables + ldh a, [bnstate] + xor a, b + ldh [bnxor], a + ld a, b + ldh [bnstate], a + ret + section "data", ROM0, align[8] sintab: include "sin.inc"