From 4035bdb1140567d2dfbf30820e291334930dd906 Mon Sep 17 00:00:00 2001 From: Murad Karammaev Date: Sat, 13 Aug 2022 01:51:15 +0300 Subject: [PATCH] toyvm: print next instruction during execution --- toy_cpu_4bit/src/cpu.rs | 4 ++++ toyvm/src/main.rs | 2 ++ 2 files changed, 6 insertions(+) diff --git a/toy_cpu_4bit/src/cpu.rs b/toy_cpu_4bit/src/cpu.rs index e4d668a..86bc4b5 100644 --- a/toy_cpu_4bit/src/cpu.rs +++ b/toy_cpu_4bit/src/cpu.rs @@ -36,6 +36,10 @@ impl Cpu { } } + pub fn next_instruction(&self) -> String { + format!("{:?}", decode(self.code[self.IP.0 as usize])) + } + fn load(&mut self, reg: Register, addr: u4) { match reg { Register::R0 => self.R0 = Wrapping(self.data[u8::from(addr) as usize]), diff --git a/toyvm/src/main.rs b/toyvm/src/main.rs index a4af7f3..1fa9aea 100644 --- a/toyvm/src/main.rs +++ b/toyvm/src/main.rs @@ -35,6 +35,7 @@ fn main() -> Result<(), Box> { let code = read_code(args.code)?; let mut cpu = Cpu::new(&code); loop { + println!("{}", cpu.next_instruction()); if cpu.step() { break; } @@ -42,5 +43,6 @@ fn main() -> Result<(), Box> { cpu.visualize(); } } + cpu.visualize(); Ok(()) }