Producing Commodore 64 programs

Commodore 64 programs are stored in the PRG format on disk. Some emulators (such as CCS64 or VICE) can run PRG programs directly; others need them to be transferred to a D64 image first.

The PRG format is ludicrously simple. It has two bytes of header data: This is a little-endian number indicating the starting address. The rest of the file is a single continuous chunk of data loaded into memory, starting at that address. BASIC memory starts at memory location 2048, and that's probably where we'll want to start.

Well, not quite. We want our program to be callable from BASIC, so we should have a BASIC program at the start. We guess the size of a simple one line BASIC program to be about 16 bytes. Thus, we start our program at memory location 2064 ($0810), and the BASIC program looks like this:

10 SYS 2064
    

We SAVE this program to a file, then study it in a debugger. It's 15 bytes long:

1070:0100  01 08 0C 08 0A 00 9E 20-32 30 36 34 00 00 00
    

The first two bytes are the memory location: $0801. The rest of the data breaks down as follows:

Table 1. BASIC program breakdown

Memory LocationsValue
$0801-$08022-byte pointer to the next line of BASIC code ($080C).
$0803-$08042-byte line number ($000A = 10).
$0805Byte code for the SYS command.
$0806-$080AThe rest of the line, which is just the string " 2064".
$080BNull byte, terminating the line.
$080C-$080D2-byte pointer to the next line of BASIC code ($0000 = end of program).

That's 13 bytes. We started at 2049, so we need 2 more bytes of filler to make our code actually start at location 2064. These 17 bytes will give us the file format and the BASIC code we need to have our machine language program run.

These are just bytes—indistinguishable from any other sort of data. In Ophis, bytes of data are specified with the .byte command. We'll also have to tell Ophis what the program counter should be, so that it knows what values to assign to our labels. The .org (origin) command tells Ophis this. Thus, the Ophis code for our header and linking info is:

.byte $01, $08, $0C, $08, $0A, $00, $9E, $20
.byte $32, $30, $36, $34, $00, $00, $00, $00
.byte $00, $00
.org $0810
    

This gets the job done, but it's completely incomprehensible, and it only uses two directives—not very good for a tutorial. Here's a more complicated, but much clearer, way of saying the same thing.

.word $0801
.org  $0801

        .word next, 10       ; Next line and current line number
        .byte $9e," 2064",0  ; SYS 2064
next:   .word 0              ; End of program

.advance 2064
    

This code has many advantages over the first.