# Video Hardware Board: Namco Galaga, MAME parent set `galaga` (Namco rev. B). Sources are `src/mame/namco/galaga.cpp`, `galaga_v.cpp`, `starfield_05xx.cpp/.h`, with MAME's `src/emu/video/resnet.cpp` used only to derive palette levels. The character and sprite bit layouts live in `gfx-bitplane-spec.md`. ## Timing and resolution Source: `galaga.cpp`, machine config `galaga_state::galaga` (`set_raw`), `MASTER_CLOCK`, and the `GAME` entry for `galaga`. - Master crystal 18.432 MHz; pixel clock = 18,432,000 / 3 = **6.144 MHz**. - Horizontal: 384 pixel clocks per line, visible 0-287 (**288 pixels**), 96 blanked. - Vertical: 264 lines per frame, visible 0-223 (**224 lines**), 40 blanked. - Line rate 6,144,000 / 384 = 16,000 Hz exactly; frame rate 16,000 / 264 = **60.606 Hz**. - The raster is generated 288 wide by 224 tall and the cabinet monitor is mounted for `ROT90` (rotate 90 degrees clockwise): raster point (x, y) lands at screen (223 - y, x), so the player sees **224 wide by 288 tall**. Raster -X is screen-up, raster +X is screen-down. - Vblank edges (`galaga` config `screen_vblank`, `galaga_state::vblank_irq`, `galaga_state::screen_vblank_galaga`): at vblank *start* the main and sub Z80 IRQ lines are asserted if their enable bits are set; at vblank *end* the video latch bits Q0-Q5 are copied into the starfield generator. MAME also marks the screen `VIDEO_ALWAYS_UPDATE` because the starfield LFSR advances as a side effect of rendering; an emulator must step the LFSR every frame whether or not the frame is displayed. ## Video memory (`galaga.cpp`, `galaga_state::galaga_map`) | Range | Contents | |---|---| | `0x8000-0x83ff` | tile codes, one byte per cell (RAM 1K) | | `0x8400-0x87ff` | tile attributes, one byte per cell, same index as the code | | `0x8800-0x8bff` | work RAM 1; `0x8b80-0x8bff` is sprite bank 1 (code, color) | | `0x9000-0x93ff` | work RAM 2; `0x9380-0x93ff` is sprite bank 2 (Y, X low) | | `0x9800-0x9bff` | work RAM 3; `0x9b80-0x9bff` is sprite bank 3 (flags, X high) | | `0xa000-0xa007` | video latch, LS259 at 5K on the video board: a write to `0xa000+k` sets Q_k to data bit 0 | ## Tilemap (`galaga_v.cpp`, `tilemap_scan`, `get_tile_info`, `video_start`) The visible grid is **36 columns by 28 rows** of 8x8 tiles (288x224), but the 1 KiB of codes is organised as a 32-wide map. Display column c (0-35) and row r (0-27) select byte index: ``` if 2 <= c <= 33: index = (c - 2) + 32 * (r + 2) # main 32x28 block, rows 2-29 of the 32x32 map else: index = (r + 2) + 32 * ((c - 2) & 0x1f) # the four side columns ``` Expanded, the side columns are: c = 0 -> `0x3c0 + r + 2`, c = 1 -> `0x3e0 + r + 2`, c = 34 -> `0x000 + r + 2`, c = 35 -> `0x020 + r + 2`. In other words the top two and bottom two 32-byte rows of the square map are turned sideways to fill the left and right edges; bytes 0-1 and 30-31 of each of those four rows (16 bytes in all) are never displayed. Per cell (`get_tile_info`): - code byte: bits 0-6 = character code 0-127. Bit 7 is ignored by MAME; the hardware uses that address line to select the pre-flipped second half of the character ROM when the screen is flipped (see "Screen flip"). - attribute byte: bits 0-5 = color 0-63; bits 6-7 unused. - The `m_galaga_gfxbank` term is for the `gatsbee` bootleg and is always 0 on `galaga`. Transparency: `video_start` configures one tilemap group per color with transparent indirect entry `0x1f`; `galaga_palette` maps every character pen to palette entry `16 + n` where n is the lookup PROM nibble. So a character pixel whose lookup nibble is `0xf` (palette entry 31) is not drawn and whatever is beneath shows through. All other nibbles are opaque, including nibble 0. ## Sprites (`galaga_v.cpp`, `draw_sprites`) 64 sprites. Sprite n (0-63) uses byte pair o = 2n at offset `0x380 + o` of each of the three work RAMs: | Byte | Bits | Meaning | |---|---|---| | bank 1 `[o]` (`0x8b80+o`) | 0-6 | sprite code 0-127 (bit 7 unused) | | bank 1 `[o+1]` | 0-5 | color 0-63 (bits 6-7 unused) | | bank 2 `[o]` (`0x9380+o`) | 0-7 | Y | | bank 2 `[o+1]` | 0-7 | X low byte | | bank 3 `[o]` (`0x9b80+o`) | 0 | flip X | | | 1 | flip Y | | | 2 | double width (two 16x16 cells across) | | | 3 | double height (two cells down) | | bank 3 `[o+1]` | 0 | ninth X bit (adds 256) | | | 1 | tenth X bit (adds 512, which is past the raster: effectively hides the sprite) | Position, in raster coordinates of the 288x224 frame: ``` sx = X_low + 256 * (X_high & 3) - 40 sy = ((256 - Y + 1 - 16 * double_height) & 0xff) - 32 ``` The `+ 1` is MAME's model of the line buffer delaying sprites by one scanline; the `& 0xff` then `- 32` reproduces the wrap of the 8-bit Y counter and the 32-line vertical origin offset. Sprites are 16x16 pixels (2 bpp), so a sprite is fully visible only for sx in 0..272 and sy in 0..208. Composite sprites: a double-width and/or double-height sprite is drawn as up to four 16x16 cells with codes `code + 0` (top-left), `code + 1` (top-right), `code + 2` (bottom-left), `code + 3` (bottom-right). Cell (i, j) (i across, j down, each 0 or 1) is drawn at `(sx + 16 i, sy + 16 j)` using code offset `table[j ^ (double_height & flip_y)][i ^ (double_width & flip_x)]`, so a flipped composite swaps its cells as well as flipping each cell's pixels. Draw order: sprite 0 is drawn first and sprite 63 last, each overwriting what is under it, so **a higher-numbered sprite is on top**. Transparency: pixels whose sprite lookup nibble is `0xf` (palette entry 15) are skipped (`transpen_mask(..., 0x0f)`); nibble 0 is opaque. Screen flip (`flip_screen()` in `draw_sprites`): both flip bits are inverted; MAME does not mirror the sprite position, on the assumption the program does it. Whether the hardware itself repositions sprites in flipped mode is [unverified]. ## Palette (`galaga_v.cpp`, `galaga_palette`; PROM region "proms" in `galaga.cpp`) PROM region layout (`ROM_START(galaga)`): | Offset | PROM | Size | CRC32 / SHA1 | Use | |---|---|---|---|---| | `0x000` | `prom-5.5n` | 32 | `54603c6b` / `1a6dea13b4af155d9cb5b999a75d4f1eb9c71346` | palette | | `0x020` | `prom-4.2n` | 256 | `59b6edab` / `0281de86c236c88739297ff712e0a4f5c8bf8ab9` | character lookup | | `0x120` | `prom-3.1c` | 256 | `4a04bb6b` / `cdd4bc1013f5c11984fdc4fd10e2d2e27120c1e5` | sprite lookup | Palette PROM decode, one byte per entry, 32 entries. Weights are MAME's `compute_resistor_weights` output (no pull-up or pull-down, autoscaled so that all legs on gives 255; this is the same as weighting each leg by 1/R): | Bit | Channel | Resistor | Weight | |---|---|---|---| | 0 | Red | 1 kΩ | 33.23 | | 1 | Red | 470 Ω | 70.71 | | 2 | Red | 220 Ω | 151.06 | | 3 | Green | 1 kΩ | 33.23 | | 4 | Green | 470 Ω | 70.71 | | 5 | Green | 220 Ω | 151.06 | | 6 | Blue | 470 Ω | 81.30 | | 7 | Blue | 220 Ω | 173.70 | After summing and rounding to nearest, the 3-bit red/green value 0-7 gives levels `0, 33, 71, 104, 151, 184, 222, 255` and the 2-bit blue value 0-3 gives `0, 71, 151, 222`: MAME scales all three networks by the one factor that puts the three-leg networks at 255, so blue's two legs top out at 222. (An earlier draft of this document had blue at `0, 81, 174, 255`; MAME's screenshots settled it, 2026-09-11.) Lookup PROMs: index = `4 * color + pixel` (color 0-63 from the attribute or sprite byte, pixel 0-3 from the bitplanes); only the low nibble n of the PROM byte is used. Characters map to palette entry `16 + n` (entries 16-31), sprites to entry `n` (entries 0-15). Nibble `0xf` is the transparent value for both. MAME's file header remarks that only the first 128 bytes of each lookup PROM appear to be used (colors 0-31); whether the game ever selects colors 32-63 is [unverified]. Star palette (same function): 64 entries indexed by the 05xx's 6-bit color, bits 0-1 red, 2-3 green, 4-5 blue, low bit of each pair through 470 Ω and high bit through 220 Ω. MAME uses the same four levels for all three channels: value 0-3 -> `0, 71, 151, 222` (verified against its screenshots 2026-09-11). Star color 0 is black. ## Video latch bits (`galaga.cpp` config `galaga`; `screen_vblank_galaga`) | Q | Address | Function | |---|---|---| | 0-2 | `0xa000-0xa002` | starfield X scroll speed index, Q2 is bit 2 (see table below) | | 3 | `0xa003` | star set select A: active set A = Q3 (0 or 1) | | 4 | `0xa004` | star set select B: active set B = 2 + Q4 (2 or 3) | | 5 | `0xa005` | starfield enable (_STARCLR); 0 = stars off and LFSR reset to seed | | 6 | `0xa006` | not connected | | 7 | `0xa007` | flip screen | Q0-Q5 are sampled by the starfield device at the end of vblank; Q7 takes effect immediately. The 05xx SCROLL_Y pins are tied to ground on Galaga, so the vertical speed index is always 0. ## Starfield: Namco 05xx (`starfield_05xx.cpp`, `starfield_05xx_device`) MAME's model, derived from Robert Hildinger's 2019 reverse engineering, is reproduced here in enough detail to reimplement. **LFSR** (`get_next_lfsr_state`): 16-bit Fibonacci register, seed `0x7fff`, period 65,535. One step: ``` new_bit = b0 ^ b3 ^ b5 ^ b10 # bits of the current state state = (state >> 1) | (new_bit << 15) ``` **Hit test**: a star is generated when `(state & 0xfa14) == 0x7800`, i.e. bits 14, 13, 12, 11 are 1 and bits 15, 9, 4, 2 are 0. The eight free bits give: - star set = `(b10 << 1) | b8` (0-3); - color (BBGGRR) bit 0 = !b5, bit 1 = !b6, bit 2 = !b7, bit 3 = !b0, bit 4 = !b1, bit 5 = !b3. The file's prose table labels the color's top bit "!B4"; bit 4 is forced to 0 by the hit mask, and the code reads bit 3. This document follows the code. **Frame procedure** (`draw_starfield`, `set_scroll_speed`, config values `STARFIELD_X_OFFSET_GALAGA = 16`, `STARFIELD_X_LIMIT_GALAGA = 272`), run once per frame when enabled: 1. Advance the LFSR `pre = 22 * 256 + dx[index_x]` steps, where `dx = {0, 1, 2, 3, -4, -3, -2, -1}` indexed by Q2:Q1:Q0. 2. For raster y = 0..223, then x = 16..271: if the hit test passes and the star set equals active set A or B, plot palette index `32 + color` at (x, y); then advance the LFSR one step. This is 256 x 224 = 57,344 steps. 3. Advance the LFSR `10 * 256 = 2,560` steps. The total is `65,536 + dx` steps per frame against a period of 65,535, which is what scrolls the pattern: | Q2 Q1 Q0 | steps/frame | raster X shift/frame | |---|---|---| | 0 0 0 | 65,536 | -1 | | 0 0 1 | 65,537 | -2 | | 0 1 0 | 65,538 | -3 | | 0 1 1 | 65,539 | -4 | | 1 0 0 | 65,532 | +3 | | 1 0 1 | 65,533 | +2 | | 1 1 0 | 65,534 | +1 | | 1 1 1 | 65,535 | 0 (stationary) | Consequences: - The LFSR is reset only by Q5 = 0, so keep its state across frames and run the exact step counts above; the pattern is a stream, not a per-frame table. - Stars occupy raster columns 16-271 and all 224 lines; the outer 16 columns on each side never show stars. - 256 hits per period, 64 per set; two active sets give at most 128, less one color-0 (black) star per set and those falling in the 32 blanked lines. - "Blinking" is not a chip feature: the program alternates Q3/Q4 so a different pair of sets is visible; MAME simply switches sets at vblank. - MAME calls `draw_starfield` with flip = 0: stars ignore the flip-screen bit. - Stars are strictly background: the 05xx output is blanked wherever a tile or sprite pixel is produced (header notes on the _OE and OE_CHAIN pins). ## Screen flip Q7 of the video latch (`flip_screen_set`). Characters: the hardware inverts the vertical timing and selects the second 128 characters of `gg1_9.4l`, which are stored pre-flipped in X (`get_tile_info` comment); MAME instead flips the whole tilemap and adds `0x80` to the code to compensate. Sprites: see above. Stars: unaffected. An emulator that mirrors the final framebuffer can ignore the second character set and use codes 0-127 only. ## Rendering order (`screen_update_galaga`) 1. At the end of vblank copy Q0-Q5 into the starfield state. 2. Fill the 288x224 frame with black (MAME uses its dedicated black pen, not palette entry 0). 3. Starfield pass: run the frame procedure above, plotting stars. 4. Sprites 0..63 in ascending order, skipping lookup nibble `0xf` pixels. 5. Tilemap, 36x28 tiles via the scan formula, skipping lookup nibble `0xf` pixels. Tiles therefore sit **in front of** sprites, and sprites show through only where a character pixel is transparent. 6. Rotate 90 degrees clockwise for display. Nothing else is drawn: Galaga has no bullet or scroll hardware; shots are ordinary sprites.