// SPDX-License-Identifier: MIT //! MARIA's DMA against the measured totals ([HF], [SG] App. 3; tier 3), counted //! exactly on the board, and the seeded faults that must break them (the reference's //! tests/test_maria_dma.py). //! //! A cartridge that only loops (JMP to itself; NMI and IRQ point at an RTI) is //! started directly; the display list list and display lists are written into //! the board's RAM and MARIA's registers set, so the counts are the board's own //! HALT periods, one per 4 MARIA clocks the CPU was held. Per NTSC frame: 242 //! DMA lines ([GLD] sheet 17), zones start on the first DMA line, so 16-line //! zones end 15 times and 8-line zones 30 times among them, and the trailing edge //! of vertical blank adds its DLL fetch once. use std::cell::RefCell; use std::collections::BTreeMap; use std::rc::Rc; use a7800::cartridge::connector::Linear; use a7800::machine::NTSC; use a7800::system::{direct, System}; fn looping_rom() -> Vec { let mut rom = vec![0u8; 0x4000]; rom[0..3].copy_from_slice(&[0x4c, 0x00, 0xc0]); // $C000: JMP $C000 rom[3] = 0x40; // $C003: RTI rom[0x3FFA..0x4000].copy_from_slice(&[0x03, 0xc0, 0x00, 0xc0, 0x03, 0xc0]); rom } fn system_with(zone: u8, dli: bool, headers: &[u8], faults: &[&str]) -> System { let mut system = direct(Box::new(Linear::new(&looping_rom()).unwrap()), NTSC); let board = system.board_mut(); for f in faults { board.faults().add(f).unwrap(); } let ram = &mut board.ram; // index = address - $1800 for k in 0..32 { // DLL at $1800, every zone's list at $1900 ram[3 * k..3 * k + 3].copy_from_slice(&[(if dli { 0x80 } else { 0 }) | (zone - 1), 0x19, 0x00]); } ram[0x100..0x100 + headers.len()].copy_from_slice(headers); let maria = &mut board.maria; maria.write_register(0x0C, 0x18); // DPPH maria.write_register(0x10, 0x00); // DPPL maria.write_register(0x1C, 0x40); // CTRL: DMA on, 160A system } fn held_periods_per_frame(system: &mut System) -> u64 { system.run_frames(2); let before = system.board().halted_periods; system.run_frames(1); system.board().halted_periods - before } #[test] fn null_lists_16_a_line_24_on_zone_ends_8_a_frame() { assert_eq!( held_periods_per_frame(&mut system_with(16, false, &[], &[])) * 4, 242 * 16 + 15 * 8 + 8 ); } #[test] fn the_zone_end_costs_8_more() { assert_eq!( held_periods_per_frame(&mut system_with(8, false, &[], &[])) * 4, 242 * 16 + 30 * 8 + 8 ); } #[test] fn dlis_cost_no_dma() { assert_eq!( held_periods_per_frame(&mut system_with(16, true, &[], &[])), held_periods_per_frame(&mut system_with(16, false, &[], &[])) ); } // Four objects on every line: 4-byte header + 1 byte = 8 + 3; with 2 bytes 8 + 6; // 5-byte header + 1 byte = 10 + 3; indirect 10 + 6; indirect with CWIDTH 10 + 9. const H4: [u8; 4] = [0x00, 0x20 | 31, 0xE0, 0x10]; const H4W2: [u8; 4] = [0x00, 0x20 | 30, 0xE0, 0x10]; const H5: [u8; 5] = [0x00, 0x40, 0xE0, 0x20 | 31, 0x10]; const IND: [u8; 5] = [0x00, 0x60, 0x1A, 0x20 | 31, 0x10]; #[test] fn per_object_costs() { let cases: [(&[u8], u64, bool); 5] = [ (&H4, 11, false), (&H4W2, 14, false), (&H5, 13, false), (&IND, 16, false), (&IND, 19, true), ]; for (headers, per_object, cwidth) in cases { let four: Vec = headers.iter().copied().cycle().take(headers.len() * 4).collect(); let mut system = system_with(16, false, &four, &[]); if cwidth { system.board_mut().maria.write_register(0x1C, 0x50); } let per_line = 16 + 4 * per_object; // a multiple of 4 in every case: no rounding assert_eq!( held_periods_per_frame(&mut system) * 4, 242 * per_line + 15 * 8 + 8, "{headers:?}" ); } } /// Zones that fill the 242 DMA lines exactly (15 x 16 + 2), every entry with /// the DLI bit: entry 0 is read at VBLANK's end and entries 1-16 on zone ends, /// the last of them on the last DMA line (probes/dliprobe.s phase 5). With /// `only`, that entry alone has the bit (phase 2: `only=16`). fn dlis_per_frame(faults: &[&str], only: Option) -> usize { let mut system = system_with(16, true, &[], faults); let ram = &mut system.board_mut().ram; ram[3 * 15] = 0x80 | 1; // entry 15: 2 lines if let Some(only) = only { for k in 0..32 { if k != only { ram[3 * k] &= 0x7F; } } } let seen = Rc::new(RefCell::new(0usize)); let s = seen.clone(); system .board_mut() .bus_observers .push(Box::new(move |kind, address, _value, _clock| { if kind == 'R' && address == 0xFFFA { *s.borrow_mut() += 1; // the NMI vector fetch: one per DLI taken } })); system.run_frames(2); *seen.borrow_mut() = 0; system.run_frames(1); let n = *seen.borrow(); n } #[test] fn dli_from_every_entry_read_including_the_last_dma_line() { // [SG]: "following DMA on the last line of the PREVIOUS zone", no exception // for the frame's last line; C23 (MAME 0.285 and A7800 5.2 take 16). assert_eq!(dlis_per_frame(&[], None), 17); assert_eq!(dlis_per_frame(&["c23-mame"], None), 16); } #[test] fn last_line_dli_flag_survives_to_the_vbe_fetch() { // [GLD] sheet 17 (docs/gld-reading.md): the DLI latch is cleared only when a // line DMA starts, so the flag from the last DMA line raises the NMI after // that line's DMA and again after the next VBE fetch. assert_eq!(dlis_per_frame(&[], Some(16)), 2); assert_eq!(dlis_per_frame(&["dli-clear-on-nmi"], Some(16)), 1); // ProSystem NTSC assert_eq!(dlis_per_frame(&["c23-mame"], Some(16)), 0); // MAME, A7800 assert_eq!(dlis_per_frame(&[], Some(5)), 1); // a middle zone: once } #[test] fn seeded_fault_last_line_cost_dropped() { let mut system = system_with(16, false, &[], &["last-line-cost-dropped"]); assert_ne!(held_periods_per_frame(&mut system) * 4, 242 * 16 + 15 * 8 + 8); } #[test] fn end_of_list_is_byte_one_and_5f() { // Ninja Golf and Crossbow end lists with non-zero bytes (C5): $80 in byte 1. let terminator = [0x00, 0x80]; assert_eq!( held_periods_per_frame(&mut system_with(16, false, &terminator, &[])) * 4, 242 * 16 + 15 * 8 + 8 ); let mut faulty = system_with(16, false, &terminator, &["end-test-zero"]); assert_ne!(held_periods_per_frame(&mut faulty) * 4, 242 * 16 + 15 * 8 + 8); } /// CPU periods from HALT's release to the DLI's NMI request, for each DLI. fn nmi_delay(faults: &[&str]) -> Vec { let mut system = system_with(16, true, &[], faults); system.board_mut().nmi_delays = Some(Vec::new()); system.run_frames(2); system.board_mut().nmi_delays.take().unwrap() } #[test] fn dli_one_cycle_after_halt_is_released() { // [1702B] p. 14: "the NMI- pin will be asserted for one cpu cycle, one cycle // after HALT- is released": requested in the second CPU cycle after release. let delays = nmi_delay(&[]); assert!(!delays.is_empty() && delays.iter().all(|&d| d == 1)); assert!(nmi_delay(&["dli-early"]).iter().all(|&d| d == 0)); } #[test] fn line_ram_and_160a_pixels() { // One 4-byte header, 1 byte $55 of palette 1 at position 0: cells 0-3 = colour 1. let mut system = system_with(16, false, &[0x00, 0x20 | 31, 0xE1, 0x00], &[]); let rom_gfx = 0xE100 - 0xC000; { let board = system.board_mut(); let linear = board.cartridge.as_any_mut().downcast_mut::().unwrap(); for page in 0..16 { linear.rom[rom_gfx + page * 0x100] = 0x55; } board.maria.write_register(0x00, 0x00); // background board.maria.write_register(0x05, 0x1A); // P1C1 } // DMA switched on mid-frame runs the rest of frame 0 from MARIA's power-on // list pointer (whatever the bus holds); a 5-byte header read there can set // write mode 1, which persists. Start clean on the next frame. system.run_frames(1); system.board_mut().maria.write_mode = 0; let lines: Rc>>> = Rc::new(RefCell::new(BTreeMap::new())); let l = lines.clone(); system.board_mut().maria.on_line = Some(Box::new(move |line, pixels, _kill| { l.borrow_mut().insert(line, pixels.to_vec()); })); system.run_frames(1); let row = lines.borrow()[&100].clone(); assert_eq!(&row[107..115], &[0x1A; 8]); // 4 cells x 2 clocks assert_eq!(row[115], 0x00); }