Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a disk file using assembly on Commodore 64

Tags:

assembly

c64

6502

I'm trying to learn how to write disk files using kernal routines following this Codebase64 Tutorial.

I have copied my routine, written in Acme Crossassembler, below. It failed to open file and gives error message: "FILE NOT OPENED"

; Definitions
SETNAM = $FFBD
SETFLS = $FFBA
OPEN   = $FFC0
CHKOUT = $FFC9
READST = $FFB7
CLOSE  = $FFC3
CLRCHN = $FFCC
CHROUT = $ffd2      

;Basic Start
    * = $0801                               ; BASIC start address (#2049)
    !byte $0d,$08,$dc,$07,$9e,$20,$34,$39   ; BASIC loader to start at     $c000...
    !byte $31,$35,$32,$00,$00,$00           ; puts BASIC line 2012 SYS 49152

;Program Code
    * = $c000                               ; Can be executed by writing sys 49152

    ldx #<message0         
    ldy #>message0   
    jsr printMessage    


save2file:      
    ; call SETNAM   
    lda #fname_end-fname    ; file name size
    ldx #<fname             ; file name vector
    ldy #>fname             ; file name vector
    jsr SETNAM              ; call SETNAM

    ; call SETFLS
    lda #$00
    ldx $BA                 ; last used device number
    bne +
        ldx #$08            ; default to device 8
+   ldy #$00
    jsr SETFLS              ; call SETLFS

    ;call OPEN
    jsr OPEN                ; call OPEN
    bcs .error1             ; if carry set, the file could not be opened

    ; call CHKOUT
    ldx #$02                ; filenumber=2
    jsr CHKOUT              ; file 2 now used as output

    ; Copy border color to the file
    jsr READST              ; call READST (read status byte)
    bne .error2             ; write error
    lda $d020               ; get byte from memory
    jsr CHROUT              ; write to file

    ldx #<message1         
    ldy #>message1     
    jsr printMessage

.close
    lda #$02      ; filenumber 2
    jsr CLOSE     ; call CLOSE
    jsr CLRCHN    ; call CLRCHN
    rts

.error1
    ldx #<errorMsg1         
    ldy #>errorMsg1   
    jsr printMessage
    jmp .close

.error2
    ldx #<errorMsg2         
    ldy #>errorMsg2   
    jsr printMessage    
    jmp .close        

fname:  !tx "DATA,S,W"
fname_end:

message0:   !by 141 : !scr"SAVING" : !by 0
message1:   !by 141 : !scr"COLORS SAVED" : !by 0
errorMsg1:  !by 141 : !scr"FILE NOT OPENED" : !by 0
errorMsg2:  !by 17 : !scr"WRITE ERROR" : !by 0

;==========================================================================
; printMessage
;   Prints null terminated string to the memory
;   Input: x,y adress vector of text string 
;==========================================================================
temp     = $fb          ;zero page pointer

printMessage:   
    stx temp            ;save string pointer LSB
    sty temp+1          ;save string pointer MSB
    ldy #0              ;starting string index

-   lda (temp),y        ;get a character
    beq +               ;end of string
        jsr CHROUT      ;print character
        iny             ;next
        bne -
    inc temp+1             
    bne -       
+ rts               

I've prepared the Basic Routine listed below using C64 Programmer's Reference. It works as expected in the very same environment.

10 OPEN 3,8,3, "O:DATA FILE,S,W"
20 PRINT#3, "SENT TO DISK"
30 CLOSE 3      

So, why my asm routine doesn't work?

I'm testing on Vice 2.4

like image 850
wizofwor Avatar asked Apr 11 '17 21:04

wizofwor


People also ask

Can you run machine code on the C-64 with an assembler?

This is why sometimes you’ll have to build to disk, then reboot your C-64, load the compiled program and take a look at it without the assembler in the way. So this is the short and simple answer to the recurring question. You can run machine code on the C-64 with an assembler.

What is the best assembler for Commodore 64?

The Machine Shop 1.3 by Don French and John Shifflett is an assembler for the Commodore 64 that evolved from "The Assembler" for the VIC-20. Full instructions and walk through are on the disk. A review of "The Machine Shop" can be found in Ahoy! magazine issue 23 on page 86.

Do I need a C-64 REU for a Commodore 128?

If you have a Commodore 128, it can be switched to C-64 mode and you can use the extra 64K as a REU. But it’s really not a must, you can just use a stock machine. I assume you already know how download C-64 software, handle .D64 files, run an emulator, or if you have a real C-64, how to transfer a .D64 to a disk or an SD2IEC unit.

Are there any good Commodore 64 emulators out there?

There are plenty of Commodore 64 emulators out there. VICE, the Versatile Commodore Emulator, is a program that runs on a Unix, Win32, or Mac OS X machines and emulates the C64 (and every other 65xx Commodore machine too).


1 Answers

Apperently the problem was in Logical number and secondary adress as indicated by J...

I have fixed it by changing parts .i.e:

    ; call SETFLS
    lda #$03
    ldx $BA                 ; last used device number
    bne +
        ldx #$08            ; default to device 8
+   ldy #$03
    jsr SETFLS              ; call SETLFS

...

    ; call CHKOUT
    ldx #$03                ; filenumber=3
    jsr CHKOUT             ; file 2 now used as output

...

.close
    lda #$03      ; filenumber 3
    jsr CLOSE     ; call CLOSE
    jsr CLRCHN    ; call CLRCHN
    rts

There are other issues like "COLORS SAVED" message got sent to the file instead of screen, but those can be fixed easily.

like image 152
wizofwor Avatar answered Oct 11 '22 18:10

wizofwor