Jump to content

SaveKey for Dummies!

Recommended Posts

  • 6 months later...

I noticed that reading the SaveKey at the very start sometimes failed for me (in about 1 out of 10 cases). Maybe it is a problem of my SaveKey, I'm not sure.

Anyway, by waiting a few milliseconds after startup (~80 scan lines), these problems went away.

Link to comment
Share on other sites

  • 10 months later...
On 3/6/2016 at 9:25 AM, Thomas Jentzsch said:

I found no easy to use code for loading and storing on a SaveKey or AtariVox. Therefore I am presenting my code here.

All it takes are:

  • add the attached include file to your code's directory
  • define the three variables/constants at the beginning of the code
  • call WriteSaveKey for storing your score etc.
  • call ReadSaveKey for reading what you have stored


  • if you have multiple scores to store (e.g. different game modes), you have to modify the address used in SetupSaveKey.
  • the X register is not used in any i2c subroutine.
  • after calling WriteSaveKey you must wait at least 5ms (~80 scan lines) before accessing the SaveKey again.
  • update i2c v2.3 attached, fixes unwanted noise issues when using an AtariVox

skBuffer    = <score RAM>       ; define the RAM address you want to store
SK_BYTES    = <n>               ; define how many bytes your want to store
SAVEKEY_ADR = $xxxx             ; ask Albert for a free slot! 
    include "i2c_v2.3.inc"      ; a highly optimized (for space) version   
    i2c_subs                    ; this makes the i2c macros of the include file known to the code 

WriteSaveKey SUBROUTINE         ; total cycles = 1923 (for 3 bytes)
; setup SaveKey:
    jsr     SetupSaveKey        ; 6+927
    bcc     .noSKfound          ; 2/3

; write high score:
    ldx     #SK_BYTES-1         ; 2 = 937   
    lda     skBuffer,x          ; 4
    jsr     i2c_txbyte          ;6+296      transmit to EEPROM
    dex                         ; 2
    bpl     .loopWriteSK        ; 2/3=932

; stop write:
    jsr     i2c_stopwrite       ; 6+42=48   terminate write and commit to memory
    rts                         ; 6

ReadSaveKey SUBROUTINE          ; total cycles = 2440 (for 3 bytes)
; setup SaveKey:
    jsr     SetupSaveKey        ;6+927
    bcc     .noSKfound          ; 2/3

; start read:
    jsr     i2c_stopwrite       ;6+42       end of "fake" write
    jsr     i2c_startread       ;6+284      Start signal and $a1 command byte

; read high score:
    ldx     #SK_BYTES-1         ; 2 = 1275
    jsr     i2c_rxbyte          ;6+333      read byte from EEPROM
    cmp     #$ff                ; 2         EEPROM slot empty? (we are assuming $ff for uninitialized space)
    bne     .skipEmptySK        ; 2/3        no, skip clear
    lda     #0                  ; 2         clear EEPROM slot
    sta     skBuffer,x          ; 4
    dex                         ; 2
    bpl     .loopReadSK         ; 2/3=1061

; stop read:
    jsr     i2c_stopread        ;6+92=98    terminate read
    rts                         ; 6

SetupSaveKey SUBROUTINE         ; = 927
; detect SaveKey:
    jsr     i2c_startwrite      ;6+312
    bne     .exitSK             ; 2/3

; setup address:
    clv                         ; 2
    lda     #>SAVEKEY_ADR       ; 2         upper byte of address
    jsr     i2c_txbyte          ;6+296
    lda     #<SAVEKEY_ADR       ; 2         lower byte offset
    jmp     i2c_txbyte          ;3+296      returns C==1


; 176 bytes in total (less if you inline the subroutines)

I hope this answers all questions. Else let me know. icon_smile.gif

SaveKey & AtariVox memory allocation list

i2c_v2.2.inc 5.18 kB · 253 downloads

i2c_v2.3.inc 5.35 kB · 247 downloads

Hello, do you have example program using this in bB please?


How do I actually call the methods?

Do I do something like WriteSaveKey $3001 = s? S being the variable? Thank you

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...