From 1fd2882816f0d6a8467789055087e6d0c306ccad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com> Date: Mon, 5 Jun 2023 01:16:20 +0100 Subject: [PATCH] chore: new benchmark modes --- .github/workflows/main.yml | 2 ++ frontends/sdl/src/main.rs | 39 +++++++++++++++++++++++++++++++------- src/gb.rs | 8 ++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7e001c45..8a3becd9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -129,3 +129,5 @@ jobs: run: cd frontends/sdl && cargo run --release -- --headless --cycles 10000000 - name: Run benchmark run: cd frontends/sdl && cargo run --release -- --benchmark + - name: Run benchmark, only for CPU + run: cd frontends/sdl && cargo run --release -- --benchmark --cpu-only diff --git a/frontends/sdl/src/main.rs b/frontends/sdl/src/main.rs index ccba9557..276782a8 100644 --- a/frontends/sdl/src/main.rs +++ b/frontends/sdl/src/main.rs @@ -40,17 +40,22 @@ const VOLUME: f32 = 64.0; pub struct Benchmark { count: usize, chunk_size: Option<usize>, + cpu_only: Option<bool>, } impl Benchmark { - pub fn new(count: usize, chunk_size: Option<usize>) -> Self { - Self { count, chunk_size } + pub fn new(count: usize, cpu_only: Option<bool>, chunk_size: Option<usize>) -> Self { + Self { + count, + cpu_only, + chunk_size, + } } } impl Default for Benchmark { fn default() -> Self { - Self::new(50000000, None) + Self::new(50000000, None, None) } } @@ -221,13 +226,18 @@ impl Emulator { self.load_rom(None); } - pub fn benchmark(&mut self, params: Benchmark) { + pub fn benchmark(&mut self, params: &Benchmark) { println!("Going to run benchmark..."); let count = params.count; let chunk_size = params.chunk_size.unwrap_or(1); + let cpu_only = params.cpu_only.unwrap_or(false); let mut cycles = 0u64; + if cpu_only { + self.system.set_all_enabled(false); + } + let initial = SystemTime::now(); if chunk_size > 1 { @@ -320,7 +330,7 @@ impl Emulator { Event::KeyDown { keycode: Some(Keycode::B), .. - } => self.benchmark(Benchmark::default()), + } => self.benchmark(&Benchmark::default()), Event::KeyDown { keycode: Some(Keycode::T), .. @@ -491,11 +501,16 @@ impl Emulator { } } - pub fn run_benchmark(&mut self, params: Benchmark) { + pub fn run_benchmark(&mut self, params: &Benchmark) { let count = params.count; let chunk_size = params.chunk_size.unwrap_or(1); + let cpu_only = params.cpu_only.unwrap_or(false); let mut cycles = 0u64; + if cpu_only { + self.system.set_all_enabled(false); + } + let initial = SystemTime::now(); if chunk_size > 1 { @@ -640,6 +655,16 @@ struct Args { )] benchmark: bool, + #[arg( + long, + default_value_t = 500000000, + help = "The size of the benchmark in clock ticks" + )] + bench_count: usize, + + #[arg(long, default_value_t = false, help = "Run benchmark only for the CPU")] + cpu_only: bool, + #[arg( long, default_value_t = false, @@ -711,7 +736,7 @@ fn main() { // not and runs it accordingly, note that if running in headless // mode the number of cycles to be run may be specified if args.benchmark { - emulator.run_benchmark(Benchmark::new(500000000, None)); + emulator.run_benchmark(&Benchmark::new(args.bench_count, Some(args.cpu_only), None)); } else if args.headless { emulator.run_headless(if args.cycles > 0 { Some(args.cycles) diff --git a/src/gb.rs b/src/gb.rs index 437c5ece..7b18a41f 100644 --- a/src/gb.rs +++ b/src/gb.rs @@ -775,6 +775,14 @@ impl GameBoy { (*self.gbc).borrow_mut().set_serial_enabled(value); } + pub fn set_all_enabled(&mut self, value: bool) { + self.set_ppu_enabled(value); + self.set_apu_enabled(value); + self.set_dma_enabled(value); + self.set_timer_enabled(value); + self.set_serial_enabled(value); + } + pub fn clock_freq(&self) -> u32 { self.clock_freq } -- GitLab