summaryrefslogtreecommitdiff
path: root/src/core.rs
diff options
context:
space:
mode:
authorOxore <oxore@protonmail.com>2019-11-10 18:16:02 +0300
committerOxore <oxore@protonmail.com>2019-11-10 18:16:02 +0300
commit2b85707f48e691d3b574a06aa511b182e7140d10 (patch)
treedf5c90b997ee9c4ccb641bc3742132667eaab9be /src/core.rs
parent9f9569447bdd1c7a82baebbd88cdb37e30eb180c (diff)
Add comments, format code
Diffstat (limited to 'src/core.rs')
-rw-r--r--src/core.rs28
1 files changed, 20 insertions, 8 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),
}
}