Skip to content

Commit

Permalink
fix(cpu): Delay IRQ by one clock after CLI is called
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Aug 26, 2024
1 parent 9433b19 commit 48783d5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
7 changes: 6 additions & 1 deletion internal/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type CPU struct {

NMIPending bool `msgpack:"alias:NmiPending"`
IRQPending bool `msgpack:"alias:IrqPending"`
irqDelay uint8

Stall uint16

Expand Down Expand Up @@ -99,7 +100,11 @@ func (c *CPU) Step() uint {
if c.NMIPending {
c.nmi()
} else if c.IRQPending && !c.Status.InterruptDisable {
c.irq()
if c.irqDelay == 0 {
c.irq()
} else {
c.irqDelay--
}
}

code := c.ReadMem(c.ProgramCounter)
Expand Down
1 change: 1 addition & 0 deletions internal/cpu/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ func cld(c *CPU, _ AddressingMode) {
// [CLI Instruction Reference]: https://www.nesdev.org/obelisk-6502-guide/reference.html#CLI
func cli(c *CPU, _ AddressingMode) {
c.Status.InterruptDisable = false
c.irqDelay = 1
}

// clv - Clear Overflow Flag
Expand Down

0 comments on commit 48783d5

Please sign in to comment.