# Video Hardware ## Timing and resolution - Pixel clock: 6.144 MHz (master clock ÷ 3). - Horizontal: 384 total, 256 visible (rest is blanking). - Vertical: 264 total, 224 visible. - **Native visible resolution: 256×224**, refresh rate **60.606 Hz** (6,144,000 / (384×264) — a very slightly-off-60Hz rate typical of this era; matching it exactly matters only if you care about frame-perfect timing parity with real hardware). - The cabinet is a vertical/portrait monitor: the 256×224 image is generated in what is logically landscape order but displayed rotated 90°, so it appears as roughly 224 wide × 256 tall on screen. A pygame recreation can simply render directly into a 224×256-ish portrait window and skip the rotation step entirely (only real hardware and CRT-accurate emulators need to reason about the physical rotation). ## Tilemap (video RAM, `0x5000`-`0x53ff`) A flat 32×32 grid of 8×8-pixel tiles (1024 bytes, one tile index per byte); only 32 columns × 28 rows are within the visible 256×224 area. The visible raster is source rows **16–239 inclusive**, corresponding to tile rows **2–29**. It is not source rows 0–223: composition must crop the top two and bottom two tile rows before passing the native frame to the portrait rotation. ``` tile_index_in_ram = row * 32 + col tile_code = videoram[tile_index_in_ram] # which 8x8 tile to draw color_group = attributeram[col * 2 + 1] & 0x07 # 3 bits -> 0-7 scroll_y = attributeram[col * 2] # source Y offset for this column ``` Critically, **color is selected per column, not per tile.** `attributeram` (the same physical RAM block as sprites, at `0x5800`-`0x583f`) holds one byte pair per column: an even-offset byte used for **per-column vertical scroll** and an odd-offset byte whose low 3 bits pick one of 8 color groups shared by every tile in that column. For output row $y$, column $x$ selects its tile pixel from source row $(y + 16 + \mathrm{scroll\_y}[x]) \bmod 256$. Tiles are 2 bits per pixel (4 shades, one of which is transparent/background), so 8 groups × 4 shades = 32 palette entries — exactly the size of the color PROM. This column-level coloring is why original Galaxian screenshots show flat-colored horizontal bands (e.g. a solid-color score/title strip) — worth replicating faithfully rather than "fixing" it as if it were a bug. ## Sprites (`0x5840`-`0x585f`, 8 sprites × 4 bytes) ``` byte 0: Y position (screen Y = 240 - byte0; slots 0–2 instead compare against the preceding scanline, so their effective Y is one pixel greater) byte 1: bits 0-5 = tile code (0-63, decoded from the same gfx ROMs as background tiles) bit 6 = horizontal flip bit 7 = vertical flip byte 2: bits 0-2 = color group (0-7, same shared 32-entry palette as tiles) byte 3: X position (add 1 pixel — a documented hardware offset) ``` The sprite line buffer also creates a hardware edge clip: normal sprites do not emit source pixels before X=17, while horizontally flipped sprites do not emit source pixels after X=238. This is separate from ordinary 256-pixel framebuffer clipping and must be applied before a cabinet/display rotation; it prevents a ship from appearing in the otherwise illegal left strip. ## Palette decode (32-byte PROM, `6l.bpr`) Each byte is one palette entry, decoded through a weighted-resistor DAC — lower resistance contributes more brightness. Bit-to-channel mapping: | Bit | Channel | Resistor | |---|---|---| | 0 | Red | 1 kΩ | | 1 | Red | 470 Ω | | 2 | Red | 220 Ω | | 3 | Green | 1 kΩ | | 4 | Green | 470 Ω | | 5 | Green | 220 Ω | | 6 | Blue | 470 Ω | | 7 | Blue | 220 Ω | A practical closed-form approximation good enough for a software recreation (real hardware's true resistor-ladder math will differ by a few least-significant bits, which is not perceptible): ```python def decode_color(byte): r = 0x21 * ((byte >> 0) & 1) + 0x47 * ((byte >> 1) & 1) + 0x97 * ((byte >> 2) & 1) g = 0x21 * ((byte >> 3) & 1) + 0x47 * ((byte >> 4) & 1) + 0x97 * ((byte >> 5) & 1) b = 0x55 * ((byte >> 6) & 1) + 0xAA * ((byte >> 7) & 1) # only 2 resistors -> scale so both-on = 0xFF return (r, g, b) ``` ## Starfield Galaxian's signature scrolling background stars come from a 17-bit linear-feedback shift register (LFSR). Build its fixed source positions once by advancing it through the hardware's 512×256 generator space, rather than reclocking it live every frame: ```python generator = 0 for y in range(256): for x in range(512): feedback = ((~generator >> 16) & 1) ^ ((generator >> 4) & 1) generator = (generator << 1) | feedback if ((~generator >> 16) & 1) and (generator & 0xff) == 0xff: color = (~generator >> 8) & 0x3f if color: stars.append((x, y, color)) ``` This produces 252 nonzero-color source stars. At scroll position `p`, each source `(source_x, source_y, color)` maps to `x = ((source_x + p) & 0x1ff) >> 1` and `y = (source_y + ((p + source_x) >> 9)) & 0xff`. Draw it only if `(y & 1) ^ ((x >> 3) & 1)` is true; that gate creates the twinkle. Star color comes from its own 64-entry palette, not the 32-entry tile/sprite PROM. The `0x7004` register gates the whole pass; board flips and viewport cropping belong to composition. The star RGB palette is also deterministic: the two resistor bits for each channel produce MAME's derived channel levels `(0, 194, 214, 255)`. Red reads source bits 5/4, green 3/2, and blue 1/0, with each pair ordered as 150-ohm then 100-ohm contributions. A simplified but visually faithful recreation can precompute the same table, store a scroll offset, and increment it once per frame — this captures the actual "look" of the effect without needing to be a cycle-exact replica of the scanline-level gate. ## Bullets ("shells" and the enemy "missile") Bullets are **not** sprites — they're driven by a small dedicated 8-entry × 4-byte RAM block (`0x5860`-`0x587f`), of which only the Y (offset+1) and X (offset+3) bytes are used per entry: - Entries 0-6: player "shells" (rendered as a short white vertical streak). - Entry 7: the enemy "missile"/bomb (rendered yellow). In the board's unrotated 256×256 raster coordinates, derive the four-pixel-wide, one-pixel-high segment exactly as follows: ``` source_y = bulletram[offset + 1] source_x = bulletram[offset + 3] raster_y = 255 - source_y # slots 3–7; slots 0–2 use (256 - source_y) & 0xff raster_x = 251 - source_x # left edge after the hardware's four x decrements pixel_shape = ((1, 1, 1, 1),) ``` The physical portrait monitor rotates that logical horizontal segment, producing the vertical streak seen by the player. The shell color is `(239, 239, 239)` and the missile color is `(239, 239, 0)`. Board flip handling and conversion to the final 224-pixel visible viewport remain composition concerns, rather than descriptor decode. The board has a single shell counter and a single missile counter per scanline. When multiple shell entries match the same scanline, the highest-numbered matching shell entry replaces earlier ones; the missile is selected independently. This arbitration, and the first-three-slot one-scanline comparator offset, must be modeled before judging visual shot/enemy overlap against the ROM's collision decisions. ## Frame composition boundary The pure native-raster compositor draws black/enabled stars, transparent tiles, sprites, then bullets. Tile pen zero is transparent; sprites render from slot 7 down to slot 0 so lower-numbered slots retain line-buffer priority. It applies per-sprite flips followed by board X/Y flips, the 17-pixel sprite line-buffer edge clip, per-scanline shell/missile arbitration, and clipping to the 256×224 visible frame. A real hardware limitation worth reproducing for authenticity (and because it's the actual reason the player can only have one shot in flight): there is only one Y-match comparator for shells and one for missiles, so only one shell and one missile can visibly render on any given scanline even if more than one entry matches — the last matching entry wins. In practice this just means: enforce "one active player shot at a time" as a game-logic rule, which is simpler to implement directly than to model the scanline race condition.