From d63f5580be8ffe4e27697872a261dec186946b54 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Wed, 4 Dec 2024 23:04:56 +0200 Subject: [PATCH] implement non-working draw instruction --- src/display.rs | 13 +++++++++++++ src/emulator.rs | 11 +++++++++-- src/instruction.rs | 7 +++++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/display.rs b/src/display.rs index eff0094..47abfa7 100644 --- a/src/display.rs +++ b/src/display.rs @@ -10,6 +10,8 @@ pub trait Display { fn clear(&self); /// Renders the display data on screen. 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. @@ -46,4 +48,15 @@ impl Display for TerminalDisplay { 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 + } } diff --git a/src/emulator.rs b/src/emulator.rs index 438dd92..bbbe2ae 100644 --- a/src/emulator.rs +++ b/src/emulator.rs @@ -6,6 +6,7 @@ use log::{debug, info, trace, warn}; use std::fs::File; use std::io::Read; use std::path::Path; +use std::{thread, time}; const MEMORY_SIZE: usize = 4096; const NUMBER_OF_REGISTERS: usize = 16; @@ -111,8 +112,10 @@ where // decode & execute 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> { @@ -134,7 +137,11 @@ where self.registers[register as usize] += data } 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 => { warn!("Unknown instruction: {:04x}, skipping.", instruction); diff --git a/src/instruction.rs b/src/instruction.rs index 0f4ddfd..76ffd4f 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -1,4 +1,3 @@ -use log::info; use std::fmt; use std::fmt::{Display, Formatter, LowerHex}; /* @@ -80,7 +79,11 @@ impl Instruction { } 0xD000..=0xDFFF => { // 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 _ => ProcessorInstruction::UnknownInstruction,