updated emulator to support importing .dsb binaries
This commit is contained in:
@@ -18,7 +18,8 @@ impl Interrupt {
|
|||||||
// TODO: This should be TryFrom.
|
// TODO: This should be TryFrom.
|
||||||
impl From<u8> for Interrupt {
|
impl From<u8> for Interrupt {
|
||||||
#[allow(unreachable_code)]
|
#[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.");
|
todo!("Implement this once a hardware interrupt convention is established.");
|
||||||
|
|
||||||
// Self::Software(_code)
|
// Self::Software(_code)
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ pub struct Processor {
|
|||||||
pub registers: RegFile,
|
pub registers: RegFile,
|
||||||
pub halted: bool,
|
pub halted: bool,
|
||||||
pub io_devices: Vec<Arc<dyn IODevice>>,
|
pub io_devices: Vec<Arc<dyn IODevice>>,
|
||||||
|
|
||||||
|
pub dustbin: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::needless_pass_by_ref_mut)]
|
#[allow(clippy::needless_pass_by_ref_mut)]
|
||||||
@@ -28,6 +30,7 @@ impl Processor {
|
|||||||
registers: RegFile::default(),
|
registers: RegFile::default(),
|
||||||
halted: false,
|
halted: false,
|
||||||
io_devices,
|
io_devices,
|
||||||
|
dustbin: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,8 +62,12 @@ impl Processor {
|
|||||||
// Decode and execute the instruction.
|
// Decode and execute the instruction.
|
||||||
let instruction = Instruction::decode(val)?;
|
let instruction = Instruction::decode(val)?;
|
||||||
|
|
||||||
|
println!("Executing instruction: {instruction}");
|
||||||
|
|
||||||
instruction.execute(self);
|
instruction.execute(self);
|
||||||
|
|
||||||
|
println!("ok!");
|
||||||
|
|
||||||
Ok((addr, instruction))
|
Ok((addr, instruction))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +81,10 @@ impl Processor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn reg(&mut self, reg: Register) -> &mut u32 {
|
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> {
|
pub fn display(&mut self) -> Vec<u8> {
|
||||||
|
|||||||
@@ -176,11 +176,31 @@ impl Editor {
|
|||||||
.set_directory(&work_dir)
|
.set_directory(&work_dir)
|
||||||
.pick_file()
|
.pick_file()
|
||||||
{
|
{
|
||||||
if let Ok(contents) = std::fs::read_to_string(&path) {
|
match path.extension().and_then(|ext| ext.to_str()) {
|
||||||
self.path = Some(path.clone());
|
Some("dsb") => {
|
||||||
self.text.clone_from(&contents);
|
let contents = match std::fs::read(&path) {
|
||||||
self.buffer = contents;
|
Ok(contents) => contents,
|
||||||
self.unsaved = false;
|
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(
|
std::env::set_current_dir(
|
||||||
@@ -346,22 +366,14 @@ impl Editor {
|
|||||||
// builds the current file
|
// builds the current file
|
||||||
if ui.button("Build").clicked() && !self.unsaved {
|
if ui.button("Build").clicked() && !self.unsaved {
|
||||||
if let Some(path) = &self.path {
|
if let Some(path) = &self.path {
|
||||||
let mut assembler = CompilerEngine::new();
|
let mut compiler = CompilerEngine::new();
|
||||||
if let Err(error) = assembler.assemble(path) {
|
compiler.start_compilation(path);
|
||||||
self.error = Some(format!("Failed to assemble: {error:?}"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let instructions = match assembler.result() {
|
// Or block until done
|
||||||
Some(Ok(instructions)) => instructions,
|
let instructions = match compiler.wait_for_result() {
|
||||||
Some(Err(error)) => {
|
Ok(instructions) => instructions,
|
||||||
self.error = Some(format!("Failed to assemble: {error:?}"));
|
Err(e) => {
|
||||||
return;
|
self.error = Some(e.to_string());
|
||||||
}
|
|
||||||
None => {
|
|
||||||
self.error = Some(
|
|
||||||
"Failed to assemble: No result available".to_string(),
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user