-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_computer.py
939 lines (745 loc) · 28.4 KB
/
test_computer.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
import pytest
import random
from typing import Generator
from gates import NOT16, AND16, OR16
from utils import (
ZERO16,
int_to_bit_vector,
to_int,
sample_bits,
is_n_bit_vector,
is_non_negative,
is_positive,
make_one_hot,
SymbolicInstruction,
)
from arithmetic import INC16
from memory import (
DFF,
BIT,
REGISTER16,
RAM8,
RAM64,
RAM512,
RAM4K,
RAM8K,
RAM16K,
PC,
)
from computer import (
DEST_SYMBOL_TO_INSTRUCTION,
COMP_SYMBOL_TO_INSTRUCTION,
JUMP_SYMBOL_TO_INSTRUCTION,
CPU,
Memory,
Computer,
is_valid_instruction,
)
NUMBER_OF_SAMPLES_TO_DRAW_PER_TEST = 8
def _build_a_instruction(value: int) -> int:
"""Helper function to build an A-instruction."""
# pre-conditions
assert 0 <= value < 2**15, "value must be an integer in [0, 2**15)"
# body
return value
def _build_c_instruction(dest: int, comp: int, jump: int) -> int:
"""Helper function to build a C-instruction."""
# pre-conditions
assert 0 <= dest < 2**3, "dest must be an integer in [0, 8)"
assert 0 <= comp < 2**7, "comp must be an integer in [0, 128)"
assert 0 <= jump < 2**3, "jump must be an integer in [0, 8)"
# body
out = 0b111 << 13 | comp << 6 | dest << 3 | jump
# post-conditions
assert isinstance(out, int), "output must be an integer"
assert 0 <= out < 2**16, "output must be an integer in [0, 2**16)"
return out
def _create_random_a_instruction() -> tuple[bool, ...]:
value = random.randint(0, 2**15 - 1)
int_instruction = _build_a_instruction(value)
out = int_to_bit_vector(int_instruction, n=16)
# post-conditions
assert out[0] == False, "A-instruction must start with a `0`"
assert to_int(out) == value, "A-instruction must be the value in the instruction"
return out
def _get_next_c_instruction() -> Generator[tuple[bool, ...], None, None]:
"""A generator of all possible CPU C-instructions."""
for dest in DEST_SYMBOL_TO_INSTRUCTION.values():
for comp in COMP_SYMBOL_TO_INSTRUCTION.values():
for jump in JUMP_SYMBOL_TO_INSTRUCTION.values():
bin_instuction = _build_c_instruction(dest, comp, jump)
yield int_to_bit_vector(bin_instuction, n=16)
def _create_random_dff() -> DFF:
value = random.choice([True, False])
return DFF(value)
def _create_random_bit() -> BIT:
dff = _create_random_dff()
return BIT(dff)
def _create_random_register() -> REGISTER16:
bits = tuple(_create_random_bit() for _ in range(16))
return REGISTER16(bits)
def _create_random_ram8() -> RAM8:
registers = tuple(_create_random_register() for _ in range(8))
out = registers[0].out
return RAM8(registers, out)
def _create_random_ram64() -> RAM64:
ram8s = tuple(_create_random_ram8() for _ in range(8))
out = ram8s[0].out
return RAM64(ram8s, out)
def _create_random_ram512() -> RAM512:
ram64s = tuple(_create_random_ram64() for _ in range(8))
out = ram64s[0].out
return RAM512(ram64s, out)
def _create_random_ram4k() -> RAM4K:
ram512s = tuple(_create_random_ram512() for _ in range(8))
out = ram512s[0].out
return RAM4K(ram512s, out)
def _create_random_ram8k() -> RAM8K:
ram4ks = tuple(_create_random_ram4k() for _ in range(2))
out = ram4ks[0].out
return RAM8K(ram4ks, out)
def _create_random_ram16k() -> RAM16K:
ram4ks = tuple(_create_random_ram4k() for _ in range(4))
out = ram4ks[0].out
return RAM16K(ram4ks, out)
def _create_random_pc() -> PC:
register = _create_random_register()
return PC(register)
def _create_random_memory() -> Memory:
"""Returns a random memory."""
ram16k = _create_random_ram16k()
screen = _create_random_ram8k()
keyboard = _create_random_register()
out = sample_bits(16)
return Memory(ram16k, screen, keyboard, out)
def _create_random_valid_memory_address() -> tuple[bool, ...]:
"""Returns a random valid address for `Memory`."""
idx = random.randint(0, 2**14 + 2**13 - 1)
return int_to_bit_vector(idx, n=15)
def _create_random_invalid_memory_address() -> tuple[bool, ...]:
"""Returns a random invalid address for `Memory`."""
idx = random.randint(2**14 + 2**13, 2**15 - 1)
return int_to_bit_vector(idx, n=15)
def _create_random_cpu() -> CPU:
a_register = _create_random_register()
d_register = _create_random_register()
pc = _create_random_pc()
out_m = ZERO16
write_m = False
_zr = True
_ng = False
return CPU(
a_register=a_register,
d_register=d_register,
pc=pc,
_zr=_zr,
_ng=_ng,
out_m=out_m,
write_m=write_m,
)
def _to_symbol(c_instruction: tuple[bool, ...]) -> SymbolicInstruction:
"""Converts a C-instruction into a symbolic instruction."""
# pre-conditions
assert is_n_bit_vector(
c_instruction, n=16
), f"instruction must be a 16-bit tuple, got: {c_instruction}"
# body
comp = c_instruction[3:10]
dest = c_instruction[10:13]
jump = c_instruction[13:16]
dest_to_symbol = {v: k for k, v in DEST_SYMBOL_TO_INSTRUCTION.items()}
comp_to_symbol = {v: k for k, v in COMP_SYMBOL_TO_INSTRUCTION.items()}
jump_to_symbol = {v: k for k, v in JUMP_SYMBOL_TO_INSTRUCTION.items()}
dest_symbol = dest_to_symbol[to_int(dest)]
comp_symbol = comp_to_symbol[to_int(comp)]
jump_symbol = jump_to_symbol[to_int(jump)]
out = SymbolicInstruction(
dest=dest_symbol,
comp=comp_symbol,
jump=jump_symbol,
)
# post-conditions
assert isinstance(out, SymbolicInstruction), "output must be a SymbolicInstruction"
return out
def _create_valid_instruction() -> tuple[bool, ...]:
"""Returns a random 16-bit instruction."""
return random.choice(
[
_create_random_a_instruction(),
random.choice(list(_get_next_c_instruction())),
]
)
def _create_invalid_instruction() -> tuple[bool, ...]:
"""Returns a random 16-bit instruction."""
instruction = (True,) + sample_bits(15)
while is_valid_instruction(instruction):
instruction = (True,) + sample_bits(15)
# post-conditions
assert is_n_bit_vector(instruction, n=16), "instruction must be 16-bit"
assert not is_valid_instruction(instruction), "instruction must be invalid"
return instruction
@pytest.mark.parametrize(
"cpu, a_instruction, in_m, reset",
[
(
_create_random_cpu(),
_create_random_a_instruction(),
sample_bits(16),
False,
)
for _ in range(NUMBER_OF_SAMPLES_TO_DRAW_PER_TEST)
],
)
def test_cpu_runs_a_instructions(
cpu: CPU,
a_instruction: tuple[bool, ...],
in_m: tuple[bool, ...],
reset: bool,
) -> None:
# When
new_cpu = cpu(a_instruction, in_m, reset)
# Then
assert (
new_cpu.a_register.out == a_instruction
), "A register must store the value of the A-instruction"
assert (
new_cpu.address_m == a_instruction[1:]
), "`address_m` in next time step must be the value of the A-instruction in the current time step"
assert (
new_cpu.d_register.out == cpu.d_register.out
), "D register must not change when executing an A-instruction"
assert (
to_int(new_cpu.pc_out) == (to_int(cpu.pc_out) + 1) % 2**15
), "PC must increment by 1 when executing an A-instruction"
assert (
not new_cpu.write_m
), "write_m must be `False` when executing an A-instruction"
@pytest.mark.parametrize(
"cpu, c_instruction, in_m, reset",
[
(
_create_random_cpu(),
c_instruction,
sample_bits(16),
False,
)
for c_instruction in _get_next_c_instruction()
],
)
def test_cpu_runs_c_instructions(
cpu: CPU,
c_instruction: tuple[bool, ...],
in_m: tuple[bool, ...],
reset: bool,
) -> None:
# Given
symbolic_instruction = _to_symbol(c_instruction)
# When
new_cpu = cpu(c_instruction, in_m, reset)
# Then
assert isinstance(cpu.out_m, tuple), "`out_m` must be a tuple"
assert is_n_bit_vector(cpu.out_m, n=16), "`out_m` must be a 16-bit tuple"
assert isinstance(new_cpu.out_m, tuple), "`out_m` must be a tuple"
assert is_n_bit_vector(new_cpu.out_m, n=16), "`out_m` must be a 16-bit tuple"
# Then (dest)
if "A" in symbolic_instruction.dest:
assert new_cpu.a_register.out == new_cpu.out_m, "`A` must be written to `out_m`"
if "A" not in symbolic_instruction.dest:
assert new_cpu.a_register.out == cpu.a_register.out, "`A` must not be changed"
if "M" in symbolic_instruction.dest:
assert new_cpu.write_m, "`new_write_m` must be `True` when M in destination"
if "M" not in symbolic_instruction.dest:
assert (
not new_cpu.write_m
), "`new_write_m` must be `False` when M not in destination"
if "D" in symbolic_instruction.dest:
assert new_cpu.d_register.out == new_cpu.out_m, "`D` must be written to `out_m`"
if "D" not in symbolic_instruction.dest:
assert new_cpu.d_register.out == cpu.d_register.out, "`D` must not be changed"
# Then (comp)
if symbolic_instruction.comp == "0":
assert to_int(new_cpu.out_m) == 0, "`out_m` must be `0` when comp is `0`"
if symbolic_instruction.comp == "1":
assert to_int(new_cpu.out_m) == 1, "`out_m` must be `1` when comp is `1`"
if symbolic_instruction.comp == "-1":
assert all(b for b in new_cpu.out_m), f"`out_m` must be `-1` when comp is `-1`"
if symbolic_instruction.comp == "D":
assert (
new_cpu.out_m == cpu.d_register.out
), "`out_m` must be `D` when comp is `D`"
if symbolic_instruction.comp == "A":
assert (
new_cpu.out_m == cpu.a_register.out
), "`out_m` must be `A` when comp is `A`"
if symbolic_instruction.comp == "!D":
assert new_cpu.out_m == NOT16(
cpu.d_register.out
), "`out_m` must be `!D` when comp is `!D`"
if symbolic_instruction.comp == "!A":
assert new_cpu.out_m == NOT16(
cpu.a_register.out
), "`out_m` must be `!A` when comp is `!A`"
if symbolic_instruction.comp == "-D":
assert new_cpu.out_m == INC16(
NOT16(cpu.d_register.out)
), "`out_m` must be `-D` when comp is `-D`"
if symbolic_instruction.comp == "-A":
assert new_cpu.out_m == INC16(
NOT16(cpu.a_register.out)
), "`out_m` must be `-A` when comp is `-A`"
if symbolic_instruction.comp == "D+1":
assert new_cpu.out_m == INC16(
cpu.d_register.out
), "`out_m` must be `D+1` when comp is `D+1`"
if symbolic_instruction.comp == "A+1":
assert new_cpu.out_m == INC16(
cpu.a_register.out
), "`out_m` must be `A+1` when comp is `A+1`"
if symbolic_instruction.comp == "D-1":
assert (
to_int(new_cpu.out_m) == (to_int(cpu.d_register.out) - 1) % 2**16
), "`out_m` must be `D-1` when comp is `D-1`"
if symbolic_instruction.comp == "A-1":
assert (
to_int(new_cpu.out_m) == (to_int(cpu.a_register.out) - 1) % 2**16
), "`out_m` must be `A-1` when comp is `A-1`"
if symbolic_instruction.comp == "D+A":
assert (
to_int(new_cpu.out_m)
== (to_int(cpu.d_register.out) + to_int(cpu.a_register.out)) % 2**16
), "`out_m` must be `D+A` when comp is `D+A`"
if symbolic_instruction.comp == "D-A":
assert (
to_int(new_cpu.out_m)
== (to_int(cpu.d_register.out) - to_int(cpu.a_register.out)) % 2**16
), "`out_m` must be `D-A` when comp is `D-A`"
if symbolic_instruction.comp == "A-D":
assert (
to_int(new_cpu.out_m)
== (to_int(cpu.a_register.out) - to_int(cpu.d_register.out)) % 2**16
), "`out_m` must be `A-D` when comp is `A-D`"
if symbolic_instruction.comp == "D&A":
assert new_cpu.out_m == AND16(
cpu.d_register.out, cpu.a_register.out
), "`out_m` must be `D&A` when comp is `D&A`"
if symbolic_instruction.comp == "D|A":
assert new_cpu.out_m == OR16(
cpu.d_register.out, cpu.a_register.out
), "`out_m` must be `D|A` when comp is `D|A`"
if symbolic_instruction.comp == "M":
assert new_cpu.out_m == in_m, "`out_m` must be `in_m` when comp is `M`"
if symbolic_instruction.comp == "!M":
assert new_cpu.out_m == NOT16(in_m), "`out_m` must be `!M` when comp is `!M`"
if symbolic_instruction.comp == "-M":
assert new_cpu.out_m == INC16(
NOT16(in_m)
), "`out_m` must be `-M` when comp is `-M`"
if symbolic_instruction.comp == "M+1":
assert new_cpu.out_m == INC16(in_m), "`out_m` must be `M+1` when comp is `M+1`"
if symbolic_instruction.comp == "M-1":
assert (
to_int(new_cpu.out_m) == (to_int(in_m) - 1) % 2**16
), "`out_m` must be `M-1` when comp is `M-1`"
if symbolic_instruction.comp == "D+M":
assert (
to_int(new_cpu.out_m)
== (to_int(cpu.d_register.out) + to_int(in_m)) % 2**16
), "`out_m` must be `D+M` when comp is `D+M`"
if symbolic_instruction.comp == "D-M":
assert (
to_int(new_cpu.out_m)
== (to_int(cpu.d_register.out) - to_int(in_m)) % 2**16
), "`out_m` must be `D-M` when comp is `D-M`"
if symbolic_instruction.comp == "M-D":
assert (
to_int(new_cpu.out_m)
== (to_int(in_m) - to_int(cpu.d_register.out)) % 2**16
), "`out_m` must be `M-D` when comp is `M-D`"
if symbolic_instruction.comp == "D&M":
assert new_cpu.out_m == AND16(
cpu.d_register.out, in_m
), "`out_m` must be `D&M` when comp is `D&M`"
if symbolic_instruction.comp == "D|M":
assert new_cpu.out_m == OR16(
cpu.d_register.out, in_m
), "`out_m` must be `D|M` when comp is `D|M`"
# Then (jump)
if symbolic_instruction.jump == "null":
assert (
to_int(new_cpu.pc_out) == (to_int(cpu.pc_out) + 1) % 2**15
), "PC must increment by 1 when jump is `null`"
if symbolic_instruction.jump == "JGT":
if is_positive(cpu.out_m):
assert (
new_cpu.pc_out == cpu.a_register.out[1:]
), "PC must jump to `A` when jump is `JGT` and ALU output is positive"
if not is_positive(cpu.out_m):
assert (
to_int(new_cpu.pc_out) == (to_int(cpu.pc_out) + 1) % 2**15
), "PC must increment by 1 when jump is `JGT` and ALU output non-positive"
if symbolic_instruction.jump == "JEQ":
if cpu.out_m == ZERO16:
assert (
new_cpu.pc_out == cpu.a_register.out[1:]
), "PC must jump to `A` when jump is `JEQ` and ALU output is zero"
if cpu.out_m != ZERO16:
assert (
to_int(new_cpu.pc_out) == (to_int(cpu.pc_out) + 1) % 2**15
), "PC must increment by 1 when jump is `JEQ` and ALU output is non-zero"
if symbolic_instruction.jump == "JGE":
if is_non_negative(cpu.out_m):
assert (
new_cpu.pc_out == cpu.a_register.out[1:]
), "PC must jump to `A` when jump is `JGE` and ALU output is non-negative"
if not is_non_negative(cpu.out_m):
assert (
to_int(new_cpu.pc_out) == (to_int(cpu.pc_out) + 1) % 2**15
), "PC must increment by 1 when jump is `JGE` and ALU output is negative"
if symbolic_instruction.jump == "JLT":
if not is_non_negative(cpu.out_m):
assert (
new_cpu.pc_out == cpu.a_register.out[1:]
), "PC must jump to `A` when jump is `JLT` and ALU output is negative"
if is_non_negative(cpu.out_m):
assert (
to_int(new_cpu.pc_out) == (to_int(cpu.pc_out) + 1) % 2**15
), "PC must increment by 1 when jump is `JLT` and ALU output is non-negative"
if symbolic_instruction.jump == "JNE":
if cpu.out_m != ZERO16:
assert (
new_cpu.pc_out == cpu.a_register.out[1:]
), "PC must jump to `A` when jump is `JNE` and ALU output is non-zero"
if cpu.out_m == ZERO16:
assert (
to_int(new_cpu.pc_out) == (to_int(cpu.pc_out) + 1) % 2**15
), "PC must increment by 1 when jump is `JNE` and ALU output is zero"
if symbolic_instruction.jump == "JLE":
if not is_positive(cpu.out_m):
assert (
new_cpu.pc_out == cpu.a_register.out[1:]
), "PC must jump to `A` when jump is `JLE` and ALU output is non-positive"
if is_positive(cpu.out_m):
assert (
to_int(new_cpu.pc_out) == (to_int(cpu.pc_out) + 1) % 2**15
), "PC must increment by 1 when jump is `JLE` and ALU output is positive"
if symbolic_instruction.jump == "JMP":
assert (
new_cpu.pc_out == cpu.a_register.out[1:]
), "PC must jump to `A` when jump is an unconditional `JMP`"
@pytest.mark.parametrize(
"cpu, invalid_instruction, in_m, reset",
[
(
_create_random_cpu(),
_create_invalid_instruction(),
sample_bits(16),
True,
)
for _ in range(NUMBER_OF_SAMPLES_TO_DRAW_PER_TEST)
],
)
def test_cpu_throws_assertion_error_when_given_invalid_instruction(
cpu: CPU,
invalid_instruction: tuple[bool, ...],
in_m: tuple[bool, ...],
reset: bool,
) -> None:
# When / Then
with pytest.raises(AssertionError):
cpu(invalid_instruction, in_m, reset)
@pytest.mark.parametrize(
"cpu, instruction, in_m, reset",
[
(
_create_random_cpu(),
_create_valid_instruction(),
sample_bits(16),
True,
)
for _ in range(NUMBER_OF_SAMPLES_TO_DRAW_PER_TEST)
],
)
def test_program_counter_is_reset_when_reset_control_bit_is_asserted(
cpu: CPU,
instruction: tuple[bool, ...],
in_m: tuple[bool, ...],
reset: bool,
) -> None:
# Given
ZERO15 = (False,) * 15
# When
new_cpu = cpu(instruction, in_m, reset)
# Then
assert (
new_cpu.pc_out == ZERO15
), "PC must be reset to `0` when reset control bit is asserted"
@pytest.mark.parametrize(
"memory, xs, address, load",
[
(
_create_random_memory(),
sample_bits(16),
_create_random_valid_memory_address(),
True,
)
for _ in range(NUMBER_OF_SAMPLES_TO_DRAW_PER_TEST)
],
)
def test_memory_loads_value_at_valid_address_and_returns_it_next_time_step(
memory: Memory,
xs: tuple[bool, ...],
address: tuple[bool, ...],
load: bool,
) -> None:
# Given
address_idx = to_int(address)
# When
new_memory = memory(xs, address, load)
# Then
if 0 <= address_idx < 2**14:
assert (
new_memory.ram.state[address_idx] == xs
), "memory must load value at address when load is asserted"
if 2**14 <= address_idx < 2**14 + 2**13:
offset_address_idx = address_idx - 2**14
assert (
new_memory.screen.state[offset_address_idx] == xs
), "screen must load value at address when load is asserted"
assert (
new_memory.out == xs
), "out must return the value stored at the previous time step"
@pytest.mark.parametrize(
"memory, xs, address, load",
[
(
_create_random_memory(),
sample_bits(16),
_create_random_invalid_memory_address(),
True,
)
for _ in range(NUMBER_OF_SAMPLES_TO_DRAW_PER_TEST)
],
)
def test_memory_throws_assertion_error_when_given_invalid_address(
memory: Memory,
xs: tuple[bool, ...],
address: tuple[bool, ...],
load: bool,
) -> None:
# When / Then
with pytest.raises(AssertionError):
memory(xs, address, load)
@pytest.mark.parametrize(
"memory, xs, address, load",
[
(
_create_random_memory(),
sample_bits(16),
_create_random_valid_memory_address(),
False,
)
for _ in range(NUMBER_OF_SAMPLES_TO_DRAW_PER_TEST)
],
)
def test_memory_does_not_load_value_at_valid_address_but_outputs_it_next_time_step_when_load_is_false(
memory: Memory,
xs: tuple[bool, ...],
address: tuple[bool, ...],
load: bool,
) -> None:
# Given
address_idx = to_int(address)
# When
new_memory = memory(xs, address, load)
# Then
assert (
new_memory.ram.state == memory.ram.state
), "RAM must not change when load is `False`"
assert (
new_memory.screen.state == memory.screen.state
), "screen must not change when load is `False`"
assert (
new_memory.keyboard.out == memory.keyboard.out
), "keyboard must not change when load is `False`"
if 0 <= address_idx < 2**14:
assert (
new_memory.out == memory.ram.state[address_idx]
), "out must be the value at address of the RAM"
if 2**14 <= address_idx < 2**14 + 2**13:
offset_address_idx = address_idx - 2**14
assert (
new_memory.out == memory.screen.state[offset_address_idx]
), "out must be the value at offset address of the screen"
if address_idx == 2**14 + 2**13:
assert new_memory.out == memory.keyboard.out, "out must be the value at address"
def test_computer_can_store_value_in_ram() -> None:
# Given
instructions_int = (
# set RAM[3] = 1
0b0000000000000011, # @3
0b1110111111001000, # M=1
)
instructions = tuple(int_to_bit_vector(i, n=16) for i in instructions_int)
computer = Computer.create(instructions)
# When
new_computer = computer(reset=True)
for _ in range(len(instructions)):
new_computer = new_computer(reset=False)
# Then
assert all(
s == ZERO16 for s in computer.memory.ram.state
), "all RAM addresses must initially be `0`"
assert all(
s == ZERO16 for s in new_computer.memory.ram.state[:3]
), "all other RAM addresses before must be `0`"
assert new_computer.memory.ram.state[3] == make_one_hot(n=16, i=15)
assert new_computer.memory.out == make_one_hot(n=16, i=15)
assert all(
s == ZERO16 for s in new_computer.memory.ram.state[4:]
), "all other RAM addresses after must be `0`"
def test_computer_can_store_ram_value_in_d_register() -> None:
# Given
instructions_int = (
# set RAM[3] = 1
0b0000000000000011, # @3
0b1110111111001000, # M=1
# set D = RAM[3]
0b1111110000010000, # D=M
)
instructions = tuple(int_to_bit_vector(i, n=16) for i in instructions_int)
computer = Computer.create(instructions)
# When
new_computer = computer(reset=True)
for _ in range(len(instructions)):
new_computer = new_computer(reset=False)
new_computer = new_computer(reset=False)
# Then
for state in computer.memory.ram.state:
assert state == ZERO16, "all RAM addresses must initially be `0`"
for i, state in enumerate(new_computer.memory.ram.state):
if i == 3:
assert state == make_one_hot(n=16, i=15)
if i != 3:
assert state == ZERO16, "all other RAM addresses must be `0`"
assert new_computer.cpu.d_register.out == make_one_hot(n=16, i=15)
def test_computer_can_add_value_in_ram_to_d_register() -> None:
# Given
instructions_int = (
# set RAM[0] = 1
0b0000000000000000, # @0
0b1110111111001000, # M=1
# set RAM[1] = 1
0b0000000000000001, # @1
0b1110111111001000, # M=1
# set D = RAM[0]
0b0000000000000000, # @0
0b1111110000010000, # D=M
# set D = D + RAM[1]
0b0000000000000001, # @1
0b1111000010010000, # D=D+M
)
instructions = tuple(int_to_bit_vector(i, n=16) for i in instructions_int)
computer = Computer.create(instructions)
# When
new_computers: list[Computer] = [None for _ in range(len(instructions) + 2)] # type: ignore
new_computers[0] = computer(reset=True)
for t in range(1, len(instructions) + 1):
new_computers[t] = new_computers[t - 1](reset=False)
# Then
assert all(
s == ZERO16 for s in computer.memory.ram.state
), "all RAM addresses must initially be `0`"
# Then (at line 1)
assert new_computers[1].cpu.a_register.out == ZERO16
# Then (at line 2)
assert new_computers[2].cpu.a_register.out == ZERO16
assert new_computers[2].cpu.out_m == make_one_hot(n=16, i=15)
assert new_computers[2].cpu.write_m
assert new_computers[2].memory.out == make_one_hot(n=16, i=15)
# Then (at line 3)
assert new_computers[3].cpu.a_register.out == make_one_hot(n=16, i=15)
# Then (at line 4)
assert new_computers[4].cpu.a_register.out == make_one_hot(n=16, i=15)
assert new_computers[4].cpu.out_m == make_one_hot(n=16, i=15)
assert new_computers[4].cpu.write_m
assert new_computers[4].memory.out == make_one_hot(n=16, i=15)
# Then (at line 5)
assert new_computers[5].cpu.a_register.out == ZERO16
assert new_computers[5].memory.out == make_one_hot(n=16, i=15)
# Then (at line 6)
assert new_computers[6].cpu.a_register.out == ZERO16
assert new_computers[6].memory.out == make_one_hot(n=16, i=15)
assert new_computers[6].cpu.d_register.out == make_one_hot(n=16, i=15)
# Then (at line 7)
assert new_computers[7].cpu.a_register.out == make_one_hot(n=16, i=15)
assert new_computers[7].memory.out == make_one_hot(n=16, i=15)
# Then (at line 8)
assert new_computers[8].cpu.a_register.out == make_one_hot(n=16, i=15)
assert new_computers[8].memory.out == make_one_hot(n=16, i=15)
assert new_computers[8].cpu.d_register.out == make_one_hot(n=16, i=14)
assert all(
s == ZERO16 for s in new_computers[8].memory.ram.state[2:]
), "all other RAM addresses must be `0`"
def test_computer_can_add_two_numbers() -> None:
# Given
instructions_int = (
# set RAM[0] = 1
0b0000000000000000, # @0
0b1110111111001000, # M=1
# set RAM[1] = 1
0b0000000000000001, # @1
0b1110111111001000, # M=1
# set D = RAM[0]
0b0000000000000000, # @0
0b1111110000010000, # D=M
# set D = D + RAM[1]
0b0000000000000001, # @1
0b1111000010010000, # D=D+M
# set RAM[2] = D
0b0000000000000010, # @2
0b1110001100001000, # M=D
)
instructions = tuple(int_to_bit_vector(i, n=16) for i in instructions_int)
computer = Computer.create(instructions)
# When
new_computer = computer(reset=True)
for _ in range(len(instructions)):
new_computer = new_computer(reset=False)
# Then
assert all(
s == ZERO16 for s in computer.memory.ram.state
), "all RAM addresses must initially be `0`"
assert new_computer.memory.ram.state[0] == make_one_hot(n=16, i=15)
assert new_computer.memory.ram.state[1] == make_one_hot(n=16, i=15)
assert new_computer.memory.ram.state[2] == make_one_hot(n=16, i=14)
assert new_computer.cpu.d_register.out == make_one_hot(n=16, i=14)
assert all(
s == ZERO16 for s in new_computer.memory.ram.state[3:]
), "all other RAM addresses must be `0`"
def test_computer_can_draw_a_single_pixel_on_the_screen() -> None:
# Given
instructions_int = (
# set MEMORY[2^14] = SCREEN[0] = 1
0b0100000000000000, # @2^14
0b1110111111001000, # M=1
)
instructions = tuple(int_to_bit_vector(i, n=16) for i in instructions_int)
computer = Computer.create(instructions)
# When
new_computers: list[Computer] = [None for _ in range(len(instructions) + 2)] # type: ignore
new_computers[0] = computer(reset=True)
for t in range(1, len(instructions) + 1):
new_computers[t] = new_computers[t - 1](reset=False)
# Then (after line 1)
assert new_computers[1].cpu.address_m == make_one_hot(n=15, i=0)
# Then (after line 2)
assert new_computers[2].cpu.address_m == make_one_hot(n=15, i=0)
assert new_computers[2].cpu.write_m
assert new_computers[2].cpu.out_m == make_one_hot(n=16, i=15)
assert new_computers[2].memory.ram.state == new_computers[0].memory.ram.state
assert new_computers[2].memory.screen.state[0] == make_one_hot(n=16, i=15)
assert new_computers[2].memory.out == make_one_hot(n=16, i=15)
assert (
new_computers[2].memory.screen.state[1:]
== new_computers[0].memory.screen.state[1:]
), "all other screen pixels must be `0`"