From 7ecfff3e0d3c249a6cb2bd0d7dd9831162893c16 Mon Sep 17 00:00:00 2001 From: Denis-Cosmin NUTIU Date: Mon, 2 Dec 2024 23:39:22 +0200 Subject: [PATCH] implement clear display instruction --- src/display.rs | 4 ++-- src/emulator.rs | 25 +++++++++++++++++++++---- src/instruction.rs | 2 +- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/display.rs b/src/display.rs index bfed099..c2ed33b 100644 --- a/src/display.rs +++ b/src/display.rs @@ -7,7 +7,7 @@ const DISPLAY_HEIGHT: usize = 32; /// Display trait pub trait Display { /// Re-draws the display. - fn redraw(&self); + fn clear(&self); /// Draws the display. fn draw(&mut self); } @@ -28,7 +28,7 @@ impl TerminalDisplay { impl Display for TerminalDisplay { /// Re-draws the display. - fn redraw(&self) { + fn clear(&self) { // ANSI Escape code to move cursor to row 1 column 1 // See https://stackoverflow.com/a/4062051 print!("{esc}[2J{esc}[1;1H", esc = 27 as char); diff --git a/src/emulator.rs b/src/emulator.rs index 9e1e723..d71ca42 100644 --- a/src/emulator.rs +++ b/src/emulator.rs @@ -2,7 +2,7 @@ use crate::display::Display; use crate::instruction::Instruction; use crate::stack::Stack; use anyhow::anyhow; -use log::{debug, info}; +use log::{debug, info, warn}; use std::fs::File; use std::io::Read; use std::path::Path; @@ -103,12 +103,29 @@ where let instruction = self.fetch_instruction()?; self.program_counter += 2; - debug!("PC={} {:04x}", self.program_counter, instruction) + debug!("PC={} {:04x}", self.program_counter, instruction); // decode & execute - //let decode = self.decode_instruction(instruction); + self.execute_instruction(instruction)?; + } + Ok(()) + } - // execute + fn execute_instruction(&mut self, instruction: Instruction) -> Result<(), anyhow::Error> { + match instruction.raw() { + // Clear Display + 0x00E0 => { + info!("clear display"); + self.display.clear() + } + // Jump + 0x1000..=0x1FFF => { + info!("jump to {}", instruction); + } + // Unknown instruction + _ => { + warn!("Unknown instruction: {:04x}, skipping.", instruction); + } } Ok(()) } diff --git a/src/instruction.rs b/src/instruction.rs index c0e9bad..9993145 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -23,7 +23,7 @@ impl Instruction { impl Display for Instruction { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { f.write_str(&format!( - "Instruction: [{:02x}, {:02x}]", + "", ((self.data & 0xFF00) >> 8u8) as u8, (self.data & 0x00FF) as u8 ))