A C64 game - Step 24

posted in New Old Things
Published September 24, 2011
Advertisement

What use are highscores if they aren't saved?

With this step highscores are saved and loaded from your previously used medium (tape or disk). On startup the currently used medium is stored, save and load is done via bog standard kernal routines.

The additions for this are rather miniscule. Both save and load routines work on a continious piece of memory. Fortunately both highscore names and scores are right next to each other.

For saving note that the auto-delete function in the common floppy drive is borked. Therefore we manually delete the file first and save it afterwards.
The "S0:" in front of the file name is the special code for Scratching the file.

HIGHSCORE_DELETE_FILENAME
          !text "S0:HIGHSCORE"
         
HIGHSCORE_DELETE_FILENAME_END

!zone SaveScores
SaveScores
          ;delete old save file first
          lda #HIGHSCORE_DELETE_FILENAME_END - HIGHSCORE_DELETE_FILENAME
          ldx #<HIGHSCORE_DELETE_FILENAME
          ldy #>HIGHSCORE_DELETE_FILENAME
          jsr KERNAL_SETNAM
          
          lda #$0F ; file number 15
          ldx DRIVE_NUMBER
          ldy #$0F ; secondary address 15
          jsr KERNAL_SETLFS
          jsr $FFC0 ; call OPEN
          
          ; if carry set, the file could not be opened
          bcs .ErrorDelete
          ldx #$0F ; filenumber 15
          jsr $FFC9 ; call CHKOUT (file 15 now used as output)
          
.close
          lda #$0F ; filenumber 15
          jsr $FFC3 ; call CLOSE
          ldx #$00 ; filenumber 0
          jsr $FFC9 ; call CHKOUT (reset output device)
          jmp .SaveNow
          
.ErrorDelete
          ;Akkumulator contains BASIC error code
          ;most likely errors:
          ;A = $5 (DEVICE NOT PRESENT)
          ;... error handling for open errors ...
          lda #65
          sta $cc00
          jmp .close 
          
          ; even if OPEN failed, the file has to be closed
          
.SaveNow
          lda #9
          ldx #<HIGHSCORE_FILENAME
          ldy #>HIGHSCORE_FILENAME
          jsr KERNAL_SETNAM
          
          lda #$00
          ldx DRIVE_NUMBER
          ldy #$00
          jsr KERNAL_SETLFS
          
          lda #<HIGHSCORE_SCORE
          sta $C1
          lda #>HIGHSCORE_SCORE
          sta $C2
          ldx #<HIGHSCORE_DATA_END
          ldy #>HIGHSCORE_DATA_END
          lda #$C1 ; start address located in $C1/$C2
          jsr $FFD8 ; call SAVE
          ;if carry set, a save error has happened;
          bcs .SaveError
          rts

Loading the score back in means mostly inserting the corresponding load calls:

;--------------------------------------------------
;load high scores
;returns 1 if ok, 0 otherwise
;--------------------------------------------------
!zone LoadScores
LoadScores
          ;disable kernal messages (do not want to see load error etc.)
          lda #$00
          jsr KERNAL_SETMSG
          
          ;set logical file parameters
          lda #15
          ldx DRIVE_NUMBER
          ldy #0
          jsr KERNAL_SETLFS
          
          ;set filename
          lda #9
          ldx #<HIGHSCORE_FILENAME
          ldy #>HIGHSCORE_FILENAME
          jsr KERNAL_SETNAM
          
          ;load to address
          lda #$00 ; 0 = load
          ldx #<HIGHSCORE_SCORE
          ldy #>HIGHSCORE_SCORE
          jsr KERNAL_LOAD
          bcs .LoadError 
          
          ;flag whether ok or not is set into the Carry flag
          lda #1
          rts
          
.LoadError
          lda #0
          rts

step24.zip


Previous Step Next Step



Previous Entry A C64 game - Step 23
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement