BSave and BLoad for the Commodore 64

I wrote this little subroutine package for the Commodore 64 in 2002. You can likely still find a cached Usenet post to comp.sys.cbm referring to it.

I thought that I would add in an open-source license header and would present it here.

The source code below is for two machine language library routines which can be called from BASIC. The first routine will save a section of the C64’s memory to disk. The second will load a file from disk into a specific spot in memory. These functions are akin to Applesoft BASIC’s BSAVE and BLOAD binary I/O commands.

I did not wedge the CHRGET routine to add any new command verbs to BASIC; one must use the SYS command to invoke each subroutine. A jump-table at the beginning of the module controls which of the two functions you’ll execute. Address 49152 is the BSAVE routine. Address 49152+3 is the BLOAD routine.

Please note that the filename parameter must always be in a variable. I didn’t have the routine that would parse a literal properly readily available … and still don’t remember how to do it off the top of my head. 😉

; BSaVe and bLoaD extentsions for the Commodore 64
;
; License: MIT / X11
; Copyright (c) 2009, 2015 by James K. Lawless
; see http://www.mailsend-online.com/license2015.php
;
;
; To use in C64 BASIC:
; sys 49152,a$,start-addr,end-addr
; The above will save to file named in a$
; sys 49152+3,a$,start-addr
; The above will load a file named in a$
; to the specified location
;
; To save the default BASIC screen:
; a$=";screen,s,w";
; sys 49152,a$,1024,1024+999
;
; To reload the default BASIC screen
; a$=";screen,s,r";
; sys 49152+3,a$,1024

   *=$c000
; jump table for functions
   jmp save
   jmp load
flag .BYTE 00
save = *
   lda #$00
   sta flag
   jmp main
load = *
   lda #$01
   sta flag
   jmp main

main = *
   jsr $aefd ; skip comma
   jsr $b08b ; search for variable
   jsr $b185 ; find address
   sta $fb
   tya
   sta $fc
   lda #$00
   tay
   lda ($fb),y
   pha
   iny
   lda ($fb),y
   pha
   iny
   lda ($fb),y
   tay
   pla
   tax
   pla
   jsr $ffbd ; SETNAM
   lda #$02
   ldx #$08
   ldy #$00
   jsr $ffba ; SETLFS

   lda flag
   cmp #$00
   beq dosave
   jmp doload

dosave = *
   jsr $aefd ; skip comma
   jsr $ad8a ; eval expr
   jsr $b7f7 ; get uint in a,y
   sta $fc
   tya
   sta $fb

   jsr $aefd ; skip comma
   jsr $ad8a ; eval expr
   jsr $b7f7 ; get uint in a,y
   sta $fd
   tya
   tax
   ldy $fd
   lda #$fb

   jsr $ffd8 ; save ram
   rts

doload = *
   jsr $aefd ; skip comma
   jsr $ad8a ; eval expr
   jsr $b7f7 ; get uint in a,y
   sta $fd
   tya
   tax
   ldy $fd
   lda #$00
   jsr $ffd5 ; load ram
   rts
Advertisements

About Jim Lawless

I've been programming computers for about 36 years ... 30 of that professionally. I've been a teacher, I've worked as a consultant, and have written articles here and there for publications like Dr. Dobbs Journal, The C/C++ Users Journal, Nuts and Volts, and others.
This entry was posted in Programming and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s