diff --git a/src/display.rs b/src/display.rs index c2ed33b..eff0094 100644 --- a/src/display.rs +++ b/src/display.rs @@ -8,8 +8,8 @@ const DISPLAY_HEIGHT: usize = 32; pub trait Display { /// Re-draws the display. fn clear(&self); - /// Draws the display. - fn draw(&mut self); + /// Renders the display data on screen. + fn render(&mut self); } /// Display models the Chip8's display. @@ -33,8 +33,8 @@ impl Display for TerminalDisplay { // See https://stackoverflow.com/a/4062051 print!("{esc}[2J{esc}[1;1H", esc = 27 as char); } - /// Draws the display. - fn draw(&mut self) { + /// Renders the display data on screen. + fn render(&mut self) { for row in 0..32 { for column in 0..64 { if self.display_data[row * DISPLAY_WIDTH + column] { diff --git a/src/emulator.rs b/src/emulator.rs index bc61e26..438dd92 100644 --- a/src/emulator.rs +++ b/src/emulator.rs @@ -109,7 +109,6 @@ where } last_program_counter = self.program_counter; - // decode & execute self.execute_instruction(instruction)?; } @@ -128,11 +127,14 @@ where } ProcessorInstruction::SetRegister(register, data) => { trace!("Set register {} to data {:04x}", register, data); - self.registers[register] = data + self.registers[register as usize] = data } ProcessorInstruction::AddValueToRegister(register, data) => { trace!("Add to register {} data {:04x}", register, data); - self.registers[register] += data + 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}") } ProcessorInstruction::UnknownInstruction => { warn!("Unknown instruction: {:04x}, skipping.", instruction); diff --git a/src/instruction.rs b/src/instruction.rs index 32f2af1..0f4ddfd 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -1,6 +1,6 @@ +use log::info; use std::fmt; use std::fmt::{Display, Formatter, LowerHex}; -use log::info; /* Although every instruction will have a first nibble that tells you what kind of instruction it is, the rest of the nibbles will have different meanings. To differentiate these meanings, @@ -20,16 +20,18 @@ pub enum ProcessorInstruction { /// Jumps to a given address Jump(u16), /// Sets the register in the first argument to the given value - SetRegister(usize, u8), + SetRegister(u8, u8), /// Adds the value to the register - AddValueToRegister(usize, u8), - UnknownInstruction + AddValueToRegister(u8, u8), + /// Draws to the screen. + Draw(u8, u8, u8), + UnknownInstruction, } #[derive(Debug)] pub struct Instruction { data: u16, - processor_instruction: ProcessorInstruction + processor_instruction: ProcessorInstruction, } impl Instruction { @@ -55,9 +57,7 @@ impl Instruction { fn decode_instruction(data: u16) -> ProcessorInstruction { match data { // Clear Display - 0x00E0 => { - ProcessorInstruction::ClearScreen - } + 0x00E0 => ProcessorInstruction::ClearScreen, // Jump 0x1000..=0x1FFF => { // 1NNN @@ -66,16 +66,24 @@ impl Instruction { // Set Register 0x6000..=0x6FFF => { // 6XNN - ProcessorInstruction::SetRegister(Self::grab_first_nibble(data), Self::grab_last_byte(data)) + ProcessorInstruction::SetRegister( + Self::grab_first_nibble(data), + Self::grab_last_byte(data), + ) } - 0x7000..0x7FFF => { + 0x7000..=0x7FFF => { // 7XNN - ProcessorInstruction::AddValueToRegister(Self::grab_first_nibble(data), Self::grab_last_byte(data)) + ProcessorInstruction::AddValueToRegister( + Self::grab_first_nibble(data), + Self::grab_last_byte(data), + ) + } + 0xD000..=0xDFFF => { + // DXYN + ProcessorInstruction::Draw(Self::grab_first_nibble(data), Self::grab_middle_nibble(data), Self::grab_last_nibble(data)) } // Unknown instruction - _ => { - ProcessorInstruction::UnknownInstruction - } + _ => ProcessorInstruction::UnknownInstruction, } } @@ -90,8 +98,18 @@ impl Instruction { } /// Grabs the first nibble from the data. - fn grab_first_nibble(data: u16) -> usize { - ((data & 0x0F00) >> 8) as usize + fn grab_first_nibble(data: u16) -> u8 { + ((data & 0x0F00) >> 8) as u8 + } + + /// Grabs the middle nibble from the data. + fn grab_middle_nibble(data: u16) -> u8 { + ((data & 0x00F0) >> 4) as u8 + } + + /// Grabs the last nibble from the data. + fn grab_last_nibble(data: u16) -> u8 { + (data & 0x000F) as u8 } }