// SPDX-License-Identifier: MIT //! Schemes and identification (the reference's tests/test_cartridge.py), and the //! connector's boundary (tests/test_cartridge_boundary.py), read from the source //! as the reference reads its own. use std::path::Path; use a2600::cartridge::atari::{Banked, Plain}; use a2600::cartridge::connector::Cartridge; use a2600::cartridge::{detect_super_chip, identify, load, mame_hash, Options, Rom}; /// n 4K banks, each filled with its own number. fn banks(n: u8) -> Vec { (0..n).flat_map(|i| vec![i; 0x1000]).collect() } #[test] fn _2k_appears_twice() { let image: Vec = (0..0x800).map(|i| (i & 0xFF) as u8).collect(); let mut cart = Plain::new(&image).unwrap(); assert_eq!(cart.access(0x1000, 0, 0, false), Some(0)); assert_eq!(cart.access(0x1800, 0, 0, false), Some(0)); assert_eq!(cart.access(0x17FF, 0, 0, false), Some(0xFF)); } #[test] fn cartridge_ignores_a12_low() { assert_eq!(Plain::new(&[0u8; 0x1000]).unwrap().access(0x0080, 0x55, 0, false), None); } #[test] fn hotspots_switch_on_any_access_and_read_the_new_bank() { for (scheme, count, first) in [("F8", 2u8, 0x1FF8u16), ("F6", 4, 0x1FF6), ("F4", 8, 0x1FF4)] { let mut cart = Banked::new(&banks(count), scheme, false, None, 0).unwrap(); for bank in 0..count { assert_eq!(cart.access(first + bank as u16, 0, 0, false), Some(bank)); // C20: the new bank's byte assert_eq!(cart.access(0x1234, 0x99, 1, false), Some(bank)); // a write cycle switches as well } cart.access(first, 0x00, 0, false); assert_eq!(cart.bank, 0); } } #[test] fn mame_power_on_banks() { assert_eq!(Banked::new(&banks(2), "F8", false, None, 0).unwrap().bank, 1); assert_eq!(Banked::new(&banks(4), "F6", false, None, 0).unwrap().bank, 0); assert_eq!(Banked::new(&banks(8), "F4", false, None, 0).unwrap().bank, 7); assert_eq!(Banked::new(&banks(2), "F8", false, Some(0), 0).unwrap().bank, 0); } #[test] fn super_chip_windows_and_write_on_read() { let mut cart = Banked::new(&banks(2), "F8", true, None, 0).unwrap(); assert_eq!(cart.access(0x1005, 0x5A, 0, false), None); // write window: stores, drives nothing assert_eq!(cart.access(0x1085, 0, 1, false), Some(0x5A)); // read window cart.access(0x1005, 0x3C, 2, false); // a read of the write window stores the floating bus assert_eq!(cart.access(0x1085, 0, 3, false), Some(0x3C)); let bank = cart.bank as u8; assert_eq!(cart.access(0x1100, 0, 4, false), Some(bank)); // ROM above the RAM } #[test] fn detector_by_size_and_super_chip() { let plain = banks(4); let (family, sc, source, _) = identify(&plain, Path::new("/nonexistent")).unwrap(); // every bank's first 256 bytes repeat one value: looks like SC assert_eq!((family.as_str(), sc, source.as_str()), ("F6", true, "detector")); let mut varied = banks(2); varied[0x10] = 0x77; assert!(!detect_super_chip(&varied)); assert!(identify(&[0u8; 10001], Path::new("/nonexistent")).is_err()); } #[test] fn explicit_scheme_wins() { let loaded = load( Rom::Bytes(&banks(2)), Some("F8SC"), None, &mame_hash(), Options::default(), ) .unwrap(); assert_eq!(loaded.scheme, "F8SC"); assert_eq!(loaded.source, "explicit"); } fn scheme_sources() -> Vec<(String, String)> { let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("src/cartridge"); let mut out = Vec::new(); for entry in std::fs::read_dir(dir).unwrap().flatten() { let name = entry.file_name().to_string_lossy().into_owned(); if name.ends_with(".rs") && name != "mod.rs" { out.push((name, std::fs::read_to_string(entry.path()).unwrap())); } } out } /// Scheme modules use the connector and nothing else of the board (docs/cartridge.md). #[test] fn schemes_do_not_reach_into_the_board() { let allowed = ["super::connector", "crate::cartridge_state", "serde", "std::"]; for (name, text) in scheme_sources() { for line in text.lines().filter(|l| l.trim_start().starts_with("use ")) { let path = line.trim_start().trim_start_matches("use ").trim(); assert!(allowed.iter().any(|a| path.starts_with(a)), "{name}: {line}"); } } } /// There is no R/W pin: `write` is read only by the schemes the documents /// allow (3F, FE): every other scheme names it `_write` in `access`. #[test] fn only_documented_schemes_look_at_the_write_hint() { let may_read_write = ["Tigervision3F", "ActivisionFE"]; for (name, text) in scheme_sources() { let mut current = String::new(); for line in text.lines() { if let Some(rest) = line.strip_prefix("impl Cartridge for ") { current = rest.trim_end_matches(" {").to_string(); } if line.contains("fn access(") && !current.is_empty() { let reads = line.contains(" write: bool"); assert!( !reads || may_read_write.contains(¤t.as_str()), "{name}: {current}" ); assert!( line.contains("address: u16") && line.contains("t: u64"), "{name}: {line}" ); } } } }