# mastersystem.asm
A two-pass Z80 assembler (Zilog syntax) written in Luau, used to build the
Master System package's homebrew carts inside the engine; any Z80 machine's
homebrew (SG-1000, MSX, ColecoVision, Spectrum) can use it too. Labels and
`.local` labels, `equ`/`=`, `org`, `db/dw/ds`, `align`, `assert`, expressions
(`$` = here, `lo()`/`hi()`), and the full documented instruction set with
IX/IY, CB and ED groups (see the header of `init.luau`).
```lua
local Asm = require("mastersystem.asm")
local bin, symbols = Asm.assemble(sourceText, { org = 0, size = 0x8000, fill = 0xFF })
```
# gamegear (emulatorCore)
Sega Game Gear console spec for the room's console TVs: the Master System
machine (`mastersystem.machine`) in Game Gear mode — 160×144 window, 12-bit
palette, Start button on port 0, stereo SN76489 — at 59.92 Hz, 44.1 kHz stereo
sound, battery RAM saved under `gamegear_saves/`, save states.
- Carts: `.gg` raw or a `.zip` holding one.
- `tv = true`; `padBar` names D-pad, 1, 2, Start. Pad 1 only (the Game Gear is a handheld).
- Runs on the station's worker thread (`workerModule`), drawing only the frame shown.
- Built-in carts: CC0 homebrew in `/zero/source/gamegear_roms/`.
# mastersystem.homebrew
The Master System package's own homebrew carts, written here in Z80 assembly
and assembled in the engine with `mastersystem.asm`: a shared runtime (boot,
VDP setup, vblank wait, pads + Pause, sprite table, text, BCD scores, PSG
sound effects and a two-voice music player, RNG, battery RAM, line-interrupt
hook), a 5x7 font and tile helpers that turn pixel art written as strings into
SMS planar tiles, the SDSC + TMR SEGA headers, and one module per game. All of
it, and the ROMs it builds, is original work released under **CC0 1.0**
(public domain): copy, change and redistribute freely.
```lua
local H = require("mastersystem.homebrew")
H.build() -- assembles every game into /zero/source/mastersystem_roms/
```
Each game advances the byte at RAM `$C000` once per logic step (the
logic-rate probe the speed checks read).
# mastersystem.machine
The Sega Master System / Game Gear / SG-1000 machine (NTSC) as Luau: Z80
(`neogeo.machine.z80`), the SMS VDP (`vdp.luau`), SN76489 (`megadrive.psg`),
cart loader and mappers (`cart.luau`), I/O and the line scheduler (`init.luau`).
```lua
local Sms = require("mastersystem.machine")
local m = Sms.new(cartBytes, { gg = false }) -- .sms/.gg/.sg or a zip of one
m:runFrame(true) -- one 59.92 Hz frame (262 lines x 228 T-states)
m.vdp.fb -- 256*192*4 RGBA8 (160*144 on a Game Gear)
m:setPad(p1, p2) -- station pad words: d-pad, A = button 1, B = button 2, Start = Pause / GG Start
m:drainAudio() -- (buffer, frames) 44.1 kHz stereo f32
m:saveRam() / m:loadRam(s) -- cartridge battery RAM
m:snapshot() / m:restore(s) -- save states
m:watchRam(addr) -- count a game's own logic-step counter
```
# mastersystem (emulatorCore)
Sega Master System (and SG-1000) console spec for the room's console TVs and
any RetroConsole cabinet: 256×192 at 59.92 Hz, the SN76489 as 44.1 kHz stereo
PCM, cartridge battery RAM saved under `mastersystem_saves/`, and
whole-machine save states. The machine is `mastersystem.machine`.
- Carts: `.sms`, `.sg` (raw, copier header dropped) or a `.zip` holding one.
- `tv = true`: a room TV offers it by itself; `padBar` names its buttons (D-pad, 1, 2, Pause).
- Runs on the station's worker thread (`workerModule`), drawing only the frame shown (`drawControl`).
- `setPad(m, p1, p2)`: d-pad, A → button 1, B → button 2, Start → Pause (the Z80 NMI).
- The shelf's built-in carts are the package's CC0 homebrew (`mastersystem.homebrew`) in `/zero/source/mastersystem_roms/`.
- `helpers` (`make`, `keyboard`, `ram`) is shared with the Game Gear spec.
# megadrive.psg
The SN76489 PSG as found in Sega machines (Mega Drive, Master System, Game
Gear): three square tones, Sega 16-bit noise LFSR, 2 dB attenuation steps,
the latch/data protocol and the Game Gear stereo register. Use it embedded
(`p:fill(n, rate)` fills `p.mixL/mixR` for another chip's mix, as the Mega
Drive's YM2612 does) or standalone (`p:advance(clocks)` then `p:drain()` for
44.1 kHz stereo f32, for a Master System core).
```lua
local Psg = require("megadrive.psg")
local p = Psg.new(3579545)
p:write(0x8F); p:write(0x1A); p:write(0x90) -- tone 1 period 0x1AF, full volume
p:advance(59736); local buf, frames = p:drain()
```
# ng — Neo Geo machine core
The Neo Geo MVS/AES as Luau, one file per chip. `neogeo_core.emulatorCore`
calls it; anything else that wants a Neo Geo can too.
```lua
local Ng = require("neogeo.machine")
local m = Ng.new(gameZipBytes, { bios = neogeoZipBytes })
m:runFrame() -- one 59.185 Hz frame: 264 lines, 68000 + Z80 + timers
m.lspc.fb -- 320*224*4 RGBA8, row-major
m:setPad(p1, p2) -- 10-bit words: right left up down A B C D START SELECT(=coin on MVS)
m:drainAudio() -- YM2610 output since the last call: (buf, frames), 44.1 kHz stereo f32
m:saveRam() / m:loadRam(s) -- MVS backup RAM (64 KB)
local s = m:snapshot() -- whole-machine save state; m:restore(s)
```
| file | what |
|---|---|
| `zip.luau` | zip central directory, DEFLATE (stored/fixed/dynamic), gzip, CRC-32 |
| `romset.luau` | MAME file roles by name (`*.p1 *.s1 *.cN *.m1 *.vN`, `v1x/v2x`), BIOS by priority, byte-order and half-swap detection for P ROMs, C-ROM pair interleave |
| `m68k.luau` | 68000: 65536-entry decoded closure table, full ISA, exceptions, autovectored IRQs, manual timings |
| `z80.luau` | Z80: full ISA with undocumented flags/opcodes, IM 0/1/2, NMI, HALT |
| `lspc.luau` | video: 381 sprites, sticky chains, H/V shrink (L0 ROM or derived), auto-animation, fix layer, 2×4096 palette, 96 sprites/line, per-line rendering, lazy C-ROM tile cache |
| `ym2610.luau` | YM2610 sample-level: FM (8 algorithms, SSG-EG, LFO), SSG, ADPCM-A/B, timers A/B → Z80 IRQ, end flags, 44.1 kHz stereo mix |
| `rtc.luau` | uPD4990A serial clock with TP pulse |
| `snapshot.luau` | save states |
| `init.luau` | memory map, I/O, banking, system latches, watchdog, LSPC timer/VBlank IRQs, sound latches, scheduler, frame skip |
Frame skip: when a frame takes longer than its 16.9 ms slot, the next frame
runs without drawing (game speed is unaffected).
Sound: the YM2610 is emulated sample by sample at its native 55.5 kHz (4 FM
channels with all 8 algorithms, feedback, DT/MUL, ADSR, SSG-EG, LFO AM/PM and
channel-3 special mode; 3 SSG tones + noise + envelope; 6 ADPCM-A channels and
ADPCM-B from the V-ROMs) and mixed to 44.1 kHz stereo f32 with a DC blocker and
a 15 kHz low-pass. `m:drainAudio()` hands it to the host, which plays it through
`stations.pcmout` (rate-matched to the audio stream). Metal Slug costs about
0.5 ms of chip time per emulated frame. Tests:
`luau /zero/source/neogeo.package/tests/ym2610_tests.luau` (synthetic data only).