control moved into gemma for launch/stop/step of cpu core

This commit is contained in:
Trevor Merritt 2024-10-18 10:52:39 -04:00
parent d829b9f03f
commit 1eeb746ccf
3 changed files with 47 additions and 68 deletions

View File

@ -44,7 +44,7 @@ impl Chip8SystemMemory {
} }
} }
pub fn peek(self, address: u16) -> u8 { pub fn peek(&self, address: u16) -> u8 {
trace!("PEEK: {} / {}", address, self.memory[address as usize].clone()); trace!("PEEK: {} / {}", address, self.memory[address as usize].clone());
self.memory[address as usize] self.memory[address as usize]
} }

View File

@ -1,9 +1,10 @@
use std::time::Instant; use std::time::Instant;
use crate::support::gemma_egui_support::GemmaEguiSupport; use crate::support::gemma_egui_support::{EGuiFileList, GemmaEguiSupport};
use crate::support::gemma_egui_state::GemmaEGuiState; use crate::support::gemma_egui_state::GemmaEGuiState;
use eframe::egui; use eframe::egui;
use egui::Ui; use egui::Ui;
use gemma::chip8::computer::Chip8Computer; use gemma::chip8::computer::Chip8Computer;
use gemma::chip8::computer_manager::Chip8ComputerManager;
mod support; mod support;
@ -15,8 +16,8 @@ fn main() -> eframe::Result {
..Default::default() ..Default::default()
}; };
let mut state = GemmaEGuiState::default(); // let mut state = GemmaEGuiState::default();
let mut computer = Chip8Computer::new(); let mut computer = Chip8ComputerManager::new();
let mut cps_counter = 0; let mut cps_counter = 0;
let mut last_counter_update = Instant::now(); let mut last_counter_update = Instant::now();
let cps_refresh = 5; let cps_refresh = 5;
@ -25,39 +26,48 @@ fn main() -> eframe::Result {
eframe::run_simple_native("EGUI Emma", options, move |ctx, _frame| { eframe::run_simple_native("EGUI Emma", options, move |ctx, _frame| {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
let should_render_frame = Instant::now().duration_since(last_frame_start).as_millis() >= 10; let local_computer = computer.state();
//if state.display_video {
if should_render_frame { GemmaEguiSupport::video_view(local_computer, ui);
last_frame_start = Instant::now(); // }
if state.display_video {
GemmaEguiSupport::video_view(&computer, ui);
}
ui.heading("EGUI Gemma"); ui.heading("EGUI Gemma");
GemmaEguiSupport::controls_view(&mut computer, &mut state, ui);
// if state.display_memory {
GemmaEguiSupport::memory_view(&local_computer, ui);
// }
if state.display_memory { // if state.display_registers {
GemmaEguiSupport::memory_view(&computer, &mut state, ui); GemmaEguiSupport::registers_view(&local_computer, ui);
// }
computer.tick();
ui.with_layout(egui::Layout::left_to_right(egui::Align::TOP), |ui| {
if ui.button("Start").clicked() {
computer.core_should_run = true;
}
if ui.button("Step").clicked() {
computer.one_step = true;
} }
if state.display_registers { if ui.button("Stop").clicked() {
GemmaEguiSupport::registers_view(&computer, ui); computer.core_should_run = false;
// state.is_running = false;
} }
if ui.button("Reset").clicked() {
computer.reset();
// state.is_running = false;
} }
});
//
// if ui.button(format!("Load {}", state.selected_rom_filename)).clicked() {
// // load the bin...
// let read_bin = std::fs::read(PathBuf::from(format!("resources/roms/{}", state.selected_rom_filename))).unwrap();
// // ...then feed the system.
// system.load_bytes_to_memory(0x200, &read_bin);
// println!("Loaded {}", state.selected_rom_filename);
// }
// EGuiFileList::display_path(PathBuf::from("resources/roms"), &mut state.selected_rom_filename, ui);
if state.is_running {
computer.step_system();
cps_counter += 1;
}
let rt = Instant::now();
let ms = rt.duration_since(last_counter_update).as_millis();
if ms > 5000 {
let cps = (cps_counter * 1000) / ms;
println!("Executing {cps} instructions per 5s ({ms}/{cps_counter})");
last_counter_update = rt;
cps_counter = 0;
}
}); });
}) })
} }

