// SPDX-License-Identifier: MIT //! Milestone 5's schemes, rule by rule, as their documents state them (the //! reference's tests/test_schemes.py). use std::collections::BTreeSet; use a2600::cartridge::connector::Cartridge; use a2600::cartridge::dpc::{DPC, MUSIC_HZ, NTSC_CPU_HZ}; use a2600::cartridge::licensed::{ ActivisionFE, CommaVid, MNetworkE7, MegaboyF0, ThirtyTwoInOne, Tigervision3F, E0, UA, }; use a2600::cartridge::supercharger::{parse_load, Supercharger}; /// Every `unit`-byte page filled with its own index. fn tagged(size: usize, unit: usize) -> Vec { (0..size).map(|i| ((i / unit) & 0xFF) as u8).collect() } fn read(cart: &mut dyn Cartridge, address: u16) -> Option { cart.access(address, 0, 0, false) } #[test] fn e0_slices_and_fixed_last() { let mut cart = E0::new(&tagged(0x2000, 0x400), (4, 5, 6)).unwrap(); let got: Vec<_> = [0x1000, 0x1400, 0x1800, 0x1C00] .iter() .map(|&a| read(&mut cart, a).unwrap()) .collect(); assert_eq!(got, [4, 5, 6, 7]); read(&mut cart, 0x1FE1); // slice 1 into $1000 read(&mut cart, 0x1FEA); // slice 2 into $1400 read(&mut cart, 0x1FF3); // slice 3 into $1800 let got: Vec<_> = [0x1000, 0x1400, 0x1800, 0x1C00] .iter() .map(|&a| read(&mut cart, a).unwrap()) .collect(); assert_eq!(got, [1, 2, 3, 7]); } #[test] fn _3f_latches_on_writes_below_40_only() { let mut cart = Tigervision3F::new(&tagged(0x2000, 0x800), None).unwrap(); assert!(read(&mut cart, 0x1000) == Some(3) && read(&mut cart, 0x1800) == Some(3)); cart.access(0x003F, 0x01, 0, true); assert!(read(&mut cart, 0x1000) == Some(1) && read(&mut cart, 0x1FFF) == Some(3)); cart.access(0x003F, 0x02, 0, false); // a read with $02 floating: no latch cart.access(0x0040, 0x02, 0, true); // a TIA mirror above $3F: no latch assert_eq!(read(&mut cart, 0x1000), Some(1)); } #[test] fn fe_takes_d5_of_the_byte_after_01fe() { let mut cart = ActivisionFE::new(&tagged(0x2000, 0x1000), 0).unwrap(); assert_eq!(cart.access(0x1000, 0, 0, false), Some(0)); cart.access(0x01FE, 0x34, 0, true); // a JSR's PCL push cart.access(0x1001, 0, 1, false); cart.snoop(0xD0); // the new PC high byte, $Dxxx: bank 1 assert_eq!(cart.access(0x1000, 0, 2, false), Some(1)); cart.access(0x01FE, 0, 3, false); // an RTS's PCL pull cart.access(0x01FF, 0, 4, false); cart.snoop(0xF2); // PCH pulled from RIOT RAM, $Fxxx: bank 0 assert_eq!(cart.access(0x1000, 0, 5, false), Some(0)); cart.access(0x01FF, 0, 6, false); cart.snoop(0xD0); // not after $01FE: nothing assert_eq!(cart.access(0x1000, 0, 7, false), Some(0)); } #[test] fn e7_rom_pages_ram_windows_and_fixed_area() { let mut cart = MNetworkE7::new(&tagged(0x4000, 0x800), 0, 0).unwrap(); read(&mut cart, 0x1FE3); assert_eq!(read(&mut cart, 0x1000), Some(3)); assert_eq!(read(&mut cart, 0x1A00), Some(7)); read(&mut cart, 0x1FE7); // 1K of RAM at $1000 cart.access(0x1010, 0x5A, 0, true); assert_eq!(read(&mut cart, 0x1410), Some(0x5A)); read(&mut cart, 0x1FE9); // RAM block 1 at $1800 cart.access(0x1820, 0xA5, 0, true); assert_eq!(read(&mut cart, 0x1920), Some(0xA5)); read(&mut cart, 0x1FE8); assert_eq!(read(&mut cart, 0x1920), Some(0x00)); } #[test] fn e7_12k_maps_pages_0_1_twice() { let mut cart = MNetworkE7::new(&tagged(0x3000, 0x800), 0, 0).unwrap(); let mut pages = Vec::new(); for page in 0..7 { read(&mut cart, 0x1FE0 + page); pages.push(read(&mut cart, 0x1000).unwrap()); } assert_eq!(pages, [0, 1, 0, 1, 2, 3, 4]); assert_eq!(read(&mut cart, 0x1A00), Some(5)); } #[test] fn commavid_ram_read_low_write_high() { let image: Vec = (0..0x800).map(|i| (i & 0xFF) as u8).collect(); let mut cart = CommaVid::new(&image, 0).unwrap(); cart.access(0x1400, 0x42, 0, true); assert_eq!(read(&mut cart, 0x1000), Some(0x42)); assert_eq!(read(&mut cart, 0x1801), Some(1)); } #[test] fn ua_banks_from_0220_and_0240() { let mut cart = UA::new(&tagged(0x2000, 0x1000), 0).unwrap(); read(&mut cart, 0x0240); assert_eq!(read(&mut cart, 0x1000), Some(1)); read(&mut cart, 0x0220); assert_eq!(read(&mut cart, 0x1000), Some(0)); } #[test] fn f0_steps_on_each_1ff0_access() { let mut cart = MegaboyF0::new(&tagged(0x10000, 0x1000), 0).unwrap(); for expected in [1, 2, 3] { read(&mut cart, 0x1FF0); assert_eq!(read(&mut cart, 0x1000), Some(expected)); } } #[test] fn _32in1_next_game_on_each_reset() { let mut cart = ThirtyTwoInOne::new(&tagged(0x10000, 0x800), 0).unwrap(); assert!(read(&mut cart, 0x1000) == Some(0) && read(&mut cart, 0x1800) == Some(0)); cart.reset(); assert_eq!(read(&mut cart, 0x1FFF), Some(1)); } fn dpc_image() -> Vec { let graphics: Vec = (0..0x800).map(|i| (i & 0xFF) as u8).collect(); [vec![0u8; 0x2000], graphics, vec![0u8; 255]].concat() } fn dpc(image: &[u8]) -> DPC { DPC::new(image, 1, NTSC_CPU_HZ, MUSIC_HZ).unwrap() } #[test] fn dpc_random_number_generator_has_255_states() { let mut cart = dpc(&dpc_image()); cart.access(0x1070, 0, 0, true); // reset to 0 let values: Vec = (0..256).map(|_| read(&mut cart, 0x1000).unwrap()).collect(); // Horton's taps worked by hand from 0: the inverse of bits 7, 5, 4, 3 shifted in. assert_eq!(values[..6], [0x01, 0x03, 0x07, 0x0F, 0x1E, 0x3D]); assert_eq!(values[255], values[0]); assert_eq!(values[..255].iter().collect::>().len(), 255); assert!(!values.contains(&0xFF)); } #[test] fn dpc_fetcher_reads_decrement_and_mask_by_the_flag() { let mut cart = dpc(&dpc_image()); cart.access(0x1050, 0x10, 0, true); // fetcher 0 pointer low cart.access(0x1058, 0x00, 0, true); // pointer high cart.access(0x1048, 0x0E, 0, true); // end count cart.access(0x1040, 0x10, 0, true); // start count = pointer low: the compare sets the flag let first = read(&mut cart, 0x1010).unwrap(); // masked read, flag set assert_eq!(first, ((!0x010u16 & 0x7FF) & 0xFF) as u8); assert_eq!(read(&mut cart, 0x1038), Some(0xFF)); // flag read(&mut cart, 0x1008); // pointer now $00E: the end count clears the flag assert_eq!(read(&mut cart, 0x1038), Some(0x00)); assert_eq!(read(&mut cart, 0x1010), Some(0)); // masked } #[test] fn dpc_program_banks_switch_as_f8() { let mut image = dpc_image(); image[0x0100] = 0xAA; image[0x1100] = 0xBB; let mut cart = dpc(&image); assert_eq!(read(&mut cart, 0x1100), Some(0xBB)); read(&mut cart, 0x1FF8); assert_eq!(read(&mut cart, 0x1100), Some(0xAA)); } /// Not in the reference's tests (added for a gate blind spot): the read groups /// $1018-$1037 shape the graphics byte as dpc.py writes them (nibbles swapped, /// bits reversed, shifted right, shifted left) while the flag is set. #[test] fn dpc_read_groups_shape_the_byte() { let mut cart = dpc(&dpc_image()); cart.access(0x1048, 0x00, 0, true); // end count cart.access(0x1040, 0x10, 0, true); // start count for (group, expected) in [(0x18u16, 0xFEu8), (0x20, 0xF7), (0x28, 0x77), (0x30, 0xDE)] { cart.access(0x1050, 0x10, 0, true); // pointer low $10 = the start count: flag set // the graphics byte at the pointer's complement, $7EF, is $EF assert_eq!(read(&mut cart, 0x1000 + group), Some(expected), "group {group:#x}"); } } fn supercharger_image(start: u16, control: u8) -> Vec { let pages = vec![0u8; 0x2000]; let mut header = vec![0u8; 0x100]; header[0..8].copy_from_slice(&[(start & 0xFF) as u8, (start >> 8) as u8, control, 1, 0, 0, 0x0C, 0x01]); let sum = |b: &[u8]| b.iter().map(|&x| x as i32).sum::(); header[4] = ((0x55 - sum(&header[0..4]) - sum(&header[5..8])) & 0xFF) as u8; header[0x10] = 0x00; // RAM bank 1, block 0 header[0x40] = ((0x55 - sum(&pages[0..256])) & 0xFF) as u8; [pages, header].concat() } fn supercharger() -> Supercharger { Supercharger::new(&supercharger_image(0xF800, 0x0E), None, None, 0).unwrap() } #[test] fn supercharger_header_and_pages_check() { let (info, pages) = parse_load(&supercharger_image(0xF800, 0x0E), 0).unwrap(); assert!(info.start == 0xF800 && info.pages == 1 && pages[0].0 == 0); } /// Horton's example: LDA $F000,X (X = $66), then LDA ($8B),Y to $1100. #[test] fn supercharger_writes_on_the_fifth_address_transition() { let mut cart = supercharger(); cart.set_control(0x0E); // mode 3: RAM bank 1 at $1000, bank 3 at $1800; writes on let bus = [0x1800, 0x1801, 0x1802, 0x1066, 0x1803, 0x1804, 0x008B, 0x008C, 0x1100]; let driven: Vec<_> = bus .iter() .enumerate() .map(|(t, &a)| cart.access(a, 0, t as u64, false)) .collect(); assert!(driven[driven.len() - 1] == Some(0x66) && cart.ram[0x100] == 0x66); } /// Horton's second example: a NOP's repeated fetch is one transition. #[test] fn supercharger_counts_transitions_not_cycles() { let mut cart = supercharger(); cart.set_control(0x0E); let bus = [ 0x1FC9, 0x1FCA, 0x1FCB, 0x1008, 0x1FCC, 0x1FCD, 0x1FCD, 0x1FCE, 0x1FCF, 0x17E3, ]; for (t, &address) in bus.iter().enumerate() { cart.access(address, 0, t as u64, false); } assert_eq!(cart.ram[0x7E3], 0x08); // bank 1's $7E3 } #[test] fn supercharger_control_register_through_the_latch() { let mut cart = supercharger(); cart.access(0x1016, 0, 0, false); // read $F016 cart.access(0x1FF8, 0, 1, false); assert!(cart.control == 0x16 && (cart.lower, cart.upper) == (1, 3)); }