add tests

This commit is contained in:
Denis-Cosmin Nutiu 2024-11-22 23:14:52 +02:00
parent 811b4fa788
commit 6e76f96467
3 changed files with 92 additions and 20 deletions

23
Cargo.lock generated
View file

@ -9,6 +9,7 @@ dependencies = [
"anyhow", "anyhow",
"env_logger", "env_logger",
"log", "log",
"pretty_assertions",
] ]
[[package]] [[package]]
@ -81,6 +82,12 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]]
name = "diff"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
[[package]] [[package]]
name = "env_filter" name = "env_filter"
version = "0.1.2" version = "0.1.2"
@ -128,6 +135,16 @@ version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "pretty_assertions"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ae130e2f271fbc2ac3a40fb1d07180839cdbbe443c7a27e1e3c13c5cac0116d"
dependencies = [
"diff",
"yansi",
]
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.11.1" version = "1.11.1"
@ -235,3 +252,9 @@ name = "windows_x86_64_msvc"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "yansi"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049"

View file

@ -7,3 +7,6 @@ edition = "2021"
log = "0.4.22" log = "0.4.22"
env_logger = "0.11.5" env_logger = "0.11.5"
anyhow = "1.0.93" anyhow = "1.0.93"
[dev-dependencies]
pretty_assertions = "1.4.1"

View file

@ -6,31 +6,6 @@ use std::path::Path;
const MEMORY_SIZE: usize = 4096; const MEMORY_SIZE: usize = 4096;
const NUMBER_OF_REGISTERS: usize = 16; const NUMBER_OF_REGISTERS: usize = 16;
/// Emulator emulates the Chip8 CPU.
pub struct Emulator {
memory: [u8; MEMORY_SIZE],
registers: [u16; NUMBER_OF_REGISTERS],
program_counter: u16,
}
impl Emulator {
/// Creates a new `Emulator` instance.
///
pub fn new() -> Emulator {
let mut emulator = Emulator {
memory: [0; 4096],
registers: [0; 16],
program_counter: 0,
};
emulator.load_font_data();
emulator
}
fn load_font_data(&mut self) {
info!("Loading font data...");
const FONT_SPRITES: [u8; 80] = [ const FONT_SPRITES: [u8; 80] = [
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0 0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
0x20, 0x60, 0x20, 0x20, 0x70, // 1 0x20, 0x60, 0x20, 0x20, 0x70, // 1
@ -49,6 +24,47 @@ impl Emulator {
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E 0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
0xF0, 0x80, 0xF0, 0x80, 0x80, // F 0xF0, 0x80, 0xF0, 0x80, 0x80, // F
]; ];
/// Emulator emulates the Chip8 CPU.
pub struct Emulator {
/// Memory represents the emulator's memory.
memory: [u8; MEMORY_SIZE],
/// Registers holds the general purpose registers.
registers: [u8; NUMBER_OF_REGISTERS],
/// The index register store memory addresses.
index_register: u16,
/// The program counter register tracks the currently executing instruction.
program_counter: u16,
/// The delay timer register. It is decremented at a rate of 60 Hz until it reaches 0.
delay_timer: u8,
/// The sound timer register. It is decremented at a rate of 60 Hz until it reaches 0.
/// It plays a beeping sound when it's value is different from 0.
sound_timer: u8,
/// The stack pointer register.
stack_pointer: u8,
}
impl Emulator {
/// Creates a new `Emulator` instance.
///
pub fn new() -> Emulator {
let mut emulator = Emulator {
memory: [0; 4096],
registers: [0; 16],
index_register: 0,
program_counter: 0,
delay_timer: 0,
sound_timer: 0,
stack_pointer: 0,
};
emulator.load_font_data();
emulator
}
fn load_font_data(&mut self) {
info!("Loading font data...");
FONT_SPRITES FONT_SPRITES
.iter() .iter()
.enumerate() .enumerate()
@ -89,3 +105,33 @@ impl Emulator {
Ok(()) Ok(())
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn test_load_font_data() {
let emulator = Emulator::new();
assert_eq!(emulator.memory[0xf0..0xf0 + 80], FONT_SPRITES)
}
#[test]
fn test_load_rom_ibm_logo() {
// Setup
let mut file = File::open("roms/ibm-logo.ch8").expect("Failed to test open ROM");
let mut rom_file_data: [u8; 132] = [0; 132];
file.read(&mut rom_file_data)
.expect("Failed to read test ROM");
// Test
let mut emulator = Emulator::new();
emulator
.load_rom("roms/ibm-logo.ch8")
.expect("failed to load ROM");
// Assert
assert_eq!(emulator.memory[0x200..0x200 + 132], rom_file_data)
}
}