SDCC's New 6502 Back End Builds Atari Programs: tebe Shows How

In July 2023, tebe, author of Mad Pascal and the MADS assembler, posted a short recipe on AtariAge for compiling C programs for the Atari with SDCC, the Small Device C Compiler. SDCC is a long-established open-source C compiler best known for 8051, Z80-family and STM8 targets. It now also supports the MOS 6502 (and WDC 65C02), which gives Atari programmers another C compiler alongside cc65, vbcc, KickC and llvm-mos.

The recipe

SDCC doesn't know anything about Atari executables, so tebe's method works in three steps.

1. Compile for the 6502. Put the code at $2000 and start the compiler's data area in zero page at $80:

sdcc -mmos6502 --code-loc 0x2000 --data-loc 0x0080 test.c

His test program is the classic one-liner effect, copying the scan-line counter into the background colour register:

#define color  ((char *)0xd01a)
#define vcount ((char *)0xd40b)

void main(void)
{
    while (1) { *color = *vcount; }
}

2. Convert the output. SDCC writes an Intel HEX file (test.ihx). A separate hex2bin utility, which compiles easily with GCC on Windows, turns it into a raw binary.

3. Wrap it as an XEX. A tiny MADS source file loads the binary at $2000 and sets the run address:

 org $2000
 ins 'test.bin',0,$100
 run $200a

Starting at $200A skips part of SDCC's start-up code, which would otherwise crash the Atari.

Caveats

This is a proof of concept, not a finished tool chain. The raw binary contains a lot of redundant data, and you have to look at it yourself to work out how long the real code is before you set the length in the ins line. The recipe also uses no Atari-specific library: hardware is reached through plain pointers to the chip registers. The thread also links a comparison of 6502 C compilers on gglabs.us for anyone weighing SDCC against the alternatives.

Getting it

SDCC is available from sdcc.sourceforge.net. Tebe attached his example files to the AtariAge thread as sdcc_example.zip.

Credits