C64 Disk drive access

(Seems my theme shows Code as black text on black background – will fix that eventually 😉

There are always aspects of c64 programming that you haven’t tested and exploring it is also a new learning. Assuming things that turn out to be wrong is always a nice learning and then you are expected to remember and next time around you are expected to navigate better.

Normally when loading, I use fastloaders which means that the entire area of how IO works, is sort of delegated. But in order to load and save status files, it is always an option to revert to having DOS handle that.

The standard way of handling this would be like this:

	lda #loadname_end-loadname
	ldx #<loadname
	ldy #>loadname
	jsr setnam

	lda #$02
        // Logical file
	ldx $ba 	// last used device number
	bne !+
	ldx #$08     	// default to device 8
!:	ldy #$02	// Secondary address
	jsr setlfs 	// call SETLFS - OK with no vectors

	jsr open
	bcs Error

And this is where I ran into the same issue I had experienced before, but seemingly didn’t learn from.

When executing Open as per the above, the Carry flag is signalling that an error has occurred. The error is typically that the communication channel couldn’t be established – most importantly as the drive didn’t respond or if you have too many files already open. Please mind that the error is NOT including the scenario that the file in question doesn’t exist.

So if you want to detect if the file exists, you must ask the drive to prepare for reading by calling chkin with the current logical file number in X, and then fetch a byte. The Error label hear is reached when your read failed, and a common reason is that the file didn’t exist.

	ldx #$02
 // Logical file
	jsr chkin

	jsr chrin
	// Store away the A if you need this

	jsr readst
	beq !+

Error:	// Handle an error 

!:	// Reading was ok

Some references:
https://www.pagetable.com/c64ref/kernal/

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.