riscii

An emulator for the RISC II
Log | Files | Refs | LICENSE

commit c90302e7c736484e0a5bb66117837dbdea047029
parent fc41f83e8121b05d5179b79a22aa17d631dd7f10
Author: Ryan Jeffrey <ryan@ryanmj.xyz>
Date:   Fri, 11 Nov 2022 15:39:18 -0800

Use reference counted global system for mutability

Diffstat:
Msrc/data_path.rs | 3---
Msrc/debug_window.rs | 27+++++++++++++++++++--------
Msrc/main.rs | 13++++++++-----
Msrc/system.rs | 11+++++++++++
Msrc/window.rs | 15++++++++++-----
5 files changed, 48 insertions(+), 21 deletions(-)

diff --git a/src/data_path.rs b/src/data_path.rs @@ -14,13 +14,10 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. use alu::ALU; -use config::Config; use cpu::{OutputPins, ProcessorStatusWord, RegisterFile, SIZEOF_INSTRUCTION}; use instruction::*; -use memory::Memory; use shifter::Shifter; use std::fmt; -use util::Result; pub struct SCCBits { pub z: bool, diff --git a/src/debug_window.rs b/src/debug_window.rs @@ -21,12 +21,14 @@ use sdl2::keyboard::Keycode; use sdl2::pixels::*; use sdl2::rect::Rect; use sdl2::ttf::{Font, Sdl2TtfContext}; +use std::cell::RefCell; +use std::rc::Rc; use system::System; use util::Result; pub struct DebugWindow<'a> { pane: Pane, - system: &'a System, + system: Rc<RefCell<System>>, config: &'a Config, font: Font<'a, 'static>, } @@ -34,7 +36,7 @@ pub struct DebugWindow<'a> { impl<'a> DebugWindow<'a> { pub fn new( config: &'a Config, - system: &'a System, + system: Rc<RefCell<System>>, context: &mut Context, ttf: &'a mut Sdl2TtfContext, ) -> Result<Self> { @@ -47,9 +49,9 @@ impl<'a> DebugWindow<'a> { let debug_font = { ttf.load_font("debug.otf", 20)? }; Ok(Self { font: debug_font, - pane: pane, - system: system, - config: config, + pane, + system, + config, }) } @@ -121,11 +123,13 @@ impl<'a> Drawable for DebugWindow<'a> { const OBJ_DEFAULT_COLOR: Color = Color::RGB(0xFF, 0xFF, 0xFF); const OBJ_USE_COLOR: Color = Color::RGB(0xFa, 0x10, 0x10); - let dp = self.system.data_path(); // Data path reference. + let system = self.system.clone(); + let system = system.borrow(); + let dp = system.data_path(); // Data path reference. // Describe the phase of the clock. self.draw_static_str( - match self.system.phase() { + match system.phase() { Phase::One => "φ₁", Phase::Two => "φ₂", Phase::Three => "φ₃", @@ -448,7 +452,14 @@ impl<'a> Drawable for DebugWindow<'a> { Ok(()) } - fn handle_key_down(&mut self, kc: Keycode) {} + fn handle_key_down(&mut self, kc: Keycode) { + match kc { + Keycode::P => { + self.system.clone().borrow_mut().toggle_pause(); + } + _ => {} + } + } fn handle_key_up(&mut self, kc: Keycode) {} fn get_window_id(&self) -> u32 { self.pane.get_id() diff --git a/src/main.rs b/src/main.rs @@ -44,7 +44,9 @@ use config::Config; use debug_window::DebugWindow; use sdl::{make_font_context, Context, Drawable}; use sdl2::event::{Event, WindowEvent}; +use std::cell::RefCell; use std::error::Error; +use std::rc::Rc; use system::System; use window::MainWindow; @@ -123,21 +125,21 @@ fn handle_events( fn main() -> Result<(), Box<dyn Error>> { let config = Config::init()?; - let system = System::new(&config)?; println!( "Running emulator with the following configuration: \n{}\n", config ); + let system = Rc::new(RefCell::new(System::new(&config)?)); //println!("Opening binary file {}.", path); //let program = fs::read(path)?; let mut sdl_context = Context::new()?; let mut font_context = make_font_context()?; - let mut main_window = MainWindow::new(&config, &system, &mut sdl_context)?; + let mut main_window = MainWindow::new(&config, system.clone(), &mut sdl_context)?; let mut debug_window = if config.is_debug_mode() { Some(DebugWindow::new( &config, - &system, + system.clone(), &mut sdl_context, &mut font_context, )?) @@ -155,13 +157,14 @@ fn main() -> Result<(), Box<dyn Error>> { } _ => {} } + system.borrow_mut().tick(); debug_window = if let Some(mut win) = debug_window { - win.draw(&mut sdl_context); + win.draw(&mut sdl_context)?; Some(win) } else { None }; - main_window.draw(&mut sdl_context); + main_window.draw(&mut sdl_context)?; } Ok(()) } diff --git a/src/system.rs b/src/system.rs @@ -38,6 +38,8 @@ pub struct System { pins_out: OutputPins, /// True if the pipeline is currently suspended as a result of a memory operation. pipeline_suspended: bool, + /// True if the system's emulation is paused, false if not. + is_paused: bool, } impl System { @@ -51,6 +53,7 @@ impl System { phase: Phase::One, pins_out: OutputPins::new(), pipeline_suspended: false, + is_paused: false, }) } @@ -58,7 +61,15 @@ impl System { &mut self.mem } + pub fn toggle_pause(&mut self) { + self.is_paused = !self.is_paused + } + pub fn tick(&mut self) { + if self.is_paused { + return; + } + let cur_phase = self.phase.clone(); self.clock.tick_and_wait(cur_phase); diff --git a/src/window.rs b/src/window.rs @@ -17,15 +17,16 @@ use config::Config; use sdl::{Context, Drawable, Pane}; -use sdl2::event::{Event, WindowEvent}; use sdl2::keyboard::Keycode; use sdl2::pixels::*; +use std::cell::RefCell; +use std::rc::Rc; use system::System; use util::Result; pub struct MainWindow<'a> { pane: Pane, - system: &'a System, + system: Rc<RefCell<System>>, config: &'a Config, } @@ -49,7 +50,11 @@ impl<'a> Drawable for MainWindow<'a> { } impl<'a> MainWindow<'a> { - pub fn new(config: &'a Config, system: &'a System, context: &mut Context) -> Result<Self> { + pub fn new( + config: &'a Config, + system: Rc<RefCell<System>>, + context: &mut Context, + ) -> Result<Self> { Ok(Self { pane: Pane::new( config.get_win_width(), @@ -57,8 +62,8 @@ impl<'a> MainWindow<'a> { format!("RISC II"), context, )?, - system: system, - config: config, + system, + config, }) } }