Looking for some good disk I/O examples

Basic and Machine Language

Moderator: Moderators

Post Reply
The Geek on Skates
Vic 20 Drifter
Posts: 33
Joined: Fri Jul 12, 2019 6:11 am
Website: http://www.geekonskates.com
Occupation: Code Monkey

Looking for some good disk I/O examples

Post by The Geek on Skates »

Hey guys,

So in BASIC, I can use commands like OPEN, PRINT#, INPUT# and CLOSE to read/write files on disk; but how would this work in Assembly? I've done some research and it looks like there are some memory addresses in the Kernal I can work with for this, but the only examples I've been able to find are for the C64. Of course I also looked at cc65, just cuz (and to be honest, I'd prefer C over Assembly most of the time) but cc65 can do inline Assembly, so learning it in Assembly would help with cc65 as well. So it looks like the process is roughly:

1. Set a couple registers and JSR SETNAM (to set the name of the file to save, like in BASIC the OPEN command has that parameter)
2. Set a few more registers and JSR SETLFS (to set a few numbers I don't fully understand even in BASIC, but they work :D ). Seriously though I know the first number is kind of like a pointer (like the "1" in i.e. INPUT#1). And I know second one is the device number, 1 for tape or 8[-15] disk. The third ("secondary") number is a total mystery to me, and I'm guessing it's because the examples (and Kernal reference) I found were for the C64.
3. JSR OPEN
4. I forget the shorthand word for this, but it was to "check in" or "check out" (set the default input/output device). It's funny, their shorthand is so goofy that at first I was like, "what the... JSR CHICKEN?" :D
5. I'm guessing I would need to use GETCHAR and PUTCHAR after that (depending on whether I'm reading or writing)
6. Then there was a "restore default input/output device" subroutine (again, I forget the shorthand)
7. then JSR CLOSE and you're done

But when I tried to do that (using code I accidentally deleted), it seemed to save blank/empty SEQ files. So I kind of feel like I'm missing a step or three (maybe something in those mysterious numbers for SETLFS, or something else?). Or maybe the addresses are different for the VIC than they were for the C64? Regardless, it would sure be great if there were some tutorials out there.
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: Looking for some good disk I/O examples

Post by chysn »

I can point you to some concise examples in some of my code. The save and load routines for the cartridge version of ZEPTOPOLIS are simple and robust. This forum's own Mike taught me a ton about disk operations while I was working on my assembler, and that stuff is kind of boiled down in the ZEPTOPOLIS code. The game saves a specific range of memory, and it supports device selection, file naming, error handling, etc. It's complete but not complex.

Here's the file in the public repository: https://github.com/Chysn/VIC20-ZEPTOPOL ... s-cart.asm

Search that file for the DiskSave and DiskLoad routines. Those routines do a few other things, but they're mostly single-minded.

This program also has a routine (LoadMenu) for getting a disk directory. That was no freaking picnic, but I've used it for other stuff since, so it was worthwhile.

Hope these examples provide a decent start for you!
VIC-20 Projects: wAx Assembler, TRBo: Turtle RescueBot, Helix Colony, Sub Med, Trolley Problem, Dungeon of Dance, ZEPTOPOLIS, MIDI KERNAL, The Archivist, Ed for Prophet-5

WIP: MIDIcast BASIC extension

he/him/his
User avatar
srowe
Vic 20 Scientist
Posts: 1340
Joined: Mon Jun 16, 2014 3:19 pm

Re: Looking for some good disk I/O examples

Post by srowe »

The Geek on Skates wrote: Sat Jan 29, 2022 7:34 pm 2. Set a few more registers and JSR SETLFS (to set a few numbers I don't fully understand even in BASIC, but they work :D ). Seriously though I know the
I don't think these parameters were every clearly explained in any Commodore docs.
  1. logical file number (LFN) - this is the computer's numeric reference to this file operation. It is equivalent to a file descriptor returned by the C function open() (except of course the caller has to provide it)
  2. device - this is either an internal device (tape, screen, keyboard, RS-232) or an external device (disk drive, printer)
  3. secondary address - this is the device's reference to this file operation. It's purpose depends on the target device, for a disk drive it selects either the command channel (15) or a normal file channel
User avatar
ops
Vic 20 Dabbler
Posts: 88
Joined: Mon Feb 19, 2018 11:25 am
Location: Finland

Re: Looking for some good disk I/O examples

Post by ops »

User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Looking for some good disk I/O examples

Post by Mike »

What regards file I/O, the KERNAL jump table provides the same calls for both VIC-20 and C64, so there is no direct need to seek out for VIC-20 specific examples. You are more likely to find examples for the C64 online, by sheer numbers of C64 (still) in existence.

That being said, here is an example that does the equivalent of Unix cat on the VIC-20: viewtopic.php?t=9197&start=14 - as I mentioned in the docs, it also runs on a C64. The KERNAL jump table allows for a more or less a 1:1 translation of the original BASIC program.

Note redirected file I/O is half duplex only: you can have either input redirected (via CHKIN - change KERNAL input) or output (via CHKOUT, change KERNAL output) to a file, not both at the same time! If you want to copy/pipe between two files, it is preferable to employ a buffer, to avoid a CHKIN-CHKOUT-CLRCHN-fest. Here is a file copy program that does exactly this: viewtopic.php?t=5342&start=21. The auxiliary machine code is located at $1CC3, just take a monitor disassembler of your choice to inspect it. :wink:
The Geek on Skates
Vic 20 Drifter
Posts: 33
Joined: Fri Jul 12, 2019 6:11 am
Website: http://www.geekonskates.com
Occupation: Code Monkey

Re: Looking for some good disk I/O examples

Post by The Geek on Skates »

Wow, thanks a lot everyone! :)

There's a lot to go over but I can't wait to dive in! Interesting stuff, and potentially game-changing (cheesy pun not intended... at first :D). In all my games up till now, I've had to use a password system for loading/saving data (think Castlevania, only 2-4 characters lol). But if I could put my game on a D64 image, I might be able to do things like high scores, start screens like the original Legend of Zelda had, stuff like that. I'm a long way from there, and still have a ton of learning to do and a few more mistakes to make, but I went from having minimal info on the subject to a ton of examples and a better explanation of how it all works.
Post Reply