From e308362533f36e4e230b9907e54641d4c11e281f Mon Sep 17 00:00:00 2001 From: zxq5 Date: Thu, 19 Jun 2025 15:18:48 +0100 Subject: [PATCH] idk --- assembler/src/lexer.rs | 3 +- emulator/src/emulator/system/emulator.rs | 26 +++++++++++-- emulator/src/emulator/system/model.rs | 28 ++++++++++++-- emulator/src/emulator/system/processor/mod.rs | 3 ++ emulator/src/emulator/ui/control_unit.rs | 21 ++++++++++ emulator/src/emulator/ui/editor.rs | 24 ++++++------ emulator/src/emulator/ui/history.rs | 38 ++++--------------- emulator/src/emulator/ui/interface.rs | 34 +++++++++++++++-- resources/dsa/test.dsa | 6 ++- 9 files changed, 128 insertions(+), 55 deletions(-) diff --git a/assembler/src/lexer.rs b/assembler/src/lexer.rs index 5879317..ac6b14d 100644 --- a/assembler/src/lexer.rs +++ b/assembler/src/lexer.rs @@ -11,10 +11,9 @@ pub fn lexer(mut program: String, module: u64) -> Result, AssembleErr program = program.replace(",", ""); let lines = program.lines(); - let mut literal; + let mut literal = String::new(); for line in lines { - literal = String::new(); for token in line.split_whitespace() { if token.starts_with("//") { break; diff --git a/emulator/src/emulator/system/emulator.rs b/emulator/src/emulator/system/emulator.rs index a3416e8..485b158 100644 --- a/emulator/src/emulator/system/emulator.rs +++ b/emulator/src/emulator/system/emulator.rs @@ -10,7 +10,7 @@ use std::{ use crate::emulator::misc::rpc::{Activity, RpcClient}; use crate::emulator::system::{ - model::{Command, Running, State}, + model::{Command, PersistentState, Running, State}, processor::Processor, }; @@ -70,10 +70,26 @@ pub fn run_emulator( Command::Stop => { running = Running::Paused; } - Command::Reset => { + Command::Reset(x) => { running = Running::Paused; + + match x { + 0 => { + processor.clear(); + processor.reset(); + instruction_count = 0; + } + 1 => { + processor.reset(); + instruction_count = 0; + } + 2 => { + processor.clear(); + } + _ => unreachable!(), + } + processor.reset(); - instruction_count = 0; } Command::Step => { running = Running::Paused; @@ -158,6 +174,8 @@ pub fn run_emulator( memory_view, &mut history, ); + println!("running state"); + // println!("state!!! {:?}", state.history); let _ = state_tx.send(state); } } else { @@ -185,6 +203,6 @@ fn state( memory_view, display_view: cpu_lock.display(), error: None, - history: hsclone, + persistent: PersistentState { history: hsclone }, } } diff --git a/emulator/src/emulator/system/model.rs b/emulator/src/emulator/system/model.rs index 5fdceb1..59e5603 100644 --- a/emulator/src/emulator/system/model.rs +++ b/emulator/src/emulator/system/model.rs @@ -19,7 +19,7 @@ pub enum Command { Start, Stop, Step, - Reset, + Reset(usize), Interrupt(Interrupt), // Performs direct read/write operations on the emulator's memory. @@ -205,8 +205,9 @@ pub struct State { pub stack_view: Vec, pub memory_view: Vec, pub display_view: Vec, - pub history: Vec<(u32, Instruction)>, pub error: Option, + + pub persistent: PersistentState, } impl Default for State { @@ -218,8 +219,29 @@ impl Default for State { stack_view: vec![], memory_view: vec![], display_view: vec![], - history: vec![], + persistent: PersistentState::default(), error: None, } } } + +#[derive(Clone, Debug)] +pub struct PersistentState { + pub history: Vec<(u32, Instruction)>, +} + +impl Default for PersistentState { + fn default() -> Self { + Self { history: vec![] } + } +} + +impl PersistentState { + pub fn update(&mut self, new_state: &PersistentState) { + self.history.extend(new_state.history.clone()); + if self.history.len() > 32768 { + let len = self.history.len() - 32768; + self.history.drain(..len); + } + } +} diff --git a/emulator/src/emulator/system/processor/mod.rs b/emulator/src/emulator/system/processor/mod.rs index 64a049d..fcd16d0 100644 --- a/emulator/src/emulator/system/processor/mod.rs +++ b/emulator/src/emulator/system/processor/mod.rs @@ -35,6 +35,9 @@ impl Processor { // set all registers to zero // run memory.reset() self.registers.reset(); + } + + pub fn clear(&mut self) { self.memory.reset(); } diff --git a/emulator/src/emulator/ui/control_unit.rs b/emulator/src/emulator/ui/control_unit.rs index 8aade22..3d8046a 100644 --- a/emulator/src/emulator/ui/control_unit.rs +++ b/emulator/src/emulator/ui/control_unit.rs @@ -64,6 +64,27 @@ impl Component for ControlPanel { }); } + // Resets the emulator and all attached devices + if ui.button("Reset All").clicked() { + self.sender.send(Command::Reset(0)).unwrap_or_else(|_| { + state.error = Some("Failed to send command".to_string()); + }); + } + + // Resets the emulator and all attached devices + if ui.button("Clear Registers").clicked() { + self.sender.send(Command::Reset(1)).unwrap_or_else(|_| { + state.error = Some("Failed to send command".to_string()); + }); + } + + // Resets the emulator and all attached devices + if ui.button("Clear RAM").clicked() { + self.sender.send(Command::Reset(2)).unwrap_or_else(|_| { + state.error = Some("Failed to send command".to_string()); + }); + } + ui.separator(); // Status info diff --git a/emulator/src/emulator/ui/editor.rs b/emulator/src/emulator/ui/editor.rs index 555fcae..3209933 100644 --- a/emulator/src/emulator/ui/editor.rs +++ b/emulator/src/emulator/ui/editor.rs @@ -12,17 +12,24 @@ use crate::emulator::{ }; pub struct Editor { + // editor state path: Option, + unsaved: bool, text: String, buffer: String, - unsaved: bool, + + // output / loading output: Vec, - sender: Sender, - cursor_col: usize, - cursor_line: usize, - visible: bool, load_offset: u32, offset_str: String, + + // cursor - currently unused + cursor_col: usize, + cursor_line: usize, + + // other + visible: bool, + sender: Sender, error: Option, } @@ -372,13 +379,6 @@ impl Editor { self.error = Some("Invalid offset".to_string()); } } - - // Resets the emulator and all attached devices - if ui.button("Reset Emulator").clicked() { - self.sender.send(Command::Reset).unwrap_or_else(|_| { - self.error = Some("Failed to send command".to_string()); - }); - } }); } } diff --git a/emulator/src/emulator/ui/history.rs b/emulator/src/emulator/ui/history.rs index 1f0a95d..90f871c 100644 --- a/emulator/src/emulator/ui/history.rs +++ b/emulator/src/emulator/ui/history.rs @@ -1,19 +1,9 @@ -use std::{ffi::OsStr, path::PathBuf, sync::mpsc::Sender}; +use egui::{Context, Ui}; -use common::prelude::Instruction; -use egui::{Align, Context, Key, Layout, Ui}; -use rfd::FileDialog; - -use dsa_editor::{CodeEditor, ColorTheme, Syntax}; - -use crate::emulator::{ - system::model::{Command, State}, - ui::interface::Component, -}; +use crate::emulator::{system::model::State, ui::interface::Component}; pub struct History { visible: bool, - history: Vec<(u32, Instruction)>, } impl Component for History { @@ -29,14 +19,12 @@ impl Component for History { super::interface::Category::Control } - fn render(&mut self, state: &mut State, ui: &mut Ui, ctx: &Context) { - self.update(state); - + fn render(&mut self, state: &mut State, ui: &mut Ui, _ctx: &Context) { egui::ScrollArea::vertical() .id_salt("output_scroll") .max_width(400.0) .show(ui, |ui| { - if self.history.is_empty() { + if state.persistent.history.is_empty() { ui.label( egui::RichText::new("No output data") .font(egui::FontId::monospace(12.0)) @@ -51,7 +39,9 @@ impl Component for History { .striped(false) .show(ui, |ui| { // Process bytes in chunks of 4 - for (idx, instruction) in self.history.iter().enumerate() { + for (idx, instruction) in + state.persistent.history.iter().enumerate() + { ui.label(format!("{}: ", idx)); // Hex column @@ -78,18 +68,6 @@ impl Component for History { impl History { #[must_use] pub fn new() -> Self { - Self { - visible: false, - history: Vec::with_capacity(1000), - } - } - - fn update(&mut self, state: &mut State) { - self.history.extend(state.history.clone()); - state.history.clear(); - if self.history.len() > 1000 { - let len = self.history.len() - 1000; - self.history.drain(..len); - } + Self { visible: false } } } diff --git a/emulator/src/emulator/ui/interface.rs b/emulator/src/emulator/ui/interface.rs index 6ada6da..db10a0c 100644 --- a/emulator/src/emulator/ui/interface.rs +++ b/emulator/src/emulator/ui/interface.rs @@ -1,4 +1,4 @@ -use crate::emulator::system::model::{Command, Running, State}; +use crate::emulator::system::model::{Command, PersistentState, Running, State}; use std::sync::mpsc::{Receiver, Sender}; pub trait Component { @@ -37,6 +37,7 @@ pub struct EmulatorUI { pub sender: Sender, pub receiver: Receiver, pub state: State, + pub persistent: PersistentState, pub components: Vec>, } @@ -47,6 +48,7 @@ impl EmulatorUI { sender, receiver, state: State::default(), + persistent: PersistentState::default(), components: vec![], } } @@ -58,6 +60,8 @@ impl EmulatorUI { fn update_state(&mut self) { while let Ok(state) = self.receiver.try_recv() { self.state = state; + self.persistent.update(&self.state.persistent); + self.state.persistent = self.persistent.clone(); } } } @@ -105,8 +109,32 @@ impl eframe::App for EmulatorUI { }); }); - egui::TopBottomPanel::bottom("bottom_panel").show(ctx, |_ui| { - // interface::bottompanel::render_bottom_panel(self, ui, ctx); + egui::TopBottomPanel::bottom("bottom_panel").show(ctx, |ui| { + ui.horizontal_centered(|ui| { + ui.group(|ui| { + ui.add_space(10.0); + ui.strong("Authors:"); + ui.add_space(5.0); + ui.label("zxq5"); + ui.label("nullndvoid"); + ui.add_space(10.0); + ui.separator(); + ui.add_space(10.0); + ui.strong("Version"); + ui.add_space(5.0); + ui.label("1.0.0"); + ui.add_space(10.0); + ui.separator(); + ui.add_space(10.0); + ui.strong("Source:"); + ui.add_space(5.0); + ui.hyperlink_to( + "https://git.zxq5.dev/LowLevelDevs/damn_simple_architecture", + "https://git.zxq5.dev/LowLevelDevs/damn_simple_architecture", + ); + ui.add_space(10.0); + }); + }); }); } } diff --git a/resources/dsa/test.dsa b/resources/dsa/test.dsa index 405f8aa..26d21dd 100644 --- a/resources/dsa/test.dsa +++ b/resources/dsa/test.dsa @@ -2,7 +2,11 @@ include print "../resources/dsa/print.dsa" include fib "../resources/dsa/fib.dsa" dw stack: 0x10000 -db string: "An idiot admires complexity, a genius admires simplicity, a physicist tries to make it simple, for an idiot anything the more complicated it is the more he will admire it, if you make something so clusterfucked he can't understand it he's gonna think you're a god cause you made it so complicated nobody can understand it. That's how they write journals in Academics, they try to make it so complicated people think you're a genius" +db string: "An idiot admires complexity, a genius admires simplicity, +a physicist tries to make it simple, for an idiot anything the more complicated it is, +the more he will admire it, if you make something so clusterfucked he can't understand it he's +gonna think you're a god cause you made it so complicated nobody can understand it. +That's how they write journals in Academics, they try to make it so complicated people think you're a genius" init: ldw stack, bpr