Chirp/tests/chip8_test_suite.rs

111 lines
2.8 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, Screen) {
2023-03-28 02:31:54 +00:00
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, Screen::default())
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 screen: Screen) {
// 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 screen, 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
screen.print_screen();
Screen::from(test.screen).print_screen();
assert_eq!(screen.grab(..).unwrap(), test.screen);
2023-03-28 02:31:54 +00:00
}
#[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
)
}