SaveKey

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andrew Davie

Installing SaveKey under CDFJ should be pretty straightforward. But I had some issues, so here's a few tips.

Firstly, the SaveKey for Dummies is where you should go for a how-to and for the required include file download. Now because the SaveKey code is written for "normal" 6502, you have to be aware of issues if you're using the CDFJ "overload immediate load" functionality.  That is, where "lda #" is replaced by reading from data streams instead of loading the immediate value.

Easy fix; wrap the SaveKey code with a disable of the "fast fetch" immediate override. Here's the basic part of my SaveKey code in CDFJ...

                    ldx #FASTOFF
                    stx SETMODE         ; fast fetch OFF for savekey code compatibility

    ; Load savekey and send to ARM/C

                    jsr ReadSaveKey

                    lda #>_DS_SK            ; H/L ordering
                    sta DSPTR
                    lda #<_DS_SK
                    sta DSPTR

                    ldx #0 ;SK_BYTES
.sendSK             lda _SAVEKEY_IDENT,x
                    sta DSWRITE
                    inx
                    cpx #SK_BYTES
                    bcc .sendSK

                    ldx #>_DS_TO_ARM
                    stx DSPTR
                    ldx #<_DS_TO_ARM
                    stx DSPTR

                    lda #_FN_LOAD_SAVEKEY
                    sta DSWRITE                     ; --> _RUN_FUNC (OS or VB)

                    jsr runARM

                    lda #FASTON
                    sta SETMODE             ; "Fast Fetch" enable for rest of game

Essentially: turn off fast fetch, load the savekey data, transfer it to the ARM memory area, then call a function in ARM/C to process the data. This is done pretty much at startup in my code, before any screen display.

For completeness, here's my ReadSaveKey function; I don't need it to be super-efficient because it runs before the game starts a display. I went for readibility. It sets up a data buffer, then tries to read the savekey data - if it's not OK then writes the buffer to the SaveKey. Please note the line commented CRITICAL; this one tripped me up big time.

Essentially, if you are writing to SaveKey and you then go on to read back from it, you must halt the write before performing a read.

; setup SaveKey:

    ; pre-cleer GridLock flags buffers
    ; will exit with _SAVEKEY_IDENT = _SK_GRIDLOCK_ID if valid

_GRIDLOCK_SK_ID = 0x87

                    ldx #SK_BYTES-1
                    lda #0
.clearFlagsKey      sta _SAVEKEY_IDENT,x
                    dex
                    bpl .clearFlagsKey

    ; Note: _SAVEKEY_IDENT = 0 at this point
    ; Now unlock the first 10 levels
   
                    lda #%11111111
                    sta _SAVEKEY_UNLOCKED
                    lda #%11
                    sta _SAVEKEY_UNLOCKED+1

.verify             jsr SetupSaveKey
                    bcc .noSKfound

                    jsr i2c_stopwrite
                    jsr i2c_startread

                    jsr i2c_rxbyte
                    cmp #_GRIDLOCK_SK_ID
                    beq .verifiedGridLock
                    jsr i2c_stopread        ; CRITICAL

                    ldx #_GRIDLOCK_SK_ID
                    stx _SAVEKEY_IDENT
                   
                    jsr WriteSaveKey
                    jsr i2c_stopwrite       ; CRITICAL
                    jmp .verify


                    lda #_GRIDLOCK_SK_ID
.verifiedGridLock   sta _SAVEKEY_IDENT

                    ldx #1
.loopReadSK         jsr i2c_rxbyte
                    sta _SAVEKEY_IDENT,x
                    inx
                    cpx #SK_BYTES
                    bcc .loopReadSK

 
.noSKfound          jsr i2c_stopread
 
    ; SK present IFF _SAVEKEY_IDENT == _GRIDLOCK_SK_ID, and vars will hold SK content
    ; 0 if not present, and in that case the vars will hold valid (first-time-init) data

                    rts

Lastly, to allocate a "slot" to your game, visit SaveKey & AtariVox Memory Allocation List to see what's available and then send a message to Albert at AtariAge to allocate you a slot.