diff options
author | Oxore <oxore@protonmail.com> | 2019-11-10 18:16:02 +0300 |
---|---|---|
committer | Oxore <oxore@protonmail.com> | 2019-11-10 18:16:02 +0300 |
commit | 2b85707f48e691d3b574a06aa511b182e7140d10 (patch) | |
tree | df5c90b997ee9c4ccb641bc3742132667eaab9be | |
parent | 9f9569447bdd1c7a82baebbd88cdb37e30eb180c (diff) |
Add comments, format code
-rw-r--r-- | src/core.rs | 28 | ||||
-rw-r--r-- | src/main.rs | 19 | ||||
-rw-r--r-- | src/ram.rs | 18 |
3 files changed, 42 insertions, 23 deletions
diff --git a/src/core.rs b/src/core.rs index a4c0ce8..20bf177 100644 --- a/src/core.rs +++ b/src/core.rs @@ -45,7 +45,10 @@ pub struct Core { } impl Core { - pub fn with_ram_from_hex(hex: String) -> Result<Self, String> { + /// + /// Constructor + /// + pub fn new_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), @@ -53,12 +56,18 @@ impl Core { Ok(Self { pc: 0, ram }) } + /// + /// Fetch and execute one instruction + /// pub fn step(&mut self) -> u16 { let op = self.fetch(); self.exec(op); self.pc } + /// + /// Get opcode of current instruction + /// pub fn op(&self) -> Opcode { self.ram.array[self.pc as usize].op().0 } @@ -84,6 +93,9 @@ impl Core { } } + /// + /// Execute one instruction without incrementing PC + /// fn exec(&mut self, op: Op) { match op.opcode { Opcode::Nop => (), @@ -112,13 +124,13 @@ pub trait Isa8051 { 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, 2, None), - OPCODE_MOV_DIR_DATA => (Opcode::Mov, 3, None), - OPCODE_PUSH => (Opcode::Push, 2, None), - OPCODE_POP => (Opcode::Pop, 2, None), - OPCODE_SJMP => (Opcode::Sjmp, 2, None), - _ => (Opcode::Illegal, 1, None), + OPCODE_NOP => (Opcode::Nop, 1, None), + OPCODE_MOV_A_DATA => (Opcode::Mov, 2, None), + OPCODE_MOV_DIR_DATA => (Opcode::Mov, 3, None), + OPCODE_PUSH => (Opcode::Push, 2, None), + OPCODE_POP => (Opcode::Pop, 2, None), + OPCODE_SJMP => (Opcode::Sjmp, 2, None), + _ => (Opcode::Illegal, 1, None), } } diff --git a/src/main.rs b/src/main.rs index f5dd8c1..d588823 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,10 @@ use std::env; use std::fs; use std::io; use std::io::prelude::*; -use std::{thread, time}; -use std::thread::sleep; -use std::sync::{Arc, Mutex, Condvar}; use std::string::String; +use std::sync::{Arc, Condvar, Mutex}; +use std::thread::sleep; +use std::{thread, time}; mod core; mod ram; @@ -34,13 +34,13 @@ fn cli_controller(asyncpair: Arc<(Mutex<bool>, Condvar)>) { "stop" => { println!("stop"); *should_stop.lock().unwrap() = true; - }, + } "start" => { println!("start"); *should_stop.lock().unwrap() = false; cvar.notify_one(); - }, - _ => () + } + _ => (), } } } @@ -51,18 +51,17 @@ fn main() { println!("Please, specify ihex file"); return; } - let data = fs::read_to_string(&args[1]) - .expect(&format!("Unable to read file {}", &args[1])); + let data = fs::read_to_string(&args[1]).expect(&format!("Unable to read file {}", &args[1])); let asyncpair = Arc::new((Mutex::new(false), Condvar::new())); let asyncpair2 = asyncpair.clone(); - let core = match Core::with_ram_from_hex(String::from(data)) { + let core = match Core::new_with_ram_from_hex(String::from(data)) { Ok(value) => value, Err(err_string) => { println!("{}", err_string); return; - }, + } }; println!("{}", core.op()); @@ -41,10 +41,10 @@ impl HexLine { fn from(s: &str) -> Result<Self, usize> { // The shortest possible sequence is EOF (:00000001FF) if s.len() < 11 { - return Err(usize::max_value()) + return Err(usize::max_value()); } if &s[0..1] != ":" { - return Err(0) + return Err(0); } let offset = match u16::from_str_radix(&s[3..7], 16) { Ok(value) => value, @@ -57,19 +57,27 @@ impl HexLine { // If EOF reached if &s[7..9] == "01" { - return Ok(HexLine { rectyp: Rectyp::EndOfFile, offset, data: vec![0] }) + return Ok(HexLine { + rectyp: Rectyp::EndOfFile, + offset, + data: vec![0], + }); } else if &s[7..9] == "00" { let mut counter = 9; let mut data = vec![]; while counter < s.len() - 2 && counter < (9 + bytecount * 2) { - data.push(match u8::from_str_radix(&s[counter..counter+2], 16) { + data.push(match u8::from_str_radix(&s[counter..counter + 2], 16) { Ok(value) => value, Err(_) => return Err(counter), }); counter += 2; } // TODO: check checksum - return Ok(HexLine { rectyp: Rectyp::Data, offset, data }) + return Ok(HexLine { + rectyp: Rectyp::Data, + offset, + data, + }); } Err(3) |