emulator core struct
This commit is contained in:
parent
4a2c15e5ec
commit
0459390d78
|
@ -11,6 +11,7 @@ use futures::executor;
|
|||
use gb_emu_lib::{
|
||||
connect::{AudioOutput, EmulatorMessage, JoypadState, Renderer, RomFile},
|
||||
util::scale_buffer,
|
||||
EmulatorCore,
|
||||
};
|
||||
use gilrs::{
|
||||
ff::{BaseEffect, BaseEffectType, EffectBuilder, Replay, Ticks},
|
||||
|
@ -81,7 +82,6 @@ fn main() {
|
|||
bootrom_path: args.bootrom,
|
||||
connect_serial: args.connect_serial,
|
||||
verbose: args.verbose,
|
||||
step_by: args.step_by,
|
||||
cycle_count: args.cycle_count,
|
||||
};
|
||||
|
||||
|
@ -97,13 +97,22 @@ fn main() {
|
|||
|
||||
let (output, _stream) = create_audio_output();
|
||||
|
||||
gb_emu_lib::init(
|
||||
let mut core = EmulatorCore::init(
|
||||
receiver,
|
||||
options,
|
||||
Box::new(WindowRenderer::new(factor, Some(Gilrs::new().unwrap()))),
|
||||
output,
|
||||
tile_window,
|
||||
);
|
||||
|
||||
match args.step_by {
|
||||
Some(step_size) => loop {
|
||||
core.run_stepped(step_size);
|
||||
},
|
||||
None => loop {
|
||||
core.run();
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn create_audio_output() -> (AudioOutput, Stream) {
|
||||
|
|
120
lib/src/lib.rs
120
lib/src/lib.rs
|
@ -6,10 +6,7 @@
|
|||
bigint_helper_methods
|
||||
)]
|
||||
|
||||
use crate::{
|
||||
processor::memory::Memory,
|
||||
util::{pause, print_cycles},
|
||||
};
|
||||
use crate::{processor::memory::Memory, util::pause};
|
||||
use connect::{AudioOutput, EmulatorMessage, Renderer, RomFile};
|
||||
use once_cell::sync::OnceCell;
|
||||
use processor::{memory::Rom, Cpu};
|
||||
|
@ -35,7 +32,6 @@ pub struct Options {
|
|||
pub bootrom_path: Option<String>,
|
||||
pub connect_serial: bool,
|
||||
pub verbose: bool,
|
||||
pub step_by: Option<usize>,
|
||||
pub cycle_count: bool,
|
||||
}
|
||||
|
||||
|
@ -47,13 +43,21 @@ static VERBOSE: OnceCell<bool> = OnceCell::new();
|
|||
pub const WIDTH: usize = 160;
|
||||
pub const HEIGHT: usize = 144;
|
||||
|
||||
pub fn init(
|
||||
pub struct EmulatorCore {
|
||||
receiver: Receiver<EmulatorMessage>,
|
||||
cpu: Cpu,
|
||||
cycle_num: usize,
|
||||
cycle_count: bool,
|
||||
}
|
||||
|
||||
impl EmulatorCore {
|
||||
pub fn init(
|
||||
receiver: Receiver<EmulatorMessage>,
|
||||
options: Options,
|
||||
mut window: Box<dyn Renderer>,
|
||||
output: AudioOutput,
|
||||
tile_window: Option<Box<dyn Renderer>>,
|
||||
) {
|
||||
) -> Self {
|
||||
VERBOSE.set(options.verbose).unwrap();
|
||||
|
||||
let rom = match options.rom {
|
||||
|
@ -72,7 +76,7 @@ pub fn init(
|
|||
Ok(data) => Rom::load(data, maybe_save),
|
||||
Err(e) => {
|
||||
println!("Error reading ROM: {e}");
|
||||
return;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -88,14 +92,16 @@ pub fn init(
|
|||
Ok(data) => Some(data),
|
||||
Err(e) => {
|
||||
println!("Error reading bootROM: {e}");
|
||||
return;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut cpu = Cpu::new(
|
||||
Self::new(
|
||||
receiver,
|
||||
Cpu::new(
|
||||
Memory::init(
|
||||
bootrom,
|
||||
rom,
|
||||
|
@ -105,55 +111,89 @@ pub fn init(
|
|||
tile_window,
|
||||
),
|
||||
bootrom_enabled,
|
||||
);
|
||||
),
|
||||
options.cycle_count,
|
||||
)
|
||||
}
|
||||
|
||||
let mut cycle_num = 0;
|
||||
fn new(receiver: Receiver<EmulatorMessage>, cpu: Cpu, cycle_count: bool) -> Self {
|
||||
Self {
|
||||
receiver,
|
||||
cpu,
|
||||
cycle_num: 0,
|
||||
cycle_count,
|
||||
}
|
||||
}
|
||||
|
||||
verbose_println!("\n\n Begin execution...\n");
|
||||
match options.step_by {
|
||||
Some(step_size) => loop {
|
||||
process_messages(&receiver, &mut cpu);
|
||||
pub fn run(&mut self) {
|
||||
self.process_messages();
|
||||
self.cycle_num += 1;
|
||||
if self.cycle_count {
|
||||
self.print_cycles();
|
||||
}
|
||||
self.run_cycle();
|
||||
}
|
||||
|
||||
pub fn run_stepped(&mut self, step_size: usize) {
|
||||
loop {
|
||||
self.process_messages();
|
||||
for _ in 0..step_size {
|
||||
cycle_num += 1;
|
||||
if options.cycle_count {
|
||||
print_cycles(&cycle_num);
|
||||
self.cycle_num += 1;
|
||||
if self.cycle_count {
|
||||
self.print_cycles();
|
||||
}
|
||||
run_cycle(&mut cpu);
|
||||
self.run_cycle();
|
||||
}
|
||||
print!(" ...{cycle_num} cycles - press enter to continue\r");
|
||||
print!(
|
||||
" ...{} cycles - press enter to continue\r",
|
||||
self.cycle_num
|
||||
);
|
||||
stdout().flush().unwrap();
|
||||
pause();
|
||||
},
|
||||
None => loop {
|
||||
process_messages(&receiver, &mut cpu);
|
||||
cycle_num += 1;
|
||||
if options.cycle_count {
|
||||
print_cycles(&cycle_num);
|
||||
}
|
||||
run_cycle(&mut cpu);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn process_messages(receiver: &Receiver<EmulatorMessage>, cpu: &mut Cpu) {
|
||||
while let Ok(msg) = receiver.try_recv() {
|
||||
match msg {
|
||||
EmulatorMessage::Stop => {
|
||||
cpu.memory.flush_rom();
|
||||
exit(0);
|
||||
pub fn run_until_buffer_full(&mut self) {
|
||||
while !self.cpu.memory.is_audio_buffer_full() {
|
||||
loop {
|
||||
self.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run_cycle(cpu: &mut Cpu) {
|
||||
fn run_cycle(&mut self) {
|
||||
let will_pause = unsafe { PAUSE_QUEUED };
|
||||
let pause_enabled = unsafe { PAUSE_ENABLED };
|
||||
cpu.exec_next();
|
||||
if !pause_enabled && cpu.reg.pc >= 0x100 {
|
||||
self.cpu.exec_next();
|
||||
if !pause_enabled && self.cpu.reg.pc >= 0x100 {
|
||||
unsafe { PAUSE_ENABLED = true };
|
||||
}
|
||||
if will_pause {
|
||||
pause_then_step();
|
||||
}
|
||||
}
|
||||
|
||||
fn process_messages(&mut self) {
|
||||
while let Ok(msg) = self.receiver.try_recv() {
|
||||
match msg {
|
||||
EmulatorMessage::Stop => {
|
||||
self.cpu.memory.flush_rom();
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn print_cycles(&self) {
|
||||
if self.cycle_num % 45678 != 0 {
|
||||
return;
|
||||
}
|
||||
let instructions_per_second = 400000;
|
||||
print!(
|
||||
"cycle {} - approx {} seconds on real hardware\r",
|
||||
self.cycle_num,
|
||||
self.cycle_num / instructions_per_second
|
||||
);
|
||||
stdout().flush().unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -222,6 +222,10 @@ impl Memory {
|
|||
pub fn flush_rom(&mut self) {
|
||||
self.rom.flush();
|
||||
}
|
||||
|
||||
pub fn is_audio_buffer_full(&self) -> bool {
|
||||
self.apu.is_buffer_full()
|
||||
}
|
||||
}
|
||||
|
||||
impl Cpu {
|
||||
|
|
|
@ -124,6 +124,10 @@ impl Apu {
|
|||
executor::block_on(self.output.send_rb.push_slice(&converted)).unwrap();
|
||||
}
|
||||
|
||||
pub fn is_buffer_full(&self) -> bool {
|
||||
self.output.send_rb.is_full()
|
||||
}
|
||||
|
||||
pub fn get_register(&self, addr: Address) -> u8 {
|
||||
if self.apu_enable {
|
||||
self.make_register(addr)
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
use crate::{processor::Direction, PAUSE_ENABLED, PAUSE_QUEUED, VERBOSE};
|
||||
use std::{
|
||||
io::{self, stdout, Write},
|
||||
mem::transmute,
|
||||
};
|
||||
use std::{io, mem::transmute};
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! verbose_println {
|
||||
|
@ -45,19 +42,6 @@ pub(crate) fn pause() -> String {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn print_cycles(cycles: &i32) {
|
||||
if *cycles % 45678 != 0 {
|
||||
return;
|
||||
}
|
||||
let instructions_per_second = 400000;
|
||||
print!(
|
||||
"cycle {} - approx {} seconds on real hardware\r",
|
||||
cycles,
|
||||
cycles / instructions_per_second
|
||||
);
|
||||
stdout().flush().unwrap();
|
||||
}
|
||||
|
||||
pub(crate) fn is_verbose() -> bool {
|
||||
match VERBOSE.get() {
|
||||
Some(v) => *v,
|
||||
|
|
Loading…
Reference in a new issue