reformat code

This commit is contained in:
Denis-Cosmin Nutiu 2024-12-04 22:43:04 +02:00
parent bc56fca299
commit 89d4196ccf
3 changed files with 43 additions and 23 deletions

View file

@ -8,8 +8,8 @@ const DISPLAY_HEIGHT: usize = 32;
pub trait Display { pub trait Display {
/// Re-draws the display. /// Re-draws the display.
fn clear(&self); fn clear(&self);
/// Draws the display. /// Renders the display data on screen.
fn draw(&mut self); fn render(&mut self);
} }
/// Display models the Chip8's display. /// Display models the Chip8's display.
@ -33,8 +33,8 @@ impl Display for TerminalDisplay {
// 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);
} }
/// Draws the display. /// Renders the display data on screen.
fn draw(&mut self) { fn render(&mut self) {
for row in 0..32 { for row in 0..32 {
for column in 0..64 { for column in 0..64 {
if self.display_data[row * DISPLAY_WIDTH + column] { if self.display_data[row * DISPLAY_WIDTH + column] {

View file

@ -109,7 +109,6 @@ where
} }
last_program_counter = self.program_counter; last_program_counter = self.program_counter;
// decode & execute // decode & execute
self.execute_instruction(instruction)?; self.execute_instruction(instruction)?;
} }
@ -128,11 +127,14 @@ where
} }
ProcessorInstruction::SetRegister(register, data) => { ProcessorInstruction::SetRegister(register, data) => {
trace!("Set register {} to data {:04x}", register, data); trace!("Set register {} to data {:04x}", register, data);
self.registers[register] = data self.registers[register as usize] = data
} }
ProcessorInstruction::AddValueToRegister(register, data) => { ProcessorInstruction::AddValueToRegister(register, data) => {
trace!("Add to register {} data {:04x}", 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 => { ProcessorInstruction::UnknownInstruction => {
warn!("Unknown instruction: {:04x}, skipping.", instruction); warn!("Unknown instruction: {:04x}, skipping.", instruction);

View file

@ -1,6 +1,6 @@
use log::info;
use std::fmt; use std::fmt;
use std::fmt::{Display, Formatter, LowerHex}; 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, 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, 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 /// Jumps to a given address
Jump(u16), Jump(u16),
/// Sets the register in the first argument to the given value /// Sets the register in the first argument to the given value
SetRegister(usize, u8), SetRegister(u8, u8),
/// Adds the value to the register /// Adds the value to the register
AddValueToRegister(usize, u8), AddValueToRegister(u8, u8),
UnknownInstruction /// Draws to the screen.
Draw(u8, u8, u8),
UnknownInstruction,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Instruction { pub struct Instruction {
data: u16, data: u16,
processor_instruction: ProcessorInstruction processor_instruction: ProcessorInstruction,
} }
impl Instruction { impl Instruction {
@ -55,9 +57,7 @@ impl Instruction {
fn decode_instruction(data: u16) -> ProcessorInstruction { fn decode_instruction(data: u16) -> ProcessorInstruction {
match data { match data {
// Clear Display // Clear Display
0x00E0 => { 0x00E0 => ProcessorInstruction::ClearScreen,
ProcessorInstruction::ClearScreen
}
// Jump // Jump
0x1000..=0x1FFF => { 0x1000..=0x1FFF => {
// 1NNN // 1NNN
@ -66,16 +66,24 @@ impl Instruction {
// Set Register // Set Register
0x6000..=0x6FFF => { 0x6000..=0x6FFF => {
// 6XNN // 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 // 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 // Unknown instruction
_ => { _ => ProcessorInstruction::UnknownInstruction,
ProcessorInstruction::UnknownInstruction
}
} }
} }
@ -90,8 +98,18 @@ impl Instruction {
} }
/// Grabs the first nibble from the data. /// Grabs the first nibble from the data.
fn grab_first_nibble(data: u16) -> usize { fn grab_first_nibble(data: u16) -> u8 {
((data & 0x0F00) >> 8) as usize ((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
} }
} }