# snes.asm — 65C816 assembler
A two-pass 65C816 assembler in Luau, for Super Nintendo homebrew built inside the engine (also fine for any 65816 / 6502-family target).
```lua
local Asm = require("snes.asm")
local bin, symbols = Asm.assemble(source, { map = "lorom", size = 0x40000, fill = 0xFF, blobs = { tiles = "..." } })
```
- Addresses are 24-bit. `org $808000` sets the full address; the file offset comes from the map: `lorom` = (bank & $7F) * $8000 + (addr & $7FFF), running past $FFFF continues at the next bank's $8000; `hirom` = addr & $3FFFFF; `flat` = addr - org of the first `org`.
- Immediate widths: `.a8 .a16 .i8 .i16` (also `.smart`-style tracking: `rep #$20/$10/$30` and `sep` with a constant switch the widths for the lines after them), or a `.b`/`.w` suffix on the mnemonic.
- Operand sizes are decided in pass 1 so the layout never moves: `<expr` direct page, `!expr` or `|expr` absolute, `>expr` long; a mnemonic suffix `.b .w .l` does the same. Without a prefix: a value known in pass 1 that is <= $FF is direct page, a plain number literal > $FFFF is long, everything else is absolute (labels are absolute: their bank comes from DBR/PBR). `jsl`/`jml` are always long.
- Every 65816 addressing mode: `#`, dp, dp,x, dp,y, (dp), (dp,x), (dp),y, [dp], [dp],y, abs, abs,x, abs,y, long, long,x, sr,s, (sr,s),y, (abs) / (abs,x) for jmp/jsr, [abs] for jml, rel8, rel16 (`brl`, `per`), `mvn src,dst` / `mvp src,dst` (bank numbers or `#bank`, written in WLA/ca65 source order: source first).
- Directives: `org`, `db/byte`, `dw/word`, `dl/long` (24-bit), `ds n[,fill]`, `align n`, `assert expr`, `incbin name` (bytes from `opts.blobs[name]`), `name equ expr` / `name = expr`.
- Expressions: `$FF`, `0xFF`, `%0101`, `'c'`, `*` (here), `+ - * / % << >> & | ^ ~ ( )`, `lo() hi() bank()` and `^label` (bank byte).
- Labels `name:`, local `.x` scoped to the last global label. Errors name the line.
# snes.spcasm — SPC700 assembler
A two-pass SPC700 assembler in Luau for Super Nintendo sound programs (the APU's IPL boot ROM and the homebrew sound driver are built with it).
```lua
local SpcAsm = require("snes.spcasm")
local bytes, symbols, base = SpcAsm.assemble(source, { org = 0x0200, blobs = { samples = "..." } })
```
- Sony-style syntax: `mov a,#$12`, `mov $12,a`, `mov !$1234,a`, `mov a,(x)`, `mov a,(x)+`, `mov a,[$12+x]`, `mov a,[$12]+y`, `mov a,$12+x`, `mov a,!table+y`, `mov $12,#$34`, `mov $12,$34` (dest, source), `movw ya,$12`, `set1 $12.3`, `bbs $12.3,label`, `cbne $12,label`, `dbnz y,label`, `mov1 c,$1234.5`, `or1 c,/$1234.5`, `tcall 3`, `pcall $80`, `call label`, `jmp [!table+x]`, `push psw`, `mul ya`, `div ya,x`, every opcode of the set.
- Operand sizes are decided in pass 1: `!expr` is absolute; otherwise a value known in pass 1 and <= $FF is direct page and anything else absolute.
- Directives: `org`, `db/byte`, `dw/word`, `ds n[,fill]`, `align n`, `assert expr`, `incbin name` (bytes from `opts.blobs[name]`), `name equ expr` / `name = expr`; labels `name:`, local `.x`. Expressions as in `snes.asm` (shares its evaluator).
- Returns the bytes from the lowest to the highest address written, the symbol table and the lowest address.
# snes — Super Nintendo core
A Super Nintendo / Super Famicom (NTSC) emulator written in Luau for the hangout room's console TVs, with in-engine tools to write SNES software and two original CC0 games.
Modules (require paths):
- `snes.cpu65816` — reusable WDC 65C816 CPU (native/emulation, 8/16-bit M/X, every addressing mode, decimal, block moves, WAI/STP, NMI/IRQ, master-clock timing by memory region and MEMSEL).
- `snes.spc700` — reusable Sony SPC700 CPU.
- `snes.machine` — the console: LoROM/HiROM carts (copier header, zip, SRAM), WRAM + port, multiplier/divider, auto/manual joypads, NMI and H/V timer IRQs, 8-channel DMA (all modes) and HDMA (direct/indirect), the line-based S-PPU (modes 0-7, mode 7 + EXTBG, windows, colour math, OBJ limits, mosaic, offset-per-tile, hi-res 512), the APU (SPC700 + S-DSP: BRR, gaussian, ADSR/GAIN, pitch mod, noise, echo/FIR) with an IPL boot ROM written here from the documented protocol, save states.
- `snes.snes_core` (emulatorCore) — the TV core: 512x224, worker thread, 44.1 kHz audio, battery saves, pads.
- `snes.asm` — two-pass 65C816 assembler (LoROM/HiROM/flat layout, width tracking).
- `snes.spcasm` — two-pass SPC700 assembler.
- `snes.homebrew` — 65816 runtime, SPC700 sound driver + synthesised BRR instruments + music compiler, and the CC0 games **Horizon Kart** (mode 7, 1P, battery save) and **Puck Rush** (2P air hockey: windows, colour math, HDMA spotlight). `H.build(true)` writes them to `/zero/source/snes_roms/`.
Licence of the games, runtime, driver, instruments and music: CC0 1.0 (public domain), written for this world. No commercial ROMs are included; players upload their own `.sfc/.smc/.zip`.
Tests: `tests/cpu_tests.luau`, `tests/ppu_tests.luau`, `tests/apu_tests.luau`, `tests/snes_tests.luau` (run each with `execute { file = ... }`).