Skip to content

Commit

Permalink
Remove magic numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnsAnns committed May 25, 2024
1 parent 6756eeb commit 2105f24
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
20 changes: 13 additions & 7 deletions src/cpu/interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ pub enum InterruptTypes {

const INTERRUPT_FLAG_ADDRESS: u16 = 0xFF0F;
const INTERRUPT_ENABLE_ADDRESS: u16 = 0xFFFF;
const INTERRUPT_CALL_ADDRESS: u16 = 0x0040;

impl CPU {
/// Set the interrupt flag for the given interrupt
pub fn set_interrupt_flag(&mut self, interrupt: InterruptTypes) {
let interrupt_flag = self.memory.read_byte(INTERRUPT_FLAG_ADDRESS);
self.memory.write_byte(INTERRUPT_FLAG_ADDRESS, interrupt_flag | (1 << interrupt as u8));
self.memory.write_byte(
INTERRUPT_FLAG_ADDRESS,
interrupt_flag | (1 << interrupt as u8),
);
}

/// Check for interrupts and handle them
Expand All @@ -33,23 +37,25 @@ impl CPU {
}

for i in 0..=4 {
// Check if the interrupt flag is set and the interrupt is enabled
if interrupt_flag & (1 << i) != 0 && interrupt_enable & (1 << i) != 0 {

// Disable all interrupts
self.memory.write_byte(INTERRUPT_ENABLE_ADDRESS, 0);

// Clear the interrupt flag
let interrupt_flag = self.memory.read_byte(INTERRUPT_FLAG_ADDRESS);
self.memory.write_byte(INTERRUPT_FLAG_ADDRESS, interrupt_flag & !(1 << i));
let interrupt_flag = self.memory.read_byte(INTERRUPT_FLAG_ADDRESS);
self.memory
.write_byte(INTERRUPT_FLAG_ADDRESS, interrupt_flag & !(1 << i));

// Call the interrupt handler at the appropriate address
// https://gbdev.io/pandocs/Interrupt_Sources.html
self.call_n16(0x0040 + (i as u16 * 8));
self.call_n16(INTERRUPT_CALL_ADDRESS + (i as u16 * 8));

// We return early, interrupts are based on a priority system
// and if one interrupt is handled, we don't want to handle another
return true;
}
}

return false;
}
}
}
3 changes: 1 addition & 2 deletions src/cpu/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ impl CPU {
/// ensure to first set the next instruction
/// by decoding it (see `decode.rs`)
pub fn step(&mut self) -> Result<&InstructionResult, String> {

// Check whether the elapsed time is equal or greater than CPU_FREQUENCY
// Otherwise, sleep for the remaining time to ensure we're running at the correct speed
if self.last_step_result.cycles == 0 {
Expand All @@ -52,7 +51,7 @@ impl CPU {
if self.check_and_handle_interrupts() {
self.last_step_result.cycles = 5;
self.last_step_result.bytes = 0;
return Ok(&self.last_step_result)
return Ok(&self.last_step_result);
}

self.last_step_result = match &self.next_instruction {
Expand Down
8 changes: 6 additions & 2 deletions src/cpu/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ impl CPU {

if overflow {
log::debug!("Timer overflow - resetting to modulo & setting interrupt flag");
self.memory.write_byte(TIMER_COUNTER_ADDRESS, self.memory.read_byte(TIMER_MODULO_ADDRESS));
self.memory.write_byte(
TIMER_COUNTER_ADDRESS,
self.memory.read_byte(TIMER_MODULO_ADDRESS),
);
self.set_interrupt_flag(InterruptTypes::Timer);
} else {
self.memory.write_byte(TIMER_COUNTER_ADDRESS, new_val);
Expand All @@ -25,6 +28,7 @@ impl CPU {
pub fn get_timer_modulo(&mut self) -> u64 {
let timer_speed = self.memory.read_byte(0xFF07) & 0b11;

// See: https://gbdev.io/pandocs/Timer_and_Divider_Registers.html#ff07--tac-timer-control
match timer_speed {
0b00 => 256,
0b01 => 4,
Expand All @@ -33,4 +37,4 @@ impl CPU {
_ => panic!("Invalid timer speed"),
}
}
}
}

0 comments on commit 2105f24

Please sign in to comment.