I’ve had a weekend of really good progress with my SD Card reader – the 6502 computer can now read directories and files, which means I’m definitely getting somewhere!
I ran into a few snags getting all the pieces to play nice together. In particular, I was stymied because the Arduino didn’t want to read one of the SD cards I wanted to use and I couldn’t figure out why. I eventually found a couple mentions online that the SD library doesn’t deal with some smaller SD cards correctly. I was using a 16 MB SD card that came with an old camera, and that is pretty small by modern SD card standards. Once I knew it was an actual problem and not something I was doing wrong, I felt okay about just moving on with a different card.
I’ve also found that this is a tricky combination of hardware to debug, for one particular reason: I’m using all the Arduino’s digital inputs, including D0 and D1, which are ordinarily used by the Arduino’s serial port. That means I can’t write debug information out the serial port like I’m used to. It also means I have to be very careful about the state of the whole system when I’m uploading a sketch to the Arduino. If the P:65 is trying to drive one of those serial pins at the same time that I’m trying to upload, I end up with a bunch of communications errors and I usually have to unplug the Arduino a couple of times before everything works correctly again.
All that said, I’m pretty comfortable now that I’m firmly in the software side of this project. I’ve done a lot of work on the Arduino side to set up my communications protocol and the brains to service it. I’ve been borrowing ideas from a few sources, such as the Commodore 64 Programmer’s Reference Guide and also some of my old textbooks like Operating System Design by Comer & Fossum and Modern Computer Architecture by Rafiquzzaman & Chandra, along with newer stuff like Linux Device Drivers by Corbet, Rubini, and Kroah-Hartman .
In particular, the interface the 6502 talks to supports multiple channels. Channel 0 is used for commands and responses, while the other channels are used for files. Commands are sent as ASCII strings; for example, “or:/p65/Makefile” means “open the file /p65/Makefile for reading”. The command channel responds with the channel number that’s been opened for the file (or -1 for an error, if I ever get around to adding error checking).
The Arduino-side software I wrote can handle having multiple files open at a time, with interleaved reads (and eventually writes), but it sounds like the actual SD card library for the Arduino might not be able to. I’m trying to decide if that’s a problem I want to get sidetracked into now, or put off until later.
In order to read/write each channel independently, I’ve created a number of IO classes such as “DirectoryReader” and “FileReader”. These classes keep track of which file(s) are open in a given channel and the position within the file. In the case of the DirectoryReader, we also have to format and buffer the data for each directory entry, so that the 6502 can read the information back one character at a time.
On the 6502 side, I’ve got the beginnings of a file descriptor table that should be able to control access to both the SD card and the computer’s serial port. The next big task is to integrate that code into my operating system, and then look at making the file and stdio libraries in CC65 work with it. I expect that’s going to be a very interesting problem.