implement HALT

This commit is contained in:
Alex Janka 2023-02-07 22:49:08 +11:00
parent 89370fd077
commit 122bfee8df
2 changed files with 13 additions and 4 deletions

View file

@ -29,6 +29,7 @@ pub struct CPU {
pub last_instruction_addr: u16, pub last_instruction_addr: u16,
pub window: Window, pub window: Window,
pub gpu: GPU, pub gpu: GPU,
halted: bool,
} }
// Hz // Hz
@ -45,10 +46,19 @@ impl CPU {
last_instruction_addr: 0x0, last_instruction_addr: 0x0,
window, window,
gpu: GPU::default(), gpu: GPU::default(),
halted: false,
} }
} }
pub fn exec_next(&mut self) { pub fn exec_next(&mut self) {
let interrupt_cycles = self.handle_interrupts();
self.increment_timers(interrupt_cycles);
if self.halted {
self.increment_timers(1);
return;
}
self.last_instruction_addr = self.reg.pc; self.last_instruction_addr = self.reg.pc;
let opcode = self.next_opcode(); let opcode = self.next_opcode();
self.last_instruction = opcode; self.last_instruction = opcode;
@ -69,9 +79,6 @@ impl CPU {
let cycles = self.run_opcode(opcode); let cycles = self.run_opcode(opcode);
self.memory.user_mode = false; self.memory.user_mode = false;
self.increment_timers(cycles); self.increment_timers(cycles);
let interrupt_cycles = self.handle_interrupts();
self.increment_timers(interrupt_cycles);
} }
fn increment_timers(&mut self, cycles: u8) { fn increment_timers(&mut self, cycles: u8) {
@ -149,6 +156,7 @@ impl CPU {
} }
fn service_interrupt(&mut self, addr: u16) { fn service_interrupt(&mut self, addr: u16) {
self.halted = false;
self.push(self.reg.pc); self.push(self.reg.pc);
self.reg.pc = addr; self.reg.pc = addr;
self.memory.ime = false; self.memory.ime = false;

View file

@ -552,7 +552,8 @@ impl CPU {
2 2
} }
0x76 => { 0x76 => {
panic!("halt") self.halted = true;
1
} }
0x77 => { 0x77 => {
self.memory.set(self.reg.hl, self.reg.get_8(Reg8::A)); self.memory.set(self.reg.hl, self.reg.get_8(Reg8::A));