-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulate_test.go
120 lines (96 loc) · 2.33 KB
/
emulate_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
package main
import (
"testing"
)
type mockGraphics struct{}
func (m *mockGraphics) Render() {
return
}
func (m *mockGraphics) BindBuffer([]byte) {
return
}
func generateMockChip8() *chip8 {
chip := NewCHIP8(&mockGraphics{})
return chip
}
func TestCLS(t *testing.T) {
chip := generateMockChip8()
chip.disp[0] = 0xF1
chip.disp[1] = 0x5A
chip.disp[1023] = 0xFF
chip.EmulateDecodedInstruction(0x00E0)
for i := 0; i < len(chip.disp); i++ {
if chip.disp[i] != 0x0 {
t.Error("Test Failed: expected 0x00E0 instruction to clear disp bits, but did not")
}
}
}
func TestRet(t *testing.T) {
chip := generateMockChip8()
chip.pc = 0x8
chip.sp = 2
chip.stack[0] = 0x1
chip.stack[1] = 0x2
chip.stack[2] = 0x3
chip.EmulateDecodedInstruction(0x00EE)
if chip.pc != 0x5 {
t.Errorf("Test Failed: expected program counter to update, found value 0x%x", chip.pc)
}
if chip.sp != 1 {
t.Errorf("Test Failed: expected stack pointer to decrement, found 0x%x", chip.sp)
}
}
func TestSetPC(t *testing.T) {
var tests = []struct {
opcode uint16
result address
}{
{0x1015, 0x015},
{0x1000, 0x000},
{0x1FFF, 0xFFF},
}
for _, test := range tests {
chip := generateMockChip8()
chip.EmulateDecodedInstruction(test.opcode)
if chip.pc != test.result {
t.Errorf("Test Failed: expected program counter to update to 0x%x, found instead 0x%x", test.result, chip.pc)
}
}
}
func TestCall(t *testing.T) {
type setup struct {
stack []address
sp uint8
pc address
}
type result struct {
sp uint8
pc address
stackTop address
}
var tests = []struct {
setup setup
res result
}{
{setup{[]address{0x1, 0x2, 0x3}, 2, 0x200}, result{3, 0x00F, 0x200}},
}
for _, test := range tests {
chip := generateMockChip8()
chip.sp = test.setup.sp
chip.pc = test.setup.pc
for i, s := range test.setup.stack {
chip.stack[i] = s
}
chip.EmulateDecodedInstruction(0x200F)
if chip.pc != test.res.pc {
t.Errorf("Expected program counter to update to 0x%x, got 0x%x", test.res.pc, chip.pc)
}
if chip.sp != test.res.sp {
t.Errorf("Expected stack pointer to update to %d, got %d", test.res.sp, chip.sp)
}
actualStackTop := chip.stack[chip.sp]
if actualStackTop != test.res.stackTop {
t.Errorf("Expected address at top of stack to be 0x%x, got 0x%x", test.res.stackTop, chip.stack[chip.sp])
}
}
}