-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore_test.go
More file actions
462 lines (345 loc) · 11.5 KB
/
Copy pathcore_test.go
File metadata and controls
462 lines (345 loc) · 11.5 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package cpu
import (
"os"
"runtime"
"strings"
"testing"
"xubiod/6502-experiment/assembler"
)
var invalid_nmos = map[byte]uint8{
0x02: 0, 0x03: 0, 0x04: 0, 0x07: 0, 0x0B: 0, 0x0C: 0, 0x0F: 0,
0x12: 0, 0x13: 0, 0x14: 0, 0x17: 0, 0x1A: 0, 0x1B: 0, 0x1C: 0, 0x1F: 0,
0x22: 0, 0x23: 0, 0x27: 0, 0x2B: 0, 0x2F: 0,
0x32: 0, 0x33: 0, 0x34: 0, 0x37: 0, 0x3A: 0, 0x3B: 0, 0x3C: 0, 0x3F: 0,
0x42: 0, 0x43: 0, 0x44: 0, 0x47: 0, 0x4B: 0, 0x4F: 0,
0x52: 0, 0x53: 0, 0x54: 0, 0x57: 0, 0x5A: 0, 0x5B: 0, 0x5C: 0, 0x5F: 0,
0x62: 0, 0x63: 0, 0x64: 0, 0x67: 0, 0x6B: 0, 0x6F: 0,
0x72: 0, 0x73: 0, 0x74: 0, 0x77: 0, 0x7A: 0, 0x7B: 0, 0x7C: 0, 0x7F: 0,
0x80: 0, 0x82: 0, 0x83: 0, 0x87: 0, 0x89: 0, 0x8B: 0, 0x8F: 0,
0x92: 0, 0x93: 0, 0x97: 0, 0x9B: 0, 0x9C: 0, 0x9E: 0, 0x9F: 0,
0xA3: 0, 0xA7: 0, 0xAB: 0, 0xAF: 0,
0xB2: 0, 0xB3: 0, 0xB7: 0, 0xBB: 0, 0xBF: 0,
0xC2: 0, 0xC3: 0, 0xC7: 0, 0xCB: 0, 0xCF: 0,
0xD2: 0, 0xD3: 0, 0xD4: 0, 0xD7: 0, 0xDA: 0, 0xDB: 0, 0xDC: 0, 0xDF: 0,
0xE2: 0, 0xE3: 0, 0xE7: 0, 0xEB: 0, 0xEF: 0,
0xF2: 0, 0xF3: 0, 0xF4: 0, 0xF7: 0, 0xFA: 0, 0xFB: 0, 0xFC: 0, 0xFF: 0,
}
func TestExists(t *testing.T) {
c := NewCore()
var i uint16
var ok bool
var finalOk bool
failCount := 0
for i < 256 {
finalOk = false
_, ok = invalid_nmos[byte(i)]
finalOk = finalOk || ok
_, ok = c.execMapNil[byte(i)]
finalOk = finalOk || ok
_, ok = c.execMapByte[byte(i)]
finalOk = finalOk || ok
_, ok = c.execMapShort[byte(i)]
finalOk = finalOk || ok
if !finalOk {
t.Errorf("opcode %0X: should exist but doesn't!!", i)
failCount++
}
i++
}
t.Logf("\n%d/%d pass", 256-failCount, 256)
}
func TestValidity(t *testing.T) {
c := NewCore()
for i := range 256 {
c.prepare()
c.SetWriterPtr(0x0200)
c.Write([]byte{byte(i & 0xFF)})
c.PC = 0x0200
valid := c.StepOnce()
_, ok := invalid_nmos[byte(i&0xFF)]
if ok && valid {
t.Logf("core returns valid, instruction isn't valid -> %02x", i)
t.Fail()
} else if !ok && !valid {
t.Logf("core returns invalid, instruction is valid -> %02x", i)
t.Fail()
}
}
}
func TestResetRoutine(t *testing.T) {
c := NewCore()
resetRoutine := []byte{
0xa2, 0xff, // LDX __Imm(0xFF)
0x9a, // TXS ____i()
0x78, // SEI ____i()
0x18, // CLC ____i()
0xd8, // CLD ____i()
0x58, // CLI ____i()
0xb8, // CLV ____i()
0xa9, 0x00, // LDA __Imm(0x00)
0xa2, 0x00, // LDX __Imm(0x00)
0xa0, 0x00, // LDY __Imm(0x00)
}
c.Write(resetRoutine)
c.PC = 0x0000
// purposefully poison registers
c.A = 0xDE
c.X = 0xAD
c.Y = 0x24
executing := true
for c.Memory[c.PC] != 0x00 && executing {
executing = c.StepOnce()
}
if c.A != 0 {
t.Errorf("accumulator not 0, was \"%1X\"", c.A)
}
if c.X != 0 {
t.Errorf("X not 0, was \"%1X\"", c.X)
}
if c.Y != 0 {
t.Errorf("Y not 0, was \"%1X\"", c.Y)
}
if c.S != 0xFF {
t.Errorf("stack pointer not FF, was \"%1X\"", c.S)
}
if c.PC != 0x000E {
t.Errorf("program counter not 20E, was \"%1X\"", c.PC)
}
t.Log("\n" + c.CompleteDump(runtime.GOOS != "windows"))
}
func TestArithmeticADC(t *testing.T) {
c := NewCore()
// numbers to test adding
lefts_t := []byte{0x50, 0x50, 0x50, 0x50, 0xD0, 0xD0, 0xD0, 0xD0}
right_t := []byte{0x10, 0x50, 0x90, 0xD0, 0x10, 0x50, 0x90, 0xD0}
// should carry be set before adding?
carry_set_t := []byte{0xEA, 0x38, 0xEA, 0x38, 0xEA, 0x38, 0xEA, 0x38} // 0xEA is a NOP, 0x38 is a SEC
// expected carry and overflow flags
carry_out_t := []byte{0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01} // 0x01 means set
overflow__t := []byte{0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00} // 0x40 means set
for idx, left := range lefts_t {
right := right_t[idx]
prg := []byte{
carry_set_t[idx], // either a NOP or SEC
0xa9, left,
0x8d, 0x00, 0x00,
0xa9, right,
0x6d, 0x00, 0x00,
0x00,
}
stdProcedure(c, prg)
var carryOnAdd byte = 0
if carry_set_t[idx] == 0x38 {
carryOnAdd += 1
}
if c.A != left+right+carryOnAdd {
t.Errorf("adc fail - (%1x + %1x) - answer\texpected %1x\tgot %1x", left, right, left+right+carryOnAdd, c.A)
}
if c.Flags&FLAG_CARRY != carry_out_t[idx] {
t.Errorf("adc fail - (%1x + %1x) - carry\texpected %1x\tgot %1x", left, right, carry_out_t[idx], c.Flags&FLAG_CARRY)
}
if c.Flags&FLAG_OVERFLOW != overflow__t[idx] {
t.Errorf("adc fail - (%1x + %1x) - V flag\texpected %1x\tgot %1x", left, right, overflow__t[idx], c.Flags&FLAG_OVERFLOW)
}
t.Log("\n" + c.CompleteDump(runtime.GOOS != "windows"))
}
}
func TestDecimalADC(t *testing.T) {
c := NewCore()
// numbers to test adding
lefts_t := []byte{0x50, 0x50, 0x50, 0x50, 0xD0, 0xD0, 0xD0, 0xD0}
right_t := []byte{0x10, 0x50, 0x90, 0xD0, 0x10, 0x50, 0x90, 0xD0}
// should carry be set before adding?
carry_set_t := []byte{0xEA, 0xEA, 0xEA, 0xEA, 0xEA, 0xEA, 0xEA, 0xEA} // 0xEA is a NOP, 0x38 is a SEC
// results
results_t := []byte{0x60, 0x00, 0x40, 0x80, 0x40, 0x80, 0xC0, 0x00} // verified with visual6502.org
// expected flags
carry_out_t := []byte{0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01} // 0x01 means set
overflow__t := []byte{0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00} // 0x40 means set
negative__t := []byte{0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80} // 0x80 means set
for idx, left := range lefts_t {
right := right_t[idx]
prg := []byte{
0xf8,
carry_set_t[idx], // either a NOP or SEC
0xa9, left,
0x8d, 0x00, 0x00,
0xa9, right,
0x6d, 0x00, 0x00,
0x00,
}
stdProcedure(c, prg)
if c.A != results_t[idx] {
t.Errorf("adc decimal fail - (%1x + %1x) - answer\texpected %1x\tgot %1x", left, right, results_t[idx], c.A)
}
if c.Flags&FLAG_CARRY != carry_out_t[idx] {
t.Errorf("adc decimal fail - (%1x + %1x) - carry\texpected %1x\tgot %1x", left, right, carry_out_t[idx], c.Flags&FLAG_CARRY)
}
if c.Flags&FLAG_OVERFLOW != overflow__t[idx] {
t.Errorf("adc decimal fail - (%1x + %1x) - V flag\texpected %1x\tgot %1x", left, right, overflow__t[idx], c.Flags&FLAG_OVERFLOW)
}
if c.Flags&FLAG_NEGATIVE != negative__t[idx] {
t.Errorf("adc decimal fail - (%1x + %1x) - N flag\texpected %1x\tgot %1x", left, right, negative__t[idx], c.Flags&FLAG_NEGATIVE)
}
t.Log("\n" + c.CompleteDump(runtime.GOOS != "windows"))
}
}
func TestArithmeticSBC(t *testing.T) {
c := NewCore()
// numbers to test subtracting
right_t := []byte{0x50, 0x50, 0x50, 0x50, 0xD0, 0xD0, 0xD0, 0xD0}
lefts_t := []byte{0xF0, 0xB0, 0x70, 0x30, 0xF0, 0xB0, 0x70, 0x30}
// should carry be set before subtracting?
carry_set_t := []byte{0xEA, 0x38, 0xEA, 0x38, 0xEA, 0x38, 0xEA, 0x38} // 0xEA is a NOP, 0x38 is a SEC
// expected carry and overflow flags
carry_out_t := []byte{0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01} // 0x01 means set
overflow__t := []byte{0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00} // 0x40 means set
for idx, left := range lefts_t {
right := right_t[idx]
prg := []byte{
carry_set_t[idx], // either a NOP or SEC
0xa9, left,
0x8d, 0x00, 0x00,
0xa9, right,
0xed, 0x00, 0x00,
0x00,
}
stdProcedure(c, prg)
var carryOnSub byte = 0
if carry_set_t[idx] == 0x38 {
carryOnSub += 1
}
var pre = (uint16(right) - uint16(left)) + uint16(carryOnSub)
var r = byte(pre & 0xFF)
if c.A != r {
t.Errorf("sbc fail - (%1x - %1x) - answer\texpected %1x\tgot %1x", right, left, r, c.A)
}
if c.Flags&FLAG_CARRY != carry_out_t[idx] {
t.Errorf("sbc fail - (%1x - %1x) - carry\texpected %1x\tgot %1x", right, left, carry_out_t[idx], c.Flags&FLAG_CARRY)
}
if c.Flags&FLAG_OVERFLOW != overflow__t[idx] {
t.Errorf("sbc fail - (%1x - %1x) - V flag\texpected %1x\tgot %1x", right, left, overflow__t[idx], c.Flags&FLAG_OVERFLOW)
}
t.Log("\n" + c.CompleteDump(runtime.GOOS != "windows"))
}
}
func TestDecimalSBC(t *testing.T) {
c := NewCore()
// numbers to test subtracting
lefts_t := []byte{0xF0, 0xB0, 0x70, 0x30, 0xF0, 0xB0, 0x70, 0x30}
right_t := []byte{0x50, 0x50, 0x50, 0x50, 0xD0, 0xD0, 0xD0, 0xD0}
// should carry be set before adding?
carry_set_t := []byte{0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38} // 0xEA is a NOP, 0x38 is a SEC
// results
results_t := []byte{0x00, 0x40, 0x80, 0x20, 0x80, 0x20, 0x60, 0xa0} // verified with visual6502.org
// expected flags
carry_out_t := []byte{0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01} // 0x01 means set
overflow__t := []byte{0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00} // 0x40 means set
negative__t := []byte{0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80} // 0x80 means set
for idx, left := range lefts_t {
right := right_t[idx]
prg := []byte{
0xf8,
carry_set_t[idx], // either a NOP or SEC
0xa9, left,
0x8d, 0x00, 0x00,
0xa9, right,
0xed, 0x00, 0x00,
0x00,
}
stdProcedure(c, prg)
if c.A != results_t[idx] {
t.Errorf("sbc decimal fail - (M: %1x; A: %1x) - answer\texpected %1x\tgot %1x", left, right, results_t[idx], c.A)
}
if c.Flags&FLAG_CARRY != carry_out_t[idx] {
t.Errorf("sbc decimal fail - (M: %1x; A: %1x) - carry\texpected %1x\tgot %1x", left, right, carry_out_t[idx], c.Flags&FLAG_CARRY)
}
if c.Flags&FLAG_OVERFLOW != overflow__t[idx] {
t.Errorf("sbc decimal fail - (M: %1x; A: %1x) - V flag\texpected %1x\tgot %1x", left, right, overflow__t[idx], c.Flags&FLAG_OVERFLOW)
}
if c.Flags&FLAG_NEGATIVE != negative__t[idx] {
t.Errorf("sbc decimal fail - (M: %1x; A: %1x) - N flag\texpected %1x\tgot %1x", left, right, negative__t[idx], c.Flags&FLAG_NEGATIVE)
}
t.Log("\n" + c.CompleteDump(runtime.GOOS != "windows"))
}
}
func TestGeneralStackOps(t *testing.T) {
var prg []byte
loopCount := 32
c := NewCore()
asm := assembler.New()
prg, _ = asm.Parse("LDX #$00\n" + strings.Repeat("TXA\nPHA\nINX\n", loopCount))
stdProcedure(c, prg)
var atStack byte
for number := range loopCount {
atStack = c.Memory[0x01FF-uint16(number)]
if atStack != byte(number) {
t.Errorf("general stack fail - expected %02x\tgot %02x", number, atStack)
}
}
t.Log("\n" + c.CompleteDump(runtime.GOOS != "windows"))
}
func testFromSuite(t *testing.T) {
c := NewCore()
c.Features.ConsoleOutOnBreak = true
c.Features.Traceback = 16
testsuite, err := os.ReadFile("testsuite/6502_functional_test.bin")
if err != nil {
t.Fatalf("testsuite could not load\n%s", err)
}
writeCount := c.Write(testsuite)
if writeCount != len(testsuite) {
t.Logf("testsuite couldn't fit entirely in core\nwrote %d bytes instead of %d bytes, continuing anyway..", writeCount, len(testsuite))
}
c.PC = 0x0400
var exe bool = true
var lastpc uint16 = 0x0000
var pcmoved bool = true
var pcmovedcounter uint8 = 16
for exe {
exe = c.StepOnce()
if !exe {
t.Log("invalid instruction encountered!!!")
t.Fail()
}
pcmoved = lastpc != c.PC
if !pcmoved {
// t.Log("program counter hasn't moved")
pcmovedcounter--
if pcmovedcounter == 0 {
t.Log("pc hasn't moved in a while, calling off execution")
t.Fail()
exe = false
}
} else {
pcmovedcounter = 16
}
lastpc = c.PC
}
t.Log(c.CompleteDump(runtime.GOOS != "windows"))
}
// Writes reset procedure followed by the given program. Goes into a standard execution
// loop afterwards; breaking on `BRK` (`0x00`) or an invalid instruction. Does no checks
// itself, do that after calling this.
func stdProcedure(c *Core, program []byte) {
c.prepare()
resetRoutine := []byte{
0xa2, 0xff, // LDX __Imm(0xFF)
0x9a, // TXS ____i()
0x78, // SEI ____i()
0x18, // CLC ____i()
0xd8, // CLD ____i()
0x58, // CLI ____i()
0xb8, // CLV ____i()
0xa9, 0x00, // LDA __Imm(0x00)
0xa2, 0x00, // LDX __Imm(0x00)
0xa0, 0x00, // LDY __Imm(0x00)
}
c.Write(resetRoutine)
c.Write(program)
c.PC = 0x0000
var exe bool = true
for c.Memory[c.PC] != 0x00 && exe {
exe = c.StepOnce()
}
}