# pcengine.huc6280
The Hudson HuC6280 CPU in Luau: the 65C02 instruction set plus the HuC6280's
extras (SXY/SAX/SAY, CLA/CLX/CLY, ST0/ST1/ST2, TAM/TMA with eight 8 KB MMU
pages over a 21-bit space, CSL/CSH, SET and the T flag, BSR, TST, block
transfers TII/TDD/TIN/TIA/TAI, BBR/BBS/RMB/SMB), its on-die timer and its
interrupt controller (IRQ2/BRK, IRQ1, timer). Cycles are counted in 7.16 MHz
units; in low speed (CSL) each CPU cycle costs four.
```lua
local HuC6280 = require("pcengine.huc6280")
local cpu = HuC6280.new({
read = function(phys) return 0xFF end, -- 21-bit physical read (I/O, open bus)
write = function(phys, v) end, -- 21-bit physical write
map = function(bank) return buf, off, writable end, -- fast path: a buffer for an 8 KB bank, or nil
})
cpu.reset()
local used = cpu.run(455) -- run at least 455 cycles, returns cycles used
cpu.setIrq(HuC6280.IRQ1, true) -- level-triggered IRQ1 / IRQ2 lines
```
The timer ($0C00-$0FFF) and interrupt controller ($1400-$17FF) are on the die:
route those I/O-page addresses to `cpu.timerRead/timerWrite/irqRead/irqWrite`.
`cpu.getState()/setState()` for save states, `cpu.regs()` for tests.
# 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).
# megadrive.machine
The Sega Mega Drive / Genesis machine (NTSC) as Luau, one file per chip. The
`megadrive_core.emulatorCore` spec calls it; anything else that wants a Mega
Drive can too.
```lua
local Md = require("megadrive.machine")
local m = Md.new(cartBytes) -- .md/.bin/.gen, interleaved .smd, or a zip of one
m:runFrame(true) -- one 59.92 Hz frame: 262 lines x 488 68000 cycles
m.vdp.fb -- 320*224*4 RGBA8 (H32 games centred)
m:setPad(p1, p2) -- station pad words: d-pad, A B C, Start
m:drainAudio() -- (buffer, frames) 44.1 kHz stereo f32 (YM2612 + PSG)
m:saveRam() / m:loadRam(s) -- battery SRAM
m:snapshot() / m:restore(s) -- save states
m:watchRam(addr) -- count a game's own frame counter (logic-rate probe)
```
| file | what |
|---|---|
| `init.luau` | memory map, I/O pads (TH protocol), Z80 BUSREQ/RESET/bank, SRAM, TMSS, scheduler, V-int/H-int |
| `vdp.luau` | VDP ports, DMA (68k/fill/copy, CPU stall), status/HV, line renderer (planes, window, sprites, scroll modes, shadow/highlight, H32/H40) |
| `ym2612.luau` | YM2612 sample-level FM: 6 ch, DAC, CSM, LFO, SSG-EG, timers; mixes the PSG; 44.1 kHz stereo out |
| `cart.luau` | raw/SMD/zip loader and header (title, SRAM window) |
| `state.luau` | save-state codec |
The 68000, Z80 and zip reader come from the Neo Geo package (`neogeo.machine.m68k`, `.z80`, `.zip`).
# pcengine
A NEC PC Engine / TurboGrafx-16 emulator written in Luau, packaged as an
`emulatorCore` so the console appears on the room's console TVs beside the
NES, Game Boy, Neo Geo, Mega Drive and Master System. Players upload their own
`.pce` HuCards (or a zip of one); the shelf carries original homebrew written
for this package and released under CC0.
- `pcengine_core.emulatorCore` — the console spec: 512×240 at 59.83 Hz (256 / 336 / 512-pixel modes all span the tube), worker thread, pad bar D-pad / I / II / Run / Select, pad 2 through the multitap, 44.1 kHz stereo, Tennokoe-style backup RAM under `pcengine_saves/`, save states.
- `huc6280.module` (`require("pcengine.huc6280")`) — the HuC6280 CPU: 65C02 core plus MMU (TAM/TMA, 8 KB pages of a 21-bit space), block transfers, T flag, CSL/CSH, ST0-2, BBR/BBS/RMB/SMB, TST, timer and interrupt controller. Reusable by any 65C02-family machine.
- `machine.module` (`require("pcengine.machine")`) — the machine: HuC6270 VDC line renderer (BAT with virtual sizes, 64 sprites 16×16..32×64 with the 16-per-line limit, overflow and sprite-0 collision, per-line scroll latch, RCR + vblank IRQs, VRAM→VRAM and SATB DMA, burst mode), HuC6260 VCE (512 colours, three dot clocks), PSG (6 wavetable channels, DDA, noise, LFO, balance → 44.1 kHz), HuCard loader (.pce, copier header, zip, 384 KB layout), joypad + multitap, backup RAM gate, 263×455 scheduler, save states.
- `asm.module` (`require("pcengine.asm")`) — a two-pass HuC6280 assembler, reusable for any 65C02 machine.
- `homebrew.module` (`require("pcengine.homebrew")`) — the HuC6280 runtime (raster-split table, multitap pads, music + SFX drivers, text), Luau art/music helpers and the games below; `build()` writes the carts.
- `tests/cpu_tests.luau` (54 checks) and `tests/pce_tests.luau` (78 checks, incl. PSG LFO and timer-driven DDA).
## 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 HuCards were written here in HuC6280 assembly and are released under
**CC0 1.0 Universal** (public domain dedication: copy, modify, redistribute,
commercially or not, without asking). Source: `homebrew.module/*.luau`.
| HuCard | players | what it shows |
|---|---|---|
| `pcengine_roms/nebula_storm.pce` — Nebula Storm | 1 | horizontal shooter: four parallax bands split by raster-compare interrupts under a fixed status bar, ~60 sprites (32×16 ship, waves, aimed bullets, explosions, a 64×32 boss), palette-cycled stars, 3-voice wavetable music with sampled DDA kick + snare streamed by the timer interrupt, high score in backup RAM |
| `pcengine_roms/tank_duel.pce` — Tank Duel | 1-2 (CPU drives tank 2 until a pad-2 button is pressed) | arena duel in the 336-pixel mode, both pads through the multitap, bouncing shells, first to 5, DDA drums |
Every game advances the zero-page byte `$2000` (RAM offset 0) once per logic
step: the logic-rate probe (`m:watchRam(0)`).
## Not emulated
CD-ROM², SuperGrafx, Arcade Card, the Street Fighter II mapper, Populous cart RAM, US bit-reversed dumps, PAL, mid-line raster effects, 6-button pads.