Reorganized code with statuses, to introduce an intro scene.

Added routine to print text during intro; things needs to be developed.
This commit is contained in:
giomba 2017-12-24 14:17:37 +01:00
parent 7d8233fde2
commit 1d7226df45
1 changed files with 139 additions and 64 deletions

203
snake.asm
View File

@ -4,10 +4,17 @@
; ---------------------------------------------------------------------- ; ----------------------------------------------------------------------
; Where is the snake head in video memory? Do math to calculate address ; Where is the snake head in video memory? Do math to calculate address
; using $a0-$a1 ; using pointer at $a0-$a1
tileMem = $a0 tileMem = $a0
; Pointer to status string ($a3-$a4)
printStatusString = $a3 printStatusString = $a3
; Pointer to intro string ($a3-$a4)
printIntroString = $a3
; Pointer to screen position where to print intro string ($fb-$fc)
introScreenStart = $fb
org $801 org $801
. = $801 ; 10 SYS9216 ($2400) BASIC autostart . = $801 ; 10 SYS9216 ($2400) BASIC autostart
BYTE #$0b,#$08,#$0a,#$00,#$9e,#$39,#$32,#$31,#$36,#$00,#$00,#$00 BYTE #$0b,#$08,#$0a,#$00,#$9e,#$39,#$32,#$31,#$36,#$00,#$00,#$00
@ -54,10 +61,14 @@ length:
random: random:
BYTE BYTE
; Status (0 intro playing, 1 game running) ; Status (0 fist time intro playing, 1 init (title) screen, 2 game running)
status: status:
BYTE BYTE
; Intro counter
introCounter:
BYTE
; Costants ; Costants
; ---------------------------------------------------------------------- ; ----------------------------------------------------------------------
@ -78,6 +89,9 @@ foodColor:
gameoverString: gameoverString:
BYTE "GAME IS OVER" BYTE "GAME IS OVER"
BYTE #0 BYTE #0
intro0string:
BYTE "SNAKE BY GIOMBA"
BYTE #0
; List ; List
; ---------------------------------------------------------------------- ; ----------------------------------------------------------------------
@ -94,23 +108,6 @@ start:
; Clear screen, initialize keyboard, restore interrupts ; Clear screen, initialize keyboard, restore interrupts
jsr $ff81 jsr $ff81
; Set screen colors
; - - - - - - - - -
lda #12
sta $d020 ; overscan
lda #9
sta $d021 ; center
; Upper bar -- fill with reversed spaces, color yellow
ldx #39
upperbarLoop:
lda #$a0 ; reversed color space
sta $400,x
lda #7
sta $d800,x
dex
cpx #$ff
bne upperbarLoop
; Disable all interrupts ; Disable all interrupts
sei sei
@ -144,30 +141,26 @@ upperbarLoop:
lda #0 lda #0
jsr sidtune jsr sidtune
; Put first piece of food ; Set status as first-time intro playing
lda foodTile lda #0
sta $500 sta status
; Enable interrupts ; Enable interrupts
cli cli
jsr fullreset intro0running:
; Simple intro
intro:
ldx 53280
inx
stx 53280
jsr $ffe4 jsr $ffe4
cmp #$20 cmp #$20
bne intro bne intro0running
lda #1 ; Set init variables of the game
jsr fullreset
; Set status as game playing
lda #2
sta status sta status
endless: endless:
; Endless loop, show that there is enogh time after the interrupt ; Endless loop, show that there is enough time after the interrupt
ldx $400 ldx $400
inx inx
stx $400 stx $400
@ -176,6 +169,37 @@ endless:
; Full reset ; Full reset
; ---------------------------------------------------------------------- ; ----------------------------------------------------------------------
fullreset: fullreset:
; Clear screen
ldx #$ff
lda #$20
fullresetCLS:
sta $400,x
sta $500,x
sta $600,x
sta $700,x
dex
cpx #$ff
bne fullresetCLS
; Set screen colors
lda #12
sta $d020 ; overscan
lda #9
sta $d021 ; center
; Upper bar -- fill with reversed spaces, color yellow
ldx #39
upperbarLoop:
lda #$a0 ; reversed color space
sta $400,x
lda #7
sta $d800,x
dex
cpx #$ff
bne upperbarLoop
; Init game variables
lda foodTile
sta $500 ; Put first piece of food
lda #4 lda #4
sta irqn sta irqn
lda #6 lda #6
@ -188,29 +212,85 @@ fullreset:
sta listStart sta listStart
lda #5 lda #5
sta length sta length
lda #0
sta status
rts rts
; Interrupt Handler ; Interrupt Handler
; ---------------------------------------------------------------------- ; ----------------------------------------------------------------------
irq: irq:
; Things that must be done every interrupt (50Hz)
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; Acknoweledge IRQ ; Acknoweledge IRQ
dec $d019 dec $d019
; Save registers in stack
pha
txa
pha
tya
pha
; Check status ; Check status
lda status lda status
cmp #0
bne checkStatus1
jsr status0
jmp checkEndStatus
checkStatus1:
cmp #1 cmp #1
beq doGame ; if status is 1, must do game bne checkStatus2
jmp irqalways ; else simply do every-interrupt tasks (always-do) jsr status1
jmp checkEndStatus
checkStatus2
cmp #2
bne checkEndStatus
jsr status2
jmp checkEndStatus
checkEndStatus:
doGame: ; Play music
jsr sidtune + 3
; Increase random value
ldx random
inx
stx random
; Restore registers from stack
pla
tay
pla
tax
pla
; Go to original system routine
jmp $ea31
status0:
status1:
lda #<intro0string
sta printIntroString
lda #>intro0string
sta printIntroString + 1
lda #$00
sta introScreenStart
lda #$05
sta introScreenStart + 1
jsr printIntro
ldx $401
inx
stx $401
rts
status2: ; do Game
; Check counter ; Check counter
ldx irqn ldx irqn
dex dex
stx irqn stx irqn
beq irqsometime ; if counter is 0, then do these "rare" things beq irqsometime ; if counter is 0, then do these "rare" things
jmp irqalways ; else skip this section, and go to always-do interrupt routine rts ; else do nothing and simply return
; as you can see, game actually runs at 12 Hz
irqsometime: irqsometime:
; Things that must be done only one in four interrupts (12 Hz) ; Things that must be done only one in four interrupts (12 Hz)
@ -396,14 +476,6 @@ foodOK:
lda calcTileY lda calcTileY
jsr printByte jsr printByte
; debug -- print snake head X,Y
ldy #$1f
lda snakeX
jsr printByte
ldy #$22
lda snakeY
jsr printByte
ldy #0 ldy #0
jsr calcTileMem ; calc food address in memory jsr calcTileMem ; calc food address in memory
lda (tileMem),y ; check if memory is empty lda (tileMem),y ; check if memory is empty
@ -464,22 +536,7 @@ checkEndSelfEat:
skipPauseTests: skipPauseTests:
irqalways: rts
; Things that must be done every interrupt (50Hz)
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; Play music
jsr sidtune + 3
; Increase random value
ldx random
inx
stx random
; Go to original system routine
jmp $ea31
brk
; Game is over ; Game is over
; ---------------------------------------------------------------------- ; ----------------------------------------------------------------------
@ -611,3 +668,21 @@ printStatusSkipSpace:
jmp printStatusLoop jmp printStatusLoop
printStatusEnd: printStatusEnd:
rts rts
; Print string for intro
printIntro:
ldy #0
printIntroLoop:
lda (printIntroString),y
beq printIntroEnd
cmp #$20
bne printIntroSkipSpace
lda #$60
printIntroSkipSpace:
sec
sbc #$40
sta (introScreenStart),y
iny
jmp printIntroLoop
printIntroEnd:
rts