very basic kernel usage with disk files

All aspects of programming on the Commander X16.
Post Reply
PaulForgey
Posts: 17
Joined: Tue Mar 12, 2024 3:12 am

very basic kernel usage with disk files

Post by PaulForgey »

I have a simple assembly test program which should be the moral equivalent of this basic program:
10 OPEN 2,8,2,"BOGUS.TXT,S,R"
20 IF ST<>0 THEN GOTO 50
30 GET #2,A$:PRINT A$;
40 GOTO 20
50 PRINT:PRINT "ERROR";ST
60 CLOSE 2
Curiously, this program reads a $c7 before ST has $42 if the file does not exist.

The following assembly test program just emits an endless stream of CRs.
What am I doing wrong here?
    .org $9000

READST  =$ffb7
SETLFS  =$ffba
SETNAM  =$ffbd
OPEN    =$ffc0
CHKIN   =$ffc6
CHRIN   =$ffcf
CHROUT  =$ffd2
CLALL   =$ffe7
GETIN   =$ffe4

    jmp start

error:
    .byte 0
status:
    .byte 0

start:
    lda #13
    ldx #<name
    ldy #>name
    jsr SETNAM
    lda #2
    ldx #8
    ldy #2
    jsr SETLFS
    jsr OPEN
    bcs onErr
    ldx #2
    jsr CHKIN
    bcs onErr
    
loop:
    jsr READST
    cmp #0
    bne onErr
    jsr GETIN
    bcs onErr
    jsr CHROUT
    bra loop
    
onErr:
    sta error
    jmp CLALL
    
name:
    .str "bogus.txt,s,r"
DragWx
Posts: 342
Joined: Tue Mar 07, 2023 9:07 pm

Re: very basic kernel usage with disk files

Post by DragWx »

When you run your assembly program, does the disk light start to blink? In the emulator, it'll be a red rectangle flashing in the upper right corner of the window. On actual hardware, it's an LED on the board itself.
PaulForgey
Posts: 17
Joined: Tue Mar 12, 2024 3:12 am

Re: very basic kernel usage with disk files

Post by PaulForgey »

ok that's a good reason to connect the drive light in my case, and retrospectively something that would be been obviously helpful... (this is real CX16 hardware but using the sd card).

I've since figured out that I must have had the logical file already open via prior runs as putting a call to CLALL at the beginning made things work, as RUN does.

So now my question is, shouldn't the call to OPEN have at least raised an error status? Or is it preferred to call READST regardless of carry? Some kernel reference says C is set on error from OPEN or CHKIN, others not.
PaulForgey
Posts: 17
Joined: Tue Mar 12, 2024 3:12 am

Re: very basic kernel usage with disk files

Post by PaulForgey »

One more question still remains: why am I reading one byte from a non-existent file before getting an error?
EDIT: answering myself, the error condition is not known until reading rather than at open.
DragWx
Posts: 342
Joined: Tue Mar 07, 2023 9:07 pm

Re: very basic kernel usage with disk files

Post by DragWx »

When you "open" a file on the serial bus, any errors you get will be physical errors, like the device not responding. It doesn't matter whether the actual disk drive can find the file or not, as long as the command goes through and the drive responds, the Kernal considers the channel to be successfully opened, and you get no error.

To check for errors on the disk drive side of things (such as "file not found"), you need to open channel 15 on the disk drive and read its status report byte-by-byte. This will usually read back as "0, OK,0,0", but will say something different if there was an error, such as "62, FILE NOT FOUND,0,0". If everything opened without a hitch, then don't forget to CHKIN back to the file number you originally opened before you try reading.

The reason I asked about flashing indicators is, I suspect your assembler might be converting your string "bogus.txt,s,r" literally into PETSCII lowercase characters, which probably isn't what you want.
PaulForgey
Posts: 17
Joined: Tue Mar 12, 2024 3:12 am

Re: very basic kernel usage with disk files

Post by PaulForgey »

makes total sense; at the kernel level we're dealing with the transport, and thus we attempt to read the first byte of the file in error before knowing there is an error.
Post Reply