View File

@ -4,12 +4,10 @@ use std::path::{Display, PathBuf};
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
use egui::{Align, Color32, ComboBox, Direction, Pos2, Response, TextBuffer}; use egui::{Align, Color32, ComboBox, Direction, Pos2, Response, TextBuffer};
use egui::accesskit::Role::ListBox;
use egui::Rect; use egui::Rect;
use egui::Vec2; use egui::Vec2;
use egui::Ui; use egui::Ui;
use egui::WidgetType::SelectableLabel; use egui::WidgetType::SelectableLabel;
use crate::support::gemma_egui_state::GemmaEGuiState;
use crate::Chip8Computer; use crate::Chip8Computer;
const CELL_WIDTH: f32 = 5.0; const CELL_WIDTH: f32 = 5.0;
@ -48,41 +46,12 @@ impl EGuiFileList {
pub struct GemmaEguiSupport {} pub struct GemmaEguiSupport {}
impl GemmaEguiSupport { impl GemmaEguiSupport {
pub fn controls_view(mut system: &mut Chip8Computer, state: &mut GemmaEGuiState, ui: &mut Ui) { pub fn controls_view(system: &mut Chip8Computer, ui: &mut Ui) {
ui.with_layout(egui::Layout::left_to_right(egui::Align::TOP), |ui| {
if ui.button("Start").clicked() {
println!("Start");
state.is_running = true;
}
if ui.button("Step").clicked() {
system.step_system();
}
if ui.button("Stop").clicked() {
println!("STOP");
state.is_running = false;
}
if ui.button("Reset").clicked() {
system.reset();
state.is_running = false;
}
});
if ui.button(format!("Load {}", state.selected_rom_filename)).clicked() {
// load the bin...
let read_bin = std::fs::read(PathBuf::from(format!("resources/roms/{}", state.selected_rom_filename))).unwrap();
// ...then feed the system.
system.load_bytes_to_memory(0x200, &read_bin);
println!("Loaded {}", state.selected_rom_filename);
}
EGuiFileList::display_path(PathBuf::from("resources/roms"), &mut state.selected_rom_filename, ui);
ui.with_layout(egui::Layout::left_to_right(Align::TOP), |ui| { ui.with_layout(egui::Layout::left_to_right(Align::TOP), |ui| {
ui.checkbox(&mut state.display_memory, "Display Memory"); // ui.checkbox(&mut state.display_memory, "Display Memory");
ui.checkbox(&mut state.display_video, "Display Video"); // ui.checkbox(&mut state.display_video, "Display Video");
ui.checkbox(&mut state.display_registers, "Display Registers"); // ui.checkbox(&mut state.display_registers, "Display Registers");
}); });
} }
@ -133,10 +102,10 @@ impl GemmaEguiSupport {
// thread::sleep(Duration::from_secs(1)); // thread::sleep(Duration::from_secs(1));
} }
pub fn memory_view(system: &Chip8Computer, gui_state: &mut GemmaEGuiState, ui: &mut Ui) { pub fn memory_view(system: &Chip8Computer, ui: &mut Ui) {
ui.label("Memory View"); ui.label("Memory View");
for i in (gui_state.memory_view_min..gui_state.memory_view_max).step_by(16) { for i in (0..=0x200).step_by(16) {
ui.with_layout(egui::Layout::left_to_right(egui::Align::TOP), |ui| { ui.with_layout(egui::Layout::left_to_right(egui::Align::TOP), |ui| {
for y in 0..16 { for y in 0..16 {
ui.label(format!("{:02x}", system.memory.peek((i + y) as u16)).as_str()); ui.label(format!("{:02x}", system.memory.peek((i + y) as u16)).as_str());