;--------------------------------------------------------------------------- ; simple example that demonstrates how to write to the flash ROM ;--------------------------------------------------------------------------- !src "../include/gmod3.inc" * = $0801 !word nextline !word 2016 !byte $9e !byte $32, $30, $36, $31 nextline: !byte 0,0,0 jmp start !src "../lib/gmod3.asm" statusdisplay = $0400 + 39 bufferpage = $0400 + 10*40 bytecount=$ae pagecount=$af ;------------------------------------------------------------------------------- start: ;sei ; clear screen ldx #0 - lda #$20 sta $0400,x sta $0500,x sta $0600,x sta $0700,x lda #$01 sta $d800,x sta $d900,x sta $da00,x sta $db00,x inx bne - ; input filename ldx #(promptend-prompt)-1 - lda prompt,x sta $0400+(40*2),x dex bpl - ldx #2 ; line ldy #(promptend-prompt) ; column clc jsr $fff0 ; set cursor jsr inputname jsr file_open lda #$00 sta pagecount sta pagecount64k sta eof ; before we can write to the flash, we must erase it ; inc $0400 ; to indicate progress ; jsr flash_chiperase ; inc $0401 ; to indicate progress ; ldy #0 ;-- ; ldx #0 .flashloop ; tya ; pha ; txa ; pha ; inc $0405 ; to indicate progress jsr showpage lda #'R' & $3f sta statusdisplay lda #0 sta bytecount - jsr file_read ldx bytecount sta bufferpage,x inc bytecount bne - ; every time the page counter is 0, we need to erase a 64k page in the ; flash memory lda pagecount bne + jsr erasepage + lda #'F' & $3f sta statusdisplay ; set byte program command and flash address ; pla ; pha ; tax lda pagecount64k sta addr2 ;ldx #0 ldx pagecount stx addr1 ;lda #0 ;sta addr0 lda #bufferpage ; program one 256byte page jsr flash_writepage lda #' ' & $3f sta statusdisplay ; pla ; tax ; pla ; tay inc pagecount bne + inc pagecount64k + eof=*+1 lda #0 sta $0400+9 bne + jmp .flashloop + ; done - inc $d020 dec $d020 jmp - ;------------------------------------------------------------------------------- erasepage: lda #'E' & $3f sta statusdisplay pagecount64k=*+1 lda #0 jsr flash_erase lda #' ' & $3f sta statusdisplay rts showpage: lda pagecount64k pha lsr lsr lsr lsr tay lda hexdigits,y sta $0400 pla and #$0f tay lda hexdigits,y sta $0401 lda pagecount pha lsr lsr lsr lsr tay lda hexdigits,y sta $0402 pla and #$0f tay lda hexdigits,y sta $0403 rts hexdigits: !scr "0123456789abcdef" ; this is very naive, does not support editing etc inputname: ldx #0 - jsr $ffcf cmp #$0d beq + sta fname,x inx cpx #16 bne - + stx namelen rts ;------------------------------------------------------------------------------- file_open: LDA namelen LDX #fname JSR $FFBD ; call SETNAM LDA #$02 ; file number 2 LDX $BA ; last used device number BNE .skip LDX #$08 ; default to device 8 .skip LDY #$02 ; secondary address 2 JSR $FFBA ; call SETLFS JSR $FFC0 ; call OPEN BCS .error ; if carry set, the file could not be opened ; check drive error channel here to test for ; FILE NOT FOUND error etc. LDX #$02 ; filenumber 2 JSR $FFC6 ; call CHKIN (file 2 now used as input) rts file_read: jmp $FFB7 ; call READST (read status byte) bne .eof ; either EOF or read error jmp $FFCF ; call CHRIN (get a byte from file) .eof and #$40 ; end of file? beq .readerror .close lda #$02 ; filenumber 2 jsr $FFC3 ; call CLOSE jsr $FFCC ; call CLRCHN lda #1 sta eof rts .error ; Akkumulator contains BASIC error code ; most likely errors: ; A = $05 (DEVICE NOT PRESENT) ;... error handling for open errors ... jmp .close ; even if OPEN failed, the file has to be closed .readerror ; for further information, the drive error channel has to be read ;... error handling for read errors ... jmp .close fname: !pet "cart.bin " namelen: !byte 0 prompt: !scr "filename:" promptend: