-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.py
732 lines (575 loc) · 23.7 KB
/
memory.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
from dataclasses import dataclass
from gates import MUX, MUX16, MUX4WAY16, MUX8WAY16, DMUX, DMUX4WAY, DMUX8WAY
from arithmetic import INC16
from utils import is_n_bit_vector, to_int
ZERO16 = (False,) * 16
@dataclass(frozen=True)
class DFF:
"""A data flip-flop gate that stores a single bit from the previous timestep. This should be called on the edge of each clock signal. This is a primitive component."""
out: bool
def __post_init__(self) -> None:
assert isinstance(self.out, bool), "`self.out` must be a `bool`"
def __call__(self, x: bool) -> "DFF":
# pre-conditions
assert isinstance(x, bool), "`x` must be a `bool`"
# body
new_dff = DFF(x)
# post-conditions
assert isinstance(new_dff, DFF), "`out` must be a `DFF`"
assert new_dff.out == x, "dff must store `x`"
return new_dff
@dataclass(frozen=True)
class BIT:
"""A 1-bit register."""
dff: DFF
def __post_init__(self) -> None:
assert isinstance(self.dff, DFF), "`dff` must be a `DFF`"
def __call__(self, x: bool, load: bool) -> "BIT":
# pre-conditions
assert isinstance(x, bool), "`x` must be a `bool`"
assert isinstance(load, bool), "`load` must be a `bool`"
# body
old_x = self.dff.out
new_x = MUX(old_x, x, load)
new_dff = self.dff(new_x)
new_bit = BIT(new_dff)
# post-conditions
assert isinstance(new_bit, BIT), "`new_bit` must be a `BIT`"
if load:
assert new_bit.out == x, "new value must be stored when load=1"
if not load:
assert new_bit.out == old_x, "old value must be kept when load=0"
return new_bit
@property
def out(self) -> bool:
return self.dff.out
@dataclass(frozen=True)
class REGISTER16:
"""A 16-bit register."""
bits: tuple[BIT, ...]
def __post_init__(self) -> None:
assert all(isinstance(b, BIT) for b in self.bits), "`bits` must be `BIT`s"
assert len(self.bits) == 16, "`bits` must be a 16-tuple of `BIT`s"
def __call__(self, xs: tuple[bool, ...], load: bool) -> "REGISTER16":
# pre-conditions
assert is_n_bit_vector(xs, n=16), "`xs` must be a 16-tuple of `bool`s"
assert isinstance(load, bool), "`load` must be a `bool`"
# body
new_bits = tuple(bit(x, load) for bit, x in zip(self.bits, xs))
new_register = REGISTER16(new_bits)
# post-conditions
assert isinstance(
new_register, REGISTER16
), "`new_register` must be a `REGISTER16`"
if load:
assert all(
b.out == v for b, v in zip(new_register.bits, xs)
), "new value must be stored when load=1"
if not load:
assert self.out == new_register.out, "old value must be kept when load=0"
return new_register
@property
def out(self) -> tuple[bool, ...]:
return tuple(b.out for b in self.bits)
@staticmethod
def create() -> "REGISTER16":
"""Creates a new 16-bit register with all bits set to 0."""
dffs = tuple(DFF(False) for _ in range(16))
bits = tuple(BIT(dff) for dff in dffs)
register = REGISTER16(bits)
# post-conditions
assert isinstance(register, REGISTER16), "`register` must be a `REGISTER16`"
assert all(
isinstance(b, BIT) for b in register.bits
), "`register.bits` must be a 16-tuple of `BIT`s"
assert all(
isinstance(b.dff, DFF) for b in register.bits
), "`register.bits` must be a 16-tuple of `BIT`s"
assert all(
isinstance(b.dff.out, bool) for b in register.bits
), "`register.bits` must be a 16-tuple of `BIT`s"
assert all(
b.dff.out == False for b in register.bits
), "`register.bits` must be a 16-tuple of `BIT`s"
return register
@dataclass(frozen=True)
class RAM8:
"""8-register memory, each 16-bits."""
registers: tuple[REGISTER16, ...]
out: tuple[bool, ...]
def __post_init__(self) -> None:
assert all(
isinstance(r, REGISTER16) for r in self.registers
), "`registers` must be a tuple of `REGISTER16`s"
assert (
len(self.registers) == 8
), "`registers` must be a 8-tuple of `REGISTER16`s"
assert is_n_bit_vector(self.out, n=16), "`out` must be a 16-tuple of `bool`s"
def __call__(
self,
xs: tuple[bool, ...],
load: bool,
address: tuple[bool, ...],
) -> "RAM8":
# pre-conditions
assert is_n_bit_vector(xs, n=16), "`xs` must be a 16-tuple of `bool`s"
assert isinstance(load, bool), "`load` must be a `bool`"
assert is_n_bit_vector(address, n=3), "`address` must be a 3-tuple of `bool`s"
# body
load_bits = DMUX8WAY(load, address)
new_registers = tuple(r(xs, load_bits[i]) for i, r in enumerate(self.registers))
new_out = MUX8WAY16(*[r.out for r in new_registers], sel=address) # type: ignore
new_ram8 = RAM8(new_registers, new_out)
# post-conditions
assert isinstance(new_ram8, RAM8), "`new_ram8` must be a `RAM8`"
assert all(
isinstance(r, REGISTER16) for r in new_ram8.registers
), "`new_ram8.registers` must be an 8-tuple of `REGISTER16`s"
if load:
address_idx = to_int(address)
selected_register = new_ram8.registers[address_idx]
assert all(
v == x for v, x in zip(selected_register.out, xs)
), "new value must be stored when load=1"
if not load:
assert new_ram8.state == self.state, "old value must be kept when load=0"
return new_ram8
@property
def state(self) -> tuple[tuple[bool, ...], ...]:
return tuple(r.out for r in self.registers)
@staticmethod
def create() -> "RAM8":
"""Creates a new 8-register memory with all bits set to 0."""
registers = tuple(REGISTER16.create() for _ in range(8))
out = ZERO16
ram8 = RAM8(registers, out)
# post-conditions
assert isinstance(ram8, RAM8), "`ram8` must be a `RAM8`"
assert all(
s == ZERO16 for s in ram8.state
), "`ram8.state` must be a 8-tuple of 16-tuples of `bool`s"
assert ram8.out == ZERO16, "`ram8.out` must be a 16-tuple of `bool`s"
return ram8
@dataclass(frozen=True)
class RAM64:
"""64-register memory, each 16-bits."""
ram8s: tuple[RAM8, ...]
out: tuple[bool, ...]
def __post_init__(self) -> None:
assert all(
isinstance(r, RAM8) for r in self.ram8s
), "`ram8s` must be a tuple of `RAM8`s"
assert len(self.ram8s) == 8, "`ram8s` must be a 8-tuple of `RAM8`s"
assert is_n_bit_vector(self.out, n=16), "`out` must be a 16-tuple of `bool`s"
def __call__(
self,
xs: tuple[bool, ...],
load: bool,
address: tuple[bool, ...],
) -> "RAM64":
# pre-conditions
assert is_n_bit_vector(xs, n=16), "`xs` must be a 16-tuple of `bool`s"
assert isinstance(load, bool), "`load` must be a `bool`"
assert is_n_bit_vector(address, n=6), "`address` must be a 6-tuple of `bool`s"
# body
load_bits = DMUX8WAY(load, address[:3])
new_ram8s = tuple(
r(xs, load_bits[i], address[3:]) for i, r in enumerate(self.ram8s)
)
new_out = MUX8WAY16(*[r.out for r in new_ram8s], sel=address[:3]) # type: ignore
new_ram64 = RAM64(new_ram8s, new_out)
# post-conditions
assert isinstance(new_ram64, RAM64), "`new_ram64` must be a `RAM64`"
assert all(
isinstance(r, RAM8) for r in new_ram64.ram8s
), "`new_ram64.ram8s` must be an 8-tuple of `RAM8`s"
if load:
address_idx = to_int(address)
assert (
new_ram64.state[address_idx] == xs
), "new value must be stored when load=1"
assert (
new_ram64.out == xs
), "new value must be returned as `out` when load=1"
if not load:
address_idx = to_int(address)
assert self.state == new_ram64.state, "old value must be kept when load=0"
assert (
new_ram64.out == self.state[address_idx]
), "old value must be returned as `out` when load=0"
return new_ram64
@property
def state(self) -> tuple[tuple[bool, ...], ...]:
output: tuple[tuple[bool, ...], ...] = ()
for ram8 in self.ram8s:
output += ram8.state
return output
@staticmethod
def create() -> "RAM64":
"""Creates a new 64-register memory with all bits set to 0."""
ram8s = tuple(RAM8.create() for _ in range(8))
out = ZERO16
ram64 = RAM64(ram8s, out)
# post-conditions
assert isinstance(ram64, RAM64), "`ram64` must be a `RAM64`"
assert all(
s == ZERO16 for s in ram64.state
), "`ram64.state` must be a 64-tuple of 16-tuples of `bool`s"
assert ram64.out == ZERO16, "`ram64.out` must be a 16-tuple of `bool`s"
return ram64
@dataclass(frozen=True)
class RAM512:
"""512-register memory, each 16-bits."""
ram64s: tuple[RAM64, ...]
out: tuple[bool, ...]
def __post_init__(self) -> None:
assert all(
isinstance(r, RAM64) for r in self.ram64s
), "`ram64s` must be a tuple of `RAM64`s"
assert len(self.ram64s) == 8, "`ram64s` must be a 8-tuple of `RAM8`s"
assert is_n_bit_vector(self.out, n=16), "`out` must be a 16-tuple of `bool`s"
def __call__(
self,
xs: tuple[bool, ...],
load: bool,
address: tuple[bool, ...],
) -> "RAM512":
# pre-conditions
assert is_n_bit_vector(xs, n=16), "`xs` must be a 16-tuple of `bool`s"
assert isinstance(load, bool), "`load` must be a `bool`"
assert is_n_bit_vector(address, n=9), "`address` must be a 9-tuple of `bool`s"
# body
load_bits = DMUX8WAY(load, address[:3])
new_ram64s = tuple(
r(xs, load_bits[i], address[3:]) for i, r in enumerate(self.ram64s)
)
new_out = MUX8WAY16(*[r.out for r in new_ram64s], sel=address[:3]) # type: ignore
new_ram512 = RAM512(new_ram64s, new_out)
# post-conditions
assert isinstance(new_ram512, RAM512), "`new_ram512` must be a `RAM512`"
assert all(
isinstance(r, RAM64) for r in new_ram512.ram64s
), "`new_ram512.ram64s` must be an 8-tuple of `RAM64`s"
address_idx = to_int(address)
if load:
assert (
new_ram512.state[address_idx] == xs
), "new value must be stored when load=1"
assert (
new_ram512.out == xs
), "new value must be returned as `out` when load=1"
if not load:
assert new_ram512.state == self.state, "old value must be kept when load=0"
assert (
new_ram512.out == self.state[address_idx]
), "out must be value of RAM at `address` when load=0"
return new_ram512
@property
def state(self) -> tuple[tuple[bool, ...], ...]:
output: tuple[tuple[bool, ...], ...] = ()
for ram64 in self.ram64s:
output += ram64.state
return output
@staticmethod
def create() -> "RAM512":
"""Creates a new 512-register memory with all bits set to 0."""
ram64s = tuple(RAM64.create() for _ in range(8))
out = ZERO16
ram512 = RAM512(ram64s, out)
# post-conditions
assert isinstance(ram512, RAM512), "`ram512` must be a `RAM512`"
assert all(
s == ZERO16 for s in ram512.state
), "`ram512.state` must be a 512-tuple of 16-tuples of `bool`s"
assert ram512.out == ZERO16, "`ram512.out` must be a 16-tuple of `bool`s"
return ram512
@dataclass(frozen=True)
class RAM4K:
"""4,096-register memory, each 16-bits."""
ram512s: tuple[RAM512, ...]
out: tuple[bool, ...]
def __post_init__(self) -> None:
assert all(
isinstance(r, RAM512) for r in self.ram512s
), "`ram512s` must be a tuple of `RAM512`s"
assert len(self.ram512s) == 8, "`ram512s` must be a 8-tuple of `RAM8`s"
assert is_n_bit_vector(self.out, n=16), "`out` must be a 16-tuple of `bool`s"
def __call__(
self,
xs: tuple[bool, ...],
load: bool,
address: tuple[bool, ...],
) -> "RAM4K":
# pre-conditions
assert is_n_bit_vector(xs, n=16), "`xs` must be a 16-tuple of `bool`s"
assert isinstance(load, bool), "`load` must be a `bool`"
assert is_n_bit_vector(address, n=12), "`address` must be a 12-tuple of `bool`s"
# body
load_bits = DMUX8WAY(load, address[:3])
new_ram512s = tuple(
r(xs, load_bits[i], address[3:]) for i, r in enumerate(self.ram512s)
)
new_out = MUX8WAY16(*[r.out for r in new_ram512s], sel=address[:3]) # type: ignore
new_ram4k = RAM4K(new_ram512s, new_out)
# post-conditions
assert isinstance(new_ram4k, RAM4K), "`new_ram4k` must be a `RAM4K`"
assert all(
isinstance(r, RAM512) for r in new_ram4k.ram512s
), "`new_ram4k.ram512s` must be an 8-tuple of `RAM512`s"
address_idx = to_int(address)
if load:
assert (
new_ram4k.state[address_idx] == xs
), "new value must be stored when load=1"
assert (
new_ram4k.out == xs
), "new value must be returned as `out` when load=1"
if not load:
assert self.state == new_ram4k.state, "old value must be kept when load=0"
assert (
new_ram4k.out == self.state[address_idx]
), "out must be value of RAM at `address` when load=0"
return new_ram4k
@property
def state(self) -> tuple[tuple[bool, ...], ...]:
output: tuple[tuple[bool, ...], ...] = ()
for ram512 in self.ram512s:
output += ram512.state
return output
@staticmethod
def create() -> "RAM4K":
"""Creates a new 4,096-register memory with all bits set to 0."""
ram512s = tuple(RAM512.create() for _ in range(8))
out = ZERO16
ram4k = RAM4K(ram512s, out)
# post-conditions
assert isinstance(ram4k, RAM4K), "`ram4k` must be a `RAM4K`"
assert all(
s == ZERO16 for s in ram4k.state
), "`ram4k.state` must be a 4,096-tuple of 16-tuples of `bool`s"
assert ram4k.out == ZERO16, "`ram4k.out` must be a 16-tuple of `bool`s"
return ram4k
@dataclass(frozen=True)
class RAM8K:
"""8,192-register memory, each 16-bits."""
ram4ks: tuple[RAM4K, ...]
out: tuple[bool, ...]
def __post_init__(self) -> None:
assert all(
isinstance(r, RAM4K) for r in self.ram4ks
), "`ram4ks` must be a tuple of `RAM4K`s"
assert len(self.ram4ks) == 2, "`ram4ks` must be a 2-tuple of `RAM4K`s"
assert is_n_bit_vector(self.out, n=16), "`out` must be a 16-tuple of `bool`s"
def __call__(
self,
xs: tuple[bool, ...],
load: bool,
address: tuple[bool, ...],
) -> "RAM8K":
# pre-conditions
assert is_n_bit_vector(xs, n=16), "`xs` must be a 16-tuple of `bool`s"
assert isinstance(load, bool), "`load` must be a `bool`"
assert is_n_bit_vector(address, n=13), "`address` must be a 13-tuple of `bool`s"
# body
load_bits = DMUX(load, address[0])
new_ram4ks = tuple(
r(xs, load_bits[i], address[1:]) for i, r in enumerate(self.ram4ks)
)
new_out = MUX16(
new_ram4ks[0].out,
new_ram4ks[1].out,
address[0],
)
new_ram8k = RAM8K(new_ram4ks, new_out)
# post-conditions
assert isinstance(new_ram8k, RAM8K), "`new_ram8k` must be a `RAM8K`"
assert all(
isinstance(r, RAM4K) for r in new_ram8k.ram4ks
), "`new_ram8k.ram4ks` must be an 2-tuple of `RAM4K`s"
address_idx = to_int(address)
if load:
assert (
new_ram8k.state[address_idx] == xs
), "new value must be stored when load=1"
assert (
new_ram8k.out == xs
), "new value must be returned as `out` when load=1"
if not load:
assert self.state == new_ram8k.state, "old value must be kept when load=0"
assert (
new_ram8k.out == self.state[address_idx]
), "out must be value of RAM at `address` when load=0"
return new_ram8k
@property
def state(self) -> tuple[tuple[bool, ...], ...]:
output: tuple[tuple[bool, ...], ...] = ()
for ram4k in self.ram4ks:
output += ram4k.state
return output
@staticmethod
def create() -> "RAM8K":
"""Creates a new 8,192-register memory with all bits set to 0."""
ram4ks = tuple(RAM4K.create() for _ in range(2))
out = ZERO16
ram8k = RAM8K(ram4ks, out)
# post-conditions
assert isinstance(ram8k, RAM8K), "`ram8k` must be a `RAM8K`"
assert all(
s == ZERO16 for s in ram8k.state
), "`ram8k.state` must be a 8,192-tuple of 16-tuples of `bool`s"
assert ram8k.out == ZERO16, "`ram8k.out` must be a 16-tuple of `bool`s"
return ram8k
@dataclass(frozen=True)
class RAM16K:
"""16,384-register memory, each 16-bits."""
ram4ks: tuple[RAM4K, ...]
out: tuple[bool, ...]
def __post_init__(self) -> None:
assert all(
isinstance(r, RAM4K) for r in self.ram4ks
), "`ram4ks` must be a tuple of `RAM4K`s"
assert len(self.ram4ks) == 4, "`ram4ks` must be a 4-tuple of `RAM4K`s"
assert is_n_bit_vector(self.out, n=16), "`out` must be a 16-tuple of `bool`s"
def __call__(
self,
xs: tuple[bool, ...],
load: bool,
address: tuple[bool, ...],
) -> "RAM16K":
# pre-conditions
assert is_n_bit_vector(xs, n=16), "`xs` must be a 16-tuple of `bool`s"
assert isinstance(load, bool), "`load` must be a `bool`"
assert is_n_bit_vector(address, n=14), "`address` must be a 14-tuple of `bool`s"
# body
load_bits = DMUX4WAY(load, address[:2])
new_ram4ks = tuple(
r(xs, load_bits[i], address[2:]) for i, r in enumerate(self.ram4ks)
)
new_out = MUX4WAY16( # type: ignore
*[r.out for r in new_ram4ks],
sel=address[:2],
)
new_ram16k = RAM16K(new_ram4ks, new_out)
# post-conditions
assert isinstance(new_ram16k, RAM16K), "`new_ram16k` must be a `RAM16K`"
assert all(
isinstance(r, RAM4K) for r in new_ram16k.ram4ks
), "`new_ram16k.ram4ks` must be an 4-tuple of `RAM4K`s"
address_idx = to_int(address)
if load:
assert (
new_ram16k.state[address_idx] == xs
), "new value must be stored when load=1"
assert (
new_ram16k.out == xs
), "new value must be returned as `out` when load=1"
if not load:
assert self.state == new_ram16k.state, "old value must be kept when load=0"
assert (
new_ram16k.out == self.state[address_idx]
), "out must be value of RAM at `address` when load=0"
return new_ram16k
@property
def state(self) -> tuple[tuple[bool, ...], ...]:
output: tuple[tuple[bool, ...], ...] = ()
for ram4k in self.ram4ks:
output += ram4k.state
return output
@staticmethod
def create() -> "RAM16K":
"""Creates a new 16,384-register memory with all bits set to 0."""
ram4ks = tuple(RAM4K.create() for _ in range(4))
ram16k = RAM16K(ram4ks, ZERO16)
# post-conditions
assert isinstance(ram16k, RAM16K), "`ram16k` must be a `RAM16K`"
assert all(
s == ZERO16 for s in ram16k.state
), "`ram16k.state` must be a 16,384-tuple of 16-tuples of `bool`s"
assert ram16k.out == ZERO16, "`ram16k.out` must be `ZERO16`"
return ram16k
@dataclass(frozen=True)
class PC:
"""A 16-bit program counter with load, inc and reset control bits."""
register: REGISTER16
def __post_init__(self) -> None:
assert isinstance(
self.register, REGISTER16
), "`register` must be a `REGISTER16`"
def __call__(
self,
xs: tuple[bool, ...],
load: bool,
inc: bool,
reset: bool,
) -> "PC":
# pre-conditions
assert is_n_bit_vector(xs, n=16), "`xs` must be a 16-tuple of `bool`s"
assert isinstance(load, bool), "`load` must be a `bool`"
assert isinstance(inc, bool), "`inc` must be a `bool`"
assert isinstance(reset, bool), "`reset` must be a `bool`"
# body
a = MUX16(self.out, INC16(self.out), inc)
b = MUX16(a, xs, load)
c = MUX16(b, ZERO16, reset)
new_register = self.register(c, True)
new_pcounter = PC(new_register)
# post-conditions
assert isinstance(new_pcounter, PC), "`new_pcounter` must be a `PCOUNTER`"
assert isinstance(
new_pcounter.register, REGISTER16
), "`new_pcounter.register` must be a `REGISTER16`"
if reset:
assert new_pcounter.out == ZERO16, "counter must be reset when reset=1"
elif load:
assert new_pcounter.out == xs, "new value must be stored when load=1"
elif inc:
assert new_pcounter.out == INC16(self.out), "counter must be incremented"
else:
assert new_pcounter.out == self.out, "counter must be unchanged"
return new_pcounter
@property
def out(self) -> tuple[bool, ...]:
return self.register.out
@staticmethod
def create() -> "PC":
"""Creates a new 16-bit program counter with all bits set to 0."""
pc = PC(REGISTER16.create())
# post-conditions
assert isinstance(pc, PC), "`pc` must be a `PC`"
assert pc.out == ZERO16, "`pc.out` must be `ZERO16`"
return pc
@dataclass(frozen=True)
class ROM32K:
"""32,768-register memory, each 16-bits. This is a primitive component."""
registers: tuple[tuple[bool, ...], ...]
def __post_init__(self) -> None:
assert all(
is_n_bit_vector(xs, n=16) for xs in self.registers
), "`registers` must be a tuple of 16-tuples of `bool`s"
assert len(self.registers) == 2**15, "`registers` must be a 32,768-tuple"
def __call__(self, address: tuple[bool, ...]) -> tuple[bool, ...]:
# pre-conditions
assert is_n_bit_vector(address, n=15), "`address` must be a 15-tuple of `bool`s"
# body
register_idx = to_int(address)
out = self.registers[register_idx]
# post-conditions
assert is_n_bit_vector(out, n=16), "`out` must be a 16-tuple of `bool`s"
return out
@staticmethod
def create(instructions: tuple[tuple[bool, ...], ...] = tuple()) -> "ROM32K":
"""Creates a `ROM32K` from a tuple of 16-bit instructions. Pads with 0's to reach 32,768 instructions if necessary."""
# pre-conditions
assert all(
is_n_bit_vector(xs, n=16) for xs in instructions
), "`instructions` must be a tuple of 16-tuples of `bool`s"
assert (
len(instructions) <= 2**15
), "`instructions` must be at most a 32,768-tuple"
# body
padding = ((False,) * 16,) * (2**15 - len(instructions))
padded_instructions = instructions + padding
rom32k = ROM32K(padded_instructions)
# post-conditions
assert isinstance(rom32k, ROM32K), "`rom32k` must be a `ROM32K`"
return rom32k