-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnes_test.go
160 lines (129 loc) · 3.11 KB
/
nes_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
package gorones
import (
"bufio"
"os"
"regexp"
"strconv"
"strings"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thara/gorones/cpu"
"github.com/thara/gorones/input"
"github.com/thara/gorones/mapper"
"github.com/thara/gorones/ppu"
)
type nopFrameRenderer struct{}
func (nopFrameRenderer) UpdateFrame(*[ppu.WIDTH * ppu.HEIGHT]uint8) {}
type nopAudioRenderer struct{}
func (nopAudioRenderer) Write(float32) {}
func Test_nestest(t *testing.T) {
f, err := os.Open("testdata/nestest.nes")
require.NoError(t, err)
defer f.Close()
rom, err := mapper.ParseROM(f)
require.NoError(t, err)
m, err := rom.Mapper()
require.NoError(t, err)
var ctrl1, ctrl2 input.StandardController
nes := NewNES(m, &ctrl1, &ctrl2, new(nopFrameRenderer), new(nopAudioRenderer))
nes.PowerOn()
nes.InitNEStest()
f, err = os.Open("testdata/nestest.log")
require.NoError(t, err)
defer f.Close()
i := 1
sc := bufio.NewScanner(f)
for sc.Scan() {
line := sc.Text()
tr := nes.cpu.Trace()
nes.step()
li, err := parseCPUTrace(line)
require.NoError(t, err)
require.Equal(t, *li, tr, "lineno:%d %s", i, line)
i++
}
require.NoError(t, sc.Err())
assert.EqualValues(t, 26560, nes.cpu.Trace().Cycles)
assert.EqualValues(t, 0, nes.ReadCPU(0x0002))
assert.EqualValues(t, 0, nes.ReadCPU(0x0003))
}
// nestest.log line example:
// C000 4C F5 C5 JMP $C5F5 A:00 X:00 Y:00 P:24 SP:FD PPU: 0, 21 CYC:7
var nestestLogLineRe = regexp.MustCompile(
`(?P<pc>.{4}) (?P<op>.{2}) (?P<op1>.{2}) (?P<op2>.{2}) (\s|\*)(?P<code>.{32})A:(?P<a>.{2}) X:(?P<x>.{2}) Y:(?P<y>.{2}) P:(?P<p>.{2}) SP:(?P<s>.{2}) PPU:(?P<dot>.{3}),(?P<line>.{3}) CYC:(?P<cyc>.*)`)
func parseCPUTrace(line string) (*cpu.Trace, error) {
re := nestestLogLineRe
var t cpu.Trace
matches := re.FindStringSubmatch(line)
toInt := func(name string, base int, out *int64) error {
i := re.SubexpIndex(name)
m := matches[i]
if len(strings.Trim(m, " ")) == 0 {
*out = 0
return nil
}
n, err := strconv.ParseInt(matches[i], base, 64)
if err != nil {
return errors.WithStack(err)
}
*out = n
return nil
}
var n int64
err := toInt("pc", 16, &n)
if err != nil {
return nil, err
}
t.PC = uint16(n)
err = toInt("op", 16, &n)
if err != nil {
return nil, err
}
t.Opcode = uint8(n)
err = toInt("op1", 16, &n)
if err != nil {
return nil, err
}
t.Operand1 = uint8(n)
err = toInt("op2", 16, &n)
if err != nil {
return nil, err
}
t.Operand2 = uint8(n)
err = toInt("a", 16, &n)
if err != nil {
return nil, err
}
t.A = uint8(n)
err = toInt("x", 16, &n)
if err != nil {
return nil, err
}
t.X = uint8(n)
err = toInt("y", 16, &n)
if err != nil {
return nil, err
}
t.Y = uint8(n)
err = toInt("p", 16, &n)
if err != nil {
return nil, err
}
t.P = cpu.NewStatus(uint8(n))
err = toInt("s", 16, &n)
if err != nil {
return nil, err
}
t.S = uint8(n)
err = toInt("cyc", 10, &n)
if err != nil {
return nil, err
}
t.Cycles = uint64(n)
inst := cpu.Decode(t.Opcode)
t.Mnemonic = inst.Mnemonic
t.AddressingMode = inst.AddressingMode
return &t, nil
}