Cart ROM and EEPROM

The Atari Lynx cartridge ROM — the .lnx binary format, the cart image layout, and how a program reads data back from the cart at run time — together with the cartridge EEPROM used for saved data.

1. Binary format

The standard binary output format generated by the linker for the Lynx target is a cart image. By specifying the config file lynx-bll.cfg the linker will generate BLL download compatible binary files.

It is of course possible to change this behaviour by using a modified startup file and linker config.

The bootloader used in the cc65 lynx library uses a very minimal bootloader that does not check the cart or show a title screen.

The advantage of this bootloader is that it allows creation of cart images to many common formats.

The physical ROM size is __BANK0BLOCKSIZE__ × 256 (there are 256 blocks per bank), which gives the three standard cart sizes:

Block sizeROM sizeConfigTypical use
512 bytes128 KBlynx-128k.cfgOld commercial games (e.g. Warbirds)
1024 bytes256 KBlynx-256k.cfgMost homebrew; newer games (Lemmings)
2048 bytes512 KBlynx-512k.cfgLargest games (EOTB)

The default lynx.cfg declares a 1024-byte block size but writes only the bytes the program uses, so its image is as small as the program. That is fine for emulators, which read the block size from the header and derive addressing from it, but a real EPROM or repro cart wants a file that is exactly the size of the part. The three configs above produce that padded image directly at link time.

1.1. Generating fixed-size cart images

A linker config (the file passed to cl65/ld65 with -C) describes the target's memory in a MEMORY block and maps the program's SEGMENTS into those areas. Areas that share the output file — written as %O — are emitted in the order they are declared, and any area may be padded up to its full size with the fill = yes attribute (the pad byte is fillval, default $00). See ld65 §5.6 for the authoritative fill/fillval reference.

The lynxcc size configs use exactly this. Each is lynx.cfg with the matching __BANK0BLOCKSIZE__, a __CARTSIZE__ symbol, and a trailing ROMFILL memory area whose size is __CARTSIZE__ minus the bytes already emitted, marked fill = yes, fillval = $42. The boot loader, directory and MAIN areas are also filled so that everything ahead of ROMFILL is a fixed length; the result is that the blocks region (boot + directory + MAIN + pad) is always exactly __BANK0BLOCKSIZE__ × 256, regardless of program size. The 64-byte EXEHDR is emulator metadata and sits ahead of that region, so the finished .lnx file is 64 + block × 256 bytes.

The $42 pad marks the region that is pure ROM padding — it is never copied into RAM (the directory entry bounds what the loader reads) and never executed, so its value is cosmetic and only makes the padding easy to spot in a hex dump; the never-loaded tail of MAIN keeps the conventional $00. Change fillval in the config if a different sentinel is preferred.

Padding does not buy more code space: usable code, data and heap are still bounded by the RAM window below the screen buffers (~47 KB with the default stack — see Memory §1). The extra ROM is space for assets streamed at run time through the numbered file API in section 3. The per-game header fields (cart name, manufacturer, rotation, AUDIN, EEPROM) can be rewritten after linking with the lnx tool, which also builds the equivalent zero-padded BLL cart images.

2. Cart access

The cart is read-only ROM, and the files on it are addressed by number rather than by name. Each file is described by an 8-byte directory entry: entry 0 is the boot executable that the loader runs at start-up, entry 1 is the next file appended after it, and so on. There is no hierarchical filesystem and there are no file names — the entry number is the whole “path”.

Because of that the runtime does not provide the POSIX open()/fopen() layer or any write() / creat() path: there is nothing to parse a name into and nothing to write to. The supported, numbered, read-only API is:

void __fastcall__ openn (int fileno)
Locate directory entry fileno and position the cart at its first byte, ready for read()/lseek(). Declared in <lynx/lynx.h>. This primitive replaced the former open(const char *name, …) wrapper: because files are just numbers, that wrapper did nothing but run a string such as "3" back through atoi() to recover the integer 3 — pure overhead now that the entry number is passed directly.
int __fastcall__ read (int fd, void *buf, unsigned count)
Copy count bytes from the current cart position into buf. The fd is ignored; reading past the end of a file simply continues into the bytes of the next one.
off_t __fastcall__ lseek (int fd, off_t offset, int whence)
Re-position within the cart. Only SEEK_SET is implemented.
void __fastcall__ lynx_load (int fileno)
Open a file and read it straight to the RAM address recorded in its directory entry.
void __fastcall__ lynx_exec (int fileno)
Like lynx_load(), then jump to the loaded code — handy for “compilation” carts that chain several small programs.

