R65's Inception: The Drivers Behind the Design
R65 looks like Rust, but most of its design decisions were made by the 65816. This first post covers the handful of pressures that shaped the language: what each one is, and which features it explains. Once you know them, most of the language's choices are predictable.
Driver 1: the hardware sets the budget
A SNES game has about 60,000 CPU cycles per frame and, on a typical 4 Mbit cart, 512 KB of ROM. Every language feature uses some of that budget, so every feature has to earn its place in cycles or bytes.
This driver explains most of what R65 leaves out:
- No generics. Monomorphization makes a copy of a function body for each type it is used with, and every copy costs ROM.
- No
OptionorResult. EachOption<u8>costs an extra byte and a branch. Return codes and sentinel values cost nothing extra. - No bounds checking. A check costs 8–12 cycles per array access, on a CPU that doesn't have many cycles to spare.
- No heap, closures or dynamic collections. The machine has no allocator, and building one would cost more than it gives back.
It also explains what R65 keeps. Structs, enums, match, const fn and structured loops all compile down to the code you would write by hand. If a feature can't be made that cheap, it isn't in the language.
Driver 2: low-level hardware when you need it
A C compiler for a 6502-family CPU hides the registers behind an ABI and moves values through memory. That's expensive on a machine with three general-purpose registers, where getting A, X and Y right decides how fast a routine runs.
R65 lets you write most code at a high level and drop down to the hardware where it counts, without leaving the language. A, X, Y, STATUS, D, DBR and S are global variables, and a function says exactly where each parameter arrives: in a register, or in a variable of your choosing.
fn place(index @ X: u16, value @ A: u8) { // arrives in X and A
ORDER[index] = value; // the whole body is STA $0200,X
}
#[zeropage(0x30)]
static mut TEMP: u8;
fn store(value @ TEMP: u8) { // the caller stores straight into TEMP
FLAGS = value;
}
Binding a parameter to a variable passes it with no stack frame at all: the caller writes the variable and the function reads it. That suits a direct-page variable when A, X and Y are already in use, or a hand-written routine that expects its input at a fixed address.
Some statements are single instructions spelled as code. Assigning one register to another is a transfer, the rotate methods are the CPU's rotate instructions, and the flags in STATUS can be set and branched on directly:
X = A; // TAX
Y = X; // TXY
STATUS.Carry = false; // CLC
A = A.rotate_right(1); // ROR, rotating through the carry flag
if STATUS.Carry { // BCC over the body: one branch on the
HITS++; // bit the rotate shifted out
}
The compiler doesn't hide register choices from you, and it doesn't make them for you either. Instead it checks them: if code overwrites the register behind an alias before the alias's last use, that's a compile error.
Driver 3: spend the type system on the bugs this CPU actually causes
Rust's borrow checker protects memory that is allocated and freed while the program runs. A SNES program doesn't have any. Its pointers point at fixed RAM, ROM and hardware registers. The bugs that actually cost 65816 programmers time are different:
- forgetting a
SEP #$20after a 16-bit operation, so the accumulator is the wrong width for the rest of the routine - a near
JSRto a function in a different bank - a table that grows past the end of its bank
- a value silently truncated or sign-extended
R65's type system targets those bugs. The processor mode is inferred from the function signature, so the compiler can insert REP/SEP itself:
fn tick(value @ A: u8) { } // m8: A is 8-bit (the default)
fn add_score(points @ A: u16) { } // m16: inferred from the u16 in A
Near and far are part of the types of functions and pointers, so a call that crosses a bank boundary has to use far. Every narrowing or reinterpreting conversion has to be written with as, so you can see every truncation in the source.
Driver 4: if it's expensive, it should look expensive
The 65816 has no multiply or divide instruction. A language that lets you write a * b hides a routine of tens to hundreds of cycles behind one character. So R65 only accepts * and / with power-of-2 constants, which compile to a few shifts. Anything else is a named call:
let half = value / 2; // one shift
let product = mul8(a, b); // a call, and it looks like one
The same rule decides where data lives. #[zeropage], #[lowram], #[ram] and #[hw] are written on the declaration, because a direct-page access and a long-addressed access cost different amounts. An immutable static goes in ROM without any attribute, because that's where it has to be anyway.
Driver 5: the hardware decides the data layout
Indexing an array of structs means multiplying the index by the struct's size, and on this CPU that multiply costs real time for any element larger than a few bytes. #[soa] stores every field of a struct as its own array, so POOL[i].hp becomes a single indexed load and the multiply disappears. Tagged enums are stored the same way: a plane of tags and a plane of payloads, so reading a tag is one LDA POOL__tag,X.
These layouts follow from the 65816's indexed addressing modes. In R65, data layout is a performance feature you choose explicitly, not something the compiler hides from you.
Driver 6: assembly is a partner, not an escape hatch
R65 doesn't try to replace assembly. asm!() puts instructions directly into a function, include_asm!() pulls in a hand-written .s file, and extern declarations give its symbols R65 signatures so the type checker can check calls into them. The standard library uses all three for DMA, the hardware multiplier and random numbers.
And why Rust syntax?
It's familiar, it's readable, and it already has good forms for what R65 needs: struct, enum, match, impl, const fn, macro_rules!. R65 borrows Rust's syntax, not its semantics. If you know Rust you can read R65 code, but the rules underneath come from the 65816.
Later posts will go into these drivers one feature at a time, starting with #[soa] and with testing ROMs from Python using Mesen-GDB. Until then, the FAQ explains each omission, and the playground lets you try R65 in the browser and see the assembly it produces.