diff --git a/examples/benchmark/src/main.rs b/examples/benchmark/src/main.rs index 430f2b8f4eee8f5b2356e41d205ae03330390816..d69c8981287de9d66d48200cf19bc34f541055cc 100644 --- a/examples/benchmark/src/main.rs +++ b/examples/benchmark/src/main.rs @@ -1,9 +1,9 @@ -use chip_ahoyto::{chip8::Chip8, util::read_file}; +use chip_ahoyto::{chip8::Chip8, chip8_neo::Chip8Neo, util::read_file}; use time::Instant; -const CYCLE_COUNT: u64 = 1_000_000_000; +const CYCLE_COUNT: u64 = 5_000_000_000; -fn main() { +fn benchmark_chip8() { let rom_path = "./resources/pong.ch8"; let rom = read_file(rom_path); @@ -15,7 +15,32 @@ fn main() { let cycles = CYCLE_COUNT; - println!("Running {} cycles for {}", cycles, rom_path); + println!("[Chip8] Running {} cycles for {}", cycles, rom_path); + + for _ in 0..CYCLE_COUNT { + chip8.tick(); + } + + let duration_s = instant.elapsed().as_seconds_f32(); + let cycles_second = cycles as f32 / duration_s; + let mega_second = cycles_second / 1000.0 / 1000.0; + + println!( + "[Chip8] Took {} seconds or {:.2} MHz CPU", + duration_s, mega_second + ); +} + +fn benchmark_chip8_neo() { + let rom_path = "./resources/pong.ch8"; + + let mut chip8 = Chip8Neo::new(); + + let instant = Instant::now(); + + let cycles = CYCLE_COUNT; + + println!("[Chip8Neo] Running {} cycles for {}", cycles, rom_path); for _ in 0..CYCLE_COUNT { chip8.tick(); @@ -25,5 +50,13 @@ fn main() { let cycles_second = cycles as f32 / duration_s; let mega_second = cycles_second / 1000.0 / 1000.0; - println!("Took {} seconds or {:.2} MHz CPU", duration_s, mega_second); + println!( + "[Chip8Neo] Took {} seconds or {:.2} MHz CPU", + duration_s, mega_second + ); +} + +fn main() { + benchmark_chip8(); + benchmark_chip8_neo(); } diff --git a/src/chip8_neo.rs b/src/chip8_neo.rs index 08e55bd2ae02b9896fff89b396196b03ce9e035d..caf47b6ffcdc648403190ef673bac78eeb1150c2 100644 --- a/src/chip8_neo.rs +++ b/src/chip8_neo.rs @@ -4,6 +4,10 @@ pub const RAM_SIZE: usize = 4096; pub const STACK_SIZE: usize = 16; pub const REGISTERS_SIZE: usize = 16; +/// The starting address for the ROM loading, should be +/// the initial PC position. +const ROM_START: usize = 0x200; + pub struct Chip8Neo { ram: [u8; RAM_SIZE], vram: [u8; DISPLAY_WIDTH * DISPLAY_HEIGHT], @@ -15,3 +19,26 @@ pub struct Chip8Neo { dt: u8, st: u8, } + +#[cfg_attr(feature = "web", wasm_bindgen)] +impl Chip8Neo { + #[cfg_attr(feature = "web", wasm_bindgen(constructor))] + pub fn new() -> Chip8Neo { + let chip8 = Chip8Neo { + ram: [0u8; RAM_SIZE], + vram: [0u8; DISPLAY_WIDTH * DISPLAY_HEIGHT], + stack: [0u16; STACK_SIZE], + registers: [0u8; REGISTERS_SIZE], + pc: ROM_START as u16, + i: 0x0, + sp: 0x0, + dt: 0x0, + st: 0x0, + }; + chip8 + } + + pub fn tick(&mut self) { + self.pc += 0x2; + } +}