// SPDX-License-Identifier: MIT //! HMOVE's window edges (the reference's tests/test_hmove_edges.py) and the picture //! rules the milestone 4 detectors found, as the die settled them //! (tests/test_picture_rules.py). mod common; use std::cell::RefCell; use std::collections::BTreeMap; use std::rc::Rc; use a2600::board::Board; use a2600::tia::TIA; use common::blank_board; const HMOVE: u16 = 0x2A; const RHB_END: u32 = 136; // the step HBLANK ends on, without and with the comb const LRHB_END: u32 = 152; /// (this line's, the next line's) step where HBLANK ends, for an HMOVE /// written on `cycle` of a line (cycle k writes on step 6k + 4). fn hblank_ends_after_hmove_on(cycle: u32) -> (i64, i64) { let mut board = blank_board(); let target = 6 * cycle + 4; while (board.tia.step_in_line + 6) % 456 != target { board.read_byte(0x1000); } let line = board.tia.line + if target < board.tia.step_in_line { 1 } else { 0 }; board.write_byte(HMOVE, 0); let tia = &mut board.tia; let mut ends: BTreeMap = BTreeMap::new(); let mut was_blank = true; while tia.line <= line + 1 { tia.step(None); if was_blank && !tia.hblank { ends.entry(tia.line).or_insert(tia.step_in_line); } was_blank = tia.hblank; } let get = |l: u64| ends.get(&l).map(|&s| s as i64).unwrap_or(-1); (get(line), get(line + 1)) } #[test] fn hmove_on_cycle_19_still_gets_the_comb() { assert_eq!(hblank_ends_after_hmove_on(19).0, LRHB_END as i64); } #[test] fn hmove_on_cycle_20_is_too_late_for_the_comb() { assert_eq!(hblank_ends_after_hmove_on(20).0, RHB_END as i64); } /// Towers' and Mott's trick: motion without the comb. #[test] fn hmove_on_cycle_73_is_cleared_by_the_wrap() { assert_eq!(hblank_ends_after_hmove_on(73).1, RHB_END as i64); } /// Seven consoles (C27); Sim2600's netlist clears it instead. #[test] fn hmove_on_cycle_74_wins_the_race_with_the_wrap() { assert_eq!(hblank_ends_after_hmove_on(74).1, LRHB_END as i64); } #[test] fn hmove_on_cycle_75_extends_the_next_line() { assert_eq!(hblank_ends_after_hmove_on(75).1, LRHB_END as i64); } /// A board whose finished lines (the 160 visible clocks) are collected. fn board_with_lines() -> (Board, Rc>>>) { let mut board = blank_board(); let lines = Rc::new(RefCell::new(Vec::new())); let sink = lines.clone(); board.tia.on_line = Some(Box::new(move |tia: &TIA| { sink.borrow_mut().push(tia.pixels[68..].to_vec()) })); (board, lines) } fn at_cycle(board: &mut Board, cycle: u32) { while (board.tia.step_in_line + 6) % 456 != 4 + 6 * cycle { board.read_byte(0x1000); } } fn lines_until(board: &mut Board, lines: &Rc>>>, more: usize) { let count = lines.borrow().len(); while lines.borrow().len() < count + more { board.read_byte(0x1000); } } /// The visible line after a playfield of PF1 = $0F, PF2 = 0 is set, with /// or without an HMOVE at the line's start (TIA.pixels, 160 clocks). fn playfield_line(hmove: bool) -> Vec { let (mut board, lines) = board_with_lines(); for (register, value) in [(0x08, 0x1E), (0x0E, 0x0F), (0x0F, 0x00)] { board.write_byte(register, value); // COLUPF, PF1, PF2 } at_cycle(&mut board, 0); // the next cycle is a line's cycle 0 if hmove { board.write_byte(HMOVE, 0); // the comb on this line } let count = lines.borrow().len(); lines_until(&mut board, &lines, 2); let line = lines.borrow()[count].clone(); line } /// C29: the playfield counter starts at RHB even when HBLANK lasts to /// LRHB (die trace hmove_playfield; Pitfall!'s trees). #[test] fn the_comb_hides_the_playfield_but_does_not_move_it() { let (plain, combed) = (playfield_line(false), playfield_line(true)); let lit: Vec = plain .iter() .enumerate() .filter(|(_, &v)| v == 0x1F) .map(|(x, _)| x) .collect(); let expected: Vec = (32..48).chain(112..128).collect(); // PF1 D3-D0, both halves assert_eq!(lit, expected); assert_eq!(combed[..8], [0; 8]); // blanked assert_eq!(combed[8..], plain[8..]); } const COLUP0: u16 = 0x06; const COLUP1: u16 = 0x07; const COLUPF: u16 = 0x08; const COLUBK: u16 = 0x09; const CTRLPF: u16 = 0x0A; const RESBL: u16 = 0x14; const ENABL: u16 = 0x1F; /// A visible line with the ball (4 clocks) in the left half, no playfield. fn ball_line(ctrlpf: u8) -> Vec { let (mut board, lines) = board_with_lines(); for (register, value) in [ (COLUP0, 0x1E), (COLUP1, 0x7C), (COLUPF, 0x4E), (COLUBK, 0x84), (CTRLPF, ctrlpf | 0x20), (ENABL, 0x02), ] { board.write_byte(register, value); } at_cycle(&mut board, 40); // cycle 40: the left half board.write_byte(RESBL, 0); lines_until(&mut board, &lines, 2); let line = lines.borrow().last().unwrap().clone(); line } /// C32: SCORE colours the playfield with the half's player colour, not /// the ball (die trace ball_score; Alpha Beam with Ernie). #[test] fn score_mode_leaves_the_ball_colupf() { let (plain, score) = (ball_line(0x00), ball_line(0x02)); let others: std::collections::BTreeSet = plain.iter().copied().filter(|&v| v != 0x85).collect(); assert_eq!(others.into_iter().collect::>(), [0x4F]); assert_eq!(score, plain); } const GRP0: u16 = 0x1B; const NUSIZ0: u16 = 0x04; const RESP0: u16 = 0x10; /// The first 16 visible clocks of the line after a 4x player ran past /// the end of the line before, with or without an HMOVE on cycle 2. fn wrapped_tail(hmove: bool) -> Vec { let (mut board, lines) = board_with_lines(); for (register, value) in [(COLUP0, 0x0E), (COLUBK, 0x84), (GRP0, 0xFF), (NUSIZ0, 0x07)] { board.write_byte(register, value); } at_cycle(&mut board, 66); board.write_byte(RESP0, 0); // the copy starts near clock 135 from the next line lines_until(&mut board, &lines, 2); // the line the copy runs past the end of at_cycle(&mut board, 2); if hmove { board.write_byte(HMOVE, 0); // HMP0 is 0: the copy keeps its place } lines_until(&mut board, &lines, 1); let line = lines.borrow().last().unwrap()[..16].to_vec(); line } /// C34: under HMOVE's comb the stuffed pulses shift out a stretched /// player's wrapped pixels instead of leaving them for after the comb /// (Towers; die trace hmove_wrap; Sea Hawk). #[test] fn hmove_gobbles_the_wrapped_tail() { let (plain, moved) = (wrapped_tail(false), wrapped_tail(true)); assert_eq!(plain[..11], [0x0F; 11]); // the tail drawn from the line's start assert_eq!(moved[..8], [0; 8]); // the comb assert_eq!(moved[8..], plain[8..]); // its eight clocks ate the tail's first eight pixels } const NUSIZ1: u16 = 0x05; const RESP1: u16 = 0x11; const GRP1: u16 = 0x1C; const ENAM1: u16 = 0x1E; const RESMP1: u16 = 0x29; /// Player 1's visible clocks while missile 1 is locked to it, then the /// missile's alone after RESMP1 lets it go on cycle 26 and GRP1 is cleared. fn released_missile(nusiz: u8) -> (Vec, Vec) { let (mut board, lines) = board_with_lines(); for (register, value) in [ (COLUP1, 0x0E), (COLUBK, 0x84), (GRP1, 0xFF), (ENAM1, 0x02), (NUSIZ1, nusiz), ] { board.write_byte(register, value); } let next_line = |board: &mut Board| -> Vec { lines_until(board, &lines, 1); let line = lines.borrow().last().unwrap().clone(); line }; at_cycle(&mut board, 47); board.write_byte(RESP1, 0); board.write_byte(RESMP1, 0x02); next_line(&mut board); let locked = next_line(&mut board); at_cycle(&mut board, 26); board.write_byte(RESMP1, 0x00); board.write_byte(GRP1, 0x00); next_line(&mut board); let alone = next_line(&mut board); let drawn = 0x0F; let at = |line: &[u8]| { line.iter() .enumerate() .filter(|(_, &v)| v == drawn) .map(|(i, _)| i) .collect() }; (at(&locked), at(&alone)) } /// C38: RESMP holds the missile from the step its player's scan counter /// shows 1, with no delay, so a released missile starts four clocks into a /// 1x player and six into a 2x one (die trace resmp_release; Frostbite). #[test] fn a_released_missile_starts_where_the_die_puts_it() { let (player, missile) = released_missile(0x30); // 1x player, 8-clock missile assert_eq!(missile, (player[0] + 4..player[0] + 12).collect::>()); let (player, missile) = released_missile(0x05); // 2x player, 1-clock missile assert_eq!(missile, vec![player[0] + 6]); }