Saving data. Since the cart itself cannot be written, persistent state (high scores, options) is kept in the cart's serial EEPROM. See the size-selectable eeprom_93c46_read/_93c66_read/_93c86_read and eeprom_93cNN_write family in <lynx/lynx.h>.

Bank 0 only. The block reader drives the cart through the CART0 read strobe, so only bank 0 is accessible. The EXEHDR header still records a __BANK1BLOCKSIZE__ and the config reserves the symbol, but the runtime has no bank 1 reader yet; a second bank would need an additional strobe path before its files could be opened.

Layout of the cart image. Every cc65 cart carries at least a one-entry directory so the boot loader can find the program. The linker writes four regions in order: the EXEHDR header (a 64-byte LYNX block holding the block sizes, cart name and manufacturer — metadata for emulators such as Handy and Mednafen, stripped from a physical ROM), the BOOTLDR boot loader, the DIRECTORY, and finally the program in MAIN. The directory begins at __STARTOFDIRECTORY__ ($00CB) and the cart is read in blocks of __BANK0BLOCKSIZE__ ($0400 = 1 KB by default; see the cart sizes in section 1).

Default cart image — one file, one directory entry EXEHDR BOOTLDR DIRECTORY MAIN LYNX header 64 B · emulator metadata Boot loader ≈ 203 B · runs at $0200 Directory — 1 entry (8 B) File 0 — your program STARTUP · CODE · RODATA · DATA · BSS copied to $0200 and run stripped from a physical ROM __STARTOFDIRECTORY__ = $00CB read in 1 KB blocks
The image cc65 produces today: header, boot loader, a one-entry directory and the single executable. Heights are not to scale.

A cart that ships extra data — graphics, levels, music — simply appends those files after the executable and gives the directory one entry per file. Each entry records where its file starts (block number plus offset) and how big it is, so openn() can seek straight to it. Only file 0 (the program) corresponds to a cc65 MAIN segment; the rest are raw blobs appended at link time.

Cart image with several files — one directory entry each EXEHDR BOOTLDR DIRECTORY MAIN LYNX header Boot loader entry 0 entry 1 entry 2 File 0 — program boot executable File 1 — graphics File 2 — music each entry locates one file's bytes
With several files the directory gains one entry per file; each entry points at the file's start block and offset further down the cart.

The 8-byte directory entry mirrors the in-RAM struct dirent for the Lynx in <dirent.h> (exposed as the zero-page FileEntry), so reading 8 bytes from the directory fills exactly the fields the loader needs:

byte 0 bytes 1–2 byte 3 bytes 4–5 bytes 6–7 start block block offset flag load address length 1 B 2 B 1 B 2 B 2 B × 1 KB block within the block $88 for entry 0 RAM destination bytes to read
The on-cart directory entry, matching struct dirent (d_blocks, d_offset, d_type, d_address, d_size).

A richer, named or hierarchical filesystem is intentionally not provided: the cart is a fixed ROM laid down at link time, so numbered entries already cover everything the format needs and avoid the code cost of name tables, directory enumeration and a write path.

3. Reading data from the cart

The cart is read-only ROM whose files are addressed by number rather than by name, and reading them is again a static library — no driver to install. The simplest path is the whole-file helpers in lynx/lynx.h: lynx_load(fileno) copies the entire numbered file into the RAM address recorded in its directory entry, and lynx_exec(fileno) does the same then jumps to that address (used by “compilation” carts that chain several small programs). For streaming or partial reads the lower-level block reader — openn to position the cart at a numbered file, read to copy bytes from the current position, and lseek (SEEK_SET only) to re-position — lets a program pull graphics, levels or music off the cart on demand. Only bank 0 is reachable; section 2 covers the cart image layout and the directory format in full.

4. EEPROM (cart save data)

Because the cart ROM cannot be written at run time, persistent state — high scores, options — is kept in the serial EEPROM chip fitted to the cart. Access is a static library of word-wide read/write calls; each EEPROM “cell” holds one 16-bit word. The calls come as a size-selectable family — pick the variant matching the chip on the cart: eeprom_93c46_read / eeprom_93c46_write for the 93C46 (64 words, cell 0–63), eeprom_93c66_* for the 93C66 (256 words, 0–255) and eeprom_93c86_* for the 93C86 (1024 words, 0–1023). A read returns the word at the given cell; a write stores a word there. There is no separate erase call — to clear a word, just write the value you want. All six are __fastcall__ functions declared in lynx/lynx.h and so need a prototype in scope; see the function reference. The former lynx_ee* names remain as deprecated aliases (define LYNX_NO_EEPROM_COMPAT to disable them); see the migration guide.