// SPDX-License-Identifier: MIT //! Rung 0, natively: the decode table's counts and the reference's numbers that need no //! reference to check (sh2-python docs/validation.md, "Rung 0" and "Speed"; the lockstep //! of every table entry is scripts/rung0.sh). use sh2::bench; use sh2::cpu::tables; use sh2::decode::{decode_index, FORMS}; use sh2::harness::Harness; use sh2::Options; #[test] fn the_decode_table_defines_the_references_counts() { // "142 forms ... 53,752 encodings are defined, 11,784 undefined, none twice." assert_eq!(FORMS.len(), 142); let index = decode_index(); let defined = index.iter().filter(|e| e.is_some()).count(); assert_eq!(defined, 53_752); assert_eq!(index.len() - defined, 11_784); } #[test] fn every_form_has_its_own_handler_and_a_pipeline_class() { let mut names: Vec<&str> = FORMS.iter().map(|f| f.handler).collect(); names.sort(); names.dedup(); assert_eq!(names.len(), FORMS.len()); let t = tables(); for (op, entry) in t.index.iter().enumerate() { if let Some(n) = entry { assert_eq!(t.plan[op].name, FORMS[*n as usize].pipeline); } else { assert_eq!(t.plan[op].name, "illegal"); assert_eq!(t.slot_illegal[op], 1); } } } #[test] fn the_speed_loop_runs_at_the_references_states() { // sh2-python, 200,000 steps of benchmarks/harness_speed.py: 378,293 states with the // cache off and 266,596 with it on (PyPy 3.11, 0d61a66). let mut off = bench::setup(false); assert_eq!(bench::run(&mut off, 200_000), 378_293); assert_eq!(off.chip.r[0], 0x34); let mut on = bench::setup(true); assert_eq!(bench::run(&mut on, 200_000), 266_596); } #[test] fn a_state_round_trip_resumes_exactly() { let mut a = bench::setup(true); bench::run(&mut a, 1_003); let saved = a.chip.capture_state(); let first: Vec = (0..500).map(|_| a.chip.step()).collect(); a.chip.restore_state(&saved); let again: Vec = (0..500).map(|_| a.chip.step()).collect(); assert_eq!(first, again); } #[test] fn a_state_loaded_into_a_fresh_chip_continues_exactly() { // The reference's tests/test_state.py (sh2-python 8824075): sh2-rust's request R1 took // 267 states over 200 cached steps continuously and 295 after a fresh-chip restore. for (cache, bus32x) in [(false, false), (true, false), (false, true), (true, true)] { let setup = if bus32x { bench::setup_32x } else { bench::setup }; let mut a = setup(cache); bench::run(&mut a, 1_000); let saved = a.chip.capture_state(); let mut b = setup(cache); b.chip.bus.low.copy_from_slice(&a.chip.bus.low); b.chip.bus.high.copy_from_slice(&a.chip.bus.high); b.chip.restore_state(&saved); let states = |h: &mut Harness| -> Vec { (0..400) .map(|_| { if h.chip.pc == 0x100 + 2 * 13 { h.chip.pc = 0x100; } h.chip.step() }) .collect() }; let (first, again) = (states(&mut a), states(&mut b)); assert_eq!(first, again); if cache && !bus32x { assert_eq!(first[..200].iter().sum::(), 267); } assert_eq!(a.chip.capture_state(), b.chip.capture_state()); assert_eq!(a.chip.bus.high, b.chip.bus.high); } } #[test] fn the_harness_stops_on_a_branch_to_itself() { let mut image = vec![0u8; 0x200]; image[..8].copy_from_slice(&[0, 0, 1, 0, 0x06, 0, 0x80, 0]); image[0x100..0x106].copy_from_slice(&[0xE0, 0x2A, 0xAF, 0xFE, 0x00, 0x09]); // MOV #42,R0; BRA $; NOP let mut harness = Harness::new(&image, 0, Vec::new(), Options::default()); harness.chip.reset(true); let result = harness.run(10_000, None); assert_eq!(result.reason, "branch-to-self"); assert_eq!(result.r0, 42); }