-
Notifications
You must be signed in to change notification settings - Fork 4
/
op_load8_test.go
129 lines (122 loc) · 2.64 KB
/
op_load8_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package z80
import (
"fmt"
"math/rand"
"testing"
"time"
)
func rGet(t *testing.T, gpr *GPR, n int) *uint8 {
t.Helper()
switch n {
case 0:
return &gpr.BC.Hi
case 1:
return &gpr.BC.Lo
case 2:
return &gpr.DE.Hi
case 3:
return &gpr.DE.Lo
case 4:
return &gpr.HL.Hi
case 5:
return &gpr.HL.Lo
case 7:
return &gpr.AF.Hi
default:
t.Fatalf("invalid r index: %d", n)
return nil
}
}
var rLabel = []string{"B", "C", "D", "E", "H", "L", "(N/A)", "A"}
var testInitGPR = GPR{
AF: Register{Hi: 0x12},
BC: Register{Hi: 0x34, Lo: 0x56},
DE: Register{Hi: 0x78, Lo: 0x9A},
HL: Register{Hi: 0xBC, Lo: 0xDE},
}
func TestLoad8_LDr1r2(t *testing.T) {
t.Parallel()
for r1 := 0; r1 <= 7; r1++ {
if r1 == 6 {
continue
}
for r2 := 0; r2 <= 7; r2++ {
if r2 == 6 {
continue
}
n := fmt.Sprintf("LD %s, %s", rLabel[r1], rLabel[r2])
c := uint8(0x40 | r1<<3 | r2)
beforeGPR := testInitGPR
afterGPR := testInitGPR
*rGet(t, &afterGPR, r1) = *rGet(t, &beforeGPR, r2)
t.Run(n, func(t *testing.T) {
tOneStep(t,
States{GPR: beforeGPR},
MapMemory{}.Put(0, c),
States{GPR: afterGPR,
SPR: SPR{PC: 0x0001, IR: Register{Lo: 0x01}}},
MapMemory{}.Put(0, c))
})
}
}
}
func TestLoad8_LDrn(t *testing.T) {
t.Parallel()
for r := 0; r <= 7; r++ {
if r == 6 {
continue
}
n := fmt.Sprintf("LD %s, n", rLabel[r])
c := uint8(0x06 | r<<3)
beforeGPR := testInitGPR
var afterGPR GPR
p := rGet(t, &afterGPR, r)
t.Run(n, func(t *testing.T) {
t.Parallel()
for n := 0; n <= 0xff; n++ {
afterGPR = testInitGPR
*p = uint8(n)
tOneStep(t,
States{GPR: beforeGPR},
MapMemory{}.Put(0, c, uint8(n)),
States{GPR: afterGPR,
SPR: SPR{PC: 0x0002, IR: Register{Lo: 0x01}}},
MapMemory{}.Put(0, c, uint8(n)))
}
})
}
}
func TestLoad8_LDrHL(t *testing.T) {
t.Parallel()
for r := 0; r <= 7; r++ {
if r == 6 {
continue
}
n := fmt.Sprintf("LD %s, (HL)", rLabel[r])
c := uint8(0x46 | r<<3)
var beforeGPR, afterGPR GPR
p := rGet(t, &afterGPR, r)
r2 := r // capture loop variable r explicitly
t.Run(n, func(t *testing.T) {
t.Parallel()
rnd := rand.New(rand.NewSource(time.Now().UnixNano() * int64(r2)))
for hl := 0; hl <= 0xffff; hl++ {
memory := MapMemory{}.Put(0, c)
if hl != 0 {
d := uint8(rnd.Intn(255) + 1)
memory.Put(uint16(hl), d)
}
wantMem := memory.Clone()
beforeGPR = testInitGPR
beforeGPR.HL.SetU16(uint16(hl))
afterGPR = beforeGPR
*p = memory.Get(uint16(hl))
tOneStep(t,
States{GPR: beforeGPR}, memory,
States{GPR: afterGPR,
SPR: SPR{PC: 0x0001, IR: Register{Lo: 0x01}}},
wantMem)
}
})
}
}