//! A new title screen for the attract mode: a block logo drawn into the tile //! layer, a new title line, and the score table moved down to make room. All //! of it within the main CPU's ROMs (replacement EPROMs), in padding verified //! unused in play (docs/paths.md, the space budget). //! //! How (main ROM, Galaga rev. B; measured 2026-10-01, `tools/z80dis.py`): //! - `rst 0x30` prints message C through the routine at 0x13b5 (table of //! pointers at 0x13ef; a message is a tile-RAM position word, a colour and //! ASCII ending in '/', and the pointer names the colour byte). The title //! screen prints message 0x0e, "GALAGA". //! - The jump at 0x0032 (`jp 0x13b5`) is pointed at a hook in the padding at //! 0x0b0f-0x0ffe: on message 0x0e it draws the logo's runs of tiles, then //! prints the message as usual; message 0x0e is redirected to the new title //! line (in the padding at 0x160c-0x16ff). //! - The score table's messages 0x0f-0x11 get new positions, and the objects //! placed with it (its two icons and the showcase formation, records at //! 0x195c-0x1983: slot, code and colour, then two position bytes, the second //! halved) move down with them. //! - Each 4 KB section keeps its checksum through its pad's last byte. pub const RST30_JUMP: u16 = 0x0032; pub const STRING_OUT: u16 = 0x13b5; pub const MESSAGE_TABLE: u16 = 0x13ef; pub const TITLE_MESSAGE: u8 = 0x0e; pub const PAD0: std::ops::RangeInclusive = 0x0b0f..=0x0ffe; pub const PAD1: std::ops::RangeInclusive = 0x160c..=0x16ff; /// The score table's objects: its two icons (0x195c, 0x1960), then the /// showcase formation that flies in under it (eight records to 0x1980), 4 /// bytes each: slot, code and colour, two position bytes (the second halved). pub const SHOWCASE_RECORDS: std::ops::Range = 0x195c..0x1984; /// The solid block tile and the horizontal line tile. pub const SOLID: u8 = 0x25; /// One run of tiles: `count` cells from (row, col) to the right, all `code` /// in colour `colour`. Screen rows and columns as the cabinet shows them /// (28 columns; the tile RAM index is (row - 2) + 32 * (29 - col)). #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Run { pub row: u8, pub col: u8, pub count: u8, pub code: u8, pub colour: u8, } #[derive(Clone, Debug, Default)] pub struct TitleSpec { pub text: Option<(String, u8)>, pub text_row: u8, pub runs: Vec, /// Rows to move "— SCORE —" (message 0x0f) and the score lines (0x10, /// 0x11, with the icons) down by. pub score_shift: u8, pub table_shift: u8, } pub fn tile_address(row: u8, col: u8) -> u16 { 0x8000 + (u16::from(row) - 2) + 32 * (29 - u16::from(col)) } fn word(rom: &[u8], at: u16) -> u16 { u16::from(rom[usize::from(at)]) | u16::from(rom[usize::from(at) + 1]) << 8 } /// The hook and the logo data, assembled for `at` (hand-assembled Z80; the /// mnemonics are beside each byte group). fn hook(at: u16, runs: &[Run]) -> Vec { let [so_lo, so_hi] = STRING_OUT.to_le_bytes(); let mut code = vec![ 0x79, // ld a,c 0xfe, TITLE_MESSAGE, // cp 0x0e 0xc2, so_lo, so_hi, // jp nz,0x13b5 (any other message: as before) 0xe5, // push hl 0xd5, // push de 0xc5, // push bc 0x21, 0, 0, // ld hl,DATA (patched below) // loop: 0x5e, // ld e,(hl) tile address, low 0x23, // inc hl 0x56, // ld d,(hl) high; 0 ends the list 0x23, // inc hl 0x7a, // ld a,d 0xb7, // or a 0x28, 0, // jr z,done (patched below) 0x46, // ld b,(hl) count 0x23, // inc hl 0x7e, // ld a,(hl) tile code 0x23, // inc hl 0x4e, // ld c,(hl) colour 0x23, // inc hl // run: 0x12, // ld (de),a the tile 0xcb, 0xd2, // set 2,d its colour, 0x400 above 0xf5, // push af 0x79, // ld a,c 0x12, // ld (de),a 0xcb, 0x92, // res 2,d 0x7b, // ld a,e the next column: 0x20 lower 0xd6, 0x20, // sub 0x20 0x5f, // ld e,a 0x30, 0x01, // jr nc,+1 0x15, // dec d 0xf1, // pop af 0x10, 0, // djnz run (patched below) 0x18, 0, // jr loop (patched below) // done: 0xc1, // pop bc 0xd1, // pop de 0xe1, // pop hl 0xc3, so_lo, so_hi, // jp 0x13b5 then print the title line ]; let loop_at: usize = 12; let run_at: usize = 26; let jr_done: usize = 19; let djnz = code.len() - 9; let jr_loop = code.len() - 7; let done_at = code.len() - 6; code[jr_done] = (done_at as i32 - (jr_done as i32 + 1)) as i8 as u8; code[djnz] = (run_at as i32 - (djnz as i32 + 1)) as i8 as u8; code[jr_loop] = (loop_at as i32 - (jr_loop as i32 + 1)) as i8 as u8; let data = at + code.len() as u16; code[10..12].copy_from_slice(&data.to_le_bytes()); for r in runs { code.extend_from_slice(&tile_address(r.row, r.col).to_le_bytes()); code.extend_from_slice(&[r.count, r.code, r.colour]); } code.extend_from_slice(&[0, 0]); code } /// The changes to the main CPU's 16 KB program for `spec`, checked against /// what this ROM revision holds. pub fn runs(main_rom: &[u8], spec: &TitleSpec) -> Result)>, String> { let at = |a: u16| main_rom[usize::from(a)]; if main_rom[usize::from(RST30_JUMP)..usize::from(RST30_JUMP) + 3] != [0xc3, 0xb5, 0x13] { return Err("rst 0x30 does not jump to 0x13b5 here".into()); } let pad_free = |r: &std::ops::RangeInclusive| r.clone().all(|a| at(a) == 0xff); if !pad_free(&PAD0) || !pad_free(&PAD1) { return Err("the padding the title uses is not free in this ROM".into()); } let mut out = Vec::new(); // The hook and the logo. let hook_at = *PAD0.start(); let code = hook(hook_at, &spec.runs); if hook_at as usize + code.len() > *PAD0.end() as usize { return Err(format!( "the logo is too big for the padding ({} bytes)", code.len() )); } out.push((RST30_JUMP + 1, hook_at.to_le_bytes().to_vec())); out.push((hook_at, code)); // The title line: a new message 0x0e. if let Some((text, colour)) = &spec.text { if text.len() > 28 || text.contains('/') { return Err(format!( "the title line {text:?} is too long or contains '/'" )); } let col = (28 - text.len() as u8) / 2; let mut msg = tile_address(spec.text_row, col).to_le_bytes().to_vec(); msg.push(*colour); msg.extend_from_slice(text.as_bytes()); msg.push(b'/'); let msg_at = *PAD1.start() + 4; out.push((msg_at, msg)); let pointer = MESSAGE_TABLE + 2 * u16::from(TITLE_MESSAGE); out.push((pointer, (msg_at + 2).to_le_bytes().to_vec())); } // The score table and its icons, moved down. for (message, shift) in [ (0x0f, spec.score_shift), (0x10, spec.table_shift), (0x11, spec.table_shift), ] { if shift == 0 { continue; } let pos_at = word(main_rom, MESSAGE_TABLE + 2 * message) - 2; let pos = word(main_rom, pos_at) + u16::from(shift); out.push((pos_at, pos.to_le_bytes().to_vec())); } if spec.table_shift > 0 { if main_rom[usize::from(SHOWCASE_RECORDS.start)..usize::from(SHOWCASE_RECORDS.start) + 4] != [0x08, 0x1b, 0x44, 0x3a] { return Err("the score table's object records are not where expected".into()); } for record in SHOWCASE_RECORDS.step_by(4) { let y = at(record + 3) .checked_add(4 * spec.table_shift) .ok_or("an object moved off the screen")?; out.push((record + 3, vec![y])); } } // Checksums: each 4 KB section sums to 0; fix with its pad's last byte. let mut image = main_rom.to_vec(); for (a, b) in &out { image[usize::from(*a)..usize::from(*a) + b.len()].copy_from_slice(b); } for (section, fill) in [(0x0000usize, *PAD0.end()), (0x1000, *PAD1.end())] { let sum = image[section..section + 0x1000] .iter() .fold(0u8, |s, &b| s.wrapping_add(b)); if sum != 0 { let v = image[usize::from(fill)].wrapping_sub(sum); image[usize::from(fill)] = v; out.push((fill, vec![v])); } } Ok(out) }