updated emulator to support importing .dsb binaries

This commit is contained in:
2025-06-22 03:52:11 +01:00
parent 7892c44d89
commit 808b51ff5f
3 changed files with 45 additions and 22 deletions
+2 -1
View File
@@ -18,7 +18,8 @@ impl Interrupt {
// TODO: This should be TryFrom.
impl From<u8> for Interrupt {
#[allow(unreachable_code)]
fn from(_code: u8) -> Self {
fn from(code: u8) -> Self {
return Self::Software(code);
todo!("Implement this once a hardware interrupt convention is established.");
// Self::Software(_code)
+11 -1
View File
@@ -17,6 +17,8 @@ pub struct Processor {
pub registers: RegFile,
pub halted: bool,
pub io_devices: Vec<Arc<dyn IODevice>>,
pub dustbin: u32,
}
#[allow(clippy::needless_pass_by_ref_mut)]
@@ -28,6 +30,7 @@ impl Processor {
registers: RegFile::default(),
halted: false,
io_devices,
dustbin: 0,
}
}
@@ -59,8 +62,12 @@ impl Processor {
// Decode and execute the instruction.
let instruction = Instruction::decode(val)?;
println!("Executing instruction: {instruction}");
instruction.execute(self);
println!("ok!");
Ok((addr, instruction))
}
@@ -74,7 +81,10 @@ impl Processor {
}
pub fn reg(&mut self, reg: Register) -> &mut u32 {
self.registers.reg(reg)
match reg {
Register::Zero => &mut self.dustbin,
_ => self.registers.reg(reg),
}
}
pub fn display(&mut self) -> Vec<u8> {
+32 -20
View File
@@ -176,11 +176,31 @@ impl Editor {
.set_directory(&work_dir)
.pick_file()
{
if let Ok(contents) = std::fs::read_to_string(&path) {
self.path = Some(path.clone());
self.text.clone_from(&contents);
self.buffer = contents;
self.unsaved = false;
match path.extension().and_then(|ext| ext.to_str()) {
Some("dsb") => {
let contents = match std::fs::read(&path) {
Ok(contents) => contents,
Err(why) => {
self.error = Some(format!("Failed to read file: {why}"));
return;
}
};
self.path = Some(path.clone());
self.output = contents;
self.unsaved = false;
self.text = String::from("Loaded Binary File!");
self.buffer = self.text.clone();
self.unsaved = false;
}
_ => {
if let Ok(contents) = std::fs::read_to_string(&path) {
self.path = Some(path.clone());
self.text.clone_from(&contents);
self.buffer = contents;
self.unsaved = false;
}
}
}
std::env::set_current_dir(
@@ -346,22 +366,14 @@ impl Editor {
// builds the current file
if ui.button("Build").clicked() && !self.unsaved {
if let Some(path) = &self.path {
let mut assembler = CompilerEngine::new();
if let Err(error) = assembler.assemble(path) {
self.error = Some(format!("Failed to assemble: {error:?}"));
return;
}
let mut compiler = CompilerEngine::new();
compiler.start_compilation(path);
let instructions = match assembler.result() {
Some(Ok(instructions)) => instructions,
Some(Err(error)) => {
self.error = Some(format!("Failed to assemble: {error:?}"));
return;
}
None => {
self.error = Some(
"Failed to assemble: No result available".to_string(),
);
// Or block until done
let instructions = match compiler.wait_for_result() {
Ok(instructions) => instructions,
Err(e) => {
self.error = Some(e.to_string());
return;
}
};