# 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.
# mastersystem
A Sega Master System / Game Gear / SG-1000 emulator written in Luau, packaged
as `emulatorCore`s so the consoles appear on the room's console TVs beside the
NES, Game Boy, Neo Geo and Mega Drive. Players upload their own `.sms/.gg/.sg`
carts (or a zip of one); the shelf carries original homebrew written for this
package and released under CC0.
- `mastersystem_core.emulatorCore` — Master System (and SG-1000) spec: 256×192 at 59.92 Hz, worker thread, pad bar D-pad / 1 / 2 / Pause, battery RAM under `mastersystem_saves/`, save states.
- `gamegear_core.emulatorCore` — Game Gear spec: the same machine in GG mode, 160×144, Start button, saves under `gamegear_saves/`.
- `machine.module` (`require("mastersystem.machine")`) — the machine: VDP (mode 4 + TMS9918 Graphic 1/2 and Text, sprites with the 8-per-line limit, overflow/collision flags, scroll locks, line/frame interrupts, V/H counters), cart loader (raw, copier header, zip, TMR SEGA + SDSC headers), Sega / Codemasters / no mapper with 32 KB cartridge RAM, pads + Pause NMI, line scheduler (262 × 228 T-states). The Z80 and zip reader are `neogeo.machine.z80/zip`; the SN76489 is `megadrive.psg`.
- `asm.module` (`require("mastersystem.asm")`) — a two-pass Z80 assembler (Zilog syntax, full documented set), reusable for any Z80 machine.
- `homebrew.module` (`require("mastersystem.homebrew")`) — the Z80 runtime, font, tile/tilemap/palette/music helpers and the games below; `build()` writes the carts.
- `tests/sms_tests.luau` — VDP, mappers, cart RAM, pads/Pause, GG Start, PSG, save states, assembler (80 checks).
## Games on the shelf (all CC0 1.0, written for this package)
The engine has no network access, so no published homebrew could be fetched;
these carts were written here in Z80 assembly and are released under
**CC0 1.0 Universal** (public domain dedication: copy, modify, redistribute,
commercially or not, without asking). Source: `homebrew.module/*.luau`.
| cart | console | players | what it shows |
|---|---|---|---|
| `mastersystem_roms/neon_trails.sms` — Neon Trails | Master System | 1-2 (CPU rides pad 2 until a pad-2 button is pressed) | light-cycle duel: tile trails, sprite heads, PSG music + effects |
| `mastersystem_roms/sky_ranger.sms` — Sky Ranger | Master System | 1 | shooter: top-row scroll lock status bar, 3-band parallax switched by line interrupts, sprites, music, high score in battery RAM |
| `gamegear_roms/gem_catch.gg` — Gem Catch | Game Gear | 1 | 160×144 window, 12-bit palette, Start button, battery high score |
Every game advances the byte at RAM `$C000` once per logic step: the
logic-rate probe (`m:watchRam(0xC000)`).