From 2b85707f48e691d3b574a06aa511b182e7140d10 Mon Sep 17 00:00:00 2001 From: Oxore Date: Sun, 10 Nov 2019 18:16:02 +0300 Subject: Add comments, format code --- src/core.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'src/core.rs') 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 { + /// + /// Constructor + /// + pub fn new_with_ram_from_hex(hex: String) -> Result { 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) { 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), } } -- cgit v1.2.3