-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_05.py
executable file
·541 lines (407 loc) · 16.8 KB
/
test_05.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#! /usr/bin/env pytest
from nand import run
import nand.component
import project_05
def test_memory_system():
mem = run(project_05.MemorySystem)
# set RAM[0] = -1
mem.in_ = -1
mem.load = 1
mem.address = 0
mem.tick(); mem.tock()
# RAM[0] holds value
mem.in_ = 9999
mem.load = 0
mem.tick(); mem.tock()
assert mem.out == -1
# Did not also write to upper RAM or Screen
mem.address = 0x2000
assert mem.out == 0
mem.address = 0x4000
assert mem.out == 0
# Set RAM[2000] = 2222
mem.in_ = 2222
mem.load = 1
mem.address = 0x2000
mem.tick(); mem.tock()
assert mem.out == 2222
# RAM[2000] holds value
mem.in_ = 9999
mem.load = 0
mem.tick(); mem.tock()
assert mem.out == 2222
# Did not also write to lower RAM or Screen
mem.address = 0
assert mem.out == -1
mem.address = 0x4000
assert mem.out == 0
# Low order address bits connected
# (note: not actually testing anything in this system?)
mem.address = 0x0001; assert mem.out == 0
mem.address = 0x0002; assert mem.out == 0
mem.address = 0x0004; assert mem.out == 0
mem.address = 0x0008; assert mem.out == 0
mem.address = 0x0010; assert mem.out == 0
mem.address = 0x0020; assert mem.out == 0
mem.address = 0x0040; assert mem.out == 0
mem.address = 0x0080; assert mem.out == 0
mem.address = 0x0100; assert mem.out == 0
mem.address = 0x0200; assert mem.out == 0
mem.address = 0x0400; assert mem.out == 0
mem.address = 0x0800; assert mem.out == 0
mem.address = 0x1000; assert mem.out == 0
mem.address = 0x2000; assert mem.out == 2222
# RAM[0x1234] = 1234
mem.address = 0x1234
mem.in_ = 1234
mem.load = 1
mem.tick(); mem.tock()
assert mem.out == 1234
# Did not also write to upper RAM or Screen
mem.address = 0x2234
assert mem.out == 0
mem.address = 0x6234
assert mem.out == 0
# RAM[0x2345] = 2345
mem.address = 0x2345
mem.in_ = 2345
mem.load = 1
mem.tick(); mem.tock()
assert mem.out == 2345
# Did not also write to lower RAM or Screen
mem.address = 0x0345
assert mem.out == 0
mem.address = 0x4345
assert mem.out == 0
### Keyboard test
## Note: this test can't be done on the isolated MemorySystem, because the necessary
## connections are only provided when the simulator detects the full Computer is being
## simulated. Instead, we test it below.
# mem.address = 0x6000
# assert mem.out == 75
### Screen test
mem.load = 1
mem.in_ = -1
mem.address = 0x4fcf
mem.tick(); mem.tock()
assert mem.out == -1
# ...load still set
mem.address = 0x504f
mem.tick(); mem.tock()
assert mem.out == -1
def test_cpu(chip=project_05.CPU):
cpu = run(chip)
cpu.instruction = 0b0011000000111001 # @12345
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 12345 and cpu.pc == 1 # and DRegister == 0
cpu.instruction = 0b1110110000010000 # D=A
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 12345 and cpu.pc == 2 # and DRegister == 12345
cpu.instruction = 0b0101101110100000 # @23456
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 23456 and cpu.pc == 3 # and DRegister == 12345
cpu.instruction = 0b1110000111010000 # D=A-D
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 23456 and cpu.pc == 4 # and DRegister == 11111
cpu.instruction = 0b0000001111101000 # @1000
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 5 # and DRegister == 11111
cpu.instruction = 0b1110001100001000 # M=D
cpu.ticktock()
assert cpu.outM == 11111 and cpu.writeM == 1 and cpu.addressM == 1000 and cpu.pc == 6 # and DRegister == 11111
cpu.instruction = 0b0000001111101001 # @1001
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1001 and cpu.pc == 7 # and DRegister == 11111
# Note confusing timing here: outM has the value to be written to memory when the clock falls. Afterward,
# outM has a nonsense value.
# TODO: always assert outM and writeM before tick/tock?
cpu.instruction = 0b1110001110011000 # MD=D-1
assert cpu.outM == 11110 and cpu.writeM == 1 and cpu.addressM == 1001 and cpu.pc == 7 # and DRegister == 11111
cpu.ticktock()
assert cpu.outM == 11109 and cpu.writeM == 1 and cpu.addressM == 1001 and cpu.pc == 8 # and DRegister == 11110
cpu.instruction = 0b0000001111101000 # @1000
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 9 # and DRegister == 11110
cpu.instruction = 0b1111010011010000 # D=D-M
cpu.inM = 11111
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 10 # and DRegister == -1
cpu.instruction = 0b0000000000001110 # @14
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 14 and cpu.pc == 11 # and DRegister == -1
cpu.instruction = 0b1110001100000100 # D;jlt
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 14 and cpu.pc == 14 # and DRegister == -1
cpu.instruction = 0b0000001111100111 # @999
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 999 and cpu.pc == 15 # and DRegister == -1
cpu.instruction = 0b1110110111100000 # A=A+1
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 16 # and DRegister == -1
cpu.instruction = 0b1110001100001000 # M=D
cpu.ticktock()
assert cpu.outM == -1 and cpu.writeM == 1 and cpu.addressM == 1000 and cpu.pc == 17 # and DRegister == -1
cpu.instruction = 0b0000000000010101 # @21
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 21 and cpu.pc == 18 # and DRegister == -1
cpu.instruction = 0b1110011111000010 # D+1;jeq
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 21 and cpu.pc == 21 # and DRegister == -1
cpu.instruction = 0b0000000000000010 # @2
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 2 and cpu.pc == 22 # and DRegister == -1
cpu.instruction = 0b1110000010010000 # D=D+A
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 2 and cpu.pc == 23 # and DRegister == 1
cpu.instruction = 0b0000001111101000 # @1000
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 24 # and DRegister == -1
cpu.instruction = 0b1110111010010000 # D=-1
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 25 # and DRegister == -1
cpu.instruction = 0b1110001100000001 # D;JGT
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 26 # and DRegister == -1
cpu.instruction = 0b1110001100000010 # D;JEQ
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 27 # and DRegister == -1
cpu.instruction = 0b1110001100000011 # D;JGE
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 28 # and DRegister == -1
cpu.instruction = 0b1110001100000100 # D;JLT
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1000 # and DRegister == -1
cpu.instruction = 0b1110001100000101 # D;JNE
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1000 # and DRegister == -1
cpu.instruction = 0b1110001100000110 # D;JLE
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1000 # and DRegister == -1
cpu.instruction = 0b1110001100000111 # D;JMP
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1000 # and DRegister == -1
cpu.instruction = 0b1110101010010000 # D=0
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1001 # and DRegister == 0
cpu.instruction = 0b1110001100000001 # D;JGT
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1002 # and DRegister == 0
cpu.instruction = 0b1110001100000010 # D;JEQ
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1000 # and DRegister == 0
cpu.instruction = 0b1110001100000011 # D;JGE
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1000 # and DRegister == 0
cpu.instruction = 0b1110001100000100 # D;JLT
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1001 # and DRegister == 0
cpu.instruction = 0b1110001100000101 # D;JNE
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1002 # and DRegister == 0
cpu.instruction = 0b1110001100000110 # D;JLE
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1000 # and DRegister == 0
cpu.instruction = 0b1110001100000111 # D;JMP
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1000 # and DRegister == 0
cpu.instruction = 0b1110111111010000 # D=1
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1001 # and DRegister == 1
cpu.instruction = 0b1110001100000001 # D;JGT
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1000 # and DRegister == 1
cpu.instruction = 0b1110001100000010 # D;JEQ
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1001 # and DRegister == 1
cpu.instruction = 0b1110001100000011 # D;JGE
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1000 # and DRegister == 1
cpu.instruction = 0b1110001100000100 # D;JLT
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1001 # and DRegister == 1
cpu.instruction = 0b1110001100000101 # D;JNE
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1000 # and DRegister == 1
cpu.instruction = 0b1110001100000110 # D;JLE
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1001 # and DRegister == 1
cpu.instruction = 0b1110001100000111 # D;JMP
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 1000 # and DRegister == 1
cpu.reset = 1
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 1000 and cpu.pc == 0 # and DRegister == 1
cpu.instruction = 0b0111111111111111 # @32767
cpu.reset = 0
cpu.ticktock()
assert cpu.writeM == 0 and cpu.addressM == 32767 and cpu.pc == 1 # and DRegister == 1
def test_computer_no_program(chip=project_05.Computer):
computer = run(chip)
for _ in range(100):
computer.ticktock()
assert computer.pc == 100
# Add.hack:
ADD_PROGRAM = [
0b0000000000000010, # @2
0b1110110000010000, # D=A
0b0000000000000011, # @3
0b1110000010010000, # D=D+A
0b0000000000000001, # @1 Note: modified to avoid address 0 (SP), which may get special treatment
0b1110001100001000, # M=D
]
def test_computer_add(chip=project_05.Computer, simulator="vector"):
computer = run(chip, simulator=simulator)
# First run (at the beginning PC=0)
computer.run_program(ADD_PROGRAM)
assert computer.peek(1) == 5
# Reset the PC
computer.reset = 1
computer.ticktock()
assert computer.pc == 0
# Second run, to check that the PC was reset correctly.
computer.poke(1, 12345)
computer.reset = 0
while computer.pc < len(ADD_PROGRAM):
computer.ticktock()
assert computer.peek(1) == 5
MAX_PROGRAM = [
# Note: modified to avoid address 0 (SP), which may get special treatment
0b0000000000000001, # 0: @1
0b1111110000010000, # 1: D=M
0b0000000000000010, # 2: @2
0b1111010011010000, # 3: D=D-M ; D = mem[1] - mem[2]
0b0000000000001010, # 4: @10
0b1110001100000001, # 5: D; JGT
0b0000000000000010, # 6: @2
0b1111110000010000, # 7: D=M ; D = mem[2]
0b0000000000001100, # 8: @12
0b1110101010000111, # 9: JMP
0b0000000000000001, # 10: @1
0b1111110000010000, # 11: D=M ; D = mem[1]
0b0000000000000011, # 12: @3
0b1110001100001000, # 13: M=D ; mem[3] = max
0b0000000000001110, # 14: @14
0b1110101010000111, # 15: JMP ; infinite loop
]
def test_computer_max(chip=project_05.Computer, simulator="vector", cycles_per_instr=1):
computer = run(chip, simulator=simulator)
computer.init_rom(MAX_PROGRAM)
# first run: compute max(3,5)
computer.poke(1, 3)
computer.poke(2, 5)
for _ in range(14*cycles_per_instr):
computer.ticktock()
assert computer.peek(3) == 5
# second run: compute max(23456,12345)
computer.reset_program()
computer.poke(1, 23456)
computer.poke(2, 12345)
# The run on these inputs needs less cycles (different branching)
for _ in range(10*cycles_per_instr):
computer.ticktock()
assert computer.peek(3) == 23456
# Copy one keycode value from the address where the keyboard is mapped to the RAM.
COPY_INPUT_PROGRAM = [
24576, # @(0x6000)
64528, # D=M (D = keycode)
1, # @1
58120, # M=D (mem[1] = D)
4, # @4
60039, # 0;JMP (infinite loop)
]
def test_computer_keyboard(chip=project_05.Computer, simulator="vector", cycles_per_instr=1):
"""A value which is presented via a special `Input` component can be read from the
address 0x6000, where the "keyboard" is mapped.
Note: can't test this at the level of MemorySystem, because the wrapper for the full
computer provides some of the necessary plumbing.
"""
computer = run(chip, simulator=simulator)
computer.init_rom(COPY_INPUT_PROGRAM)
KEY_A = ord("a")
computer.set_keydown(KEY_A)
for _ in range(4*cycles_per_instr):
computer.ticktock()
assert computer.peek(1) == KEY_A
def test_computer_tty_no_program(chip=project_05.Computer, simulator="vector"):
"""When nothing has been written address 0x6000, no value is available on the TTY "port".
"""
computer = run(chip, simulator=simulator)
for _ in range(100):
computer.ticktock()
assert computer.pc == 100
assert computer.tty_ready == True
assert computer.get_tty() == 0
# Write a few constant values to the external "tty" interface:
WRITE_TTY_PROGRAM = [
1, # @1
60432, # D=A
24576, # @(0x6000)
58120, # M=D (write 1)
0, # @0
60432, # D=A
24576, # @(0x6000)
58120, # M=D ("write" 0; no effect)
12345, # @12345
60432, # D=A
24576, # @(0x6000)
58120, # M=D (write 12345)
12, # @12
60039, # 0; JMP (infinite loop)
]
def test_computer_tty(chip=project_05.Computer, simulator="vector", cycles_per_instr=1):
"""A value which is written to the address 0x6000 can be read from outside via
a special `Output` component.
Also, the presence of a value in that component is signalled by the `tty_ready`
Note: can't test this at the level of MemorySystem, because the wrapper for the full
computer provides some of the necessary plumbing.
"""
computer = run(chip, simulator=simulator)
computer.init_rom(WRITE_TTY_PROGRAM)
# Run until a value appears (after 4 instructions):
cycles = 0
while computer.tty_ready and cycles < 1000:
computer.ticktock()
cycles += 1
print(f"cycles: {cycles}")
assert computer.tty_ready == False
assert computer.get_tty() == 1
assert computer.tty_ready == True
assert cycles == 4*cycles_per_instr # Bogus?
# Now run four more instructions; nothing written this time:
for _ in range(4*cycles_per_instr):
computer.ticktock()
assert computer.tty_ready == True
assert computer.get_tty() == 0
assert computer.tty_ready == True
# One more time, with a different value:
cycles = 0
while computer.tty_ready and cycles < 1000:
computer.ticktock()
cycles += 1
assert computer.tty_ready == False
assert computer.get_tty() == 12345
assert computer.tty_ready == True
assert cycles == 4*cycles_per_instr # Bogus?
def cycles_per_second(chip, cycles_per_instr=1):
"""Estimate the speed of CPU simulation by running Max repeatedly with random input.
"""
import random
import timeit
computer = run(chip)
computer.init_rom(MAX_PROGRAM)
CYCLES = 14*cycles_per_instr
def once():
x = random.randint(0, 0x7FFF)
y = random.randint(0, 0x7FFF)
computer.reset_program()
computer.poke(1, x)
computer.poke(2, y)
for _ in range(CYCLES):
computer.ticktock()
assert computer.peek(3) == max(x, y)
count, time = timeit.Timer(once).autorange()
return count*CYCLES/time
def test_speed(chip=project_05.Computer, cycles_per_instr=1):
cps = cycles_per_second(chip, cycles_per_instr)
print(f"Measured speed: {cps:0,.1f} cycles/s")
assert cps > 500 # Note: about 1k/s is expected, but include a wide margin for random slowness