Jump to content
  • 0
Stefan

Read disk directory in assembly

Question

Hi,

I've been testing to read the disk directory in assembly using Kernal functions.

I have found some example code for the X16 that uses secondary address $60 when reading the directory. However, I can't find that this is mentioned anywhere in the docs.

In the 1541 user manual, there is an example program at the end that reads, amongst other, the directory. It uses secondary address 0.

Secondary address $60 seems to work, but secondary address 0 returns error "File is not an input file".

Why is this so?

Share this post


Link to post
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Odd, secondary address 0 should and does work for me.
The code that I use essentially does OPEN 13,8,0,"$"  -  read byte from channel 13 and print it to the screen, until break/error/EOF - CLOSE #13.

(the printing is a bit more involved than that because it has to decode the block numbers)

Are you sure you SETNAM/ SETLFS correctly?

Share this post


Link to post
Share on other sites
  • 0

I tried again, and it works now with secondary address 0. I was pushing the right params to SETLFS so it must have been something else.

Sorry for cluttering the page.

Share this post


Link to post
Share on other sites
  • 0
Posted (edited)

The fun thing is I studied your source code for XEdit to get me going initially with the disk i/o functions !

Edited by desertfish

Share this post


Link to post
Share on other sites
  • 0

This is interesting. So the SA has to be 0 to make the $ work. Now that I know that, I am able to check some things off my list. 

Playing around with this, it looks like the directory is structured like this:

Header:
2 byte Load address
5 byte binary data 
18 byte label text
6 byte volume type (FAT32)

File Entry:
3 bytes binary data (always 0 1 1)
2 bytes file size
26 bytes text (spaces, "filename" type)

Final Entry
3 bytes binary data (always 0 1 1)
2 bytes MB free
"MB FREE."
3 bytes 0

The only way to distinguish the "MB FREE" line from a directory entry appears to be the "M" in the position right after the file size. If this is a space, then there will be more spaces, followed by the quote (") and the filename, followed by another quote, some spaces, and PRG or DIR. 

Here's a quick BASIC program to read the directory and print it on the screen:

I'm also working on a file picker, with a goal to eventually build an Orthodox File Manager on the system.

 

Share this post


Link to post
Share on other sites
  • 0
Posted (edited)

@TomXP411 what helped a lot for me is to understand that when you load "$" from the disk drive, it is actually making it as a basic program listing for you. So the format you have to parse is how a normal basic program works in memory!  Line numbers are the block sizes.

That's why the default way of doing LOAD"$",8 : LIST  works as it does (but overwrites your current basic program)

By the way the basic program you linked above gets confused about the line numbers/ block counts it seems and starts printing garbage after a few files, unfortunately.  I tried it in the emulator with a sdcard image attached with about 30 files on it.

Here's my take on doing it (in prog8)  https://github.com/irmen/prog8/blob/master/compiler/res/prog8lib/diskio.p8#L8

Edited by desertfish

Share this post


Link to post
Share on other sites
  • 0
Posted (edited)
3 hours ago, desertfish said:

@TomXP411 what helped a lot for me is to understand that when you load "$" from the disk drive, it is actually making it as a basic program listing for you. So the format you have to parse is how a normal basic program works in memory!  Line numbers are the block sizes.

That's why the default way of doing LOAD"$",8 : LIST  works as it does (but overwrites your current basic program)

By the way the basic program you linked above gets confused about the line numbers/ block counts it seems and starts printing garbage after a few files, unfortunately.  I tried it in the emulator with a sdcard image attached with about 30 files on it.

Here's my take on doing it (in prog8)  https://github.com/irmen/prog8/blob/master/compiler/res/prog8lib/diskio.p8#L8

It fails when there's a filename longer than 16 characters. I'm re-writing it to look for a null (end of line), rather than just read the filename as a fixed length. 

.. aand done. Now to save the data to RAM somewhere and build a file picker. 

 

 

Edited by TomXP411

Share this post


Link to post
Share on other sites
  • 0

So here's a dumb question... I'm working through some stuff in assembly right now, and I am not sure where the "ST" variable is in the new memory layout. The Butterfield book says to read $90 (64,VIC) or $96 (PET), but I am not sure either of those is valid on the CX16. 

Doing some quick reverse engineering (ie: going through page $200 at the end of a file, looking for 64s), it looks like address 646 ($286) is the ST variable. I'm going to go with this for now, but I'm hoping we get an API for this at some point

Share this post


Link to post
Share on other sites
  • 0
9 hours ago, desertfish said:

There is: the READST kernal routine. 

Thanks. This makes me wonder why Butterfield suggested we read the Zero Page variable directly. 

In fact, I found that by accident while browsing a really nice API list, here:

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

I finally finished my directory reader in assembly: it's basically a line-for-line translation of the BASIC code... it took a while to get the file open correctly, do the correct CHKIN, and all of the other fun stuff that goes along with it. 

Either way - now that I can read a directory, this is the first step to building my file manager. 

Edited by TomXP411
  • Like 3

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Please review our Terms of Use