xtal: A New Compiled Language Designed Around an FPGA Expansion for the XL/XE

In mid-November 2022, while his FPGA expansion board for the XL/XE was away at the PCB fabricators, AtariAge member Spaced Cowboy started a development log for xtal (pronounced "crystal", for "eXTended Atari Language"). It is a new compiled language intended to program that board, but it produces code that also runs on an unexpanded Atari. The source is on GitHub, and the thread followed its progress step by step.

How it works

xtal is a tool chain with three parts:

  • xtal-c, a compiler that turns high-level source into an intermediate assembly language;
  • xtal-a, a macro assembler that expands that intermediate code into 6502 machine code;
  • xtal, a driver that runs both stages and produces a ready-to-run .com file.

The intermediate language is written as if the 6502 had a set of wide registers in zero page. An instruction such as muls.4 r2 r3 r4 means "signed 32-bit multiply". On a stock machine each of these expands into ordinary 6502 code. The first test expression, 2 + 3 * 5 - 8 / 3, compiled to ten intermediate instructions, which became 289 6502 instructions taking about 6,000 cycles, most of them in the 32-bit divide. The idea is that the FPGA board will provide the 32-bit arithmetic unit. The 6502 then only stores the operands and reads back the result a few cycles later, so the same program can speed up without changing the source. The assembler has a separate "emitter" for each hardware target to make that possible.

What the language can do

Over the following weeks and months, xtal gained:

  • typed variables (u8, s32, then the full set of signed and unsigned 8-, 16- and 32-bit integers), with automatic widening when types are mixed;
  • if/else, while and for. Blocks use square brackets because ATASCII has no { or };
  • functions with return values and, later, arguments, with registers saved properly across nested calls;
  • pointers (16-bit, with C-style scaled pointer arithmetic), fixed-size arrays and zero-terminated strings printed through CIO.

During development, the author tested the output in Atari800MacX, using the PCLink device to run each freshly built OUT.COM straight from his Mac's /tmp directory. He also reworked the register model from sixteen 4-byte registers into 64 one-byte registers allocated in groups, to prepare for a software stack for recursion and local variables.

Floating point, then a pause

A later update covered adding a 4-byte floating-point type. After trying Wozniak's routines, he typed in the floating-point package from the 1979 6502 Software Gourmet Guide and Cookbook. He also wrote a time-travel debugging tool to step through it, and found that a puzzling result was really his own misreading of the number format. Addition, subtraction, multiplication, division and conversion to and from text were all working. Later in the thread the author said the project was on hold while he dealt with a move abroad.

Why it matters

xtal is unusual in aiming at two kinds of machine at once: a compiler that runs on a stock Atari now and can use hardware acceleration where the board is fitted. Developers interested in typed, C-style languages for the Atari can follow it as a design study. Anyone building FPGA-assisted expansions may find the emitter approach useful as a model.

Credits