use clap::{Parser, Subcommand}; use gemma::chip8::computer_manager::Chip8ComputerManager; use std::path::Path; use std::time::Instant; use std::{default::Default, path::PathBuf}; use support::ui_state::ROMPATH_DEFAULT; use support::{emmagui_support::GemmaImguiSupport, ui_state::ImGuiUiState}; mod support; /// Keypad Mappings for my Linux box /// 1 2 3 C /// 4 5 6 D /// 7 8 9 E /// A 0 B F const LIN_KEYS: [(u16, u8); 0x10] = [ (537, 0x01), (538, 0x02), (539, 0x03), (540, 0x0c), (562, 0x04), (568, 0x05), (550, 0x06), (563, 0x0d), (546, 0x7), (564, 0x8), (549, 0x9), (551, 0xe), (571, 0xa), (569, 0x0), (548, 0xb), (567, 0xf), ]; #[derive(Parser)] #[command(version, about, long_about = None)] struct GemmaImguiCLIOptions { roms_directory: Option, } fn main() { pretty_env_logger::init(); let cli_options = GemmaImguiCLIOptions::parse(); let mut system = Chip8ComputerManager::default(); println!("GOT A ROMS_DIRECTORY => [{:?}]", cli_options.roms_directory); println!("{:?}", std::env::var("PWD")); let mut ui_state = if cli_options.roms_directory.is_none() { ImGuiUiState::new(Some(PathBuf::from(ROMPATH_DEFAULT))) } else { ImGuiUiState::new(cli_options.roms_directory) }; support::simple_init(file!(), move |_, ui| { let current_time = Instant::now(); let mut num_cycles = 0; // Key Checks let down_keys = ui.io().keys_down; // START DEBUG CODE TO DISPLAY WHAT KEYS WE TRAPPED for (idx, val) in down_keys.iter().enumerate() { if *val { println!("{idx} = {val}"); } } // END DEBUG CODE for (key_code, key_reg) in LIN_KEYS { if down_keys[key_code as usize] { system.press_key(key_reg); system.wait_for_instruction(); } else { // do we need to release it? if system.is_key_pressed(key_reg) { system.release_key(key_reg); } } } let target_ms = ui_state.frame_time; let loop_start_time = Instant::now(); while Instant::now().duration_since(current_time).as_millis() < target_ms as u128 && num_cycles < ui_state.target_ips { if system.tick() { num_cycles += 1; } } let cycles_time = Instant::now().duration_since(loop_start_time); if num_cycles > 0 { println!( "Ran for {}ms and executed {}/{} cycles.", cycles_time.as_millis(), num_cycles, ui_state.target_ips ); } // GUI Parts if ui_state.show_video { GemmaImguiSupport::video_display(system.state(), &ui_state, ui); } GemmaImguiSupport::system_controls(&mut system, &mut ui_state, ui); if ui_state.show_registers { GemmaImguiSupport::registers_view(system.state(), ui); } if ui_state.show_memory { let active_instruction = system.state().registers.peek_pc(); GemmaImguiSupport::hex_memory_display( system.state().memory.clone(), (0x100, 0x10), active_instruction as i16, ui, ); } if ui_state.show_keypad { GemmaImguiSupport::keypad_display(system.state(), ui); } }); }