# Flight paths and path mods Every enemy flight in Galaga, entrance or dive, is a small program in the sub CPU's ROM run by its path interpreter (sub 0x08d3; the language is in [ram-map.md](ram-map.md), "The path language"). `src/galaga/paths.rs` turns those bytes into text and text back into bytes, and lets the board fly new paths. The main CPU still decides *when* and *who* dives; a path mod changes *how* they fly. ## The hardware rule Path mods stay within what a real Galaga board plus a daughter card could do. (The neon look, the flight-path overlays, the TAS and the movies are tools around the board, not part of it.) Concretely, a mod may only: - **change bytes of the original ROMs**, as a replacement EPROM would: the sub CPU's (path ops, the interpreter's dispatch operands, the checksum filler at 0x00ff) and the main CPU's (entrance-table words, the checksum pad byte at 0x2bff). Each ROM still passes the game's own power-on test; - **add read-only code and data at the sub CPU's 0x1000-0x3fff**, the range its address decoding leaves unconnected (MAME's driver header and memory map; not yet checked against a schematic). A card there sees the sub CPU's address bus (in practice a piggyback in the Z80 socket, since the ROM socket carries only A0-A11) and drives the data bus for those addresses; nothing else on the board answers there, and `tests/mods.rs` checks the original game never depends on that range, so the card leaves it alone. A mod may not add RAM, change anything the board does not do (no new behaviour in the Rust board for a mod's sake), or rely on the emulator in any way a real board would not reproduce: the `random_jump` handler is plain Z80 code reading the refresh register and RAM the board has. What remains to check against real hardware: the sub CPU's time budget per frame with the longer paths and the handler (the emulated board shows no slowdown), and the decoding claim above against a schematic. ### The space budget (2026-09-30) | Where | Size | Used by `mods/remix.paths` | How a real board gets it | |---|---|---|---| | sub CPU 0x1000-0x3fff (card) | 12,288 bytes | 420 (348 below 0x2000, where entrance paths must be) | a card in the sub CPU's Z80 socket | | sound CPU 0x1000-0x3fff (card) | 12,288 bytes | 0 | a card in the sound CPU's Z80 socket | | main CPU 0x4000-0x67ff | 10,240 bytes | 0 | unconnected in MAME's decode table (not yet tested here); a card in the main CPU's socket | | 0xff padding in the main ROMs | 3,340 bytes in 7 runs | 1 (checksum byte at 0x2bff) | the replacement EPROMs | | 0xff padding in the sound ROM | 233 bytes in 2 runs | 0 | the replacement EPROM | | 0xff filler in the sub ROM | 8 bytes | 1 (checksum byte at 0x00ff) | the replacement EPROM | The padding runs: main 0x0052+20, 0x0b0f+1264, 0x160c+244, 0x1fe7+24, 0x2aed+275, 0x2e75+394, 0x3b9f+1119; sound 0x001d+73, 0x0f60+160. Each was checked by overwriting it (keeping the ROM's checksum) and replaying the 20-minute movie to the same RAM, so the game does not read them in play (service mode, two-player games and the high-score entry are not in that movie). Of the card spaces, the sub and sound CPUs' are checked the same way (`tests/mods.rs`); the main CPU's is not yet. ### The board's other limits (measured 2026-09-30) Space is not the limit; these are: - **CPU time.** Each Z80 runs 50,688 T-states a frame. Measured with `Shared::time_by_pc` over stages 1-8: the sub CPU (it flies every enemy) spends about 57% of its time in its wait loop at 0x05b1, with or without the remix's paths; the sound CPU about 80% in its loop at 0x00b9. The main CPU runs its score supervisor in a background loop between interrupts, so its headroom is not visible this way; it needs time attributed to the interrupt handlers. - **Objects.** 64 sprite slots, laid out as 48 enemy slots, two fighters, two rockets and eight bombs (ram-map.md); more enemies at once would mean main-CPU changes. - **Flights at once.** The motion queue has 12 entries; at most 12 objects can fly at the same time. - **RAM.** Three 1 KB work RAMs, mostly allocated; a card could add RAM, which the hardware rule does not allow yet. - **Art and sound.** Sprites and tiles come from the graphics ROMs (new art means new graphics EPROMs); sound is the three-voice WSG and the 54xx. ### Sprites and tiles (`sprite` and `tile` lines in a mod) `sprite CODE HEX` replaces sprite CODE (00-7f) with 64 bytes in the sprite ROM's own layout: what a replacement sprite EPROM pair would hold (the CPUs never read the sprite ROM, so there is no checksum to keep). The picture side applies them (`PathMod::apply_video`: the movie renderer, the preview tool, the page and its neon layers); `tests/mods.rs` checks the result equals a pipeline built from the patched ROM. `tools/sprite_convert.py --mod-lines` writes a family's 8 lines. `tile CODE HEX` does the same for tile CODE (00-ff) with 16 bytes in the tile ROM's layout (a replacement tile EPROM; `Video::set_tile`), checked by the same test. The families and their colour codes: boss 0x08-0x0f (0 healthy, 1 after a hit), butterfly 0x10-0x17 (2), bee 0x18-0x1f (3); the transforming specials have seven drawings and no flap frame: flagship 0x50-0x56 (4), scorpion 0x58-0x5e (5), stingray 0x60-0x66 (6); and so have the challenging stages' bonus targets, dragonfly 0x68-0x6e and satellite 0x70-0x76 (both 2). Convert them with `--frames 7`, since the 8th code is not theirs (0x6f is the '150' score). The fighter is 0x00-0x07 (9; the captured fighter's red is 7): 0x00-0x05 turn as it docks after a rescue, 0x06 is upright and 0x07 a copy of it. The reserve ships at the bottom of the screen are tiles, not sprites: 4a 4b / 4c 4d in tile colour 3, a near copy of 0x06 (the same ship a pixel lower); `tools/sprite_to_tiles.py` redraws them from a sprite. The original 0x70 creature looks alike at every angle, but the game still picks its code by heading, like the rest. The converter maps a picture to the three colours by nearest colour (`--map nearest`, the bee and butterfly) or, for pictures in one hue, by brightness bands (`--map luma --luma-order 2,1,3`, the boss: Stable Diffusion's bosses are all green on green, which nearest colour turns into teal blobs). The hit colours come free: colour code 1 recolours the same pixels. ### The title screen (`galaga::title`, `title_*` lines in a mod) The attract title rearranged, on real game code (2026-10-01): - `rst 0x30` prints message C through 0x13b5 (pointer table at 0x13ef; a message is a position word, a colour and ASCII ending in '/'; ASCII maps to tile codes 0x00-0x48, so even the solid block is a character). The jump at 0x0032 now goes to a 52-byte hook in the padding at 0x0b0f: on message 0x0e (the title) it draws the logo's runs of tiles, then prints the message. - `title_run ROW COL COUNT CODE COLOUR`: the logo, 5 bytes a run. The remix's (Stable Diffusion's John Deere logo, converted by tools/mosaic.py and tools/cells_to_runs.py) is 80 runs, 402 bytes. - `title_text ROW COLOUR TEXT`: message 0x0e redirected to a new line in the padding at 0x160c. - `title_shift SCORE TABLE`: the score header and lines move down, and so do the ten object records at 0x195c-0x1983 placed with them (the table's two icons and the showcase formation that flies in under it). - Each 4 KB section keeps its checksum through its pad's last byte (0x0ffe, 0x16ff); `tests/mods.rs` boots every titled mod past the power-on test and checks the logo cell by cell in tile RAM. art/mockups/title-screen-real.png shows it; title-logo-blocks.runs is the hand-made block-letter alternative (64 runs), ready to swap in. ## Reading the paths ``` cargo run --release --bin galaga-paths > target/paths.txt ``` prints all of them from your ROM set (the listing is ROM content: generated, never committed). What a listing looks like, with made-up numbers: ``` p1234: fly 2 1 turn 20 for 36 ; speed 2 on odd frames, 1 on even; heading +20/1024 a step, 36 steps fly 2 1 turn 0 for 40 ; straight until_y 0x40 ; keep flying until Y reaches 0x40 jump_unless_bombing p1300 reenter_top ; off the bottom, back in at the top x_to_home_column jump_if_stage_b p1280 ; later stages: another variant home ; head for the formation cell end ``` A heading is 10 bits: 1024 is a full turn, so `turn 16 for 64` is one circle. Mirrored flights (the right-hand side of the formation) negate every turn and heading automatically. **Why the decode can be trusted** (`tests/paths.rs`): operand lengths are read from the interpreter's handler code; from 36 known starts it reaches 426 ops in 63 labelled paths with no overlap; the listing reassembles to the ROM's exact bytes (1,177); the main CPU's entrance table (main 0x2a3c) points only at op starts; and in the 20-minute TAS movie every live motion-queue pointer (349 distinct) is an op start. ## Writing a path mod A mod is a text file in `mods/`, in the same language plus `org ADDR` (where the following ops go) and `name:` labels. Existing paths are named `pXXXX`. - New code goes in the sub CPU's free space, `0x1000`-`0x3fff`: unconnected on the real board, so the game never reads it except through a mod's jumps. - Inside the original ROM a run must replace whole ops. The usual move is to replace a path's first op (3 bytes) with `jump new_code`, and end the new code with `jump pXXXX` back into the original, or `home`/`end`. - The sub CPU's power-on test sums its ROM (0x0000-0x0fff) and stops the game with "ROM 11" unless the sum is 0xff. A mod that changes ROM bytes gets a compensating byte at 0x00ff, the last of 8 filler bytes no path or code reads; `PathMod::runs_for` adds it. `mods/bee-figure-eight.paths` is the first: after peeling off the formation a diving bee loops once each way, then the original dive takes over. ## The `random_jump` extension The language has no way to choose at random, and no token value is free (below 0xef is a segment, 0xef-0xff are the 17 commands). A mod with the line `extend random_jump` changes the interpreter itself: - two operands of the dispatch at sub 0x090e: `cp 0xef` becomes `cp 0xee` (0xee is now a command) and `ld hl,0x0920` points at a new command table at 0x3f80, copied from the player's ROM when the mod loads, with an 18th entry; - a 36-byte Z80 handler at 0x3f00 (`paths::extension::HANDLER_CODE`): it mixes the refresh register, the object's X, the frame counter and the fighter's X, takes a bit chosen by the op's address, and either jumps through the ROM's own jump handler (0x0b46) or skips to the next token (0x090e), the way the game's `f7` does; - `random_jump LABEL` (3 bytes) jumps half the time. Chains give quarters, eighths and so on. It is deterministic (the refresh register and the rest are emulated), so movies replay. The mod checks that no original path uses 0xee as a segment and that the dispatch bytes are what the extension expects. Movies of an extended mod store the mod's source text (`"path_mod"`) and apply it to the player's ROM at load, so the copied table never lands in a committed file. ## `mods/remix-dives.paths` Bees pick one of four dives, equally likely: the original, the figure eight, a zigzag weave down in place of the straight run, or a feint (drop fast, pull up hard with a half turn, fly home). Butterflies do a wide S down the screen half the time; mirrored dives from the right-hand side weave the other way, so pairs cross. `movies/tas-remix-dives.json`: stage 10 in 8 minutes without losing a life. ## Entrances The main CPU's entrance table (main 0x2a3c, 24 words: bits 0-12 a path in the sub CPU, bits 13-15 a start position and heading) is shared by families of stages. Measured on the 20-minute TAS movie: | Stages | Entries | |---|---| | 1, 4, 6, 8, 10, 12, 14 | 0, 1 | | 2, 5, 9, 13 | 2, 3, 4, 5 | | 3 (challenging) | 6, 7 | | 7 (challenging) | 8, 9 | | 11 (challenging) | 10 (or 22), 11 | A mod line `entrance N LABEL` points entry N at a path of its own, keeping the entry's start bits. Entrance paths must lie below 0x2000 (13 address bits). The main CPU's power-on test sums each 4 KB ROM section to 0, so the mod sets 0x2bff, the last byte of the 275-byte 0xff pad at 0x2aed-0x2bff in the table's section (`[disasm: gg1-3.s]` ".ds 0x0113 ; pad"), to keep it. A regular entrance path should keep the originals' shape: its own segments, then `jump_if_transient` to the original's transient branch (transients fly through and leave), then `home` and a long segment. **Challenging stages are different**: a shooting gallery. Their original paths (entries 6 and 7 for stage 3: p01e8, p01f5) fly a pattern and then a long straight segment and `end`, with no `home`; objects in state 3 that leave the screen are taken out of play. A challenge path must do the same, or the enemies settle into a formation that should not be there (the first remix rosette did exactly that). In the stage 1 family the first group enters at the top heading down, the second from the lower left heading right; positive turns bend a rightward flight up. ### Challenging stages, measured - **A fixed script.** With the ship standing still, holding left or holding right (no shooting), every enemy is in the same place on every frame. - **Waves on a beat.** Each wave of eight launches on a 32-frame tick after the previous wave is gone; unshot, waves come every 320 frames, so clearing a wave quickly brings the next one sooner. Each enemy's own flight, relative to its launch, is unchanged by kills. - **One ship gets close.** `galaga-gallery` plans rockets against a forecast of the script and gets 38 of 40 on the original first challenging stage with one ship (beam 64 x 16, firing as fast as the 51xx allows: a press every 4 frames, docs/custom-io.md). - **Perfect with the dual fighter, proven on the board.** `galaga-tas --dual` gets captured and rescued like a player and keeps the pair (movies/tas-remix-dual.json); `galaga-gallery` then plans each challenging stage from it and the game's own counter judges (0x9288, docs/ram-map.md). A run the game calls PERFECT is a proof that the stage can be perfected; they are kept in proofs/ and `tests/proofs.rs` replays them. A miss proves nothing (the planner is a heuristic), but repeated misses on the same enemy point at the design. - **"NUMBER OF HITS 41" was our input chip, not the game.** A run once destroyed all 40 and the game counted 41: two rockets 2 frames apart met one enemy a frame apart. Checked against MAME (2026-10-02): MAME's 51xx, running the chip's own program, debounces fire over two frames down and two up, so a cabinet fires at most every 4 frames and rockets are never that close; ours passed one-frame taps (fixed, docs/custom-io.md). With legal fire the planner, set to make double counts (`--seek-double`), made none in six runs of the original game's stages 3 and 7. - **How challenge enemies are removed.** The game's cull (`case_254D`, `[disasm: gg1-3.s]`, and observed) takes a state-3 object out of play once it is past the top or bottom of the screen (open-ended tests), or while its side coordinate is in a band of about 11 pixels at the wrap seam; each object is checked only every few frames, so one flying sideways at 3-4 px a frame can jump the band and wrap round the screen instead, and its path then ends in plain view (the gate in `tests/mods.rs` caught one). Fast challenge paths should leave by the top or bottom. - Ship and rockets, measured: the ship moves 1 and 2 px on alternate frames, 3 frames after the stick; a rocket appears 4 frames after the press, at the ship (y 265), and climbs 6 px a frame; two rockets at most. ## Seeing a path before playing it: `galaga-pathshot` ``` cargo run --release --bin galaga-pathshot -- movies/tas-seed1.json 1150 1750 target/shots/stage1.png --mod mods/remix.paths ``` plays the movie to frame 1150 on the unmodified board, lays the mod over it there, plays on to 1750 and draws every enemy's whole flight over the last frame, with a margin around the screen so flights that leave it show. It is the real board flying the paths, not a simulation. Stage starts of `movies/tas-seed1.json` are in its note; start a hundred frames before one. ## Pitfalls (learned the hard way) The first S-swoop made butterflies vanish off the top of the screen and pop back in mid-screen. Two facts explain it, and every new path should respect them: - **Know the heading before relying on it.** A butterfly leaves its peel-off heading steeply sideways, about 64 degrees off straight down, not down. A turn designed for "starting from straight down" sent it up and off the top. Headings are 10 bits and `heading n` sets n x 4; measured from the game's own flights, 0x300 (`heading 0xc0`) is straight down, and the mirror image of `heading 0xc0` is itself. - **Much of the original runs on fixed timings.** The butterfly dive after p03ac flies set-length segments and then `reenter_top`, trusting that the butterfly has left the bottom of the screen by then. Joining it after a detour breaks that trust. Leave the screen on your own terms first: `until_y 0x06` (just below the bottom) and then jump to the re-entry (p03c4). The queue's Y counts up the screen: screen y is about 318 - 2 x queue Y, so 0x0f is the bottom edge and the original re-enters at 0x06-0x07. - **Design where the player can see it.** The first rosette for the challenging stage started with a half loop; the group enters heading down, so it turned up and flew most of the rosette above the screen. `galaga-pathshot` shows this at once. `tests/mods.rs` fails any mod in which a flying enemy (transients aside) jumps more than 24 px in one frame inside the screen, or stops being an object there without exploding; it checks every mod for 20,000 frames and every committed modded movie in full. The game itself clears enemies around a lost fighter, a capture and game over (measured), so the check stands aside for 240 frames after any of those. The unmodified game passes over the whole 20-minute TAS movie. ### The remix's challenging stages, easy to hard Designed with `galaga-pathshot --idle` and ranked with two measures: the flights' speed and time on screen (no shooting), and `galaga-gallery`'s single-ship hits out of 40 (beam 64 x 16; it varies by about 1-2 between runs). The original game's stages score 34-40 single-ship, at 2.3-2.9 px/frame and 180-260 frames on screen. | Stage | Pattern | Speed (px/frame) | Frames on screen | Single-ship hits | Dual fighter | |---|---|---|---|---|---| | 3 | columns down and up the middle | 2.06 | 151 | 40 | PERFECT | | 7 | rosette and loop-the-loop | 2.86 | 186 | 39 | PERFECT | | 11 | small tightening spiral | 2.73 | 156 | 38 | PERFECT | | 15 | zigzag lattice, climbing hairpins | 2.72 | 141 | 40 | PERFECT | | 19 | chain of tight loops, crossing sweeps | 4.20 | 161 | 40 | PERFECT | | 23 | hurricane: tight loops both ways | 3.80 | 136 | 40 | PERFECT | | 27 | boomerang: dive, a tight S across, out | 2.87 | 137 | 40 | PERFECT | Measured 2026-10-02, firing as fast as the 51xx allows (a press every 4 frames): single-ship hits with `galaga-gallery` at beam 64 x 16 from movies/tas-remix.json; dual fighter as the game counts it, every stage proven in proofs/ (from movies/tas-remix-dual.json; stages 31 and 35 too). Stage 31 keeps the original (entries 20 and 21). Stages 19, 23 and 27 were reworked the same day to be perfectable with the dual fighter (Aubrey: challenging stages should be fun, and not that hard if you know where to be and when to shoot). Before, they flew at 4-5 px a frame and left climbing away from the rockets or out the top corners; the best the planner found was 39, 37 and 36. Now every exit leaves down the screen, facing the rockets, at 3 px a frame or less on the last leg. What makes a challenge easy or hard, learned designing these: - **Columns are easy, level streams are hard.** The ship moves 1.5 px a frame; enemies coming down toward it in a column (or climbing away slowly, at 2 px a frame) can be shot one after another, while a stream sliding sideways at 2 px a frame outruns it (a level crossing scored 29 where columns scored 37). - **Mirrored streams far apart are hard.** Each wave flies as two mirror images; when they are far apart the ship can cover only one, and half the wave escapes. Keep them close (converging) for easy stages. - **High and tight is hard.** Enemies looping high up take long rocket flights, and two rockets cannot keep up with a bunched stream. - **Speed and time on screen** set the pressure. - **Exits should face the rockets.** Rockets climb 6 px a frame; an enemy leaving upward at 4 gains only 2 a frame on them, so a stream climbing out is nearly uncatchable once above mid-screen, and one leaving through a top corner or the far edge is out of reach. Leaving down the screen, every enemy comes at the rockets head-on. - **No tight columns at speed.** Eight enemies 4 frames apart in one column arrive faster than two rocket slots cycle; slow them or spread them. ## `mods/remix.paths` Everything in remix-dives, plus new entrances: the stage 1 family's top group drops in and flies two interlocking loops, its lower group climbs away from the fighter and snakes across mid-screen; in the first and every challenging stage from 3 to 27 has a new pattern, easy to hard (the table above), each flying off the screen as the originals do. `movies/tas-remix.json`: stage 31 in 72,000 frames (20 minutes) without losing a life; `movies/tas-remix-dual.json`: the dual fighter from stage 3 to stage 37.