2021-01-19 02:47:09 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
2021-03-23 02:41:22 +00:00
|
|
|
use clap::{crate_authors, crate_description, crate_name, crate_version, App, Arg};
|
2021-06-02 06:50:16 +00:00
|
|
|
use gb::Egui;
|
2021-03-21 07:10:56 +00:00
|
|
|
use gb::LR35902_CLOCK_SPEED;
|
2021-06-07 22:23:48 +00:00
|
|
|
use gb::{handle_gamepad_input, Cycle, LR35902};
|
|
|
|
use gilrs::Gilrs;
|
2021-01-19 02:47:09 +00:00
|
|
|
use pixels::{Pixels, SurfaceTexture};
|
2021-03-21 00:56:26 +00:00
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
use winit::dpi::LogicalSize;
|
|
|
|
use winit::event::{Event, VirtualKeyCode};
|
|
|
|
use winit::event_loop::{ControlFlow, EventLoop};
|
|
|
|
use winit::window::{Window, WindowBuilder};
|
2021-01-19 02:47:09 +00:00
|
|
|
use winit_input_helper::WinitInputHelper;
|
2020-12-23 09:25:16 +00:00
|
|
|
|
2021-06-02 06:50:16 +00:00
|
|
|
#[cfg(feature = "debug")]
|
|
|
|
use gb::RegisterPair;
|
|
|
|
|
2021-01-19 02:47:09 +00:00
|
|
|
// 160 x 144
|
|
|
|
const GB_WIDTH: u32 = 160;
|
|
|
|
const GB_HEIGHT: u32 = 144;
|
2021-03-21 00:56:26 +00:00
|
|
|
const SCALE: f64 = 5.0;
|
|
|
|
|
|
|
|
const LR35902_CYCLE_TIME: f64 = 1.0f64 / LR35902_CLOCK_SPEED as f64;
|
2021-03-27 17:10:18 +00:00
|
|
|
const CYCLES_IN_FRAME: Cycle = Cycle::new(70224);
|
2021-01-19 02:47:09 +00:00
|
|
|
|
2021-06-02 06:50:16 +00:00
|
|
|
#[cfg(feature = "debug")]
|
|
|
|
const STEP_MODE_BY_DEFAULT: bool = true;
|
|
|
|
|
2021-01-19 02:47:09 +00:00
|
|
|
fn main() -> Result<()> {
|
2021-03-23 02:41:22 +00:00
|
|
|
let app = App::new(crate_name!())
|
|
|
|
.version(crate_version!())
|
|
|
|
.author(crate_authors!())
|
|
|
|
.about(crate_description!());
|
|
|
|
|
|
|
|
let m = app
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("rom")
|
|
|
|
.value_name("ROM_FILE")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.index(1)
|
2021-04-14 06:21:45 +00:00
|
|
|
.help("Path to the Game ROM"),
|
2021-03-23 02:41:22 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("boot")
|
|
|
|
.short("b")
|
|
|
|
.long("boot")
|
|
|
|
.value_name("FILE")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("Path to Boot ROM"),
|
|
|
|
)
|
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
let mut game_boy = match m.value_of("boot") {
|
|
|
|
Some(path) => LR35902::boot_new(path).expect("Failed to load boot ROM"),
|
|
|
|
None => LR35902::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// This is a required value so if we program gets here,
|
|
|
|
// a string **will** have been provided to the rom argument
|
|
|
|
let rom_path = m
|
|
|
|
.value_of("rom")
|
|
|
|
.expect("ROM Path not provided despite it being a required argument");
|
|
|
|
|
|
|
|
game_boy
|
|
|
|
.load_cartridge(rom_path)
|
|
|
|
.expect("Failed to load ROM");
|
|
|
|
|
2021-04-14 06:21:45 +00:00
|
|
|
let default_title = "DMG-01 Emulator";
|
2021-06-07 02:30:08 +00:00
|
|
|
let cartridge_title = game_boy.rom_title().unwrap_or(default_title);
|
2021-04-14 06:21:45 +00:00
|
|
|
|
2021-05-04 04:11:39 +00:00
|
|
|
// Initialize Gamepad Support
|
|
|
|
let mut gilrs = Gilrs::new().expect("Failed to initialize Gilrs");
|
|
|
|
|
2021-03-23 02:41:22 +00:00
|
|
|
// Initialize GUI
|
2021-01-19 02:47:09 +00:00
|
|
|
let event_loop = EventLoop::new();
|
|
|
|
let mut input = WinitInputHelper::new();
|
2021-04-14 06:21:45 +00:00
|
|
|
let window = create_window(&event_loop, cartridge_title)?;
|
2021-06-02 06:50:16 +00:00
|
|
|
|
|
|
|
let (mut pixels, mut egui) = {
|
|
|
|
let size = window.inner_size();
|
|
|
|
let scale_factor = window.scale_factor();
|
|
|
|
let surface_texture = SurfaceTexture::new(size.width, size.height, &window);
|
|
|
|
let pixels = Pixels::new(GB_WIDTH, GB_HEIGHT, surface_texture)?;
|
|
|
|
let egui = Egui::new(size.width, size.height, scale_factor, pixels.context());
|
|
|
|
|
|
|
|
(pixels, egui)
|
|
|
|
};
|
2021-01-28 04:07:31 +00:00
|
|
|
|
2021-03-21 00:56:26 +00:00
|
|
|
let mut now = Instant::now();
|
2021-04-14 04:02:13 +00:00
|
|
|
let mut cycles_in_frame: Cycle = Default::default();
|
2021-06-02 06:50:16 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "debug")]
|
|
|
|
let mut step_mode = STEP_MODE_BY_DEFAULT;
|
|
|
|
|
2021-01-19 02:47:09 +00:00
|
|
|
event_loop.run(move |event, _, control_flow| {
|
2021-06-02 06:50:16 +00:00
|
|
|
// Update egui
|
|
|
|
egui.handle_event(&event);
|
|
|
|
|
2021-01-19 02:47:09 +00:00
|
|
|
if let Event::RedrawRequested(_) = event {
|
2021-06-02 06:50:16 +00:00
|
|
|
// Prepare egui
|
|
|
|
egui.prepare(&game_boy);
|
|
|
|
|
|
|
|
// Render everything together
|
|
|
|
let render_result = pixels.render_with(|encoder, target, ctx| {
|
|
|
|
// Render the texture
|
|
|
|
ctx.scaling_renderer.render(encoder, target);
|
|
|
|
|
|
|
|
// Render egui
|
|
|
|
egui.render(encoder, target, ctx);
|
|
|
|
});
|
|
|
|
|
|
|
|
if render_result
|
2021-01-19 02:47:09 +00:00
|
|
|
.map_err(|e| anyhow!("pixels.render() failed: {}", e))
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.update(&event) {
|
|
|
|
if input.key_pressed(VirtualKeyCode::Escape) || input.quit() {
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-02 06:50:16 +00:00
|
|
|
#[cfg(feature = "debug")]
|
|
|
|
if input.key_pressed(VirtualKeyCode::S) {
|
|
|
|
step_mode = !step_mode;
|
2021-01-19 02:47:09 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 06:50:16 +00:00
|
|
|
if let Some(scale_factor) = input.scale_factor() {
|
|
|
|
egui.scale_factor(scale_factor);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(size) = input.window_resized() {
|
|
|
|
pixels.resize_surface(size.width, size.height);
|
|
|
|
egui.resize(size.width, size.height);
|
2021-05-04 04:11:39 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 00:56:26 +00:00
|
|
|
// Emulate Game Boy
|
2021-06-02 06:50:16 +00:00
|
|
|
let mut elapsed_cycles: Cycle = Default::default();
|
2021-03-21 00:56:26 +00:00
|
|
|
let delta = now.elapsed().subsec_nanos();
|
|
|
|
now = Instant::now();
|
|
|
|
|
2021-06-02 06:50:16 +00:00
|
|
|
#[cfg(feature = "debug")]
|
|
|
|
if step_mode {
|
|
|
|
if input.key_pressed(VirtualKeyCode::Space) {
|
|
|
|
if let Some(event) = gilrs.next_event() {
|
|
|
|
handle_gamepad_input(&mut game_boy, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
for _ in 0..egui.config.spacebar_step {
|
|
|
|
elapsed_cycles += game_boy.step();
|
|
|
|
}
|
|
|
|
|
|
|
|
cycles_in_frame %= CYCLES_IN_FRAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
game_boy.get_ppu().copy_to_gui(pixels.get_frame());
|
|
|
|
window.request_redraw();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-21 00:56:26 +00:00
|
|
|
let cycle_time = Duration::from_secs_f64(LR35902_CYCLE_TIME).subsec_nanos();
|
2021-03-27 17:10:18 +00:00
|
|
|
let pending_cycles = Cycle::new(delta / cycle_time);
|
2021-03-21 00:56:26 +00:00
|
|
|
|
|
|
|
while elapsed_cycles <= pending_cycles {
|
2021-05-04 04:11:39 +00:00
|
|
|
if let Some(event) = gilrs.next_event() {
|
2021-06-07 22:23:48 +00:00
|
|
|
handle_gamepad_input(&mut game_boy.bus.joypad, event);
|
2021-05-04 04:11:39 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 00:56:26 +00:00
|
|
|
elapsed_cycles += game_boy.step();
|
2021-06-02 06:50:16 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "debug")]
|
|
|
|
{
|
|
|
|
let pc = game_boy.register_pair(RegisterPair::PC);
|
|
|
|
|
|
|
|
if let Some(break_point) = egui.break_point {
|
|
|
|
if pc == break_point {
|
|
|
|
step_mode = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-21 00:56:26 +00:00
|
|
|
}
|
2021-03-19 02:06:01 +00:00
|
|
|
|
2021-03-21 00:56:26 +00:00
|
|
|
cycles_in_frame += elapsed_cycles;
|
|
|
|
|
|
|
|
if cycles_in_frame >= CYCLES_IN_FRAME {
|
2021-06-02 06:50:16 +00:00
|
|
|
// Redraw
|
|
|
|
cycles_in_frame = Default::default();
|
2021-03-21 00:56:26 +00:00
|
|
|
|
2021-06-02 06:50:16 +00:00
|
|
|
game_boy.get_ppu().copy_to_gui(pixels.get_frame());
|
|
|
|
window.request_redraw();
|
2021-03-21 00:56:26 +00:00
|
|
|
}
|
2021-01-19 02:47:09 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-14 06:21:45 +00:00
|
|
|
fn create_window(event_loop: &EventLoop<()>, title: &str) -> Result<Window> {
|
2021-03-21 00:56:26 +00:00
|
|
|
let size = LogicalSize::new((GB_WIDTH as f64) * SCALE, (GB_HEIGHT as f64) * SCALE);
|
2021-01-19 02:47:09 +00:00
|
|
|
Ok(WindowBuilder::new()
|
2021-04-14 06:21:45 +00:00
|
|
|
.with_title(title)
|
2021-01-19 02:47:09 +00:00
|
|
|
.with_inner_size(size)
|
|
|
|
.with_min_inner_size(size)
|
2021-06-07 02:30:08 +00:00
|
|
|
.build(event_loop)?)
|
2021-01-19 02:47:09 +00:00
|
|
|
}
|