//! Galaxian's video hardware, transcribed from galemu's `video.py`, //! `render.py` and the composing half of `display.py`: palette and star //! palette decode, tile and sprite decode, the LFSR starfield, and frame //! composition in the verified layer order (stars, tiles, sprites from slot //! 7 down, bullets), with per-sprite flips, the board flips, the 17-pixel //! sprite edge clip, and the one-shell-per-scanline arbitration. pub type Rgb = [u8; 3]; pub const NATIVE_WIDTH: usize = 256; pub const NATIVE_HEIGHT: usize = 224; pub const VISIBLE_Y_START: i32 = 16; const GFX_PLANE_SIZE: usize = 0x800; const STARFIELD_STAR_COUNT: usize = 252; const STAR_RGB_MAXIMUM: i32 = 224; const BULLET_WHITE: Rgb = [0xEF, 0xEF, 0xEF]; const BULLET_YELLOW: Rgb = [0xEF, 0xEF, 0x00]; const BLACK: Rgb = [0, 0, 0]; pub fn decode_palette_entry(value: u8) -> Rgb { let bit = |n: u8| u32::from((value >> n) & 1); let red = 0x21 * bit(0) + 0x47 * bit(1) + 0x97 * bit(2); let green = 0x21 * bit(3) + 0x47 * bit(4) + 0x97 * bit(5); let blue = 0x55 * bit(6) + 0xAA * bit(7); [red as u8, green as u8, blue as u8] } pub fn decode_palette_prom(prom: &[u8]) -> Result<[Rgb; 32], String> { if prom.len() != 32 { return Err(format!( "expected a 32-byte palette PROM, got {} bytes", prom.len() )); } let mut palette = [BLACK; 32]; for (entry, &value) in palette.iter_mut().zip(prom) { *entry = decode_palette_entry(value); } Ok(palette) } /// The 64-entry star palette from the generator's resistor network /// (`build_star_palette`). pub fn build_star_palette() -> [Rgb; 64] { let minimum = STAR_RGB_MAXIMUM * 130 / 150; let middle = STAR_RGB_MAXIMUM * 130 / 100; let maximum = STAR_RGB_MAXIMUM * 130 / 60; let levels = [ 0, minimum, minimum + (255 - minimum) * (middle - minimum) / (maximum - minimum), 255, ]; let mut palette = [BLACK; 64]; for (color, entry) in palette.iter_mut().enumerate() { let color = color as i32; let level = |hi: i32, lo: i32| { levels[(((color >> hi) & 1) << 1 | ((color >> lo) & 1)) as usize] as u8 }; *entry = [level(4, 5), level(2, 3), level(0, 1)]; } palette } /// 256 tiles of 8x8 2bpp pixels, row-major, one byte per pixel (0..=3). pub fn decode_tiles(gfx_rom: &[u8]) -> Result, String> { validate_gfx_rom(gfx_rom)?; Ok((0..256) .map(|code| decode_image::<64>(gfx_rom, code * 8, 8, 8)) .collect()) } /// 64 sprites of 16x16 2bpp pixels. pub fn decode_sprites(gfx_rom: &[u8]) -> Result, String> { validate_gfx_rom(gfx_rom)?; Ok((0..64) .map(|code| decode_image::<256>(gfx_rom, code * 32, 16, 16)) .collect()) } fn validate_gfx_rom(gfx_rom: &[u8]) -> Result<(), String> { if gfx_rom.len() != 0x1000 { return Err(format!( "expected a 4096-byte graphics ROM, got {} bytes", gfx_rom.len() )); } Ok(()) } fn decode_image( gfx_rom: &[u8], offset: usize, width: usize, height: usize, ) -> [u8; N] { let mut pixels = [0u8; N]; for y in 0..height { for x in 0..width { let byte_offset = offset + (y & 7) + ((y >> 3) * 16) + ((x >> 3) * 8); let shift = 7 - (x & 7); let plane_0 = (gfx_rom[byte_offset] >> shift) & 1; let plane_1 = (gfx_rom[GFX_PLANE_SIZE + byte_offset] >> shift) & 1; pixels[y * width + x] = plane_0 | (plane_1 << 1); } } pixels } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Star { pub x: i32, pub y: i32, pub color: u8, } /// The 252 source stars of the 17-bit LFSR (`build_starfield`). pub fn build_starfield() -> Vec { let mut generator: u32 = 0; let mut stars = Vec::with_capacity(STARFIELD_STAR_COUNT); for y in 0..256 { for x in 0..512 { let feedback = ((!generator >> 16) & 1) ^ ((generator >> 4) & 1); generator = (generator << 1) | feedback; if (!generator >> 16) & 1 == 1 && (generator & 0xFF) == 0xFF { let color = ((!generator >> 8) & 0x3F) as u8; if color != 0 { stars.push(Star { x, y, color }); } } } } debug_assert_eq!(stars.len(), STARFIELD_STAR_COUNT); stars } /// Everything decoded once from the ROMs. pub struct Assets { pub palette: [Rgb; 32], pub star_palette: [Rgb; 64], pub tiles: Vec<[u8; 64]>, pub sprites: Vec<[u8; 256]>, pub starfield: Vec, } impl Assets { pub fn new(graphics: &[u8], palette_prom: &[u8]) -> Result { Ok(Assets { palette: decode_palette_prom(palette_prom)?, star_palette: build_star_palette(), tiles: decode_tiles(graphics)?, sprites: decode_sprites(graphics)?, starfield: build_starfield(), }) } } /// What the compositor needs from the board at one instant. pub struct View<'a> { pub video_ram: &'a [u8], pub object_ram: &'a [u8], pub starfield_enabled: bool, pub flip_x: bool, pub flip_y: bool, /// The starfield's scroll position: the board's vblank count. pub scroll: u32, } /// A native 256x224 frame, RGB, row-major (`compose_frame` in galemu). pub struct Frame { pub rgb: Vec, transparent: Vec, } impl Default for Frame { fn default() -> Self { Self::new() } } impl Frame { pub fn new() -> Self { Frame { rgb: vec![0; NATIVE_WIDTH * NATIVE_HEIGHT * 3], transparent: vec![true; NATIVE_WIDTH * NATIVE_HEIGHT], } } fn put(&mut self, x: i32, y: i32, color: Rgb, flip_x: bool, flip_y: bool) { if (0..NATIVE_WIDTH as i32).contains(&x) && (0..NATIVE_HEIGHT as i32).contains(&y) { let tx = if flip_x { NATIVE_WIDTH as i32 - 1 - x } else { x } as usize; let ty = if flip_y { NATIVE_HEIGHT as i32 - 1 - y } else { y } as usize; self.rgb[(ty * NATIVE_WIDTH + tx) * 3..][..3].copy_from_slice(&color); } } /// Compose the frame: black, stars where tiles are transparent, tiles, /// sprites from slot 7 down to 0, then bullets. pub fn compose(&mut self, assets: &Assets, view: &View) { let tile_ram = &view.video_ram[..0x400]; let attribute_ram = &view.object_ram[..0x40]; let sprite_ram = &view.object_ram[0x40..0x60]; let bullet_ram = &view.object_ram[0x60..0x80]; // Tiles first into a palette-index layer (compose_tilemap), which also // gives the transparency mask the stars need. self.rgb.fill(0); for visible_y in 0..NATIVE_HEIGHT { for column in 0..32usize { let source_y = (visible_y as i32 + VISIBLE_Y_START + i32::from(attribute_ram[column * 2])) & 0xFF; let tile_row = (source_y >> 3) as usize; let pixel_y = (source_y & 7) as usize; let tile = &assets.tiles[usize::from(tile_ram[tile_row * 32 + column])]; let color_base = usize::from(attribute_ram[column * 2 + 1] & 0x07) * 4; for pixel_x in 0..8usize { let pixel = tile[pixel_y * 8 + pixel_x]; let index = visible_y * NATIVE_WIDTH + column * 8 + pixel_x; self.transparent[index] = pixel == 0; if pixel != 0 { let x = (column * 8 + pixel_x) as i32; self.put( x, visible_y as i32, assets.palette[color_base + usize::from(pixel)], view.flip_x, view.flip_y, ); } } } } if view.starfield_enabled { let p = view.scroll as i32; for star in &assets.starfield { let x = ((star.x + p) & 0x1FF) >> 1; let y = (star.y + ((p + star.x) >> 9)) & 0xFF; if (y & 1) ^ ((x >> 3) & 1) == 0 { continue; } let y = y - VISIBLE_Y_START; if (0..NATIVE_WIDTH as i32).contains(&x) && (0..NATIVE_HEIGHT as i32).contains(&y) && self.transparent[y as usize * NATIVE_WIDTH + x as usize] { self.put( x, y, assets.star_palette[usize::from(star.color)], view.flip_x, view.flip_y, ); } } } for slot in (0..8usize).rev() { let entry = &sprite_ram[slot * 4..slot * 4 + 4]; let code = usize::from(entry[1] & 0x3F); let sprite_x = i32::from(entry[3]) + 1; let sprite_y = 240 - (i32::from(entry[0]) - i32::from(slot < 3)) - VISIBLE_Y_START; let color_group = usize::from(entry[2] & 0x07); let sprite_flip_x = entry[1] & 0x40 != 0; let sprite_flip_y = entry[1] & 0x80 != 0; let pixels = &assets.sprites[code]; for pixel_y in 0..16i32 { for pixel_x in 0..16i32 { let screen_x = sprite_x + pixel_x; let visible = if view.flip_x { screen_x <= 238 } else { screen_x >= 17 }; if !visible { continue; } let source_x = if sprite_flip_x { 15 - pixel_x } else { pixel_x } as usize; let source_y = if sprite_flip_y { 15 - pixel_y } else { pixel_y } as usize; let pixel = pixels[source_y * 16 + source_x]; if pixel != 0 { self.put( screen_x, sprite_y + pixel_y, assets.palette[color_group * 4 + usize::from(pixel)], view.flip_x, view.flip_y, ); } } } } // Shells: one per scanline, the highest matching slot wins; slots 0-2 // compare against V-1. Then the missile, selected independently. let mut shells: Vec<(i32, i32)> = Vec::with_capacity(7); // (y, x), insertion order like the dict for slot in 0..7usize { let entry = &bullet_ram[slot * 4..slot * 4 + 4]; let y = if slot < 3 { (256 - i32::from(entry[1])) & 0xFF } else { 255 - i32::from(entry[1]) }; let x = 251 - i32::from(entry[3]); match shells.iter_mut().find(|(sy, _)| *sy == y) { Some(existing) => existing.1 = x, None => shells.push((y, x)), } } for (y, x) in shells { for dx in 0..4 { self.put( x + dx, y - VISIBLE_Y_START, BULLET_WHITE, view.flip_x, view.flip_y, ); } } let missile = &bullet_ram[7 * 4..7 * 4 + 4]; let (mx, my) = (251 - i32::from(missile[3]), 255 - i32::from(missile[1])); for dx in 0..4 { self.put( mx + dx, my - VISIBLE_Y_START, BULLET_YELLOW, view.flip_x, view.flip_y, ); } } /// The frame rotated for the portrait monitor (pygame `rotate(-90)`, /// clockwise) as 224x256 RGBA, ready for a canvas. pub fn portrait_rgba(&self, out: &mut [u8]) { debug_assert_eq!(out.len(), NATIVE_WIDTH * NATIVE_HEIGHT * 4); let (w, h) = (NATIVE_HEIGHT, NATIVE_WIDTH); // portrait 224 wide, 256 tall for y in 0..NATIVE_HEIGHT { for x in 0..NATIVE_WIDTH { let src = (y * NATIVE_WIDTH + x) * 3; let (px, py) = (w - 1 - y, x); let dst = (py * w + px) * 4; let _ = h; out[dst..dst + 3].copy_from_slice(&self.rgb[src..src + 3]); out[dst + 3] = 255; } } } }