From 79b5aa3fdebc04023adacd0472a149d8594d1e1b Mon Sep 17 00:00:00 2001 From: Murad Date: Thu, 3 Feb 2022 23:04:39 +0200 Subject: [PATCH] add basic CPU visualization function Also add a sample program which **screams** for assembler and disassembler. With no assembler one cannot write programs for this CPU without pulling out their hear. --- src/cpu.rs | 29 +++++++++++++++++++++++++++++ src/main.rs | 10 +++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/cpu.rs b/src/cpu.rs index 966de85..b3af4dc 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -18,6 +18,7 @@ pub struct Cpu { data: [u8; 0x10], port_in: VecDeque, port_out: VecDeque, + num_cycles: u64, } impl Cpu { @@ -34,6 +35,7 @@ impl Cpu { data: [0u8; 0x10], port_in: VecDeque::::new(), port_out: VecDeque::::new(), + num_cycles: 0, } } @@ -329,6 +331,7 @@ impl Cpu { } pub fn step(&mut self) -> bool { + self.num_cycles += 1; let insn = decode(self.code[self.IP.0 as usize]); match insn { Instruction::Load { reg, addr } => self.load(reg, addr), @@ -376,6 +379,32 @@ impl Cpu { } false } + + pub fn visualize(&self) { + println!("+--+----+----------+"); + println!("|IP|{:#04X}|{:#010b}|", self.IP, self.IP); + println!("|R0|{:#04X}|{:#010b}|", self.R0, self.R0); + println!("|R1|{:#04X}|{:#010b}|", self.R1, self.R1); + println!("+--+----+----------+"); + println!("|FLAGS:{} |", if self.C { "C" } else { " " }); + println!("+------------------+"); + println!("|CYCLES:{:11}|", self.num_cycles); + println!("|PORTIN:{:11}|", self.port_in.len()); + println!("|PORTOUT:{:10}|", self.port_out.len()); + println!("+------------------+"); + println!("| MEM +0|+1|+2|+3 |"); + for i in 0..4 { + println!( + "| 0x{:X} {:02X}|{:02X}|{:02X}|{:02X} |", + i * 4, + self.data[i * 4 + 0], + self.data[i * 4 + 1], + self.data[i * 4 + 2], + self.data[i * 4 + 3] + ); + } + println!("+------------------+"); + } } #[cfg(test)] diff --git a/src/main.rs b/src/main.rs index 5f3eaf3..bd67f6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,10 +7,18 @@ mod instruction; use cpu::*; fn main() { - let mut cpu = Cpu::new(&[]); + let mut cpu = Cpu::new(&[ + 0b01001111, // R0 = 0xF + 0b01010011, // R1.l = 0x3 + 0b11100100, // ADD + 0b00100000, // [0] = R0 + 0b11111000, // R0 = 0 + 0b11110101, // HALT + ]); loop { if cpu.step() { break; } } + cpu.visualize(); }