Wednesday, August 5, 2009

Cool Commodore Hack

Although I enjoy all of the content on the PageTable blog, I like the archeology stuff the most. This post about the hack used to do directory listings on the Commodore is a great example.

Essentially, the Commodore's disk drive acted as a co-processor handling all of the mass storage management and file system operations. The Commodore's kernel and BASIC shell used a file oriented API (i.e. open, close, read, write) to communicate with the floppy drive. Commands like list-directory were performed by an open() call to a special address and file name and reading the results from the open "file" until an EOF was received.

For reasons that are unclear from the post, it seems that the Commodore's kernel and BASIC shell were unable to natively perform the list-directory operation. As a result, a hack was needed to avoid users from having to write a BASIC program every time they wanted to list a directory.

The solution was for the floppy drive to return the directory listing in the format of a BASIC program. Thus, a list-directory operation consisted of:

  • opening the special file
  • reading the returned fake program into memory as a program
  • listing the fake program to display the directory contents

What a clever hack!

If I understood the post correctly, the returned fake program simply used the structural description of a BASIC program but wasn't a valid program itself. In other words you couldn't execute the program returned by the list-directory operation. Too bad because it would have been really cool if the floppy drive returned a lambda function!