-
Notifications
You must be signed in to change notification settings - Fork 1
/
VM_test.go
380 lines (302 loc) · 11 KB
/
VM_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package lc3_test
import (
"bytes"
"fmt"
"os"
"strings"
"testing"
"lc3"
)
func TestVM(t *testing.T) {
t.Run("try to load erroneous programs", func(t *testing.T) {
testCases := []string{
"",
"\x30",
"\x30\x00\x08",
}
for i, test := range testCases {
t.Run(fmt.Sprintf("test case #%d", i), func(t *testing.T) {
program := strings.NewReader(test)
_, err := lc3.NewVM(program, nil, nil)
assertIsError(t, err)
})
}
})
t.Run("load an empty program and check PC", func(t *testing.T) {
var pc uint16 = 0x3000
program := strings.NewReader("\x30\x00")
vm, err := lc3.NewVM(program, nil, nil)
assertError(t, err, nil)
assertInitVM(t, vm, pc)
})
t.Run("load a simple program and check memory", func(t *testing.T) {
var start uint16 = 0x3000
program := strings.NewReader("\x30\x00\x12\x34")
vm, err := lc3.NewVM(program, nil, nil)
assertError(t, err, nil)
assertRegister(t, vm, lc3.Register_PC, start)
assertMemory(t, vm, start, 0x1234)
})
t.Run("test Simple instructions", func(t *testing.T) {
var canaryAddress, canaryValue uint16 = 0x2f06, 0x1234
testCases := []struct {
name string
instruction string
steps int
reg lc3.Register
value uint16
flag uint16
}{
{"NOP", "\x00\x00", 1, lc3.Register_PC, 0x3001, lc3.Flag_Z},
{"BRp x3001", "\x02\x12", 1, lc3.Register_PC, 0x3001, lc3.Flag_Z},
{"BRz x3013", "\x04\x12", 1, lc3.Register_PC, 0x3013, lc3.Flag_Z},
{"BRzp x3008", "\x06\x07", 1, lc3.Register_PC, 0x3008, lc3.Flag_Z},
{"BRz x2f03", "\x05\x02", 1, lc3.Register_PC, 0x2f03, lc3.Flag_Z},
{"BRn x2f34", "\x09\x33", 1, lc3.Register_PC, 0x3001, lc3.Flag_Z},
{"LEA R0, x3003", "\xE0\x02", 1, lc3.Register_R0, 0x3003, lc3.Flag_P},
{"LEA R1, x2F35", "\xE3\x34", 1, lc3.Register_R1, 0x2F35, lc3.Flag_P},
{"LEA R7, x3001", "\xEE\x00", 1, lc3.Register_R7, 0x3001, lc3.Flag_P},
{"NOT R0, R0", "\x90\x3f", 1, lc3.Register_R0, 0xffff, lc3.Flag_N},
{"LEA R3, x3039 + NOT R3, R1", "\xE6\x38\x96\xFF", 2, lc3.Register_R3, 0xCFC6, lc3.Flag_N},
{"ADD R0, R0, R0", "\x10\x00", 1, lc3.Register_R0, 0x0000, lc3.Flag_Z},
{"ADD R0, R0, #0", "\x10\x20", 1, lc3.Register_R0, 0x0000, lc3.Flag_Z},
{"ADD R3, R2, #5", "\x16\x25", 1, lc3.Register_R3, 0x0005, lc3.Flag_P},
{"ADD R5, R4, #-11", "\x1B\x35", 1, lc3.Register_R5, 0xfff5, lc3.Flag_N},
{"ADD R7, R0, #-14 + ADD R3, R0, R7", "\x1E\x32\x16\x07", 2, lc3.Register_R3, 0xfff2, lc3.Flag_N},
{"AND R0, R0, R0", "\x50\x00", 1, lc3.Register_R0, 0x0000, lc3.Flag_Z},
{"AND R3, R7, #-22", "\x57\xEA", 1, lc3.Register_R3, 0x0000, lc3.Flag_Z},
{"ADD R7, R0, #-14 + AND R3, R7, R7", "\x1E\x32\x57\xC7", 2, lc3.Register_R3, 0xfff2, lc3.Flag_N},
{"JMP R3", "\xC0\x00", 1, lc3.Register_PC, 0x0000, lc3.Flag_Z},
{"RET (JMP R7)", "\xC1\xC0", 1, lc3.Register_PC, 0x0000, lc3.Flag_Z},
{"ADD R5 R4 #-15 + JMP R5", "\x1B\x31\xC1\x40", 2, lc3.Register_PC, 0xfff1, lc3.Flag_N},
{"JSR x3001", "\x48\x00", 1, lc3.Register_PC, 0x3001, lc3.Flag_Z},
{"JSRR R0; check R7", "\x40\x00", 1, lc3.Register_R7, 0x3001, lc3.Flag_Z},
{"JSRR R0; check PC", "\x40\x00", 1, lc3.Register_PC, 0x0000, lc3.Flag_Z},
{"ADD R3, R3, #14 + JSRR R3", "\x16\xEE\x40\xC0", 2, lc3.Register_PC, 0x000E, lc3.Flag_P},
{"LD R0, x3001", "\x20\x00", 1, lc3.Register_R0, 0x0000, lc3.Flag_Z},
{"LD R5, x3003", "\x2A\x02\x00\x00\x00\x00\x01\x23", 1, lc3.Register_R5, 0x0123, lc3.Flag_P},
{"LD R0, x2F06", "\x21\x05", 1, lc3.Register_R0, 0x1234, lc3.Flag_P},
{"LDI R6, x3001", "\xA6\x00\x00\x00", 1, lc3.Register_R3, 0x0000, lc3.Flag_Z},
{"LDI R8, x3002", "\xA8\x01\x00\x15\x30\x01", 1, lc3.Register_R4, 0x0015, lc3.Flag_P},
{"LDR R0, R0, #0", "\x60\x00", 1, lc3.Register_R0, 0x0000, lc3.Flag_Z},
{"LEA R1, x3002 + LDR R4, R1, #1", "\xE2\x01\x68\x41\x00\x00\xFF\xFF", 2, lc3.Register_R4, 0xFFFF, lc3.Flag_N},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
program := strings.NewReader("\x30\x00" + test.instruction)
vm, err := lc3.NewVM(program, nil, nil)
assertError(t, err, nil)
vm.SetMemory(canaryAddress, canaryValue)
for i := 0; i < test.steps; i++ {
err = vm.Step()
assertError(t, err, nil)
}
assertRegister(t, vm, test.reg, test.value)
assertRegister(t, vm, lc3.Register_COND, test.flag)
})
}
})
t.Run("test ST/STI/STR instructions", func(t *testing.T) {
testCases := []struct {
name string
instruction string
memory uint16
value uint16
flag uint16
}{
{"NOP + ST R2, x3006", "\x00\x00\x34\x05", 0x3006, 0x0, lc3.Flag_Z},
{"ADD R5 R4 #-14 + ST R5, x3006", "\x1B\x32\x3A\x40", 0x3042, 0xFFF2, lc3.Flag_N},
{"NOP + STI R0, x3002", "\x00\x00\xB0\x00", 0x0000, 0x0, lc3.Flag_Z},
{"ADD R7, R4, #-1 + STI R7, x3003", "\x1F\x3F\xBE\x01\x00\x00\x12\x34", 0x1234, 0xFFFF, lc3.Flag_N},
{"NOP + STR R0, R0, #0", "\x00\x00\x70\x00", 0x0000, 0x0, lc3.Flag_Z},
{"ADD R7, R4, #8 + STR R7, R0, #1", "\x1F\x28\x7E\x12", 0x0012, 0x0008, lc3.Flag_P},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
program := strings.NewReader("\x30\x00" + test.instruction)
vm, err := lc3.NewVM(program, nil, nil)
assertError(t, err, nil)
err = vm.Step()
assertError(t, err, nil)
err = vm.Step()
assertError(t, err, nil)
assertMemory(t, vm, test.memory, test.value)
assertRegister(t, vm, lc3.Register_COND, test.flag)
})
}
})
t.Run("test PUTS trap", func(t *testing.T) {
program := strings.NewReader("\x30\x00\xE0\x01\xf0\x22\x00\x41\x00\x00")
output := bytes.NewBuffer([]byte{})
vm, err := lc3.NewVM(program, nil, output)
assertError(t, err, nil)
// LEA R0, x3002
err = vm.Step()
assertError(t, err, nil)
// PUTS (R0 == x3002 == "H\0")
err = vm.Step()
assertError(t, err, nil)
assertRegister(t, vm, lc3.Register_PC, 0x3002)
assertString(t, "A", output.String())
})
t.Run("test HALT trap", func(t *testing.T) {
program := strings.NewReader("\x30\x00\xf0\x25\x00\x00")
vm, err := lc3.NewVM(program, nil, nil)
assertError(t, err, nil)
err = vm.Step()
assertError(t, err, nil)
assertState(t, vm.State(), lc3.StateHalted)
assertRegister(t, vm, lc3.Register_PC, 0x3001)
// Can't step further
err = vm.Step()
assertIsError(t, err)
assertRegister(t, vm, lc3.Register_PC, 0x3001)
})
t.Run("test GETC trap", func(t *testing.T) {
program := strings.NewReader("\x30\x00\xf0\x20")
want := 'O'
input := strings.NewReader(string(want))
vm, err := lc3.NewVM(program, input, nil)
assertError(t, err, nil)
// To make sure that top bytes are also cleared.
vm.SetRegister(lc3.Register_R0, 0x1234)
err = vm.Step()
assertError(t, err, nil)
assertState(t, vm.State(), lc3.StateRunning)
assertRegister(t, vm, lc3.Register_PC, 0x3001)
assertRegister(t, vm, lc3.Register_R0, uint16(want))
})
t.Run("test OUT trap", func(t *testing.T) {
program := strings.NewReader("\x30\x00\xf0\x21")
want := byte(0x41)
output := bytes.NewBuffer([]byte{})
vm, err := lc3.NewVM(program, nil, output)
assertError(t, err, nil)
vm.SetRegister(lc3.Register_R0, uint16(want))
err = vm.Step()
assertError(t, err, nil)
assertString(t, output.String(), string([]byte{0x41}))
})
t.Run("execute hello-world.obj program", func(t *testing.T) {
f, closer := openTestfile(t, "testdata/hello-world.obj")
defer closer()
output := bytes.NewBuffer([]byte{})
vm, err := lc3.NewVM(f, nil, output)
assertError(t, err, nil)
var start uint16 = 0x3000
values := []uint16{0xe002, 0xf022, 0xf025, 0x0048,
0x0065, 0x006c, 0x006c, 0x006f,
0x0020, 0x0057, 0x006f, 0x0072,
0x006c, 0x0064, 0x0021, 0x0000}
assertRegister(t, vm, lc3.Register_PC, start)
for i, value := range values {
assertMemory(t, vm, start+uint16(i), value)
}
// LEA R0, HELLO_STR
// PUTS
// HALT
err = vm.Run()
assertError(t, err, nil)
assertRegister(t, vm, lc3.Register_PC, start+3)
assertRegister(t, vm, lc3.Register_R0, 0x3003)
assertRegister(t, vm, lc3.Register_COND, lc3.Flag_P)
assertString(t, "Hello World!", output.String())
assertState(t, vm.State(), lc3.StateHalted)
})
t.Run("execute loop.obj program", func(t *testing.T) {
f, closer := openTestfile(t, "testdata/loop.obj")
defer closer()
vm, err := lc3.NewVM(f, nil, nil)
assertError(t, err, nil)
err = vm.Run()
assertError(t, err, nil)
assertRegister(t, vm, lc3.Register_PC, 0x3005)
assertRegister(t, vm, lc3.Register_R0, 10)
assertRegister(t, vm, lc3.Register_R1, 0)
assertRegister(t, vm, lc3.Register_COND, lc3.Flag_Z)
assertState(t, vm.State(), lc3.StateHalted)
})
t.Run("execute reverse-string.obj program", func(t *testing.T) {
f, closer := openTestfile(t, "testdata/reverse-string.obj")
defer closer()
output := bytes.NewBuffer([]byte{})
vm, err := lc3.NewVM(f, nil, output)
assertError(t, err, nil)
err = vm.Run()
assertError(t, err, nil)
assertString(t, "4321DCBA", output.String())
assertState(t, vm.State(), lc3.StateHalted)
})
t.Run("test KBSR/KBDR memory registers", func(t *testing.T) {
program := strings.NewReader("\x30\x00")
want := 'A'
input := strings.NewReader(string(want))
vm, err := lc3.NewVM(program, input, nil)
assertError(t, err, nil)
// On memory read, KBSR highest-bit is set, KBDR contains wanted character.
assertMemory(t, vm, lc3.Memory_KBSR, 0x8000)
assertMemory(t, vm, lc3.Memory_KBDR, uint16(want))
// Nothing more to read.
assertMemory(t, vm, lc3.Memory_KBSR, 0x0000)
})
}
func assertInitVM(t *testing.T, vm *lc3.VM, pc uint16) {
t.Helper()
assertState(t, vm.State(), lc3.StateRunning)
for i := 0; i < lc3.MemorySize; i++ {
assertMemory(t, vm, uint16(i), 0)
}
for reg := lc3.Register_R0; reg < lc3.Register_COUNT; reg++ {
if reg == lc3.Register_PC {
assertRegister(t, vm, reg, pc)
} else if reg == lc3.Register_COND {
assertRegister(t, vm, reg, lc3.Flag_Z)
} else {
assertRegister(t, vm, reg, 0)
}
}
}
func assertError(t *testing.T, got, want error) {
t.Helper()
if got != want {
t.Fatalf("expected error %+v, got %+v", want, got)
}
}
func assertIsError(t *testing.T, err error) {
t.Helper()
if err == nil {
t.Fatalf("expected error, got none")
}
}
func assertRegister(t *testing.T, vm *lc3.VM, reg lc3.Register, want uint16) {
t.Helper()
got := vm.GetRegister(reg)
if got != want {
t.Fatalf("expected register %q value to be 0x%x, got 0x%x", reg, want, got)
}
}
func assertMemory(t *testing.T, vm *lc3.VM, address, want uint16) {
t.Helper()
got := vm.GetMemory(address)
if got != want {
t.Fatalf("expected memory at 0x%x to have 0x%x, got 0x%x", address, want, got)
}
}
func openTestfile(t *testing.T, name string) (*os.File, func() error) {
t.Helper()
f, err := os.Open(name)
assertError(t, err, nil)
return f, f.Close
}
func assertString(t *testing.T, want, got string) {
t.Helper()
if want != got {
t.Fatalf("expected output to be %q, got %q", want, got)
}
}
func assertState(t *testing.T, want uint8, got uint8) {
t.Helper()
if want != got {
t.Fatalf("expected vm state to be %d, got %d", want, got)
}
}