-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_c64.py
463 lines (421 loc) · 13.8 KB
/
test_c64.py
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
463
import c64
import pytest
@pytest.fixture
def cpu():
return c64.CPU()
def test_register():
reg = c64.Register(word_length=8, value=0xf0)
assert reg.get() == 0xf0
reg.set(0x0f)
assert reg.get() == 0x0f
reg.set(0x4040)
assert reg.get() == 0x40
reg.set(-1)
assert reg.get() == 0xff
def test_readonly_register():
ro_reg = c64.ReadOnlyRegister(word_length=1, value=1)
assert ro_reg.get() == 1
ro_reg.set(1) # this shouldn't fail
assert ro_reg.get() == 1
with pytest.raises(NotImplementedError):
ro_reg.set(0)
assert ro_reg.get() == 1
def test_composite_register():
ro_reg = c64.ReadOnlyRegister(word_length=1, value=1)
reg_a = c64.Register(word_length=8, value=0x0f)
reg_b = c64.Register(word_length=8, value=0xf0)
creg = c64.CompositeRegister([ro_reg, reg_a, reg_b])
assert creg.get() == (ro_reg.get() << 16) + (reg_a.get() << 8) + reg_b.get()
creg.set(0x10000)
assert creg.get() == 0x10000
with pytest.raises(NotImplementedError):
creg.set(0x00000)
creg.set(0x1abcd)
assert creg.get() == 0x1abcd
assert ro_reg.get() == 1
assert reg_a.get() == 0xab
assert reg_b.get() == 0xcd
with pytest.raises(NotImplementedError):
creg.set(0xfff0ffff)
creg.set(0xffffffff)
assert creg.get() == 0x1ffff
assert ro_reg.get() == 1
assert reg_a.get() == 0xff
assert reg_b.get() == 0xff
def test_blank_register_bank():
regbank = c64.RegisterBank()
for name, _ in regbank._regs:
assert getattr(regbank, name) == 0
assert regbank.SP == 0x100
assert regbank.P == 0
assert regbank.PC == 0
regbank.PC = 0x1234
assert regbank.PC == 0x1234
assert regbank.PCH == 0x12
assert regbank.PCL == 0x34
with pytest.raises(NotImplementedError):
regbank.SP = 0
assert regbank.SP == 0x100
regbank.SP = 0x1ab
assert regbank.SP == 0x1ab
assert regbank.S == 0xab
for name, word_length in regbank._regs:
assert getattr(regbank, '_' + name).word_length == word_length
setattr(regbank, name, int('1'*(word_length+1), base=2))
assert getattr(regbank, name) == int('1'*(word_length), base=2)
with pytest.raises(AttributeError):
regbank._NOT_A_REAL_REGISTER
with pytest.raises(AttributeError):
regbank.NOT_A_REAL_REGISTER
def test_default_value_register_bank():
with pytest.raises(AttributeError):
regbank = c64.RegisterBank({'NOT_A_REAL_REGISTER': 0xff})
with pytest.raises(NotImplementedError):
regbank = c64.RegisterBank({'SP': 0})
regbank = c64.RegisterBank({'A': 0xff, 'Y': 0xffff, 'X': 0x12,
'N': 1, 'V': 0,
'PCH': 0xff, 'SP': 0x123})
assert regbank.A == 0xff
assert regbank.Y == 0xff
assert regbank.X == 0x12
assert regbank.N == 1
assert regbank.P == 0b10000000
assert regbank.PCH == 0xff
assert regbank.PC == 0xff00
assert regbank.S == 0x23
assert regbank.SP == 0x123
def test_blank_ram():
ram = c64.RAM(8, 0xffff)
assert ram.word_length == 8
assert ram.size == 0xffff
ram.set(0x1000, 0xfa)
assert ram.get(0x1000) == 0xfa
ram.set(0x2000, 0xfffb)
assert ram.get(0x2000) == 0xfb
with pytest.raises(ValueError):
ram.set(0x1ffff, 1)
def test_initial_contents_ram():
ram = c64.RAM(8, 0xffff, initial_contents=[0xff, 0x01, 0x02, 0x03])
assert ram.word_length == 8
assert ram.size == 0xffff
assert ram.get(0) == 0xff
assert ram.get(1) == 0x01
assert ram.get(2) == 0x02
assert ram.get(3) == 0x03
ram.set(3, 0x06)
assert ram.get(3) == 0x06
def test_cpu_init():
cpu = c64.CPU()
assert cpu.reg.PC == 0
assert cpu.reg.SP == 0x100
assert cpu.ram.size == 0x10000
assert cpu.ram.word_length == 8
assert cpu.ram.get(0) == 0
def test_cpu_init_with_defaults():
cpu = c64.CPU(initial_registers={'PC': 0x1234}, initial_ram=[0xff, 0x02])
assert cpu.reg.PC == 0x1234
assert cpu.reg.SP == 0x100
assert cpu.ram.size == 0x10000
assert cpu.ram.word_length == 8
assert cpu.ram.get(0) == 0xff
assert cpu.ram.get(1) == 0x02
def test_cpu_init_with_initialized_defaults():
ram = c64.RAM(8, 0xffff, initial_contents=[0xff, 0x01, 0x02, 0x03])
regbank = c64.RegisterBank({'A': 0xff, 'Y': 0xffff, 'X': 0x12,
'N': 1, 'V': 0,
'PCH': 0xff, 'SP': 0x123})
cpu = c64.CPU(initial_registers=regbank, initial_ram=ram)
assert cpu.reg.PC == 0xff00
assert cpu.reg.SP == 0x123
assert cpu.ram.size == 0xffff
assert cpu.ram.word_length == 8
assert cpu.ram.get(0) == 0xff
assert cpu.ram.get(1) == 0x01
assert cpu.ram.get(2) == 0x02
assert cpu.ram.get(3) == 0x03
def test_next_word():
cpu = c64.CPU(initial_ram=[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0xff])
cpu.reg.PC = 0x0002
assert cpu.next_word() == 0x02
assert cpu.next_word() == 0x03
assert cpu.next_word() == 0x04
assert cpu.next_word() == 0x05
assert cpu.next_word() == 0xff
assert cpu.reg.PC == 0x0007
cpu.reg.PC = 0x0000
assert cpu.next_word() == 0x00
assert cpu.next_word() == 0x01
assert cpu.next_word() == 0x02
assert cpu.reg.PC == 0x0003
def test_bcd():
assert c64.bcd(0x43) == 43
assert c64.bcd(0x12) == 12
with pytest.raises(ValueError):
c64.bcd(0xfa)
# TODO: refactor ADC, AND, ASL to use parametrization if appropriate
def test_ADC(cpu):
cpu.reg.A = 0x02
cpu.ADC(0x04)
assert cpu.reg.A == 0x06
assert cpu.reg.P == 0b00000000
def test_ADC_with_carry_and_sign(cpu):
cpu.reg.C = 1
cpu.reg.A = 0xf2
cpu.ADC(0x04)
assert cpu.reg.A == 0xf7
assert cpu.reg.N == 0x01
assert cpu.reg.P == 0b10000000
def test_ADC_overflow(cpu):
cpu.reg.A = 0xf2
cpu.ADC(0x14)
assert cpu.reg.A == 0x06
assert cpu.reg.V == 0x01
assert cpu.reg.C == 0x01
assert cpu.reg.N == 0x01
assert cpu.reg.P == 0b11000001
def test_ADC_zero(cpu):
cpu.ADC(0x00)
assert cpu.reg.A == 0x00
assert cpu.reg.Z == 0x01
assert cpu.reg.P == 0b00000010
def test_AND_with_sign(cpu):
cpu.reg.A = 0b10111111
cpu.AND(0b11001111)
assert cpu.reg.A == 0b10001111
assert cpu.reg.N == 0x01
assert cpu.reg.P == 0b10000000
def test_AND_zero(cpu):
cpu.AND(0x12)
assert cpu.reg.A == 0x00
assert cpu.reg.Z == 0x01
assert cpu.reg.P == 0b00000010
def test_ASL(cpu):
assert cpu.ASL(0b11111110) == 0b11111100
assert cpu.reg.C == 1
assert cpu.reg.N == 1
assert cpu.reg.P == 0b10000001
def test_ASL_zero(cpu):
assert cpu.ASL(0b10000000) == 0b00000000
assert cpu.reg.Z == 1
assert cpu.reg.C == 1
assert cpu.reg.P == 0b00000011
@pytest.mark.parametrize("pc, c, value, exp_pc, exp_p", [
(0x0002, 1, 2, 0x0002, 0b00000001),
(0x0002, 0, 2, 0x0004, 0b00000000),
(0x0004, 0, -2, 0x0002, 0b00000000),
])
def test_BCC(cpu, pc, c, value, exp_pc, exp_p):
cpu.reg.PC = pc
cpu.reg.C = c
cpu.BCC(value)
assert cpu.reg.PC == exp_pc
assert cpu.reg.P == exp_p
@pytest.mark.parametrize("pc, c, value, exp_pc, exp_p", [
(0x0002, 0, 2, 0x0002, 0b00000000),
(0x0002, 1, 2, 0x0004, 0b00000001),
(0x0004, 1, -2, 0x0002, 0b00000001),
])
def test_BCS(cpu, pc, c, value, exp_pc, exp_p):
cpu.reg.PC = pc
cpu.reg.C = c
cpu.BCS(value)
assert cpu.reg.PC == exp_pc
assert cpu.reg.P == exp_p
@pytest.mark.parametrize("pc, z, value, exp_pc, exp_p", [
(0x0002, 0, 2, 0x0002, 0b00000000),
(0x0002, 1, 2, 0x0004, 0b00000010),
(0x0004, 1, -2, 0x0002, 0b00000010),
])
def test_BEQ(cpu, pc, z, value, exp_pc, exp_p):
cpu.reg.PC = pc
cpu.reg.Z = z
cpu.BEQ(value)
assert cpu.reg.PC == exp_pc
assert cpu.reg.P == exp_p
@pytest.mark.parametrize("a, value, exp_p", [
(0b00000000, 0b11111111, 0b00000010),
(0b11111111, 0b11111111, 0b11000000),
(0b10000000, 0b11111111, 0b10000000),
(0b01000000, 0b11111111, 0b01000000),
])
def test_BIT(cpu, a, value, exp_p):
cpu.reg.A = a
cpu.BIT(value)
assert cpu.reg.P == exp_p
assert cpu.reg.A == a # hasn't changed
@pytest.mark.parametrize("pc, n, value, exp_pc, exp_p", [
(0x0002, 0, 2, 0x0002, 0b00000000),
(0x0002, 1, 2, 0x0004, 0b10000000),
(0x0004, 1, -2, 0x0002, 0b10000000),
])
def test_BMI(cpu, pc, n, value, exp_pc, exp_p):
cpu.reg.PC = pc
cpu.reg.N = n
cpu.BMI(value)
assert cpu.reg.PC == exp_pc
assert cpu.reg.P == exp_p
@pytest.mark.parametrize("pc, z, value, exp_pc, exp_p", [
(0x0002, 1, 2, 0x0002, 0b00000010),
(0x0002, 0, 2, 0x0004, 0b00000000),
(0x0004, 0, -2, 0x0002, 0b00000000),
])
def test_BNE(cpu, pc, z, value, exp_pc, exp_p):
cpu.reg.PC = pc
cpu.reg.Z = z
cpu.BNE(value)
assert cpu.reg.PC == exp_pc
assert cpu.reg.P == exp_p
@pytest.mark.parametrize("pc, n, value, exp_pc, exp_p", [
(0x0002, 1, 2, 0x0002, 0b10000000),
(0x0002, 0, 2, 0x0004, 0b00000000),
(0x0004, 0, -2, 0x0002, 0b00000000),
])
def test_BPL(cpu, pc, n, value, exp_pc, exp_p):
cpu.reg.PC = pc
cpu.reg.N = n
cpu.BPL(value)
assert cpu.reg.PC == exp_pc
assert cpu.reg.P == exp_p
@pytest.mark.parametrize("pc, destination_pc, sp, p", [
(0x1234, 0x00ff, 0x1aa, 0b11000001),
(0x5234, 0x12dd, 0x1bb, 0b00001000),
(0x6234, 0x44cc, 0x1cc, 0b00000000),
(0x0000, 0x00aa, 0x1dd, 0b00010000), # This is only an assumption of what the c64 would do if B = 1
])
def test_BRK(cpu, pc, destination_pc, sp, p):
cpu.reg.PC = pc
cpu.reg.SP = sp
cpu.reg.P = p
cpu.ram.set(0xfffe, destination_pc >> 8)
cpu.ram.set(0xffff, destination_pc & 0xff)
cpu.BRK()
assert cpu.reg.PC == destination_pc
assert cpu.reg.SP == sp - 3
assert cpu.reg.P == p
assert cpu.ram.get(sp) == (pc + 1) >> 8
assert cpu.ram.get(sp-1) == (pc + 1) & 0xff
assert cpu.ram.get(sp-2) == p | 0b00010000 # is B flag set in the stack's copy of P?
@pytest.mark.parametrize("pc, v, value, exp_pc, exp_p", [
(0x0002, 1, 2, 0x0002, 0b01000000),
(0x0002, 0, 2, 0x0004, 0b00000000),
(0x0004, 0, -2, 0x0002, 0b00000000),
])
def test_BVC(cpu, pc, v, value, exp_pc, exp_p):
cpu.reg.PC = pc
cpu.reg.V = v
cpu.BVC(value)
assert cpu.reg.PC == exp_pc
assert cpu.reg.P == exp_p
@pytest.mark.parametrize("pc, v, value, exp_pc, exp_p", [
(0x0002, 0, 2, 0x0002, 0b00000000),
(0x0002, 1, 2, 0x0004, 0b01000000),
(0x0004, 1, -2, 0x0002, 0b01000000),
])
def test_BVS(cpu, pc, v, value, exp_pc, exp_p):
cpu.reg.PC = pc
cpu.reg.V = v
cpu.BVS(value)
assert cpu.reg.PC == exp_pc
assert cpu.reg.P == exp_p
@pytest.mark.parametrize("flag", ['C', 'D', 'I', 'V'])
def test_clear_commands(cpu, flag):
getattr(cpu, "CL" + flag)()
assert cpu.reg.P == 0b00000000
setattr(cpu.reg, flag, 1)
getattr(cpu, "CL" + flag)()
assert cpu.reg.P == 0b00000000
@pytest.mark.parametrize("command, target", [
('CMP', 'A'),
('CPY', 'Y'),
('CPX', 'X'),
])
@pytest.mark.parametrize("state, value, exp_p", [
(0x00, 0x00, 0b00000011),
(0x40, 0x20, 0b00000001),
(0x20, 0x40, 0b10000000),
(0xff, 0xff, 0b00000011),
])
def test_compare_commands(cpu, command, target, state, value, exp_p):
setattr(cpu.reg, target, state)
getattr(cpu, command)(value)
assert cpu.reg.P == exp_p
@pytest.mark.parametrize("command, value, exp_p", [
('DEC', 0x01, 0b00000010),
('DEC', 0xfb, 0b10000000),
('DEC', 0x02, 0b00000000),
('DEC', 0x00, 0b10000000),
('INC', 0xff, 0b00000010),
('INC', 0xfb, 0b10000000),
('INC', 0x02, 0b00000000),
])
def test_DEC_INC(cpu, command, value, exp_p):
expected_value = (value + (1 if command == "INC" else -1)) & 0xff
assert getattr(cpu, command)(value) == expected_value
assert cpu.reg.P == exp_p
@pytest.mark.parametrize("command, value, exp_p", [
('DEX', 0x02, 0b00000000),
('DEY', 0x02, 0b00000000),
('DEX', 0x01, 0b00000010),
('DEY', 0x01, 0b00000010),
('DEX', 0xfb, 0b10000000),
('DEY', 0xfb, 0b10000000),
('DEX', 0x00, 0b10000000),
('DEY', 0x00, 0b10000000),
('INX', 0x02, 0b00000000),
('INY', 0x02, 0b00000000),
('INX', 0xff, 0b00000010),
('INY', 0xff, 0b00000010),
('INX', 0xfb, 0b10000000),
('INY', 0xfb, 0b10000000),
])
def test_DEX_DEY_INX_INY(cpu, command, value, exp_p):
setattr(cpu.reg, command[2], value)
getattr(cpu, command)()
assert getattr(cpu.reg, command[2]) == (value + (1 if command.startswith("IN") else -1)) & 0xff
assert cpu.reg.P == exp_p
@pytest.mark.parametrize("a, value, exp_a, exp_p", [
(0b00000000, 0b00000000, 0b00000000, 0b00000010),
(0b00001000, 0b00000000, 0b00001000, 0b00000000),
(0b01000000, 0b11000000, 0b10000000, 0b10000000),
])
def test_EOR(cpu, a, value, exp_a, exp_p):
cpu.reg.A = a
cpu.EOR(value)
assert cpu.reg.A == exp_a
assert cpu.reg.P == exp_p
def test_JMP(cpu):
cpu.reg.PC = 0x00ff
cpu.JMP(0x00aa)
assert cpu.reg.PC == 0x00aa
def test_JSR(cpu):
cpu.reg.PC = 0x02ff
cpu.reg.SP = 0x1cc
cpu.JSR(0x00aa)
assert cpu.ram.get(0x1cc) == 0x02
assert cpu.ram.get(0x1cc-1) == 0xff-1
assert cpu.reg.SP == 0x1cc-2
assert cpu.reg.PC == 0x00aa
@pytest.mark.parametrize("target", ["A", "X", "Y"])
@pytest.mark.parametrize("value, exp_p", [
(0x14, 0b00000000),
(0xf1, 0b10000000),
(0x00, 0b00000010),
])
def test_LDA_LDX_LDY(cpu, target, value, exp_p):
setattr(cpu.reg, target, 0x87)
getattr(cpu, 'LD' + target)(value)
assert getattr(cpu.reg, target) == value
assert cpu.reg.P == exp_p
@pytest.mark.parametrize("value, exp_result, exp_p", [
(0b10101010, 0b01010101, 0b00000000),
(0b01010101, 0b00101010, 0b00000001),
(0b00000001, 0b00000000, 0b00000011),
(0b00000000, 0b00000000, 0b00000010),
])
def test_LSR(cpu, value, exp_result, exp_p):
assert cpu.LSR(value) == exp_result
assert cpu.reg.P == exp_p
def test_NOP(cpu):
exp_p = cpu.reg.P
cpu.NOP()
assert cpu.reg.P == exp_p