cardiograph-computer/javascript-old/test-programs/move-pixel-with-keypad.asm

164 lines
2.1 KiB
NASM

; Draw a pixel, and move it when a key is pressed
; 2023-08-26
#flagZ 1
#flagN 2
#keypad $1B ; contains latest key pressed
; Starting (x, y) coordinates
#input_x 0
#input_y 0
; Some handy shortcuts
#x $FA
#y $FB
#px_addr $FD ; holds return value from @xy2id
#return_addr_ptr $FE
; Main variables:
; F8
; F9 - xy2id temp
; FA - x coord
; FB - y coord
; FC - xy2id temp
; FD - xy2id return value / xy2id temp
; FE - Return address for subroutine
@setup
LDA #input_x
STO #x
LDA #input_y
STO #y
LDA @update
STO #return_addr_ptr
JMP @xy2id
@update
; draw pixel
LDA $FF
STO (#px_addr)
; determine direction
#up 5
#left 7
#down 8
#right 9
; test up
lda (#keypad)
sub #up
ftg #flagZ
fhp #flagZ
jmp @up
; test left
lda (#keypad)
sub #left
ftg #flagZ
fhp #flagZ
jmp @left
; test right
lda (#keypad)
sub #right
ftg #flagZ
fhp #flagZ
jmp @right
; test down
lda (#keypad)
sub #down
ftg #flagZ
fhp #flagZ
jmp @down
;; no key pressed...
jmp @stay_put
@up
lda (#y)
sub 1
ftg #flagN
fhp #flagN
jmp @stay_put
sto #y
jmp @xy2id
@left
lda (#x)
sub 1
ftg #flagN
fhp #flagN
jmp @stay_put
sto #x
jmp @xy2id
@right
lda (#x)
sub 4
ftg #flagZ
fhp #flagZ
jmp @stay_put
lda (#x)
add 1
sto #x
jmp @xy2id
@down
lda (#y)
sub 4
ftg #flagZ
fhp #flagZ
jmp @stay_put
lda (#y)
add 1
sto #y
jmp @xy2id
@stay_put
; draw pixel
LDA $FF
STO (#px_addr)
; TODO
; END
;; Convert a pair of (x, y) coords
;; to the address of a pixel on the display
;;
;; Call with:
;; - x in #x
;; - y in #y
;; - return address in #return_addr_ptr
;;
;; Returns:
;; - pixel address in #px_addr
@xy2id
; stash x, y...
#xy2id_y $FC
#xy2id_x $F9
LDA (#y)
STO #xy2id_y
LDA (#x)
STO #xy2id_x
STO #px_addr
; check if this is row 0...
LDA (#xy2id_y)
FHP #flagZ
JMP @xy2id_loop
JMP (#return_addr_ptr) ; if row 0, we're done
@xy2id_loop
LDA (#px_addr)
ADD 5 ; add 5 to get to the next row
STO #px_addr
LDA (#xy2id_y) ; decrement y (it's acting as a loop counter) ...
SUB 1
STO #xy2id_y
FHP #flagZ
JMP @xy2id_loop
JMP (#return_addr_ptr)