diff options
Diffstat (limited to 'src/emulators/graphical_emulator.rs')
-rw-r--r-- | src/emulators/graphical_emulator.rs | 396 |
1 files changed, 182 insertions, 214 deletions
diff --git a/src/emulators/graphical_emulator.rs b/src/emulators/graphical_emulator.rs index 14848c6..503a8f4 100644 --- a/src/emulators/graphical_emulator.rs +++ b/src/emulators/graphical_emulator.rs @@ -1,128 +1,42 @@ use crate::*; -use bedrock_core::*; - -use phosphor::*; - -use std::time::Instant; - - -pub struct GraphicalDeviceBus { - pub sys: SystemDevice, - pub mem: MemoryDevice, - pub mat: MathDevice, - pub clk: ClockDevice, - pub inp: InputDevice, - pub scr: ScreenDevice, - pub loc: LocalDevice, - pub rem: RemoteDevice, - pub fs1: FileDevice, - pub fs2: FileDevice, -} - -impl GraphicalDeviceBus { - pub fn new(config: &EmulatorConfig) -> Self { - Self { - sys: SystemDevice::new(), - mem: MemoryDevice::new(), - mat: MathDevice::new(), - clk: ClockDevice::new(), - inp: InputDevice::new(), - scr: ScreenDevice::new(config), - loc: LocalDevice::new(config), - rem: RemoteDevice::new(), - fs1: FileDevice::new(), - fs2: FileDevice::new(), - } - } - - pub fn graphical(&self) -> bool { - self.inp.accessed || self.scr.accessed - } -} - -impl DeviceBus for GraphicalDeviceBus { - fn read(&mut self, port: u8) -> u8 { - match port & 0xf0 { - 0x00 => self.sys.read(port & 0x0f), - 0x10 => self.mem.read(port & 0x0f), - 0x20 => self.mat.read(port & 0x0f), - 0x30 => self.clk.read(port & 0x0f), - 0x40 => self.inp.read(port & 0x0f), - 0x50 => self.scr.read(port & 0x0f), - 0x80 => self.loc.read(port & 0x0f), - 0x90 => self.rem.read(port & 0x0f), - 0xa0 => self.fs1.read(port & 0x0f), - 0xb0 => self.fs2.read(port & 0x0f), - _ => 0 - } - } - - fn write(&mut self, port: u8, value: u8) -> Option<Signal> { - match port & 0xf0 { - 0x00 => self.sys.write(port & 0x0f, value), - 0x10 => self.mem.write(port & 0x0f, value), - 0x20 => self.mat.write(port & 0x0f, value), - 0x30 => self.clk.write(port & 0x0f, value), - 0x40 => self.inp.write(port & 0x0f, value), - 0x50 => self.scr.write(port & 0x0f, value), - 0x80 => self.loc.write(port & 0x0f, value), - 0x90 => self.rem.write(port & 0x0f, value), - 0xa0 => self.fs1.write(port & 0x0f, value), - 0xb0 => self.fs2.write(port & 0x0f, value), - _ => None - } - } - - fn wake(&mut self) -> bool { - macro_rules! rouse { - ($id:expr, $dev:ident) => { - if self.sys.can_wake($id) && self.$dev.wake() { - self.sys.wake_id = $id; - self.sys.asleep = false; - return true; - } - }; - } - rouse!(0xb, fs2); - rouse!(0xa, fs1); - rouse!(0x9, rem); - rouse!(0x8, loc); - rouse!(0x5, scr); - rouse!(0x4, inp); - rouse!(0x3, clk); - rouse!(0x2, mat); - rouse!(0x1, mem); - rouse!(0x0, sys); - return false; - } -} - pub struct GraphicalEmulator { pub br: BedrockEmulator<GraphicalDeviceBus>, pub debug: DebugState, - pub dimensions: ScreenDimensions, + #[cfg(feature = "gamepad")] + pub gilrs: Option<Gilrs>, + pub fullscreen: bool, + pub visible: bool, pub scale: u32, - pub render_mark: Instant, - pub debug_palette: Option<[Colour; 16]>, - pub show_debug_palette: bool, - pub show_cursor: bool, + pub render_mark: Instant, // last time screen was rendered + pub frame_mark: Instant, // refreshes when clean + pub replace_palette: bool, + pub config: EmulatorConfig, } impl GraphicalEmulator { - pub fn new(config: &EmulatorConfig, debug: bool, verbose: bool) -> Self { - let devices = GraphicalDeviceBus::new(config); + pub fn new(config: EmulatorConfig, debug: bool) -> Self { Self { - br: BedrockEmulator::new(devices), - debug: DebugState::new(debug, verbose, config.symbols_path.as_ref()), - dimensions: config.dimensions, + br: BedrockEmulator::new(GraphicalDeviceBus::new(&config)), + debug: DebugState::new(debug, config.symbols_path.as_ref()), + + #[cfg(feature = "gamepad")] + gilrs: match Gilrs::new() { + Ok(gilrs) => Some(gilrs), + Err(err) => { + info!("Could not start gamepad listener: {}", err); + None + } + }, + fullscreen: config.fullscreen, - scale: config.scale, + scale: config.zoom.into(), + replace_palette: config.palette.is_some(), render_mark: Instant::now(), - debug_palette: config.debug_palette, - show_debug_palette: config.debug_palette.is_some(), - show_cursor: config.show_cursor, + frame_mark: Instant::now(), + config, + visible: false, } } @@ -130,70 +44,108 @@ impl GraphicalEmulator { self.br.core.mem.load_program(bytecode); } - pub fn run(&mut self) -> EmulatorSignal { - loop { - match self.br.evaluate(BATCH_SIZE, self.debug.enabled) { - Some(Signal::Fork) => { - self.br.core.mem.pc = 0; - self.br.core.wst.sp = 0; - self.br.core.rst.sp = 0; - } - Some(Signal::Sleep) => loop { - if self.br.dev.graphical() { - return EmulatorSignal::Promote; - } - if self.br.dev.wake() { break; } - std::thread::sleep(MIN_TICK_DURATION); - } - Some(Signal::Halt) => { - self.br.dev.loc.flush(); - self.debug.info("Program halted, exiting."); - self.debug.debug_summary(&self.br.core); - return EmulatorSignal::Halt; - } - Some(Signal::Debug1) => { - self.debug.debug_summary(&self.br.core); - } - _ => (), - } - - if self.br.dev.graphical() { - return EmulatorSignal::Promote; - } - } + pub fn run(mut self, mut phosphor: Phosphor, visible: bool) { + self.visible = visible; + let window = WindowBuilder { + dimensions: Some(self.dimensions()), + size_bounds: Some(self.size_bounds()), + fullscreen: self.fullscreen, + scale: self.scale, + title: Some(self.config.title.clone()), + cursor: match self.config.show_cursor { + true => Some(CursorIcon::Default), + false => None, + }, + icon: self.config.icon.clone(), + program: Box::new(self), + visible, + }; + phosphor.add_window(window); + phosphor.run().unwrap(); } pub fn size_bounds(&self) -> SizeBounds { - macro_rules! to_u32 { - ($opt:expr) => { - match $opt { - Some(a) => Some(u32::from(a)), - None => None, - } - }; - } match self.fullscreen { true => SizeBounds { - min_width: None, - max_width: None, + min_width: None, + max_width: None, min_height: None, max_height: None, }, false => SizeBounds { - min_width: to_u32!(self.br.dev.scr.fixed_width), - max_width: to_u32!(self.br.dev.scr.fixed_width), - min_height: to_u32!(self.br.dev.scr.fixed_height), - max_height: to_u32!(self.br.dev.scr.fixed_height), + min_width: self.br.dev.screen.fixed_width.map(u32::from), + max_width: self.br.dev.screen.fixed_width.map(u32::from), + min_height: self.br.dev.screen.fixed_height.map(u32::from), + max_height: self.br.dev.screen.fixed_height.map(u32::from), }, - } } pub fn dimensions(&self) -> Dimensions { Dimensions { - width: u32::from(self.br.dev.scr.dimensions.width), - height: u32::from(self.br.dev.scr.dimensions.height), + width: self.br.dev.screen.dimensions.width.into(), + height: self.br.dev.screen.dimensions.height.into(), + } + } +} + + +pub struct GraphicalDeviceBus { + pub system: SystemDevice, + pub memory: MemoryDevice, + pub math: MathDevice, + pub clock: ClockDevice, + pub input: InputDevice, + pub screen: ScreenDevice, + pub stream: StreamDevice, + pub file: FileDevice, + pub wake_queue: WakeQueue, +} + +impl GraphicalDeviceBus { + pub fn new(config: &EmulatorConfig) -> Self { + Self { + system: SystemDevice::new(0b1111_1100_1100_0000), + memory: MemoryDevice::new(), + math: MathDevice::new(), + clock: ClockDevice::new(), + input: InputDevice::new(), + screen: ScreenDevice::new(&config), + stream: StreamDevice::new(&config), + file: FileDevice::new(&config), + wake_queue: WakeQueue::new(), + } + } +} + + +impl DeviceBus for GraphicalDeviceBus { + fn get_device(&mut self, slot: u8) -> Option<&mut dyn Device> { + match slot { + 0x0 => Some(&mut self.system), + 0x1 => Some(&mut self.memory), + 0x2 => Some(&mut self.math ), + 0x3 => Some(&mut self.clock ), + 0x4 => Some(&mut self.input ), + 0x5 => Some(&mut self.screen), + 0x8 => Some(&mut self.stream), + 0x9 => Some(&mut self.file ), + _ => None + } + } + + fn wake(&mut self) -> bool { + for slot in self.wake_queue.iter(self.system.wake_mask) { + if let Some(device) = self.get_device(slot) { + if device.wake() { + self.system.wake_slot = slot; + self.system.asleep = false; + self.wake_queue.wake(slot); + return true; + } + } } + return false; } } @@ -202,33 +154,31 @@ impl WindowProgram for GraphicalEmulator { fn handle_event(&mut self, event: Event, r: &mut EventWriter<Request>) { match event { Event::CloseRequest => r.write(Request::CloseWindow), - Event::CursorEnter => self.br.dev.inp.on_cursor_enter(), - Event::CursorExit => self.br.dev.inp.on_cursor_exit(), - Event::CursorMove(p) => self.br.dev.inp.on_cursor_move(p), - Event::Resize(d) => self.br.dev.scr.resize(d), - Event::CharacterInput(c) => self.br.dev.inp.on_character(c), - Event::ModifierChange(m) => self.br.dev.inp.on_modifier(m), - Event::MouseButton { button, action } => - self.br.dev.inp.on_mouse_button(button, action), + Event::CursorEnter => self.br.dev.input.on_cursor_enter(), + Event::CursorExit => self.br.dev.input.on_cursor_exit(), + Event::CursorMove(p) => self.br.dev.input.on_cursor_move(p), + Event::Resize(d) => self.br.dev.screen.resize(d), + Event::CharacterInput(c) => self.br.dev.input.on_character(c), + Event::ModifierChange(m) => self.br.dev.input.on_modifier(m), + Event::MouseButton { button, action } => self.br.dev.input.on_mouse_button(button, action), Event::FocusChange(_) => (), Event::Initialise => (), Event::ScrollLines { axis, distance } => match axis { - Axis::Horizontal => self.br.dev.inp.on_horizontal_scroll(distance), - Axis::Vertical => self.br.dev.inp.on_vertical_scroll(distance), + Axis::Horizontal => self.br.dev.input.on_horizontal_scroll(distance), + Axis::Vertical => self.br.dev.input.on_vertical_scroll(distance), } Event::ScrollPixels { axis, distance } => match axis { - Axis::Horizontal => self.br.dev.inp.on_horizontal_scroll(distance / 20.0), - Axis::Vertical => self.br.dev.inp.on_vertical_scroll(distance / 20.0), + Axis::Horizontal => self.br.dev.input.on_horizontal_scroll(distance / 20.0), + Axis::Vertical => self.br.dev.input.on_vertical_scroll(distance / 20.0), } - Event::FileDrop(_path) => todo!("FileDrop"), Event::Close => (), Event::KeyboardInput { key, action } => { - self.br.dev.inp.on_keypress(key, action); + self.br.dev.input.on_keypress(key, action); if action == Action::Pressed { match key { KeyCode::F2 => { - self.show_debug_palette = !self.show_debug_palette; + self.replace_palette = !self.replace_palette; r.write(Request::Redraw); }, KeyCode::F5 => { @@ -247,83 +197,101 @@ impl WindowProgram for GraphicalEmulator { } } } + _ => (), } } fn process(&mut self, requests: &mut EventWriter<Request>) { - self.br.dev.loc.flush(); + self.br.dev.stream.flush(); + + #[cfg(feature = "gamepad")] + if let Some(gilrs) = &mut self.gilrs { + while let Some(event) = gilrs.next_event() { + self.br.dev.input.on_gamepad_event(event); + } + } - if self.br.dev.sys.asleep { + if self.br.dev.system.asleep { // Stay asleep if there are no pending wake events. if !self.br.dev.wake() { - if self.br.dev.scr.dirty { + if self.br.dev.screen.dirty { requests.write(Request::Redraw); } - std::thread::sleep(MIN_TICK_DURATION); + std::thread::sleep(TICK_DURATION); return; } // Wait for the current frame to be rendered. - if self.br.dev.scr.dirty { - if self.render_mark.elapsed() > MIN_FRAME_DURATION { - requests.write(Request::Redraw); - } - std::thread::sleep(MIN_TICK_DURATION); + if self.br.dev.screen.dirty { + requests.write(Request::Redraw); + std::thread::sleep(TICK_DURATION); return; } } // Run the processor for the remainder of the frame. - let frame_end = Instant::now() + MIN_TICK_DURATION; + let frame_end = Instant::now() + TICK_DURATION; while Instant::now() < frame_end { - match self.br.evaluate(BATCH_SIZE, self.debug.enabled) { - Some(Signal::Fork) => { - todo!("Fork") - } - Some(Signal::Sleep) => { - self.br.dev.sys.asleep = true; - break; - } - Some(Signal::Halt) => { - self.br.dev.loc.flush(); - self.debug.info("Program halted, exiting."); - self.debug.debug_summary(&self.br.core); - requests.write(Request::CloseWindow); - break; - } - Some(Signal::Debug1) => { - self.debug.debug_summary(&self.br.core); + if let Some(signal) = self.br.evaluate(BATCH_SIZE, self.debug.enabled) { + match signal { + Signal::Break => { + } + Signal::Fork | Signal::Reset => { + self.br.reset(); + } + Signal::Sleep => { + self.br.dev.system.asleep = true; + break; + } + Signal::Halt => { + self.br.dev.stream.flush(); + info!("Program halted, exiting."); + self.debug.debug_full(&self.br.core); + requests.write(Request::CloseWindow); + break; + } + Signal::Debug(debug_signal) => match debug_signal { + Debug::Debug1 => self.debug.debug_full(&self.br.core), + Debug::Debug2 => self.debug.debug_timing(&self.br.core), + _ => (), + } } - _ => (), } } - if std::mem::take(&mut self.br.dev.scr.dirty_dimensions) { + if !self.visible { + if self.br.dev.input.accessed || self.br.dev.screen.accessed { + info!("Making window visible"); + requests.write(Request::SetVisible(true)); + self.visible = true; + } + } + + if std::mem::take(&mut self.br.dev.screen.dirty_dimensions) { requests.write(Request::SetSizeBounds(self.size_bounds())); } - if self.br.dev.scr.dirty { - let elapsed = self.render_mark.elapsed(); - if self.br.dev.sys.asleep && elapsed > MIN_FRAME_DURATION { + if self.br.dev.screen.dirty { + if self.br.dev.system.asleep { requests.write(Request::Redraw); - } else if elapsed > MAX_FRAME_DURATION { + } else if self.frame_mark.elapsed() > MAX_FRAME_DURATION { requests.write(Request::Redraw); } } else { - self.render_mark = Instant::now(); + self.frame_mark = Instant::now(); } } fn render(&mut self, buffer: &mut Buffer, _full: bool) { - let screen = &mut self.br.dev.scr; + let screen = &mut self.br.dev.screen; // Generate table for calculating pixel colours from layer values. // A given screen pixel will be rendered as the colour given by // table[fg][bg], where fg and bg are the corresponding layer values. let mut table = [Colour::BLACK; 256]; - let palette = match self.debug_palette { - Some(debug_palette) => match self.show_debug_palette { - true => debug_palette, + let palette = match self.config.palette { + Some(palette) => match self.replace_palette { + true => palette, false => screen.palette, } None => screen.palette, @@ -338,7 +306,6 @@ impl WindowProgram for GraphicalEmulator { let bg = screen.bg[i]; let index = unsafe { fg.unchecked_shl(4) | bg }; *colour = table[index as usize]; - // TODO: merge fg and bg: *colour = table[screen.bg[i] as usize]; } // Copy pixels to buffer when it is a different size to the screen. @@ -374,5 +341,6 @@ impl WindowProgram for GraphicalEmulator { screen.dirty = false; self.render_mark = Instant::now(); + self.frame_mark = Instant::now(); } } |