Haxe for the Sega Mega Drive / Genesis.
Three parts: a Reflaxe compiler backend that turns Haxe into freestanding C for the 68000, a hardware SDK, and an emulator with a source-level debugger.
git clone <this repository>
cd hx68k
haxelib dev hx68k .
haxelib run hx68k setup
Then:
haxelib run hx68k new mygame a project that builds and boots
cd mygame && haxelib run hx68k rom Haxe to a ROM
haxelib run hx68k run <rom.bin> run it in the debugger emulator
haxelib run hx68k player <rom.bin> one executable with that ROM inside it
haxelib run hx68k test the gate: every check, in order
Everything is built through that one command, and everything it builds goes under export/.
Requires Haxe 4.3+, git, curl and a host gcc; the m68k toolchain arrives with SGDK through
setup and needs no installation.
Run haxelib run hx68k with no command for the full list.
Everything ticked has a test behind it that runs in haxelib run hx68k test, except the Window
entries, which need a running window: their models are gated headlessly and what the window itself
was measured doing is written down in docs/. Everything unticked is not started or not finished, and
nothing here is ticked on intent.
- Classes, pooled allocation, sized integers, fixed-capacity vectors, fixed point
- Enums as tagged unions, pattern matching as a jump table
- Inheritance with vtables only where something is overridden, interfaces as fat pointers
- Function pointers with nothing on the heap behind them
- Text and constant tables that stay in ROM
- Source maps: a 68000 address back to its Haxe file, line and function
- Optimisation passes, measured against hand-written C in 68000 cycles
- Anything beyond A6: no closures over heap state, no dynamic dispatch outside vtables
-
md.hw.*raw hardware, one volatile access per call - The resource pipeline, and every
md.*module written againstmd.hwrather than SGDK - The palette, native Haxe over
md.hw, at 327 cycles a sixteen color load against SGDK's 684 - The tilemap and the patterns, native, at 164 cycles a cell against SGDK's 334, and faster on fills and pattern uploads as well
- The sprite attribute table, native, transferred 12.3% faster than SGDK moves its own cache
- The DMA queue, native, 15.3% faster over four queued transfers and a flush, splitting one that crosses a 128 KB boundary in its source
- The PSG and the YM2612, native, with the PSG's clock folded to a literal by the region define
- The pad, native, reading a three-button controller and telling a port with one from an empty one
- The XGM sound driver,
md.Soundovermd.Z80Bus, with the assembled z80 driver carried here - Text with
md.Font, an image resource withmd.Tilemap.drawImage, and the two unpackers as 68000 assembly, aplib 37.6% faster than the hand written one and lz4w level with it - Every sample links no SGDK at all, boots from a vector table of ours and a cold VDP
bring-up, and is linked by
sdk/md.ldandsdk/rom.shrather than SGDK's makefile. The one exception istests/roms/bench, which links both on purpose to measure one against the other.docs/sgdk.mdaudits every SGDK file - Interrupts reaching Haxe:
@:md.verticaland its two siblings, on our own vector table - The VDP registers, SRAM, the z80 bus, the machine's own version, fixed point and the integer maths, with the sine table generated by a macro instead of carried
-
haxelib run hx68k new <name>writes a project that builds and boots, checked by the gate - No external tool in a build:
sizebndis gone, and images, palettes and pattern sets are now compiled byhxresand held to rescomp byte for byte across 75 checks. Sprites, music, the two compressors and the WAV path are still rescomp's
- 68000 core, 317,500 tests at 100% on final state, cycle counts and bus transactions
- Z80 core, 1,604,000 tests at 100% on state, cycles and pins
- YM2612, 1032 of 1032 fixtures bit identical to Nuked-OPN2
- SN76489 including the noise LFSR, held to 38 checks from its documentation
- Bus, memory map, Z80 arbitration, the SSF2 mapper, pads
- All three interrupts: vertical, horizontal, and the external one the HL trigger raises, with the HV counter stopping on it when register 0 asks
- NTSC and PAL, chosen by the cartridge header when the ROM loads: 262 lines at 59.92 frames a second, or 313 at 49.70
- VDP renderer: planes, window, sprites, priority, shadow and highlight, H40/H32, V28/V30, all three interlace settings, sprite masking and the per-line and per-dot limits
- VDP timing: the four entry write FIFO with its
!DTACKstall, the external access slots a line offers, and all twelve documented DMA rates, exactly rather than to a tolerance - Both HV counters, walked across a line of each width and across a whole frame
- Sound to the speakers in stereo, resampled, at about 30 ms
- A commercial ROM boots, plays and draws, held to seven frames out to 5,000
- Five hardware test ROMs in the gate: Nemesis' port access ROM at 100% on both pages, his sprite masking ROM 9 of 9 in both widths, the 240p suite's seven patterns, and two self-checking 68000 ROMs
- A read drains the write FIFO before it happens, which the port access ROM discriminates on, and the longest the bus is held matches Sega's own figure to the clock
- The vertical counter's endpoints for both standards and both heights, and what it reads under interlace
- What the bus costs: two cycles in every 128 taken from the 68000, and a Z80 reaching the 68000's bus holding it for eleven of its cycles while paying 3.3 of its own
- The color the VDP actually puts on the pin: fifteen levels rather than sixteen, spaced on a curve rather than evenly, with normal, shadow and highlight three readings of the one ramp
- A Z80 write through its window into 68000 space, which currently goes nowhere
- SRAM, as the cartridge header declares it, with the write protect bit
- EEPROM
- Break on a Haxe function, a Haxe line, or a symbol the Haxe map never heard of
- Step by instruction or by line, and stop on the nth time round
- Read a Haxe static, a local or a parameter by name, through DWARF 4 and DWARF 5
- Backtrace in Haxe names, with no frame pointer to walk
- Disassembler, held to the same fixtures as the core, 100% on all three axes
- Instruction trace beside the Haxe that produced it
- Frame profiler by Haxe function and by scanline
- VDP viewers: layout, palettes, sprites, plane cells, VRAM use
- Raster overlay: where the beam was when the code touched the VDP
- What each scanline spent the VDP's access slots on, and what the FIFO cost the 68000
- Savestates, rewind, and scrubbing back and forward through the ring
- A gdb remote, so the
m68k-elf-gdbSGDK already ships debugs a machine running here - An evaluator for DWARF expressions of more than one operation. gcc writes a
boolthat way, and such a variable is reported unreadable rather than guessed at
- SDL3 and miniaudio called directly, no framework in between
- The machine paced by its own clock, independent of the display and the sound device
- Debugger panels, dockable, each poppable into a window of its own, in a grid or floating
- A settings file, a window that edits it, and every key rebindable
- Savestate and rewind on keys, with a timeline under the toolbar to scrub the ring
- The gdb remote served from the window, so gdb reaches the machine on screen
- Hot reload: the ROM file is watched and loaded again when it changes
- A second build with the debugger left out: one executable with the ROM inside it, fullscreen, volume, and every key and gamepad button rebindable
- CI, hardware verification on a flashcart, a release on haxelib
- The player on Linux: it is written and it has not been built there
- The game framework: states, sprites, input and collision, built for this hardware
- Other 68000 machines: Neo Geo, then arcade boards
The hardware behind the emulator is written up in docs/notes/68000.md, docs/notes/z80.md, docs/notes/vdp.md, docs/notes/ym2612.md and docs/notes/psg.md: what the fixtures and the test ROMs taught, and what is inferred rather than measured.
| status.md | the evidence behind the checklist above |
| sgdk.md | every SGDK file, audited against what replaced it |
| notes/ | the hardware record: what the fixtures and the test ROMs taught |
haxelib run hx68k setup puts these in vendor/, which is gitignored. Licences differ and matter:
| Source | Licence | Use |
|---|---|---|
| reflaxe, reflaxe.CPP | MIT | The framework the compiler backend is written on |
| SGDK | MIT (gcc GPL3 + runtime exception) | The m68k toolchain and library |
| SDL3 | zlib | The window, linked as-is |
| miniaudio | MIT-0 / public domain | The audio device, called directly |
| Musashi | MIT | The known-good 68000 the compiler suite checks against, safe to port |
| Nuked-OPN2 | LGPL-2.1 | The FM reference; porting it would make that module LGPL |
| SingleStepTests m68000, z80 | fixtures | The specification both cores are held to |
| Spleen | BSD-2-Clause | The debugger's font, embedded in the source |
| Kabuto's Mega Drive notes | freely published | Hardware measured on real consoles, cited where taken |
| TmEE's VDP color measurements | freely published | The color ramp, measured off several consoles |
| VDPFIFOTesting, Sprite Masking Test | test ROMs | What the VDP's ports and sprites are held to |
| 240p Test Suite | test ROM | What the renderer's patterns are held to |
| Flamewing's BCD verifier, an illegal opcode ROM | test ROMs | Two the 68000 core is held to |
Nothing above is patched in place. The harness copies Musashi into tests/harness/.build/ before
patching it, and anything else needing modification does the same.
These are not fetched by setup, because nothing here builds against them. They were read while
writing the emulator and are listed so their terms are on the record:
| Source | Licence | Terms |
|---|---|---|
| marsdev | MIT | Non-Windows toolchain builds |
| superzazu/z80 | MIT | Safe to port |
| gwenesis | AGPL-3.0 | Read-only reference. Never copy. |
| Genesis Plus GX | non-commercial | Read-only reference. Never copy. |
Hardware behaviour here comes from documentation, from the SingleStepTests fixtures, or from test ROMs. It is not taken by reading a restrictive source and transcribing what it does.