-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembler_test.go
133 lines (131 loc) · 3.36 KB
/
assembler_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
package main
import (
"reflect"
"testing"
)
func TestFirstPassAssembler(t *testing.T) {
for i, tt := range []struct{
input string
want []uint16
}{
{
input: "",
want: []uint16{},
},
{
input: "//COMMENT\n",
want: []uint16{},
},
{
input: "@AD41\n",
want: []uint16{0xAD41},
},
{
input: "D=A\n",
want: []uint16{0xEC10},
},
{
input: "M=0\n",
want: []uint16{0xEA88},
},
{
input: "A=1\n",
want: []uint16{0xEFE0},
},
{
input: "AMD=A\n",
want: []uint16{0xEC38},
},
{
input: "A\n",
want: []uint16{0xEC00},
},
{
input: "-1\n",
want: []uint16{0xEE80},
},
{
input: "!M\n",
want: []uint16{0xFC40},
},
{
input: "D=D+A\n",
want: []uint16{0xE090},
},
{
input: "M=M+1\n",
want: []uint16{0xFDC8},
},
{
input: "D;JEQ\n",
want: []uint16{0xE302},
},
{
input: "0;JMP\n",
want: []uint16{0xEA87},
},
{
input: "D=D<<3\n",
want: []uint16{0xC990},
},
{
input: "@0006\nD=M\n@0002\nM=M+1\nA=M-1\nM=D\n",
want: []uint16{0x6, 0xFC10, 0x2, 0xFDC8, 0xFCA0, 0xE308},
},
{
input: " D=D<<3\n",
want: []uint16{0xC990},
},
{
input: "D=D<<3 // COMMENT\n",
want: []uint16{0xC990},
},
{
input: "@0000\n",
want: []uint16{0x0},
},
}{
input := make([]uint16, len(tt.input))
for i, r := range tt.input {
if r == '\n' { r = 0x80 }
input[i] = uint16(r)
}
cpu := NewBarrelShiftCPU()
computer := NewComputer(cpu)
computer.LoadProgram(NewROM32K(assembleFirstPass))
computer.SendReset(true)
computer.ClockTick()
computer.SendReset(false)
for n, ascii := range input {
computer.data_mem.ram.mem[0x1000+n] = ascii
}
var pprev, prev uint16
for {
computer.ClockTick()
//if i == 12 {
//t.Errorf("%d: %04x - R6: %04x, M0x2000: %04x", prev, cpu.instr, computer.data_mem.ram.mem[0x6], computer.data_mem.ram.mem[0x2000])
//t.Errorf("%d: %04x - R6: %04x, A: %04x, D: %04x", prev, cpu.instr, computer.data_mem.ram.mem[0x6], cpu.a.Out(), cpu.d.Out())
//}
// detect end loop
if pprev == cpu.PC() {
break
}
pprev = prev
prev = cpu.PC()
}
output := []uint16{}
outputpointer := 0x1000
endoutput := int(computer.data_mem.ram.mem[0x2])
for {
if outputpointer == endoutput {
break
}
v := computer.data_mem.ram.mem[outputpointer]
output = append(output, v)
outputpointer++
}
if !reflect.DeepEqual(output, tt.want) {
t.Errorf("%d) got %d but want %d", i, output, tt.want)
}
}
}