From 0832a0dddad7782b8ac7a06c76d3e58ab2a56864 Mon Sep 17 00:00:00 2001 From: Oxore Date: Mon, 30 Sep 2019 23:33:18 +0300 Subject: Remove most of unwraps, add Mov instruction --- src/core.rs | 16 +++++++++++++--- src/main.rs | 8 +++++++- src/ram.rs | 32 ++++++++++++++++++++++---------- 3 files changed, 42 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/core.rs b/src/core.rs index 6d9a5e1..35ef2a6 100644 --- a/src/core.rs +++ b/src/core.rs @@ -4,6 +4,7 @@ use std::fmt; #[derive(Debug, Clone)] pub enum Opcode { Nop, + Mov, Push, Pop, Illegal, @@ -20,6 +21,8 @@ pub struct Op { } const OPCODE_NOP: u8 = 0x00; +const OPCODE_MOV_A_DATA: u8 = 0x74; +const OPCODE_MOV_DIR_DATA: u8 = 0x75; const OPCODE_PUSH: u8 = 0xC0; const OPCODE_POP: u8 = 0xD0; const OPCODE_SJMP: u8 = 0xD2; @@ -42,8 +45,12 @@ pub struct Core { } impl Core { - pub fn with_ram_from_hex(hex: String) -> Self { - Self { pc: 0, ram: Ram::from_hex(hex) } + pub fn with_ram_from_hex(hex: String) -> Result { + let ram = match Ram::from_hex(hex) { + Ok(value) => value, + Err(err_string) => return Err(err_string), + }; + Ok(Self { pc: 0, ram }) } pub fn step(&mut self) -> u16 { @@ -80,10 +87,11 @@ impl Core { fn exec(&mut self, op: Op) { match op.opcode { Opcode::Nop => (), + Opcode::Mov => (), Opcode::Illegal => (), Opcode::Push => (), Opcode::Pop => (), - Opcode::Sjmp => self.sjmp(op.operand1.unwrap() as i8), + Opcode::Sjmp => self.sjmp(op.operand1.expect("JSMP has no operand") as i8), } } @@ -105,6 +113,8 @@ impl Isa8051 for u8 { fn op(&self) -> (Opcode, usize, Option) { match *self { OPCODE_NOP => (Opcode::Nop, 1, None), + OPCODE_MOV_A_DATA => (Opcode::Mov, 1, None), + OPCODE_MOV_DIR_DATA => (Opcode::Mov, 1, None), OPCODE_PUSH => (Opcode::Push, 2, None), OPCODE_POP => (Opcode::Pop, 2, None), OPCODE_SJMP => (Opcode::Sjmp, 2, None), diff --git a/src/main.rs b/src/main.rs index 0d79c33..17ef6e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,7 +47,13 @@ fn main() { let asyncpair = Arc::new((Mutex::new(false), Condvar::new())); let asyncpair2 = asyncpair.clone(); - let core = Core::with_ram_from_hex(String::from(":0300000075800008\n:00000001FF\n")); + let core = match Core::with_ram_from_hex(String::from(":0300000075800008\n:00000001FF\n")) { + Ok(value) => value, + Err(err_string) => { + println!("{}", err_string); + return; + }, + }; thread::spawn(move || { let (ref should_stop, ref condvar) = *asyncpair2; diff --git a/src/ram.rs b/src/ram.rs index c7c4d43..34723cb 100644 --- a/src/ram.rs +++ b/src/ram.rs @@ -38,16 +38,22 @@ struct HexLine { } impl HexLine { - fn from(s: &str) -> Result { + fn from(s: &str) -> Result { // The shortest possible sequence is EOF (:00000001FF) if s.len() < 11 { - return Err(1) + return Err(usize::max_value()) } if &s[0..1] != ":" { - return Err(2) + return Err(0) } - let offset = (&s[3..7]).parse::().unwrap(); // TODO: handle unwrap - let bytecount = (&s[1..3]).parse::().unwrap(); // TODO: handle unwrap + let offset = match (&s[3..7]).parse::() { + Ok(value) => value, + Err(_) => return Err(3), + }; + let bytecount = match(&s[1..3]).parse::() { + Ok(value) => value, + Err(_) => return Err(1), + }; // If EOF reached if &s[7..9] == "01" { @@ -56,7 +62,10 @@ impl HexLine { let mut counter = 9; let mut data = vec![]; while counter < s.len() - 2 && counter < (9 + bytecount * 2) { - data.push((&s[counter..counter+2]).parse::().unwrap()); // TODO handle unwrap + data.push(match(&s[counter..counter+2]).parse::() { + Ok(value) => value, + Err(_) => return Err(counter), + }); counter += 2; } // TODO: check checksum @@ -68,10 +77,13 @@ impl HexLine { } impl Ram { - pub fn from_hex(hex: String) -> Self { + pub fn from_hex(hex: String) -> Result { let mut array = [0; u16::max_value() as usize + 1]; - for line in hex.lines() { - let hex_line = HexLine::from(line).unwrap(); // TODO: handle unwrap + for (linenum, line) in hex.lines().enumerate() { + let hex_line = match HexLine::from(line) { + Ok(value) => value, + Err(charnum) => return Err(format!("ihex parsing error {}:{}", linenum, charnum)), + }; let offset = hex_line.offset; match hex_line.rectyp { Rectyp::Data => { @@ -82,6 +94,6 @@ impl Ram { Rectyp::EndOfFile => {} } } - Self { array } + Ok(Self { array }) } } -- cgit v1.2.3