Skip to content

Commit

Permalink
Merge pull request #15 from koron-go/interrupt-test
Browse files Browse the repository at this point in the history
Test and fix interruptions
  • Loading branch information
koron authored Apr 21, 2024
2 parents 6e642da + fc39bef commit 0b270b7
Show file tree
Hide file tree
Showing 5 changed files with 383 additions and 6 deletions.
1 change: 1 addition & 0 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func TestBuildWithINT(t *testing.T) {
type dummyNMI struct{}

func (dummyNMI) CheckNMI() bool { return false }
func (dummyNMI) ReturnNMI() {}

func TestBuildWithNMI(t *testing.T) {
v := dummyNMI{}
Expand Down
25 changes: 19 additions & 6 deletions cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,34 @@ type im0data struct {
start uint16
end uint16
data []uint8

base Memory
}

func newIm0data(pc uint16, d []uint8) *im0data {
func newIm0data(pc uint16, d []uint8, base Memory) *im0data {
return &im0data{
start: pc,
end: pc + uint16(len(d)-1),
data: d,
base: base,
}
}

func (im0 *im0data) Get(addr uint16) uint8 {
if addr < im0.start || addr > im0.end {
return 0
// delegate to base Memory for out of range.
return im0.base.Get(addr)
}
return im0.data[addr-im0.start]
}

func (im0 *im0data) Set(addr uint16, value uint8) {
// invalid opepration, nothing to do.
if addr >= im0.start && addr <= im0.end {
// invalid opepration, nothing to do.
return
}
// delegate to base Memory for out of range.
im0.base.Set(addr, value)
}

func (cpu *CPU) failf(msg string, args ...interface{}) {
Expand Down Expand Up @@ -218,7 +227,7 @@ func (cpu *CPU) tryInterrupt() bool {
return true
}
// check maskable interrupt.
if cpu.INT == nil {
if !cpu.IFF1 || cpu.INT == nil {
return false
}
d := cpu.INT.CheckINT()
Expand All @@ -231,6 +240,7 @@ func (cpu *CPU) tryInterrupt() bool {
cpu.SP -= 2
cpu.writeU16(cpu.SP, cpu.PC)
cpu.PC = 0x0038
cpu.IFF1 = false
return true
case 2:
if n := len(d); n != 1 {
Expand All @@ -242,7 +252,9 @@ func (cpu *CPU) tryInterrupt() bool {
cpu.HALT = false
cpu.SP -= 2
cpu.writeU16(cpu.SP, cpu.PC)
cpu.PC = toU16(d[0]&0xfe, cpu.IR.Hi)
// The LSB of interruption data is ignored in IM 2
cpu.PC = cpu.readU16(toU16(d[0]&0xfe, cpu.IR.Hi))
cpu.IFF1 = false
return true
}
// interrupt with IM 0
Expand All @@ -252,9 +264,10 @@ func (cpu *CPU) tryInterrupt() bool {
}
cpu.HALT = false
savedMemory := cpu.Memory
cpu.Memory = newIm0data(cpu.PC, d)
cpu.Memory = newIm0data(cpu.PC, d, savedMemory)
cpu.executeOne()
cpu.Memory = savedMemory
cpu.IFF1 = false
return true
}

Expand Down
Loading

0 comments on commit 0b270b7

Please sign in to comment.