Introduction
A .prg file is what the Commodore 64’s SAVE command writes and
LOAD reads: two bytes giving the address to load at, and then the bytes that go
there. It holds BASIC programs, machine-code programs, and anything else a program is made of.
Most C64 games and demos distributed as single files are PRG files. Emulators such as VICE run
them directly.
The file has no signature, no length and no start address. A machine-code program is
normally started from a one-line BASIC program, a stub such as
10 SYS 2064, that sits at the start of BASIC memory and calls the machine code
that follows it. The explorer finds that SYS and takes its number as the program’s
entry point.
A PRG file dropped on any page opens in the explorer. It shows the load address, lists the
BASIC lines with their keywords written out, finds the SYS target, and disassembles
the machine code with an instruction decoded at every byte, laid out in columns so that a jump
into the middle of another instruction can be followed. Calls to the C64’s KERNAL and accesses
to the VIC-II, SID and CIA chips and to well-known zero-page locations are shown with their
names, and undocumented and unstable opcodes are highlighted (see the
instruction set). The processor can be changed in the
options bar above the viewer.
Because the file has no signature, the explorer recognises a PRG by its extension
alone: a file is shown this way only when its name ends in .prg. Any two or
more bytes could be a PRG file, so nothing in the content can confirm it.
Structure
0x0000Load address, little endian: where the first data byte goes in memory2 bytes0x0002BASIC program (only when the file loads at$0801): a chain of tokenised lines, ended by a zero linkvariable…Machine code and data: the rest of the file, started by the stub’s SYSto the end of the file
The data’s length is the file’s length minus two. A file whose data would run past
$FFFF is damaged or is not a C64 program; the explorer says so.
An example
These 20 bytes are a complete program: a BASIC stub, 10 SYS 2061, then three
instructions that turn the border black and return to BASIC. The first line’s link,
0x080B, is the address of the next line, here the end marker.
01 08load address$08012 bytes0B 08link to the next line:$080B2 bytes0A 00line number 102 bytes9E 32 30 36 31 00the tokenSYS, the digits2061and the end of the line6 bytes00 00link of zero: the end of the program2 bytesA9 00 8D 20 D0 60lda #$00,sta $d020,rtsat$080D(2061)6 bytes
01 08 0B 08 0A 00 9E 32 30 36 31 00 00 00 A9 00 8D 20 D0 60; the 2061 is the address of the A9The processor
The C64 has a 6510, a 6502 with a built-in I/O port. A PRG does not say which processor it was written for, so the processor is a choice in the explorer’s options bar: the 6510 (the default), the NMOS 6502, which decodes the same, and the 65C02. See the 6502 instruction set for every instruction.
Not described
Known, not yet covered
- Reference and viewer: PRG files of other Commodore machines. The VIC-20, the
PET, the Plus/4 and the C128 use the same file shape with different load addresses
(
$1001or$1201,$0401,$1001,$1C01), and the C128’s BASIC 7 has more tokens. The viewer shows them as C64 files: the BASIC is not listed, and the code is labelled with the C64’s addresses. - Viewer: a
SYSwhose argument is an expression (SYS 4096+3) or a variable. Only a plain number is read. - Viewer: which bytes are code and which are data. The disassembly decodes every byte; code that unpacks or moves itself is not followed.
- Reference: the PC64
.p00wrapper, which puts a header with the file name in front of a PRG; it is a different file format.
Links
Software
- VICE – Commodore emulator that runs PRG files and has a monitor (Windows, macOS, Linux)
- cc65 – C compiler, assembler and linker for 6502 systems, with a C64 target that writes PRG files (Windows, macOS, Linux)
- 64tass – assembler for the 6502 family that writes PRG files (Windows, macOS, Linux)
- Ghidra – reverse engineering suite with a 6502 processor module (Windows, macOS, Linux)
Reference
- PRG – the C64-Wiki’s article on the file type
- BASIC token – the C64-Wiki’s table of the BASIC V2 tokens