# pcengine.machine
The PC Engine / TurboGrafx-16 machine: the HuC6280 (`pcengine.huc6280`), the
HuC6270 VDC line renderer (`vdc.luau`: BAT background with virtual sizes,
64 sprites with the 16-per-line limit, overflow/collision, per-line scroll,
raster-compare and vblank interrupts, VRAM and SATB DMA, burst mode), the
HuC6260 VCE (`vce.luau`: 512 colours, 256/341/512-pixel dot clocks drawn
into one 512×240 picture), the wavetable PSG (`psg.luau`: 6 channels, DDA,
noise, LFO, stereo balance → 44.1 kHz), HuCard loading (`cart.luau`: .pce,
copier header, zip, 384 KB layout), the joypad with a multitap, a
Tennokoe-style backup RAM, a 263-line scheduler and save states. Called by
the `pcengine` emulatorCore and the package tests.
```lua
local Pce = require("pcengine.machine")
local m = Pce.new(hucardBytes)
m:runFrame(true) ; m.fb (512x240 RGBA) ; m:setPad(p1, p2) ; m:drainAudio()
m:snapshot() / m:restore(s) ; m:saveBram() / m:loadBram(s) ; m:watchRam(0)
```
# 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.