implement non-working draw instruction

This commit is contained in:
Denis-Cosmin Nutiu 2024-12-04 23:04:56 +02:00
parent 89d4196ccf
commit d63f5580be
3 changed files with 27 additions and 4 deletions

View file

@ -10,6 +10,8 @@ pub trait Display {
fn clear(&self); fn clear(&self);
/// Renders the display data on screen. /// Renders the display data on screen.
fn render(&mut self); fn render(&mut self);
/// Draws the pixels at x and y coordinates
fn draw(&mut self, x: u8, y: u8, pixels: u8) -> bool;
} }
/// Display models the Chip8's display. /// Display models the Chip8's display.
@ -46,4 +48,15 @@ impl Display for TerminalDisplay {
print!("\n") print!("\n")
} }
} }
/// Draws the pixels at x and y coordinates
fn draw(&mut self, x: u8, y: u8, pixels: u8) -> bool {
let row = y as usize;
let column = x as usize;
for pixel in 0..=pixels {
self.display_data[row * DISPLAY_WIDTH + column + pixel as usize] = true;
}
self.render();
true
}
} }

View file

@ -6,6 +6,7 @@ use log::{debug, info, trace, 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;
use std::{thread, time};
const MEMORY_SIZE: usize = 4096; const MEMORY_SIZE: usize = 4096;
const NUMBER_OF_REGISTERS: usize = 16; const NUMBER_OF_REGISTERS: usize = 16;
@ -111,8 +112,10 @@ where
// decode & execute // decode & execute
self.execute_instruction(instruction)?; self.execute_instruction(instruction)?;
// insert some delay
thread::sleep(time::Duration::from_millis(50));
} }
Ok(())
} }
fn execute_instruction(&mut self, instruction: Instruction) -> Result<(), anyhow::Error> { fn execute_instruction(&mut self, instruction: Instruction) -> Result<(), anyhow::Error> {
@ -134,7 +137,11 @@ where
self.registers[register as usize] += data self.registers[register as usize] += data
} }
ProcessorInstruction::Draw(vx_register, vy_register, pixels) => { ProcessorInstruction::Draw(vx_register, vy_register, pixels) => {
trace!("Draw vx_register={vx_register} vy_register={vy_register} pixels={pixels}") trace!("Draw vx_register={vx_register} vy_register={vy_register} pixels={pixels}");
let x_coordinate = self.registers[vx_register as usize] & 63;
let y_coordinate = self.registers[vy_register as usize] & 31;
self.registers[0xF] = 0;
self.display.draw(x_coordinate, y_coordinate, pixels);
} }
ProcessorInstruction::UnknownInstruction => { ProcessorInstruction::UnknownInstruction => {
warn!("Unknown instruction: {:04x}, skipping.", instruction); warn!("Unknown instruction: {:04x}, skipping.", instruction);

View file

@ -1,4 +1,3 @@
use log::info;
use std::fmt; use std::fmt;
use std::fmt::{Display, Formatter, LowerHex}; use std::fmt::{Display, Formatter, LowerHex};
/* /*
@ -80,7 +79,11 @@ impl Instruction {
} }
0xD000..=0xDFFF => { 0xD000..=0xDFFF => {
// DXYN // DXYN
ProcessorInstruction::Draw(Self::grab_first_nibble(data), Self::grab_middle_nibble(data), Self::grab_last_nibble(data)) ProcessorInstruction::Draw(
Self::grab_first_nibble(data),
Self::grab_middle_nibble(data),
Self::grab_last_nibble(data),
)
} }
// Unknown instruction // Unknown instruction
_ => ProcessorInstruction::UnknownInstruction, _ => ProcessorInstruction::UnknownInstruction,