Commodore 1541 DOS Commands

Commodore LogoThe 1541 Floppy Drive was equipped with its own MOS 6502 chip and was therefore capable of understanding and executing its own commands (to format the disk, copy files, erase files, etc). The idea was that it could operate autonomously without the attached C64 to get involved – revolutionary at the time. They were called CBM DOS commands – indeed as in Disk Operating System, not to be confused with the later MS-DOS or DR-DOS.

Those DOS commands were not part of BASIC and had to be sent to the drive with a dedicated channel. In principle you needed to

  • open a channel (with OPEN 15,8,15)
  • issue the command (for example, PRINT#15,”command”)
  • and close the channel (CLOSE 15)

This worked in direct mode as well as from within programs too. Note that opening a channel followed by a BASIC disk operation involving the drive (such as LOADing or SAVEing a file) would automatically close the channel.

If you’re wondering about the significance of 15: it’s the 1541′s command channel. Channel 0 and 1 are for the operating system to transfer files, 2-14 can be used for your own operations, and 15 is good for what we’re doing now.

Here are some of the more useful commands. We’re assuming your drive number is 8 (change where appropriate):

Formatting a Floppy Disk

Nothing came pre-formatted in those days – everything was blank. The NEW command would be your best friend, taking about 10 minutes or so to give you a whopping 170KB of precious space per side. This was known as a “hard format”:

OPEN 15,8,15
PRINT#15,"NEW0:disktitle,id"
CLOSE 15

You can also abbreviate NEW with N. The presence of the 0: after the command was used to indicate which drive you were using in dual-drive setups, meaning 0 for the left and 1 for the right drive. The 1541 was a single drive and hence will only respond as 0.

Another way to format a floppy was a “soft format”, or “quick format” which would not re-write every sector on the disk. Instead, it would just erase the BAM and clear out the directory, and make every block (sector) on the drive available to be overwritten. Saves a lot of time. This was done with the INITIALIZE command (or I):

OPEN 15,8,15
PRINT#15,"INITIALIZE0:newdisk,id"
CLOSE 15

We still use the same principle with modern hard drives today.

Duplicating Files

You can duplicate THISFILE into THATFILE on the same floppy and generate your own safety copies.

OPEN 15,8,15
PRINT#15,"COPY0:thisfile=thatfile"
CLSOE 15

Can be abbreviated with C. Note that you cannot use this command to copy from one drive to the other (like drive 8 to drive 9). You could even copy up to 4 files together, presumably concatenating them – but it never worked for me.

Renaming Files

OPEN 15,8,15
PRINT#15,"RENAME0:thisfile=thatfile"
CLOSE 15

Can be abbreviated with R.

Deleting Files

The SCRATCH command will do this. Can be abbreviated with S:

OPEN 15,8,15
PRINT#15,"SCRATCH0:thisfile"
CLOSE 15

Validating your Floppy Disk

Before there was our new favourite “Device hasn’t been removed properly” message, things could still go wrong with floppies over time: write operations were started but never finished, channels were opened and prematurely closed, and so forth. If you’ve used a floppy over time for saving, renaming and deleting a lot of files, it may need a cleanup.

VALIDATE to the rescue. This command will go through existing files, defragment your sectors, update the BAM and speedup disk access to your precious 170KB of space. Can be abbreviated with V:

OPEN 15,8,15
PRINT#15,"VALIDATE"
CLOSE 15

This will however ruin your data forever if you’ve been using your drive it with direct access (i.e. without LOAD or SAVE commands – we shall speak about this in another article).

Reading the Error Channel

One final thing that we did was to diagnose error messages that the 1541 was reporting – in the rarest of circumstances of course. Usually you knew what happened (i.e. no disk in drive, bad floppy, etc), but sometimes it wasn’t obvious – or you wanted to give users an explanation.

Much like we can issue a command with PRINT#, we can also read what the drive is telling us with INPUT# – much like we can read keyboard input from a user into a variable.

INPUT# does not work in direct mode, only in BASIC programs. Here’s how:

10 open 15,8,15
20 input# 15,a,b$,c,d
30 print "error number: ";a
40 print "error name: ";b$
50 print "on track: ";c
60 print "sector: ";d
70 close 15

// run it with 
run

// if all is well will show something like
error number:  0
error name: ok
on track:  0
sector:  0

This little programme will remove the blinking red light from the drive and show you what’s wrong. Only the second variable is a string, but you can read the others in as strings too if you like.

Will report the CBM DOS version when the drive has just been switched on. Should the drive not be available (i.e. turned off) the programme will hang until you hit RUN STOP/RESTORE.

You can leave a comment on my original post.