// SPDX-License-Identifier: MIT //! The BIOS's own test, run by the BIOS itself: does the console start this image //! in 7800 mode or in 2600 mode? //! //! docs/cartridge.md "2600 dumps handed to this board": a headerless image that no //! table knows is put to the test the console applies, so the answer is the //! console's and not a heuristic of ours. The test is not re-implemented here: //! the BIOS image (bring your own, by hash) is executed by 6502-rust on a //! minimal bus that has only what the test touches: INPTCTRL's MEN and EXT, the //! two 6116s and their shadows, the 6532's RAM, MSTAT (which reads as vertical //! blank on every other read, so the BIOS's waits for the beam pass quickly), //! the TIA and the rest reading as an undriven bus, and the image mapped as the //! connector would see it (the decode as board.rs's, DOC: [MT84] Figure 2, [SG] //! App. 1; MSTAT's toggling and the once-a-frame NMI are pacing shortcuts, not //! hardware values): //! //! - 4 KiB or less: a 2600-sized image on the middle 24 contacts, so it answers //! wherever A12 is high and repeats every its-own-size (DOC: the connector); //! - otherwise flat, ending at $FFFF (a 7800 cartridge with no switching; the //! loader asks only about images of 48 KiB or less, Open decision 2). //! //! The verdict is where the BIOS jumps: through the image's reset vector with //! EXT and MEN set ("7800"), or into the 2600 start code it copies to the //! 6532's RAM at $0480 ("2600"). The NTSC BIOS checks Atari's signature; the PAL //! BIOS checks only the region byte (C8; what the dumps' own code does). //! //! Rust-forced differences (a7800-python `cartridge/bios_test.py` at d6b220d): the bus //! is 6502-rust's `Bus` trait (the reference's two callables); errors are `Result`s //! (the reference raises `ValueError` and `RuntimeError`). use sixfiveohtwo::{Bus, Frame, Mos6502}; pub const MEN: u8 = 0x02; // INPTCTRL bits (DOC: [SG]) pub const EXT: u8 = 0x04; // CPU cycles a frame, NTSC (DOC: [HF], reported console measurement); only the BIOS's // pace depends on it pub const FRAME_CYCLES: u64 = 29_850; struct TestBus { bios: Vec, image: Vec, small: bool, base: usize, ram: Vec, riot_ram: Vec, inptctrl: u8, locked: bool, data: u8, mstat: u8, dma_on: bool, } impl TestBus { fn new(bios: &[u8], image: &[u8]) -> TestBus { TestBus { bios: bios.to_vec(), image: image.to_vec(), small: image.len() <= 0x1000, base: 0x10000 - image.len(), ram: vec![0; 0x1000], riot_ram: vec![0; 0x80], inptctrl: 0, locked: false, data: 0, mstat: 0, dma_on: false, } } fn cart(&self, address: u16) -> Option { let address = address as usize; if self.small { if address & 0x1000 != 0 { return Some(self.image[address % self.image.len()]); } return None; } if address >= self.base { return Some(self.image[address - self.base]); } None } fn read_byte(&mut self, address: u16) -> u8 { let control = self.inptctrl; let a = address as usize; let mut value = None; if control & MEN == 0 { if a & 0x1000 != 0 { value = if control & EXT == 0 { Some(self.bios[a & (self.bios.len() - 1)]) } else { self.cart(address) }; } else if a & 0x80 != 0 && a & 0x200 == 0 { value = Some(self.riot_ram[a & 0x7F]); } } else if a < 0x0400 { let low = a & 0xFF; if low == 0x28 { self.mstat ^= 0x80; value = Some(self.mstat); } else if low >= 0x40 && a & 0x200 == 0 { value = Some(self.ram[0x800 | (a & 0x1FF)]); } } else if a < 0x0600 { if a & 0x80 != 0 { value = Some(self.riot_ram[a & 0x7F]); } } else if (0x1800..0x2800).contains(&a) { value = Some(self.ram[a - 0x1800]); } else if (0x2800..0x3000).contains(&a) { value = Some(self.ram[0x800 | (a & 0x7FF)]); } else if a >= 0x3000 { if a & 0x8000 != 0 && control & EXT == 0 { value = Some(self.bios[a & (self.bios.len() - 1)]); } else { value = self.cart(address); } } let value = value.unwrap_or(self.data); self.data = value; value } fn write_byte(&mut self, address: u16, value: u8) { self.data = value; let control = self.inptctrl; let a = address as usize; let tia = if control & MEN == 0 { a & 0x1080 == 0 } else { a < 0x400 && a & 0xE0 == 0 }; if tia { if !self.locked { self.inptctrl = value & 0x0F; self.locked = value & 1 != 0; } return; } if control & MEN == 0 { if a & 0x1000 == 0 && a & 0x80 != 0 && a & 0x200 == 0 { self.riot_ram[a & 0x7F] = value; } return; } if a < 0x0400 { if a & 0xFF == 0x3C { // CTRL: DMA mode 2 means MARIA will raise DLIs self.dma_on = (value >> 5) & 3 == 2; } else if (a & 0xFF) >= 0x40 && a & 0x200 == 0 { self.ram[0x800 | (a & 0x1FF)] = value; } } else if a < 0x0600 { if a & 0x80 != 0 { self.riot_ram[a & 0x7F] = value; } } else if (0x1800..0x2800).contains(&a) { self.ram[a - 0x1800] = value; } else if (0x2800..0x3000).contains(&a) { self.ram[0x800 | (a & 0x7FF)] = value; } } } impl Bus for TestBus { fn read(&mut self, address: u16, _frame: &mut Frame) -> u8 { self.read_byte(address) } fn write(&mut self, address: u16, value: u8, _frame: &mut Frame) { self.write_byte(address, value) } } /// "7800" or "2600": the mode the BIOS starts `image` in (see the module header). pub fn bios_verdict(bios: &[u8], image: &[u8], max_instructions: u64) -> Result<&'static str, String> { if image.is_empty() || image.len() > 0xC000 { return Err("the BIOS test is asked only about images of 1 byte to 48 KiB".to_string()); } let mut cpu = Mos6502::new(TestBus::new(bios, image)); cpu.frame.request_reset(); cpu.step(); let low = cpu.bus.cart(0xFFFC).ok_or("the image has no reset vector")?; let high = cpu.bus.cart(0xFFFD).ok_or("the image has no reset vector")?; let vector = low as u16 | (high as u16) << 8; let mut cycles: u64 = 0; for _ in 0..max_instructions { cycles += cpu.step() as u64; if cycles >= FRAME_CYCLES { // The NTSC BIOS advances its signature check from its DLI handler // (it waits on $F0, which the NMI routine sets): one per frame, // once it has turned MARIA's DMA on. cycles -= FRAME_CYCLES; if cpu.bus.dma_on { cpu.frame.request_non_maskable_interrupt(); } } let pc = cpu.pc; if (0x0480..0x0500).contains(&pc) { return Ok("2600"); } if pc == vector && cpu.bus.inptctrl & (MEN | EXT) == MEN | EXT { return Ok("7800"); } } Err("the BIOS reached no verdict within the instruction budget".to_string()) } /// `bios_verdict` with the reference's default budget (5,000,000 instructions). pub fn bios_verdict_default(bios: &[u8], image: &[u8]) -> Result<&'static str, String> { bios_verdict(bios, image, 5_000_000) }