# 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).
# neogeo (emulatorCore)
Neo Geo MVS/AES console spec for the RetroConsole cabinet and the room's
console station: 320×224 display at 59.185 Hz, MAME-style `.zip` game sets,
the YM2610's own sound (FM, SSG, ADPCM-A/B samples) as 44.1 kHz stereo PCM, and MVS backup RAM (high scores, BIOS
settings) persisted under `neogeo_saves/`. The machine core is `neogeo.machine`.
It needs two files from the player, and neither ships with the world:
- the game zip (Metal Slug = `mslug.zip`: `201-p1.p1`, `201-s1.s1`,
`201-c1.c1`…`201-c4.c4`, `201-m1.m1`, `201-v1.v1`, `201-v2.v2`);
- the BIOS, `neogeo.zip` (`needsBios`): a system ROM (`sp-s2.sp1`,
`sp-s.sp1`, `asia-s3.rom`, `vs-bios.rom`, `uni-bios_*.rom` …) plus
`sfix.sfix`, `sm1.sm1` and `000-lo.lo`.
`make(gameZipBytes, { bios = biosZipBytes })` builds the machine;
`validate` accepts a game zip and turns the BIOS zip away; `isBios(bytes)`
tells them apart. Input: `setPad(m, p1, p2)` takes one 10-bit word per player
(bit0 right, 1 left, 2 up, 3 down, 4 A, 5 B, 6 C, 7 D, 8 START, 9 SELECT); on
MVS a SELECT press drops a coin for that player, so Select then Start begins a
credit. `applyInput` maps the keyboard for standalone play. `snapshot(m)` /
`restore(m, s)` are whole-machine save states.
# pcmout
Plays an emulator core's own sound chip output: the PCM a core's `spec.audio.drain(m)` hands over
(interleaved stereo f32 at `spec.audio.rate`) goes into an engine audio stream on a small audio entity
that follows the console, placed in the room (spatial) so the sound comes from the TV or the handheld.
```lua
local Pcm = require("stations.pcmout")
local out = Pcm.new(consoleEntity, { rate = spec.audio.rate, spatial = true, maxDistance = 12 })
-- each frame, after the machine ran:
local buf, frames = spec.audio.drain(machine)
out:push(buf, frames, volume)
out:close() -- when the game stops or the station goes
```
Rate matching: every push is resampled (linear, continuous across pushes) by a ratio a PI controller
sets from where the stream's queue will stand against its target. At full speed the ratio stays within
1 ± 0.8 % (inaudible) and nothing changes; on a peer that cannot emulate the core's full frame rate the
integral learns the shortfall (50 of 59.185 frames/s settles at 1.18: a little lower pitch instead of gaps),
and after a hitch the burst is absorbed rather than dropped. The ratio is clamped to 0.6–1.6; anything
past the queue's target plus 60 ms is still cut as a last resort. `out.ratio` / `out.drift` show it.
Tests: `luau /zero/source/neogeo.package/tests/pcmout_tests.luau`.