Skip to content

Commit

Permalink
Prototype correct halted CPU behaviour (#40)
Browse files Browse the repository at this point in the history
Advance PC as part of executing HALT, then fetch (and discard) the next
instruction opcode as long as the CPU remains halted.
  • Loading branch information
simonowen authored and kosarev committed Nov 6, 2021
1 parent c6ad460 commit 4b6b64f
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions z80.h
Original file line number Diff line number Diff line change
Expand Up @@ -2439,6 +2439,8 @@ class internals::executor_base : public B {
if(self().on_is_z80())
self().on_set_addr_bus(self().get_ir_on_refresh());
self().on_tick(2);
if (self().on_is_halted())
return 0; // nop
self().set_pc_on_fetch(inc16(addr));
return n; }

Expand Down Expand Up @@ -3312,9 +3314,7 @@ class internals::executor_base : public B {
}
self().disable_int_on_ei(); }
void on_halt() {
self().on_set_is_halted(true);
// TODO: It seems 'HLT' doesn't really reset PC? Does 'HALT' do?
self().set_pc_on_halt(dec16(self().get_pc_on_halt())); }
self().on_set_is_halted(true); }
void on_im(unsigned mode) {
self().on_set_int_mode(mode); }

Expand Down Expand Up @@ -3741,15 +3741,8 @@ class internals::executor_base : public B {

fast_u16 pc = self().on_get_pc();

// Get past the HALT instruction, if halted. Note that
// HALT instructions need to be executed at least once to
// be skipped on an interrupt, so checking if the PC is
// at a HALT instruction is not enough here.
if(self().on_is_halted()) {
pc = inc16(pc);
self().on_set_pc(pc);
if(self().on_is_halted())
self().on_set_is_halted(false);
}

self().on_inc_r_reg();
self().on_tick(is_nmi ? 5 : 7);
Expand Down

0 comments on commit 4b6b64f

Please sign in to comment.