USBhost-64 another example application

I wrote another little example application for USBhost-64 that behaves exactly like USAVE when the latter is not given the optional start/end addresses of the block to save. In other words: a simple BASIC program save tool.
The main difference between the USAVE command in USB-BASIC and this example application is that the former is not built on top of the file access API (fileio.asm) but directly on top of the USB kernel (i.e. it does not use fopen, fwrite, fclose).

Here it is:

;
; $Id: saver.asm,v 1.1 2014/02/12 14:34:33 luigidifraia Exp $
;
;       $RCSfile: saver.asm,v $
;         $State: Exp $
;      $Revision: 1.1 $
;          $Date: 2014/02/12 14:34:33 $
;        $Author: luigidifraia $
;
; $Log: saver.asm,v $
; Revision 1.1  2014/02/12 14:34:33  luigidifraia
; Initial revision
;

; Example application for USBhost-64 that saves the BASIC program in RAM
; (C) 2014 Luigi Di Fraia. All Rights Reserved

; ----
; Boot
; ----

*=$c000

	; --------------
	; Initialize I/O
	; --------------

	jsr ioinit

	; ---------
	; Save file
	; ---------

	lda #fnamesz	; Prepare filename info
	sta $b7
	lda #<fname
	sta $bb
	lda #>fname
	sta $bc

	jsr fopenw	; Open file for write
	bcc _savefile

	lda #<errfopw
	ldy #>errfopw
	jmp $ab1e	; Error opening file for write

_savefile
	lda #$2b	; Point to TXTTAB vector
	sta $ae
	lda #$00
	sta $af

	lda #$02	; Save 2 bytes
	sta $ac
	lda #$00
	sta $ad

	jsr fwrite	; Save as PRG start address
	bcc _savedata

_closeerr
	jsr fclose

	lda #<errwrite
	ldy #>errwrite
	jmp $ab1e	; Error writing data to file

_savedata
	lda $2b		; Pointer to the Start of BASIC Program Text
	sta $ae
	lda $2c
	sta $af

	sec
	lda $2d		; Pointer to the Start of the BASIC Variable Storage Area
	sbc $2b
	sta $ac
	lda $2e
	sbc $2c
	sta $ad

	jsr fwrite	; Save BASIC area
	bcs _closeerr

	jsr fclose

	rts

; ---------------
; String messages
; ---------------

errfopw
	.text "ERROR OPENING FILE FOR WRITE"
	.byte $0d,$00

errwrite
	.text "ERROR WRITING DATA TO FILE"
	.byte $0d,$00

;----------
; Constants
;----------

fname	.asc "BASIC.PRG"
fnamesz = * - fname
	.byte $00	; Terminator for screen output purposes (debug mode only)

; ----------
; Inclusions
; ----------

.include fileio.asm

The example I previously wrote in order to load a PRG file into the Commodore 64’s RAM is definitely more compact but that’s due to the fact that the whole PRG file, including the start address, are handled transparently by the floadprg function, provided that it is given the whole file size for loading. Of course if I were to use the fopen/fread/fclose approach, the two examples would look pretty similar.

Last but not least, by using user vectors (which means changing the above code slightly) rather than $2b/$2c and $2d/$2e one can save pretty much any area of the C64’s RAM 🙂

This entry was posted in Retrocomputing, Technical 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