diff --git a/common/src/instructions.rs b/common/src/instructions.rs index 609bea8..adca96e 100644 --- a/common/src/instructions.rs +++ b/common/src/instructions.rs @@ -116,6 +116,40 @@ impl TryFrom for Register { } } +impl TryFrom<&str> for Register { + type Error = RegisterParseError; + + fn try_from(value: &str) -> Result { + match value.to_lowercase().as_str() { + "rg0" => Ok(Self::Rg0), + "rg1" => Ok(Self::Rg1), + "rg2" => Ok(Self::Rg2), + "rg3" => Ok(Self::Rg3), + "rg4" => Ok(Self::Rg4), + "rg5" => Ok(Self::Rg5), + "rg6" => Ok(Self::Rg6), + "rg7" => Ok(Self::Rg7), + "rg8" => Ok(Self::Rg8), + "rg9" => Ok(Self::Rg9), + "rga" => Ok(Self::Rga), + "rgb" => Ok(Self::Rgb), + "rgc" => Ok(Self::Rgc), + "rgd" => Ok(Self::Rgd), + "rge" => Ok(Self::Rge), + "rgf" => Ok(Self::Rgf), + "acc" => Ok(Self::Acc), + "spr" => Ok(Self::Spr), + "bpr" => Ok(Self::Bpr), + "ret" => Ok(Self::Ret), + "idr" => Ok(Self::Idr), + "mmr" => Ok(Self::Mmr), + "zero" => Ok(Self::Zero), + "null" => Ok(Self::NoReg), + _ => Err(RegisterParseError::InvalidName(value.to_string())), + } + } +} + impl std::fmt::Display for Register { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { diff --git a/common/src/instructions/args.rs b/common/src/instructions/args.rs index 471ee3c..d4c9e48 100644 --- a/common/src/instructions/args.rs +++ b/common/src/instructions/args.rs @@ -16,6 +16,7 @@ impl From for ArgsDecodeError { fn from(value: RegisterParseError) -> Self { match value { RegisterParseError::InvalidIndex(idx) => Self::InvalidRegister(idx), + RegisterParseError::InvalidName(_) => Self::InvalidRegister(0xFF), } } } diff --git a/common/src/instructions/errors.rs b/common/src/instructions/errors.rs index ae70904..8961ae5 100644 --- a/common/src/instructions/errors.rs +++ b/common/src/instructions/errors.rs @@ -6,12 +6,14 @@ use crate::prelude::*; /// Error type for parsing register numbers. pub enum RegisterParseError { InvalidIndex(u8), + InvalidName(String), } impl std::fmt::Display for RegisterParseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::InvalidIndex(idx) => write!(f, "invalid index given ({idx})"), + Self::InvalidName(name) => write!(f, "invalid name given ({name})"), } } } diff --git a/emulator/src/emulator/ui/control_unit.rs b/emulator/src/emulator/ui/control_unit.rs index 10cf86d..33e982c 100644 --- a/emulator/src/emulator/ui/control_unit.rs +++ b/emulator/src/emulator/ui/control_unit.rs @@ -5,7 +5,7 @@ use crate::emulator::{ ui::interface::Component, }; -use common::instructions::Register; +use common::{instructions::Register, prelude::Instruction}; pub struct ControlPanel { visible: bool, @@ -77,11 +77,13 @@ impl Component for ControlPanel { )); ui.label(format!("Instructions: {}", state.instructions)); ui.label(format!("PC: 0x{:08X}", state.reg_file.get(Register::Pcx))); - ui.label(format!( - "Last Instruction: {:?}", - "TODO: DECODE INSTRUCTION" // TODO: decode instruction - // Instruction::decode(state.current_state.cir) - )); + + let instruction = match Instruction::decode(state.reg_file.get(Register::Cir)) { + Ok(instruction) => instruction.to_string(), + Err(_) => "Invalid Instruction".to_string(), + }; + + ui.label(format!("Instruction: {}", instruction)); }); render_register_table(state, ui, ctx); diff --git a/emulator/src/emulator/ui/editor.rs b/emulator/src/emulator/ui/editor.rs index a6bbf7f..588bb09 100644 --- a/emulator/src/emulator/ui/editor.rs +++ b/emulator/src/emulator/ui/editor.rs @@ -279,9 +279,8 @@ impl Editor { // builds the current file if ui.button("Build").clicked() { - let instructions = assembler::assemble(&self.text); - // TODO: uncomment this once assembler works!!! + // let instructions = assembler::assemble(&self.text); // self.output = instructions // .iter() // .flat_map(|i| i.encode().to_le_bytes().to_vec()) diff --git a/emulator/src/emulator/ui/memory_inspector.rs b/emulator/src/emulator/ui/memory_inspector.rs index 37f09b2..6aeae7f 100644 --- a/emulator/src/emulator/ui/memory_inspector.rs +++ b/emulator/src/emulator/ui/memory_inspector.rs @@ -1,5 +1,7 @@ use std::{num::ParseIntError, sync::mpsc::Sender}; +use common::prelude::Instruction; + use crate::emulator::{ system::model::{Command, State}, ui::interface::Component, @@ -129,7 +131,7 @@ impl Component for MemoryInspector { .fold(0u32, |acc, &byte| acc << 8 | u32::from(byte)); ui.monospace(format!("{combined}")); - // ui.monospace(format!("{:?}", Instruction::decode(combined))); + ui.monospace(format!("{:?}", Instruction::decode(combined))); ui.monospace("TODO! instruction"); ui.end_row(); diff --git a/resources/dsa/print.dsa b/resources/dsa/print.dsa index bd21c84..e05512f 100644 --- a/resources/dsa/print.dsa +++ b/resources/dsa/print.dsa @@ -1,16 +1,12 @@ -db stack: 0x10000 -db screen: 0x20000 +dw stack: 0x10000 +dw screen: 0x20000 db string: "Dominos sucks!" +db string2: 0, 1, 2, 3, 4, 5, 6 db length: 14 -cll init -cll start -hlt - init: ldw stack, bpr mov bpr, spr - ret start: ldb length, rg0 @@ -29,5 +25,4 @@ loop: // if loop counter <= 0 return. cmp rg0, zero, jgt loop - ret