//! The video pipeline (`docs/video-hardware.md`, `docs/gfx-bitplane-spec.md`): //! the 36x28 tilemap with Namco's sideways side columns, 64 sprites across //! the three work RAMs, the palette and lookup PROMs, and the 05xx starfield //! generator, drawn in MAME's order (black, stars, sprites, tiles) into a //! 288x224 raster of palette indices, then rotated for the cabinet's //! portrait monitor. //! //! `render` must run once per frame whether or not the frame is shown: the //! starfield's LFSR advances as a side effect of drawing, exactly 65,536 + //! dx steps per frame, and its position is that stream. use super::bus::Shared; use super::roms::RomSet; pub const RASTER_WIDTH: usize = 288; pub const RASTER_HEIGHT: usize = 224; /// The cabinet's view: the raster rotated 90 degrees clockwise. pub const SCREEN_WIDTH: usize = 224; pub const SCREEN_HEIGHT: usize = 288; /// Palette index of "nothing drawn": MAME's black pen, not entry 0. pub const BLACK: u8 = 0xff; pub const STARS_COLOR_BASE: u8 = 32; /// 3-bit red/green and 2-bit blue levels from the palette PROM's resistor /// ladder (1 kΩ, 470 Ω, 220 Ω; 470 Ω, 220 Ω). MAME scales the three /// networks by one factor, so blue's two legs reach 222, not 255 (checked /// against MAME's screenshots 2026-09-11; `docs/video-hardware.md` had 255). const RG_LEVELS: [u8; 8] = [0, 33, 71, 104, 151, 184, 222, 255]; const B_LEVELS: [u8; 4] = [0, 71, 151, 222]; /// The star colours: two legs per channel, the same four levels on all three. const STAR_LEVELS: [u8; 4] = [0, 71, 151, 222]; const LFSR_SEED: u16 = 0x7fff; const LFSR_HIT_MASK: u16 = 0xfa14; const LFSR_HIT_VALUE: u16 = 0x7800; const LFSR_CYCLES_PER_LINE: i32 = 256; const SPEED_X_OFFSET: [i32; 8] = [0, 1, 2, 3, -4, -3, -2, -1]; const STARFIELD_X_OFFSET: usize = 16; const STARFIELD_PIXEL_WIDTH: usize = 256; const STARFIELD_X_LIMIT: usize = STARFIELD_X_OFFSET + 256; /// MAME's `starfield_05xx_device` for Galaga's wiring (SCROLL_Y grounded). #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct Starfield { pub enabled: bool, pub lfsr: u16, pre_vis_cycles: i32, post_vis_cycles: i32, set_a: u8, set_b: u8, } impl Starfield { pub fn new() -> Starfield { Starfield { enabled: false, lfsr: LFSR_SEED, pre_vis_cycles: 0, post_vis_cycles: 0, set_a: 0, set_b: 0, } } /// `screen_vblank_galaga` at the end of vblank: the video latch's Q0-Q5. pub fn latch(&mut self, videolatch: u8) { let index_x = usize::from(videolatch & 7); self.pre_vis_cycles = 22 * LFSR_CYCLES_PER_LINE + SPEED_X_OFFSET[index_x]; self.post_vis_cycles = 10 * LFSR_CYCLES_PER_LINE; self.set_a = (videolatch >> 3) & 1; self.set_b = ((videolatch >> 4) & 1) | 2; let on = videolatch & 0x20 != 0; if !on { self.lfsr = LFSR_SEED; } self.enabled = on; } #[inline] fn step(lfsr: u16) -> u16 { let bit = (lfsr ^ (lfsr >> 3) ^ (lfsr >> 5) ^ (lfsr >> 10)) & 1; (lfsr >> 1) | (bit << 15) } /// `draw_starfield` with flip = 0. fn draw(&mut self, raster: &mut [u8]) { if !self.enabled { return; } let mut lfsr = self.lfsr; let mut n = self.pre_vis_cycles; loop { lfsr = Self::step(lfsr); n -= 1; if n == 0 { break; } } for y in 0..RASTER_HEIGHT { for x in STARFIELD_X_OFFSET..STARFIELD_X_OFFSET + STARFIELD_PIXEL_WIDTH { if lfsr & LFSR_HIT_MASK == LFSR_HIT_VALUE { let star_set = (((lfsr >> 10) & 1) << 1 | ((lfsr >> 8) & 1)) as u8; if (star_set == self.set_a || star_set == self.set_b) && x < STARFIELD_X_LIMIT { let mut color = ((lfsr >> 5) & 0x7) | ((lfsr << 3) & 0x18) | ((lfsr << 2) & 0x20); color = !color & 0x3f; raster[y * RASTER_WIDTH + x] = STARS_COLOR_BASE + color as u8; } } lfsr = Self::step(lfsr); } } let mut n = self.post_vis_cycles; loop { lfsr = Self::step(lfsr); n -= 1; if n == 0 { break; } } self.lfsr = lfsr; } } impl Default for Starfield { fn default() -> Self { Starfield::new() } } pub struct Video { /// RGB of palette entries 0-31 (sprites 0-15, characters 16-31) and the /// 64 star colours at 32-95. pub rgb: [[u8; 3]; 96], char_lookup: [u8; 256], sprite_lookup: [u8; 256], /// 256 characters x 64 pixel values. chars: Vec, /// 128 sprites x 256 pixel values. sprites: Vec, pub starfield: Starfield, /// 288x224 palette indices, `BLACK` where nothing was drawn. pub raster: Vec, } impl Video { pub fn new(roms: &RomSet) -> Video { let mut rgb = [[0u8; 3]; 96]; for (i, entry) in rgb.iter_mut().enumerate().take(32) { let byte = roms.proms[i]; *entry = [ RG_LEVELS[usize::from(byte & 7)], RG_LEVELS[usize::from((byte >> 3) & 7)], B_LEVELS[usize::from((byte >> 6) & 3)], ]; } for (i, entry) in rgb.iter_mut().enumerate().skip(32) { let color = (i - 32) as u8; *entry = [ STAR_LEVELS[usize::from(color & 3)], STAR_LEVELS[usize::from((color >> 2) & 3)], STAR_LEVELS[usize::from((color >> 4) & 3)], ]; } let mut char_lookup = [0u8; 256]; let mut sprite_lookup = [0u8; 256]; char_lookup.copy_from_slice(&roms.proms[0x20..0x120]); sprite_lookup.copy_from_slice(&roms.proms[0x120..0x220]); for v in char_lookup.iter_mut().chain(sprite_lookup.iter_mut()) { *v &= 0x0f; } // `charlayout_2bpp`: 16 bytes per character, right half first. let mut chars = vec![0u8; 256 * 64]; for c in 0..256 { for y in 0..8 { for x in 0..8 { let byte = roms.chars[16 * c + y + 8 * (1 - x / 4)]; chars[c * 64 + y * 8 + x] = pixel(byte, x % 4); } } } // `spritelayout_galaga`: 64 bytes per sprite, four 8-byte strips per // half, leftmost strip first. let mut sprites = vec![0u8; 128 * 256]; for s in 0..128 { for y in 0..16 { for x in 0..16 { let byte = roms.sprites[64 * s + 32 * (y / 8) + 8 * (x / 4) + (y % 8)]; sprites[s * 256 + y * 16 + x] = pixel(byte, x % 4); } } } Video { rgb, char_lookup, sprite_lookup, chars, sprites, starfield: Starfield::new(), raster: vec![BLACK; RASTER_WIDTH * RASTER_HEIGHT], } } /// Remix: replace one sprite's picture with 64 bytes in the sprite ROM's /// own layout, as a replacement sprite EPROM would hold them. pub fn set_sprite(&mut self, code: usize, rom_bytes: &[u8]) { assert_eq!(rom_bytes.len(), 64, "a sprite is 64 bytes"); for y in 0..16 { for x in 0..16 { let byte = rom_bytes[32 * (y / 8) + 8 * (x / 4) + (y % 8)]; self.sprites[(code & 0x7f) * 256 + y * 16 + x] = pixel(byte, x % 4); } } } /// Replace tile `code` with 16 bytes in the tile ROM's layout /// (`charlayout_2bpp`, as `Video::new` decodes it). pub fn set_tile(&mut self, code: usize, rom_bytes: &[u8]) { assert_eq!(rom_bytes.len(), 16, "a tile is 16 bytes"); for y in 0..8 { for x in 0..8 { let byte = rom_bytes[y + 8 * (1 - x / 4)]; self.chars[(code & 0xff) * 64 + y * 8 + x] = pixel(byte, x % 4); } } } /// One frame: `screen_update_galaga`. `videolatch_at_vblank_end` is the /// video latch as it stood at the end of this frame's vblank, which is /// when the starfield samples it. pub fn render(&mut self, shared: &Shared, videolatch_at_vblank_end: u8) { self.starfield.latch(videolatch_at_vblank_end); self.raster.fill(BLACK); self.starfield.draw(&mut self.raster); let flip = shared.videolatch & 0x80 != 0; self.draw_sprites(shared, flip); self.draw_tiles(shared, flip); } fn draw_sprites(&mut self, shared: &Shared, flip: bool) { for n in 0..64 { self.draw_sprite(shared, n, flip, None); } } /// Sprite `n` at its own position, or with its top-left raster corner /// at `origin` (the remix's per-sprite atlas). fn draw_sprite(&mut self, shared: &Shared, n: usize, flip: bool, origin: Option<(i32, i32)>) { const CELL_CODE: [[u8; 2]; 2] = [[0, 1], [2, 3]]; let o = 0x380 + 2 * n; let code = shared.ram1[o] & 0x7f; let color = usize::from(shared.ram1[o + 1] & 0x3f); let y = i32::from(shared.ram2[o]); let x_low = i32::from(shared.ram2[o + 1]); let flags = shared.ram3[o]; let x_high = i32::from(shared.ram3[o + 1] & 3); let mut flip_x = flags & 1 != 0; let mut flip_y = flags & 2 != 0; let double_w = usize::from((flags >> 2) & 1); let double_h = usize::from((flags >> 3) & 1); if flip { flip_x = !flip_x; flip_y = !flip_y; } let (sx, sy) = origin.unwrap_or(( x_low + 256 * x_high - 40, ((256 - y + 1 - 16 * double_h as i32) & 0xff) - 32, )); for j in 0..=double_h { for i in 0..=double_w { let cell = CELL_CODE[j ^ (double_h & usize::from(flip_y))] [i ^ (double_w & usize::from(flip_x))]; let sprite = usize::from(code.wrapping_add(cell) & 0x7f); self.draw_cell( sprite, color, sx + 16 * i as i32, sy + 16 * j as i32, flip_x, flip_y, ); } } } /// The remix's layers, drawn by the same code as `render`: every /// sprite slot's picture in cabinet orientation, in a 256x256 RGBA atlas /// of 32x32 cells (slot n at cell (n % 8, n / 8), anchored top-left, /// transparent where the sprite is), and the tile layer as 224x288 RGBA, /// transparent where no tile pixel is. Placing each cell at its /// `objects::Object` box in slot order and the tiles over them gives /// `render`'s frame without the stars (`tests/layers.rs`). The flip bit /// is applied as `render` applies it (the game sets it only at the end /// of the power-on test; it mirrors pictures, not sprite positions). /// Leaves the raster /// holding the tile layer. pub fn render_layers(&mut self, shared: &Shared, atlas: &mut [u8], tiles: &mut [u8]) { let flip = shared.videolatch & 0x80 != 0; debug_assert_eq!(atlas.len(), 256 * 256 * 4); debug_assert_eq!(tiles.len(), SCREEN_WIDTH * SCREEN_HEIGHT * 4); atlas.fill(0); for n in 0..64 { let flags = shared.ram3[0x380 + 2 * n]; // Raster extent: x across double width, y across double height. let rw = 16 * (1 + usize::from((flags >> 2) & 1)); let rh = 16 * (1 + usize::from((flags >> 3) & 1)); for row in self.raster[..rh * RASTER_WIDTH].chunks_mut(RASTER_WIDTH) { row[..rw].fill(BLACK); } self.draw_sprite(shared, n, flip, Some((0, 0))); let (cx, cy) = (32 * (n % 8), 32 * (n / 8)); for ry in 0..rh { for rx in 0..rw { let index = self.raster[ry * RASTER_WIDTH + rx]; if index == BLACK { continue; } // Screen (u, v) in the box = (w - 1 - raster y, raster x). let (u, v) = (rh - 1 - ry, rx); let a = ((cy + v) * 256 + cx + u) * 4; atlas[a..a + 3].copy_from_slice(&self.rgb[usize::from(index)]); atlas[a + 3] = 0xff; } } } self.raster.fill(BLACK); self.draw_tiles(shared, flip); self.screen_rgba(tiles); for sy in 0..SCREEN_HEIGHT { for sx in 0..SCREEN_WIDTH { if self.raster[(SCREEN_WIDTH - 1 - sx) * RASTER_WIDTH + sy] == BLACK { tiles[(sy * SCREEN_WIDTH + sx) * 4 + 3] = 0; } } } } fn draw_cell( &mut self, sprite: usize, color: usize, sx: i32, sy: i32, flip_x: bool, flip_y: bool, ) { for py in 0..16i32 { let ry = sy + py; if ry < 0 || ry >= RASTER_HEIGHT as i32 { continue; } let src_y = if flip_y { 15 - py } else { py } as usize; for px in 0..16i32 { let rx = sx + px; if rx < 0 || rx >= RASTER_WIDTH as i32 { continue; } let src_x = if flip_x { 15 - px } else { px } as usize; let p = self.sprites[sprite * 256 + src_y * 16 + src_x]; let nibble = self.sprite_lookup[color * 4 + usize::from(p)]; if nibble == 0x0f { continue; } self.raster[ry as usize * RASTER_WIDTH + rx as usize] = nibble; } } } fn draw_tiles(&mut self, shared: &Shared, flip: bool) { for c in 0..36usize { for r in 0..28usize { let index = if (2..=33).contains(&c) { (c - 2) + 32 * (r + 2) } else { (r + 2) + 32 * ((c.wrapping_sub(2)) & 0x1f) }; let code = usize::from(shared.video_ram[index] & 0x7f); let color = usize::from(shared.video_ram[0x400 + index] & 0x3f); let (cx, cy) = if flip { (35 - c, 27 - r) } else { (c, r) }; for y in 0..8 { for x in 0..8 { let (src_x, src_y) = if flip { (7 - x, 7 - y) } else { (x, y) }; let p = self.chars[code * 64 + src_y * 8 + src_x]; let nibble = self.char_lookup[color * 4 + usize::from(p)]; if nibble == 0x0f { continue; } self.raster[(cy * 8 + y) * RASTER_WIDTH + cx * 8 + x] = 16 + nibble; } } } } } /// The raster as the cabinet shows it: 224x288 RGBA, raster (x, y) at /// screen (223 - y, x). pub fn screen_rgba(&self, out: &mut [u8]) { debug_assert_eq!(out.len(), SCREEN_WIDTH * SCREEN_HEIGHT * 4); for sy in 0..SCREEN_HEIGHT { for sx in 0..SCREEN_WIDTH { let index = self.raster[(SCREEN_WIDTH - 1 - sx) * RASTER_WIDTH + sy]; let rgb = if index == BLACK { [0, 0, 0] } else { self.rgb[usize::from(index)] }; let o = (sy * SCREEN_WIDTH + sx) * 4; out[o..o + 3].copy_from_slice(&rgb); out[o + 3] = 0xff; } } } } /// Pixel value of column `k` (0-3) of a 4-pixel group byte: high nibble is /// bit 1, low nibble bit 0, most significant bit leftmost. #[inline] fn pixel(byte: u8, k: usize) -> u8 { 2 * ((byte >> (7 - k)) & 1) + ((byte >> (3 - k)) & 1) }