Chirp/tests/chip8_test_suite.rs

119 lines
3.0 KiB
Rust
Raw Normal View History

2023-04-23 16:58:57 +00:00
// (c) 2023 John A. Breaux
// This code is licensed under MIT license (see LICENSE for details)
// When compiled, the resulting binary is licensed under version 3 of the GNU General Public License (see chip8-test-suite/LICENSE for details)
2023-03-28 02:31:54 +00:00
//! These are a series of interpreter tests using Timendus's incredible test suite
pub use chirp::*;
2023-03-28 02:31:54 +00:00
fn setup_environment() -> (CPU, Bus) {
let mut cpu = CPU::default();
2023-04-15 02:25:41 +00:00
cpu.flags = Flags {
2023-03-28 02:31:54 +00:00
debug: true,
pause: false,
..Default::default()
};
(
cpu,
bus! {
2023-04-17 10:04:23 +00:00
// Create a screen, and fill it with garbage
Screen [0x000..0x100] = b"jsuadhgufywegrwsdyfogbbg4owgbrt",
2023-03-28 02:31:54 +00:00
},
)
}
struct SuiteTest {
2023-04-17 10:04:23 +00:00
test: u8,
data: &'static [u8],
2023-03-28 02:31:54 +00:00
screen: &'static [u8],
}
fn run_screentest(test: SuiteTest, mut cpu: CPU, mut bus: Bus) {
// Set the test to run
cpu.poke(0x1ffu16, test.test);
cpu.load_program_bytes(test.data).unwrap();
2023-03-28 02:31:54 +00:00
// The test suite always initiates a keypause on test completion
while !(cpu.flags.is_paused()) {
cpu.multistep(&mut bus, 10).unwrap();
if cpu.cycle() > 1000000 {
panic!("test {} took too long", test.test)
}
2023-03-28 02:31:54 +00:00
}
// Compare the screen to the reference screen buffer
bus.print_screen().unwrap();
2023-04-17 10:04:23 +00:00
bus! {crate::cpu::bus::Region::Screen [0..256] = test.screen}
2023-03-28 02:31:54 +00:00
.print_screen()
.unwrap();
assert_eq!(bus.get_region(Screen).unwrap(), test.screen);
}
#[test]
fn splash_screen() {
2023-04-02 19:45:32 +00:00
let (cpu, bus) = setup_environment();
2023-03-28 02:31:54 +00:00
run_screentest(
SuiteTest {
test: 0,
2023-04-17 10:04:23 +00:00
data: include_bytes!("../chip8-test-suite/bin/1-chip8-logo.ch8"),
2023-03-28 02:31:54 +00:00
screen: include_bytes!("screens/chip8-test-suite/splash.bin"),
},
2023-04-02 19:45:32 +00:00
cpu,
bus,
2023-03-28 02:31:54 +00:00
)
}
#[test]
fn ibm_logo() {
let (cpu, bus) = setup_environment();
2023-03-28 02:31:54 +00:00
run_screentest(
SuiteTest {
2023-04-17 10:04:23 +00:00
test: 0x00,
data: include_bytes!("../chip8-test-suite/bin/2-ibm-logo.ch8"),
2023-03-28 02:31:54 +00:00
screen: include_bytes!("screens/chip8-test-suite/IBM.bin"),
},
cpu,
bus,
2023-03-28 02:31:54 +00:00
)
}
2023-04-17 10:04:23 +00:00
#[test]
fn corax_test() {
let (cpu, bus) = setup_environment();
run_screentest(
SuiteTest {
test: 0x00,
data: include_bytes!("../chip8-test-suite/bin/3-corax+.ch8"),
screen: include_bytes!("screens/chip8-test-suite/corax+.bin"),
},
cpu,
bus,
)
}
2023-03-28 02:31:54 +00:00
#[test]
fn flags_test() {
let (cpu, bus) = setup_environment();
2023-03-28 02:31:54 +00:00
run_screentest(
SuiteTest {
2023-04-17 10:04:23 +00:00
test: 0x00,
data: include_bytes!("../chip8-test-suite/bin/4-flags.ch8"),
2023-03-28 02:31:54 +00:00
screen: include_bytes!("screens/chip8-test-suite/flags.bin"),
},
cpu,
bus,
2023-03-28 02:31:54 +00:00
)
}
#[test]
fn quirks_test() {
let (cpu, bus) = setup_environment();
2023-03-28 02:31:54 +00:00
run_screentest(
SuiteTest {
2023-04-17 10:04:23 +00:00
test: 0x01,
data: include_bytes!("../chip8-test-suite/bin/5-quirks.ch8"),
2023-03-28 02:31:54 +00:00
screen: include_bytes!("screens/chip8-test-suite/quirks.bin"),
},
cpu,
bus,
2023-03-28 02:31:54 +00:00
)
}