Skip to content

Instantly share code, notes, and snippets.

@jmcd
Last active September 11, 2018 08:05
Show Gist options
  • Save jmcd/53b634dc4a564d5106249c6dc950d8c9 to your computer and use it in GitHub Desktop.
Save jmcd/53b634dc4a564d5106249c6dc950d8c9 to your computer and use it in GitHub Desktop.
Display like The Matrix movie on a C64 in 6502 assembly
*= $c000 ; 49152
!to "matrix.prg", cbm
!cpu 6502 ; set processor type
JSR main
RTS
_snake_count !byte $05
_snake_lengths !byte $05, $08, 10, 14, 20
_snake_xs !byte 03, 10, 20, 30, 34
_snake_ys !byte 10, 05, 20, 02, 07
_current_snake !byte $00
main
JSR setupscreen
JSR randomizeallsnakes
_main_loop
JSR drawallsnakes
JSR moveallsnakes
JMP _main_loop
RTS
moveallsnakes
LDA #$00
STA _current_snake
_moveallsnakes_loop
LDA _current_snake
CMP _snake_count
BEQ _end_moveallsnakes
LDY _current_snake
LDA _snake_ys, y
CLC
ADC #$01
STA _snake_ys, y
INC _current_snake
JMP _moveallsnakes_loop
_end_moveallsnakes
RTS
randomizeallsnakes
LDA #$00
STA _current_snake
_randomizeallsnakes_loop
LDA _current_snake
CMP _snake_count
BEQ _end_randomizeallsnakes
JSR randomizesnake
INC _current_snake
JMP _randomizeallsnakes_loop
_end_randomizeallsnakes
RTS
randomizesnake
LDY _current_snake
JSR GETRAND
LDA RANDOM+1
;AND #39
STA $0400
STA _snake_xs, y
LDA RANDOM+1
STA $0403
AND #24
STA $0401
STA _snake_ys, y
RTS
checksnake
LDY _current_snake
LDA #$00
STA _addscreen_location
LDA #$D8
STA _addscreen_location+1
LDA _snake_xs,y
STA _addscreen_x
LDA _snake_ys,y
STA _addscreen_y
JSR addscreen
LDA _addscreen_location
STA _subscreen_location
LDA _addscreen_location+1
STA _subscreen_location+1
LDA #$00
STA _subscreen_x
LDY _current_snake
LDA _snake_lengths,y
CLC
ADC #$01
STA _subscreen_y
JSR subscreen
LDA _subscreen_location
STA _isvalidscreencolorlocation_location
LDA _subscreen_location+1
STA _isvalidscreencolorlocation_location+1
JSR isvalidscreencolorlocation
LDA #$00
CMP _isvalidscreencolorlocation_result
BEQ _checksnake_end
LDA #$01
CMP _isvalidscreencolorlocation_result
BEQ _checksnake_end
JSR randomizesnake
LDY _current_snake
LDA #$00
STA _snake_ys,y
JMP drawsnake
_checksnake_end
RTS
drawallsnakes
LDA #$00
STA _current_snake
_drawallsnakes_loop
LDA _current_snake
CMP _snake_count
BEQ _end_drawallsnakes
JSR checksnake
JSR drawsnake
INC _current_snake
JMP _drawallsnakes_loop
_end_drawallsnakes
RTS
_temp_location !byte $00, $00
drawsnake
LDY _current_snake
LDA #$00
STA _addscreen_location
LDA #$D8
STA _addscreen_location+1
LDA _snake_xs,y
STA _addscreen_x
LDA _snake_ys,y
STA _addscreen_y
JSR addscreen
_drawsnake_main
LDA _addscreen_location
STA _pokescreencolorlocation_location
LDA _addscreen_location+1
STA _pokescreencolorlocation_location+1
LDA #$01
STA _pokescreencolorlocation_value
JSR pokescreencolorlocation
LDY _current_snake
LDA _addscreen_location
STA _temp_location
LDA _addscreen_location+1
STA _temp_location+1
LDX _snake_lengths, y
_tail
CPX #$00
BEQ _blacken
LDA _pokescreencolorlocation_location
STA _sub16_param1
LDA _pokescreencolorlocation_location+1
STA _sub16_param1+1
LDA #$28
STA _sub16_param2
LDA #$00
STA _sub16_param2+1
JSR sub16
LDA _sub16_result
STA _pokescreencolorlocation_location
LDA _sub16_result+1
STA _pokescreencolorlocation_location+1
LDA #$05
STA _pokescreencolorlocation_value
JSR pokescreencolorlocation
DEX
JMP _tail
_blacken
LDA _pokescreencolorlocation_location
STA _sub16_param1
LDA _pokescreencolorlocation_location+1
STA _sub16_param1+1
LDA #$28
STA _sub16_param2
LDA #$00
STA _sub16_param2+1
JSR sub16
LDA _sub16_result
STA _pokescreencolorlocation_location
LDA _sub16_result+1
STA _pokescreencolorlocation_location+1
LDA #$00
STA _pokescreencolorlocation_value
JSR pokescreencolorlocation
RTS
_setupscreen_current_location !byte $00, $04, $00, $D8 ;data
setupscreen
LDX #$00
STX $0286
STX $D020
STX $D021
LDA _setupscreen_current_location
STA _cmp16_param1
LDA _setupscreen_current_location+1
STA _cmp16_param1+1
LDA #$E8
STA _cmp16_param2
LDA #$07
STA _cmp16_param2+1
JSR cmp16
BEQ _end_setupscreen
LDA _setupscreen_current_location
STA _poke_location
LDA _setupscreen_current_location+1
STA _poke_location+1
JSR getrandomchar
LDA _getrandomchar_result
STA _poke_value
JSR poke
LDA _setupscreen_current_location+2
STA _poke_location
LDA _setupscreen_current_location+3
STA _poke_location+1
LDA #$00
STA _poke_value
JSR poke
CLC
LDA _setupscreen_current_location
ADC #$01
STA _setupscreen_current_location
LDA _setupscreen_current_location+1
ADC #$00
STA _setupscreen_current_location+1
CLC
LDA _setupscreen_current_location+2
ADC #$01
STA _setupscreen_current_location+2
LDA _setupscreen_current_location+3
ADC #$00
STA _setupscreen_current_location+3
JMP setupscreen
_end_setupscreen
RTS
_getrandomchar_result !byte $00
getrandomchar
JSR GETRAND
LDA RANDOM+1
STA _getrandomchar_result
RTS
_isvalidscreencolorlocation_location !byte $00, $00
_isvalidscreencolorlocation_result !byte $00 ; 0 valid, 1 before screen, 2 after
isvalidscreencolorlocation
LDA #$00
STA _isvalidscreencolorlocation_result
_isvalidscreencolorlocation_checkbefore
LDA _isvalidscreencolorlocation_location
STA _cmp16_param1
LDA _isvalidscreencolorlocation_location+1
STA _cmp16_param1+1
LDA #$00
STA _cmp16_param2
LDA #$D8
STA _cmp16_param2+1
CLC
JSR cmp16
BCS _isvalidscreencolorlocation_checkafter
LDA #$01
STA _isvalidscreencolorlocation_result
RTS
_isvalidscreencolorlocation_checkafter
LDA #$E8
STA _cmp16_param2
LDA #$DB
STA _cmp16_param2+1
CLC
JSR cmp16
BCC _end_isvalidscreencolorlocation
LDA #$02
STA _isvalidscreencolorlocation_result
_end_isvalidscreencolorlocation
RTS
_pokescreencolorlocation_location !byte $00, $00
_pokescreencolorlocation_value !byte $00
pokescreencolorlocation
LDA _pokescreencolorlocation_location
STA _isvalidscreencolorlocation_location
LDA _pokescreencolorlocation_location+1
STA _isvalidscreencolorlocation_location+1
JSR isvalidscreencolorlocation
LDA _isvalidscreencolorlocation_result
CMP #$00
BNE _end_pokescreencolorlocation
LDA _pokescreencolorlocation_location
STA _poke_location
LDA _pokescreencolorlocation_location+1
STA _poke_location+1
LDA _pokescreencolorlocation_value
STA _poke_value
JSR poke
_end_pokescreencolorlocation
RTS
_peek_location !byte $00, $00
_peek_result !byte $00
peek
LDA _peek_location
STA _peek_load+1
LDA _peek_location+1
STA _peek_load+2
_peek_load
LDA $0000
STA _peek_result
RTS
_poke_location !byte $00, $00
_poke_value !byte $00
poke
LDA _poke_location
STA _poke_store+1
LDA _poke_location+1
STA _poke_store+2
LDA _poke_value
_poke_store
STA $0000
RTS
_add16_param1 !byte $00, $00
_add16_param2 !byte $00, $00
_add16_result !byte $00, $00
add16
CLC
LDA _add16_param1
ADC _add16_param2
STA _add16_result
LDA _add16_param1+1
ADC _add16_param2+1
STA _add16_result+1
RTS
_sub16_param1 !byte $00, $00
_sub16_param2 !byte $00, $00
_sub16_result !byte $00, $00
sub16
SEC
LDA _sub16_param1+0
SBC _sub16_param2+0
STA _sub16_result+0
LDA _sub16_param1+1
SBC _sub16_param2+1
STA _sub16_result+1
RTS
_cmp16_param1 !byte $00, $00
_cmp16_param2 !byte $00, $00
cmp16
LDA _cmp16_param1+1
CMP _cmp16_param2+1
BNE _end_cmp16
LDA _cmp16_param1
CMP _cmp16_param2
_end_cmp16
RTS
_addscreen_location !byte $00, $00
_addscreen_x !byte $00
_addscreen_y !byte $00
addscreen
CLC ; Add x
LDA _addscreen_location
ADC _addscreen_x
STA _addscreen_location
LDA _addscreen_location+1
ADC #$00
STA _addscreen_location+1 ; Done add x
LDY _addscreen_y
_addscreen_loop
CPY #$00
BEQ _end_addscreen
LDA _addscreen_location
STA _add16_param1
LDA _addscreen_location+1
STA _add16_param1+1
LDA #$28
STA _add16_param2
LDA #$00
STA _add16_param2+1
JSR add16
LDA _add16_result
STA _addscreen_location
LDA _add16_result+1
STA _addscreen_location+1
CLC
DEY
JMP _addscreen_loop
_end_addscreen
RTS
_subscreen_location !byte $00, $00
_subscreen_x !byte $00
_subscreen_y !byte $00
subscreen
SEC ; sub x
LDA _subscreen_location
SBC _subscreen_x
STA _subscreen_location
LDA _subscreen_location+1
SBC #$00
STA _subscreen_location+1 ; Done sub x
LDY _subscreen_y
_subscreen_loop
CPY #$00
BEQ _end_subscreen
LDA _subscreen_location
STA _sub16_param1
LDA _subscreen_location+1
STA _sub16_param1+1
LDA #$28
STA _sub16_param2
LDA #$00
STA _sub16_param2+1
JSR sub16
LDA _sub16_result
STA _subscreen_location
LDA _sub16_result+1
STA _subscreen_location+1
CLC
DEY
JMP _subscreen_loop
_end_subscreen
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; From http://www.ffd2.com/fridge/math/rand1.s
RANDOM !byte 0,0
TEMP1 !byte 0,0
GETRAND
LDA RANDOM+1
STA TEMP1
LDA RANDOM
ASL
ROL TEMP1
ASL
ROL TEMP1
CLC
ADC RANDOM
PHA
LDA TEMP1
ADC RANDOM+1
STA RANDOM+1
PLA
ADC #$11
STA RANDOM
LDA RANDOM+1
ADC #$36
STA RANDOM+1
RTS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment