|
| 1 | +package lr35902 |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/colecrouter/gameboy-go/private/memory" |
| 8 | + "github.com/colecrouter/gameboy-go/private/memory/io" |
| 9 | +) |
| 10 | + |
| 11 | +var instrLengths = [0x100]int{ |
| 12 | + 1, 3, 1, 1, 1, 1, 2, 1, 3, 1, 1, 1, 1, 1, 2, 1, |
| 13 | + 0, 3, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, |
| 14 | + 2, 3, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, |
| 15 | + 2, 3, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, |
| 16 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 17 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 18 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 19 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 20 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 21 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 22 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 23 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 24 | + 1, 1, 3, 3, 3, 1, 2, 1, 1, 1, 3, 0, 3, 3, 2, 1, |
| 25 | + 1, 1, 3, 0, 3, 1, 2, 1, 1, 1, 3, 0, 3, 0, 2, 1, |
| 26 | + 2, 1, 1, 0, 0, 1, 2, 1, 2, 1, 3, 0, 0, 0,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2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, |
| 71 | + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, |
| 72 | + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, |
| 73 | + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, |
| 74 | + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, |
| 75 | + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, |
| 76 | + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, |
| 77 | + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, |
| 78 | + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, |
| 79 | + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, |
| 80 | + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, |
| 81 | + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, |
| 82 | + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, |
| 83 | + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, |
| 84 | + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, |
| 85 | +} |
| 86 | + |
| 87 | +// newTestCPU creates a fresh CPU instance and its required bus, memory and registers. |
| 88 | +func newTestCPU() (*LR35902, *memory.Memory, *memory.Bus) { |
| 89 | + bus := &memory.Bus{} |
| 90 | + ir := &io.Interrupt{} |
| 91 | + ie := &io.Interrupt{} |
| 92 | + ioreg := io.NewRegisters(nil, bus, ir) |
| 93 | + mem := &memory.Memory{Buffer: make([]uint8, 0x10000)} |
| 94 | + bus.AddDevice(0, 0xFFFF, mem) |
| 95 | + cpu := NewLR35902(nil, bus, ioreg, ie) |
| 96 | + return cpu, mem, bus |
| 97 | +} |
| 98 | + |
| 99 | +// runCyclesTest is a helper to run a test iteration for a given opcode. |
| 100 | +// adjust can be used to modify the CPU state (for CB tests or conditional flags). |
| 101 | +func runCyclesTest(t *testing.T, opcode uint8, ticks int, expectedPC int, condition bool, adjust func(cpu *LR35902)) { |
| 102 | + cpu, mem, _ := newTestCPU() |
| 103 | + cpu.registers.PC = 0 |
| 104 | + mem.Write(0, opcode) |
| 105 | + |
| 106 | + // Set up conditional flags if needed. |
| 107 | + setupConditionalByOpcode(cpu, opcode, condition) |
| 108 | + |
| 109 | + if adjust != nil { |
| 110 | + adjust(cpu) |
| 111 | + } |
| 112 | + manualClock := make(chan struct{}, ticks) |
| 113 | + cpu.clock = manualClock |
| 114 | + for j := 0; j < ticks; j++ { |
| 115 | + manualClock <- struct{}{} |
| 116 | + } |
| 117 | + done := make(chan struct{}) |
| 118 | + go func() { |
| 119 | + cpu.MClock() |
| 120 | + close(done) |
| 121 | + }() |
| 122 | + select { |
| 123 | + case <-done: |
| 124 | + if int(cpu.registers.PC) != expectedPC { |
| 125 | + t.Errorf("opcode 0x%X: PC got %d, want %d", opcode, cpu.registers.PC, expectedPC) |
| 126 | + } |
| 127 | + case <-time.After(100 * time.Millisecond): |
| 128 | + t.Errorf("opcode 0x%X did not complete within the timeout", opcode) |
| 129 | + } |
| 130 | +} |
0 commit comments