gb_test2

diff 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
line diff
     1.1 --- a/test.asm	Thu Jan 10 04:48:34 2019 +0200
     1.2 +++ b/test.asm	Thu Jan 10 06:05:21 2019 +0200
     1.3 @@ -4,7 +4,19 @@
     1.4  xoffs_center equ 4
     1.5  yoffs_center equ 12
     1.6  
     1.7 -frame_ptr equ $ff80
     1.8 +frame equ $ff80
     1.9 +bnstate equ $ff81
    1.10 +bnxor equ $ff82
    1.11 +pause equ $ff83
    1.12 +
    1.13 +BN_A		equ $01
    1.14 +BN_B		equ $02
    1.15 +BN_SELECT	equ $04
    1.16 +BN_START	equ $08
    1.17 +BN_RIGHT	equ $10
    1.18 +BN_LEFT		equ $20
    1.19 +BN_UP		equ $40
    1.20 +BN_DOWN		equ $80
    1.21  
    1.22  section "hdr", ROM0[$100]
    1.23          nop
    1.24 @@ -20,16 +32,53 @@
    1.25  	call init
    1.26  
    1.27  	xor a, a
    1.28 -	ldh [frame_ptr], a
    1.29 +	ldh [frame], a
    1.30 +	ldh [bnstate], a
    1.31 +	ldh [bnxor], a
    1.32 +	ldh [pause], a
    1.33 +
    1.34  .mainloop:
    1.35  	ldh a, [REG_LY]
    1.36  	cp a, 144
    1.37  	jr c, .wait_hsync
    1.38  
    1.39 -	; we're in vsync, increment frame counter and wait for the next frame
    1.40 -	ldh a, [frame_ptr]
    1.41 +	; we're in vsync, increment frame counter, handle input,
    1.42 +	; and wait for the next frame
    1.43 +	call read_input
    1.44 +
    1.45 +	; swap palette if A is pressed
    1.46 +	ldh a, [bnxor]
    1.47 +	and a, BN_A
    1.48 +	jr z, .skip_akey  ; skip if A haven't changed state since last frame
    1.49 +	ldh a, [bnstate]
    1.50 +	and a, BN_A
    1.51 +	jr z, .skip_akey  ; skip if A is not pressed
    1.52 +	ldh a, [REG_BGP]
    1.53 +	cpl
    1.54 +	ldh [REG_BGP], a
    1.55 +.skip_akey:
    1.56 +	; toggle pause if start is pressed
    1.57 +	ldh a, [bnxor]
    1.58 +	and a, BN_START
    1.59 +	jr z, .skip_startkey ; skip if start haven't changed state since last frame
    1.60 +	ldh a, [bnstate]
    1.61 +	and a, BN_START
    1.62 +	jr z, .skip_startkey ; skip if start is not pressed
    1.63 +	ldh a, [pause]
    1.64 +	cpl
    1.65 +	ldh [pause], a
    1.66 +.skip_startkey:
    1.67 +
    1.68 +	; increment frame if we're not paused
    1.69 +	ldh a, [pause]
    1.70 +	bit 0, a
    1.71 +	jr nz, .skip_frameinc
    1.72 +	ldh a, [frame]
    1.73  	inc a
    1.74 -	ldh [frame_ptr], a
    1.75 +	ldh [frame], a
    1.76 +.skip_frameinc:
    1.77 +
    1.78 +
    1.79  .wait_newframe:
    1.80  	ldh a, [REG_LY]
    1.81  	cp a, 0
    1.82 @@ -41,7 +90,7 @@
    1.83  	and a, STAT_MODE_MASK
    1.84  	jr nz, .wait_hsync
    1.85  
    1.86 -	ldh a, [frame_ptr]
    1.87 +	ldh a, [frame]
    1.88  	ld d, a
    1.89  
    1.90  	xor a, a
    1.91 @@ -98,7 +147,7 @@
    1.92  	and a, STAT_MODE_MASK
    1.93  	jr z, .wait_endhsync
    1.94  
    1.95 -	jr .mainloop 
    1.96 +	jp .mainloop 
    1.97  
    1.98  
    1.99  	di
   1.100 @@ -166,6 +215,40 @@
   1.101  	jr c, wait_vsync
   1.102  	ret
   1.103  
   1.104 +read_input:
   1.105 +	; read D-pad
   1.106 +	ld a, P1_DPAD
   1.107 +	ld [REG_P1], a
   1.108 +	ld a, [REG_P1]
   1.109 +	ld a, [REG_P1]
   1.110 +	cpl
   1.111 +	and a, $0f
   1.112 +	swap a
   1.113 +	ld b, a
   1.114 +	; read buttons
   1.115 +	ld a, P1_BUTTONS
   1.116 +	ld [REG_P1], a
   1.117 +	ld a, [REG_P1]
   1.118 +	ld a, [REG_P1]
   1.119 +	ld a, [REG_P1]
   1.120 +	ld a, [REG_P1]
   1.121 +	ld a, [REG_P1]
   1.122 +	ld a, [REG_P1]
   1.123 +	cpl
   1.124 +	and a, $0f
   1.125 +	or a, b
   1.126 +	ld b, a
   1.127 +	; reset port
   1.128 +	ld a, P1_DPAD | P1_BUTTONS
   1.129 +	ldh [REG_P1], a
   1.130 +	; calculate differences and save state variables
   1.131 +	ldh a, [bnstate]
   1.132 +	xor a, b
   1.133 +	ldh [bnxor], a
   1.134 +	ld a, b
   1.135 +	ldh [bnstate], a
   1.136 +	ret
   1.137 +
   1.138  section "data", ROM0, align[8]
   1.139  sintab:
   1.140  include "sin.inc"