implement clear display instruction

This commit is contained in:
Denis-Cosmin NUTIU 2024-12-02 23:39:22 +02:00
parent 28ddc9b690
commit 7ecfff3e0d
3 changed files with 24 additions and 7 deletions

View file

@ -7,7 +7,7 @@ const DISPLAY_HEIGHT: usize = 32;
/// Display trait /// Display trait
pub trait Display { pub trait Display {
/// Re-draws the display. /// Re-draws the display.
fn redraw(&self); fn clear(&self);
/// Draws the display. /// Draws the display.
fn draw(&mut self); fn draw(&mut self);
} }
@ -28,7 +28,7 @@ impl TerminalDisplay {
impl Display for TerminalDisplay { impl Display for TerminalDisplay {
/// Re-draws the display. /// Re-draws the display.
fn redraw(&self) { fn clear(&self) {
// ANSI Escape code to move cursor to row 1 column 1 // ANSI Escape code to move cursor to row 1 column 1
// See https://stackoverflow.com/a/4062051 // See https://stackoverflow.com/a/4062051
print!("{esc}[2J{esc}[1;1H", esc = 27 as char); print!("{esc}[2J{esc}[1;1H", esc = 27 as char);

View file

@ -2,7 +2,7 @@ use crate::display::Display;
use crate::instruction::Instruction; use crate::instruction::Instruction;
use crate::stack::Stack; use crate::stack::Stack;
use anyhow::anyhow; use anyhow::anyhow;
use log::{debug, info}; use log::{debug, info, warn};
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::path::Path; use std::path::Path;
@ -103,12 +103,29 @@ where
let instruction = self.fetch_instruction()?; let instruction = self.fetch_instruction()?;
self.program_counter += 2; self.program_counter += 2;
debug!("PC={} {:04x}", self.program_counter, instruction) debug!("PC={} {:04x}", self.program_counter, instruction);
// decode & execute // 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(()) Ok(())
} }

View file

@ -23,7 +23,7 @@ impl Instruction {
impl Display for Instruction { impl Display for Instruction {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(&format!( f.write_str(&format!(
"Instruction: [{:02x}, {:02x}]", "<Instruction: [{:02x}{:02x}]>",
((self.data & 0xFF00) >> 8u8) as u8, ((self.data & 0xFF00) >> 8u8) as u8,
(self.data & 0x00FF) as u8 (self.data & 0x00FF) as u8
)) ))