prep for trait code

This commit is contained in:
Trevor Merritt 2024-12-04 10:41:08 -05:00
parent cddbe0c46e
commit 494a73eb89
10 changed files with 474 additions and 201 deletions

554
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,5 @@
[workspace] [workspace]
members = [ members = ["gemma", "gemmaegui", "gemmaimgui", "gemmatelnet", "gemmasdl2"]
"gemma",
"gemmaegui",
"gemmaimgui",
"gemmatelnet",
"gemmasdl2",
]
resolver = "2" resolver = "2"
[workspace.dependencies] [workspace.dependencies]

View File

@ -24,7 +24,9 @@ pub struct Chip8Computer {
pub keypad: Keypad, pub keypad: Keypad,
pub stack: Chip8Stack, pub stack: Chip8Stack,
pub quirk_mode: QuirkMode, pub quirk_mode: QuirkMode,
pub target_ips: u16,
} }
impl Default for Chip8Computer { impl Default for Chip8Computer {
fn default() -> Self { fn default() -> Self {
Self { Self {
@ -38,6 +40,7 @@ impl Default for Chip8Computer {
keypad: Keypad::default(), keypad: Keypad::default(),
stack: Chip8Stack::default(), stack: Chip8Stack::default(),
quirk_mode: QuirkMode::default(), quirk_mode: QuirkMode::default(),
target_ips: 20,
} }
} }
} }

View File

@ -31,6 +31,9 @@ impl Default for Chip8ComputerManager {
} }
impl Chip8ComputerManager { impl Chip8ComputerManager {
pub fn target_ips(&self) -> u16 {
self.computer.target_ips
}
pub fn quirks_mode(&self) -> QuirkMode { pub fn quirks_mode(&self) -> QuirkMode {
self.computer.quirk_mode.clone() self.computer.quirk_mode.clone()
} }
@ -118,7 +121,7 @@ impl Chip8ComputerManager {
self.computer.load_bytes_to_memory(0x200, &bytes_to_load); self.computer.load_bytes_to_memory(0x200, &bytes_to_load);
} }
pub fn dump_to_string(&self, dump_type: ManagerDumpables) -> String{ pub fn dump_to_string(&self, dump_type: ManagerDumpables) -> String {
match dump_type { match dump_type {
ManagerDumpables::Video => self.computer.video_memory.format_as_string(), ManagerDumpables::Video => self.computer.video_memory.format_as_string(),
ManagerDumpables::Registers => self.computer.registers.format_as_string(), ManagerDumpables::Registers => self.computer.registers.format_as_string(),

View File

@ -1,4 +1,5 @@
use crate::chip8::computer::Chip8Computer; use crate::chip8::computer::Chip8Computer;
use crate::chip8::cpu_states::Chip8CpuStates;
use crate::chip8::cpu_states::Chip8CpuStates::WaitingForKey; use crate::chip8::cpu_states::Chip8CpuStates::WaitingForKey;
use crate::chip8::instructions::Chip8CpuInstructions::*; use crate::chip8::instructions::Chip8CpuInstructions::*;
use crate::chip8::quirk_modes::QuirkMode; use crate::chip8::quirk_modes::QuirkMode;
@ -11,7 +12,6 @@ use std::ascii::AsciiExt;
use std::fmt::{Debug, Display, Formatter}; use std::fmt::{Debug, Display, Formatter};
use std::ops::BitAnd; use std::ops::BitAnd;
use std::time::Instant; use std::time::Instant;
use crate::chip8::cpu_states::Chip8CpuStates;
/* /*
nnn or addr - A 12-bit value, the lowest 12 bits of the instruction nnn or addr - A 12-bit value, the lowest 12 bits of the instruction
n or nibble - A 4-bit value, the lowest 4 bits of the instruction n or nibble - A 4-bit value, the lowest 4 bits of the instruction
@ -680,6 +680,7 @@ impl Chip8CpuInstructions {
_ => DW(addr_param), _ => DW(addr_param),
} }
} }
pub fn execute(&self, input: &mut Chip8Computer) -> Chip8Computer { pub fn execute(&self, input: &mut Chip8Computer) -> Chip8Computer {
println!("INSTRUCTION {}", self); println!("INSTRUCTION {}", self);
let start_time = Instant::now(); let start_time = Instant::now();
@ -1088,7 +1089,8 @@ impl Chip8CpuInstructions {
.memory .memory
.poke(offset + i as u16, input.registers.peek(i)); .poke(offset + i as u16, input.registers.peek(i));
} }
input.registers.poke_i(offset + 1); println!("OFFSET: {offset:04x} / X : {x:04x}");
input.registers.poke_i(offset + *x as u16);
} }
Chip8CpuInstructions::LDRI(x) => { Chip8CpuInstructions::LDRI(x) => {
// Read registers V0 through Vx from memory starting at location I. // Read registers V0 through Vx from memory starting at location I.

View File

@ -7,4 +7,6 @@ edition = "2021"
gemma = { path = "../gemma" } gemma = { path = "../gemma" }
egui.workspace = true egui.workspace = true
eframe.workspace = true eframe.workspace = true
catppuccin-egui = { version = "5.3.0", default-features = false, features = ["egui29"] } catppuccin-egui = { version = "5.3.0", default-features = false, features = [
"egui29",
] }

View File

@ -83,7 +83,7 @@ fn main() {
let target_ms = ui_state.frame_time; let target_ms = ui_state.frame_time;
let loop_start_time = Instant::now(); let loop_start_time = Instant::now();
while Instant::now().duration_since(current_time).as_millis() < target_ms as u128 while Instant::now().duration_since(current_time).as_millis() < target_ms as u128
&& num_cycles < ui_state.target_ips && num_cycles < system.target_ips()
{ {
if system.tick() { if system.tick() {
num_cycles += 1; num_cycles += 1;
@ -95,7 +95,7 @@ fn main() {
"Ran for {}ms and executed {}/{} cycles.", "Ran for {}ms and executed {}/{} cycles.",
cycles_time.as_millis(), cycles_time.as_millis(),
num_cycles, num_cycles,
ui_state.target_ips system.target_ips()
); );
} }
// GUI Parts // GUI Parts

View File

@ -11,9 +11,7 @@ use imgui::{CollapsingHeader, Condition, ImColor32, Ui};
use log::debug; use log::debug;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::path::{Path, PathBuf}; use std::path::Path;
use imgui::sys::ImGuiIO;
use gemma::chip8::instructions::Chip8CpuInstructions;
const ROM_ROOT: &str = "resources/roms"; const ROM_ROOT: &str = "resources/roms";
@ -46,7 +44,13 @@ impl GemmaImguiSupport {
// draw area size // draw area size
let (width, height) = system_to_control.video_memory.get_resolution(); let (width, height) = system_to_control.video_memory.get_resolution();
ui.window("Video") ui.window("Video")
.size([gui_state.video_window_size[0] as f32, gui_state.video_window_size[1] as f32], Condition::Once) .size(
[
gui_state.video_window_size[0] as f32,
gui_state.video_window_size[1] as f32,
],
Condition::Once,
)
.build(|| { .build(|| {
let draw_area_size = ui.window_size(); let draw_area_size = ui.window_size();
let draw_offset = ui.window_pos(); let draw_offset = ui.window_pos();
@ -55,8 +59,8 @@ impl GemmaImguiSupport {
// and reduce the draw area size by the same values. {} // and reduce the draw area size by the same values. {}
let draw_area_size = [draw_area_size[0], draw_area_size[1] - 20.0]; let draw_area_size = [draw_area_size[0], draw_area_size[1] - 20.0];
// println!("DRAW_AREA_SIZE = {}x{}", draw_area_size[0], draw_area_size[1]); // println!("DRAW_AREA_SIZE = {}x{}", draw_area_size[0], draw_area_size[1]);
let cell_width = ((draw_area_size[0] as i32 / width)); let cell_width = (draw_area_size[0] as i32 / width);
let cell_height = ((draw_area_size[1] as i32 / height)); let cell_height = (draw_area_size[1] as i32 / height);
let origin = draw_offset; let origin = draw_offset;
let fg = ui.get_foreground_draw_list(); let fg = ui.get_foreground_draw_list();
@ -86,15 +90,14 @@ impl GemmaImguiSupport {
); );
} }
} }
}).expect("cant draw the video i guess"); })
.expect("cant draw the video i guess");
} }
pub fn quirks_picker(system: &mut Chip8ComputerManager, pub fn quirks_picker(system: &mut Chip8ComputerManager, ui: &Ui) {
ui: &Ui) {
let selectors = [Chip8, SChipModern, XOChip]; let selectors = [Chip8, SChipModern, XOChip];
for current_selector in selectors { for current_selector in selectors {
let mut working_selector = let mut working_selector = ui.selectable_config(current_selector.clone().to_string());
ui.selectable_config(current_selector.clone().to_string());
match system.quirks_mode() { match system.quirks_mode() {
Chip8 => { Chip8 => {
working_selector = working_selector.selected(true); working_selector = working_selector.selected(true);
@ -113,12 +116,7 @@ impl GemmaImguiSupport {
} }
} }
fn control_pickers(system: &mut Chip8ComputerManager, gui_state: &mut ImGuiUiState, ui: &Ui) {
fn control_pickers(
system: &mut Chip8ComputerManager,
gui_state: &mut ImGuiUiState,
ui: &Ui,
) {
if CollapsingHeader::new("Controls").build(ui) { if CollapsingHeader::new("Controls").build(ui) {
// if the system has no program loaded hide the buttons. // if the system has no program loaded hide the buttons.
let bytes: [u8; 2] = [ let bytes: [u8; 2] = [
@ -158,20 +156,13 @@ impl GemmaImguiSupport {
} }
} }
fn system_summary( fn system_summary(system: &Chip8ComputerManager, ui: &Ui) {
system: &Chip8ComputerManager,
ui: &Ui,
) {
/* System Step Counter */ /* System Step Counter */
ui.text(format!("Step {:04x}", system.num_cycles()).as_str()); ui.text(format!("Step {:04x}", system.num_cycles()).as_str());
ui.text(format!("Mode {}", system.quirks_mode())); ui.text(format!("Mode {}", system.quirks_mode()));
} }
fn rom_lister( fn rom_lister(system: &mut Chip8ComputerManager, gui_state: &mut ImGuiUiState, ui: &Ui) {
system: &mut Chip8ComputerManager,
gui_state: &mut ImGuiUiState,
ui: &Ui,
) {
/* ROM Lister */ /* ROM Lister */
if CollapsingHeader::new("Roms").build(ui) { if CollapsingHeader::new("Roms").build(ui) {
let new_filename = GuiFileList::display_path( let new_filename = GuiFileList::display_path(
@ -208,10 +199,7 @@ impl GemmaImguiSupport {
} }
} }
fn display_options( fn display_options(gui_state: &mut ImGuiUiState, ui: &Ui) {
gui_state: &mut ImGuiUiState,
ui: &Ui,
) {
if CollapsingHeader::new("Options").build(ui) { if CollapsingHeader::new("Options").build(ui) {
ui.checkbox("Show Memory", &mut gui_state.show_memory); ui.checkbox("Show Memory", &mut gui_state.show_memory);
ui.same_line(); ui.same_line();
@ -220,8 +208,8 @@ impl GemmaImguiSupport {
ui.checkbox("Show Registers", &mut gui_state.show_registers); ui.checkbox("Show Registers", &mut gui_state.show_registers);
ui.same_line(); ui.same_line();
ui.checkbox("Show Keypad", &mut gui_state.show_keypad); ui.checkbox("Show Keypad", &mut gui_state.show_keypad);
ui.input_int("Target IPS", &mut gui_state.target_ips) // ui.input_int("Target IPS", &mut system.target_ips())
.build(); // .build();
}; };
} }

View File

@ -15,10 +15,9 @@ pub struct ImGuiUiState {
pub is_running: bool, pub is_running: bool,
pub frame_time: u32, pub frame_time: u32,
pub last_frame_instant: Instant, pub last_frame_instant: Instant,
pub target_ips: i32,
pub roms_root_path: PathBuf, pub roms_root_path: PathBuf,
pub video_window_size: [i32; 2], pub video_window_size: [i32; 2],
pub control_window_size: [i32; 2] pub control_window_size: [i32; 2],
} }
impl Clone for ImGuiUiState { impl Clone for ImGuiUiState {
@ -34,10 +33,9 @@ impl Clone for ImGuiUiState {
is_running: self.is_running, is_running: self.is_running,
frame_time: self.frame_time, frame_time: self.frame_time,
last_frame_instant: self.last_frame_instant, last_frame_instant: self.last_frame_instant,
target_ips: self.target_ips,
roms_root_path: self.roms_root_path.to_owned(), roms_root_path: self.roms_root_path.to_owned(),
video_window_size: self.video_window_size, video_window_size: self.video_window_size,
control_window_size: self.control_window_size control_window_size: self.control_window_size,
} }
} }
} }
@ -55,10 +53,9 @@ impl Default for ImGuiUiState {
is_running: false, is_running: false,
frame_time: 16, frame_time: 16,
last_frame_instant: Instant::now(), last_frame_instant: Instant::now(),
target_ips: 20,
roms_root_path: PathBuf::from(ROMPATH_DEFAULT), roms_root_path: PathBuf::from(ROMPATH_DEFAULT),
control_window_size: [200, 600], control_window_size: [200, 600],
video_window_size: [800, 600] video_window_size: [800, 600],
} }
} }
} }

30
imgui.ini Normal file
View File

@ -0,0 +1,30 @@
[Window][Debug##Default]
Pos=60,60
Size=400,400
Collapsed=0
[Window][Video]
Pos=576,0
Size=771,600
Collapsed=0
[Window][Keypad]
Pos=99,89
Size=76,99
Collapsed=0
[Window][!!!! CONTROLS !!!!]
Pos=30,209
Size=479,651
Collapsed=0
[Window][Registers]
Pos=530,647
Size=292,237
Collapsed=0
[Window][System Memory]
Pos=1428,-5
Size=385,1042
Collapsed=0