-
Notifications
You must be signed in to change notification settings - Fork 4
/
op_arith16_test.go
179 lines (162 loc) · 3.91 KB
/
op_arith16_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package z80
import (
"fmt"
"testing"
)
var rArith16ss = []tReg{
{"BC", 0}, {"DE", 1}, {"HL", 2}, {"SP", 3},
}
// isOverflowS16 checks overflow as signed 8 bits variable.
func isOverflowS16(v int32) bool {
return v > 32767 || v < -32768
}
func tADChlss(t *testing.T, r tReg, hl, ss uint16, c bool) {
hl32, ss32 := uint32(hl), uint32(ss)
var c32 uint32
mem := MapMemory{}.Put(0, 0xed, 0x4a|(uint8(r.Code)<<4))
preGPR := testInitGPR
preGPR.HL.SetU16(hl)
preSPR := SPR{}
switch r.Code {
case 0:
preGPR.BC.SetU16(ss)
case 1:
preGPR.DE.SetU16(ss)
case 2:
if hl != ss {
t.Logf("warning ss not matches with HL for ADC HL, ss(=HL)")
ss = hl
}
case 3:
preSPR.SP = ss
default:
t.Fatalf("unsupported register: %d", r.Code)
}
if c {
c32 = 1
// set carry flag
preGPR.AF.Lo = 0x01
} else {
preGPR.AF.Lo = 0x00
}
sum := hl32 + ss32 + c32
var flags uint8
if sum&0x8000 != 0 {
flags |= 0x80 // S is set if result is negative
}
if sum&0xffff == 0 {
flags |= 0x40 // Z is set if result is 0
}
if hl32&0xfff+ss32&0xfff+c32 > 0xfff {
flags |= 0x10 // H: is set if carry from bit 3
}
if isOverflowS16(int32(int16(hl)) + int32(int16(ss)) + int32(c32)) {
flags |= 0x04 // PV is set if overflow (-32768~+32767)
}
// N is reset
if sum > 0xffff {
// C is set if carry from bit 7
flags |= 0x01
}
postGPR := preGPR
postGPR.HL.SetU16(uint16(sum))
postGPR.AF.Lo = flags
postSPR := preSPR
postSPR.PC = 0x0002
postSPR.IR = Register{Lo: 0x02}
tSteps(t,
fmt.Sprintf("ADC HL %[1]s (HL=%04[2]x %[1]s=%04[3]x C=%[4]t)", r.Label, hl, ss, c),
States{GPR: preGPR, SPR: preSPR}, mem, 1,
States{GPR: postGPR, SPR: postSPR}, mem, maskDefault)
}
func TestAirth16_adc16_ADChlss(t *testing.T) {
t.Parallel()
for _, r := range rArith16ss {
for _, hl := range u16casesSummary {
if r.Label == "HL" {
tADChlss(t, r, uint16(hl), uint16(hl), false)
tADChlss(t, r, uint16(hl), uint16(hl), true)
continue
}
for _, ss := range u16casesSummary {
tADChlss(t, r, uint16(hl), uint16(ss), false)
tADChlss(t, r, uint16(hl), uint16(ss), true)
}
}
}
}
func tSBChlss(t *testing.T, r tReg, hl, ss uint16, c bool) {
hl32, ss32 := uint32(hl), uint32(ss)
var c32 uint32
mem := MapMemory{}.Put(0, 0xed, 0x42|(uint8(r.Code)<<4))
preGPR := testInitGPR
preGPR.HL.SetU16(hl)
preSPR := SPR{}
switch r.Code {
case 0:
preGPR.BC.SetU16(ss)
case 1:
preGPR.DE.SetU16(ss)
case 2:
if hl != ss {
t.Logf("warning ss not matches with HL for SBC HL, ss(=HL)")
ss = hl
}
case 3:
preSPR.SP = ss
default:
t.Fatalf("unsupported register: %d", r.Code)
}
if c {
c32 = 1
// set carry flag
preGPR.AF.Lo = 0x01
} else {
preGPR.AF.Lo = 0x00
}
sum := hl32 - ss32 - c32
var flags uint8
if sum&0x8000 != 0 {
flags |= 0x80 // S is set if result is negative
}
if sum&0xffff == 0 {
flags |= 0x40 // Z is set if result is 0
}
if hl32&0xfff < ss32&0xfff+c32 {
flags |= 0x10 // H: is set if carry from bit 3
}
if isOverflowS16(int32(int16(hl)) - int32(int16(ss)) - int32(c32)) {
flags |= 0x04 // PV is set if overflow (-32768~+32767)
}
flags |= 0x02 // N is set
if sum > 0xffff {
// C is set if carry from bit 7
flags |= 0x01
}
postGPR := preGPR
postGPR.HL.SetU16(uint16(sum))
postGPR.AF.Lo = flags
postSPR := preSPR
postSPR.PC = 0x0002
postSPR.IR = Register{Lo: 0x02}
tSteps(t,
fmt.Sprintf("SBC HL %[1]s (HL=%04[2]x %[1]s=%04[3]x C=%[4]t)", r.Label, hl, ss, c),
States{GPR: preGPR, SPR: preSPR}, mem, 1,
States{GPR: postGPR, SPR: postSPR}, mem, maskDefault)
}
func TestArith16_adc16_SBChlss(t *testing.T) {
t.Parallel()
for _, r := range rArith16ss {
for _, hl := range u16casesSummary {
if r.Label == "HL" {
tSBChlss(t, r, uint16(hl), uint16(hl), false)
tSBChlss(t, r, uint16(hl), uint16(hl), true)
continue
}
for _, ss := range u16casesSummary {
tSBChlss(t, r, uint16(hl), uint16(ss), false)
tSBChlss(t, r, uint16(hl), uint16(ss), true)
}
}
}
}