summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOxore <oxore@protonmail.com>2019-09-30 23:33:18 +0300
committerOxore <oxore@protonmail.com>2019-09-30 23:33:18 +0300
commit0832a0dddad7782b8ac7a06c76d3e58ab2a56864 (patch)
treeaa96f8fb5d6a39fc803e2af2e384f77ef2cbce84
parent615ef873ca3aff557b21658b54a7cb1081404d86 (diff)
Remove most of unwraps, add Mov instruction
-rw-r--r--src/core.rs16
-rw-r--r--src/main.rs8
-rw-r--r--src/ram.rs32
3 files changed, 42 insertions, 14 deletions
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<Self, String> {
+ 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<Register>) {
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<Self, i32> {
+ fn from(s: &str) -> Result<Self, usize> {
// 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::<u16>().unwrap(); // TODO: handle unwrap
- let bytecount = (&s[1..3]).parse::<usize>().unwrap(); // TODO: handle unwrap
+ let offset = match (&s[3..7]).parse::<u16>() {
+ Ok(value) => value,
+ Err(_) => return Err(3),
+ };
+ let bytecount = match(&s[1..3]).parse::<usize>() {
+ 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::<u8>().unwrap()); // TODO handle unwrap
+ data.push(match(&s[counter..counter+2]).parse::<u8>() {
+ 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<Self, String> {
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 })
}
}