summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/core.rs28
-rw-r--r--src/main.rs19
-rw-r--r--src/ram.rs18
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());
diff --git a/src/ram.rs b/src/ram.rs
index a886db3..d0ae084 100644
--- a/src/ram.rs
+++ b/src/ram.rs
@@ -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)