-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathtest_hdl_ast.py
2025 lines (1708 loc) · 73.2 KB
/
test_hdl_ast.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
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# amaranth: UnusedPrint=no, UnusedProperty=no
import warnings
from enum import Enum, EnumMeta
from amaranth.hdl._ast import *
from amaranth.lib.enum import Enum as AmaranthEnum
from .utils import *
from amaranth._utils import _ignore_deprecated
class UnsignedEnum(Enum):
FOO = 1
BAR = 2
BAZ = 3
class SignedEnum(Enum):
FOO = -1
BAR = 0
BAZ = +1
class StringEnum(Enum):
FOO = "a"
BAR = "b"
class TypedEnum(int, Enum):
FOO = 1
BAR = 2
BAZ = 3
class ShapeTestCase(FHDLTestCase):
def test_make(self):
s1 = Shape()
self.assertEqual(s1.width, 1)
self.assertEqual(s1.signed, False)
s2 = Shape(signed=True)
self.assertEqual(s2.width, 1)
self.assertEqual(s2.signed, True)
s3 = Shape(3, True)
self.assertEqual(s3.width, 3)
self.assertEqual(s3.signed, True)
s4 = Shape(0)
self.assertEqual(s4.width, 0)
self.assertEqual(s4.signed, False)
def test_make_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Width must be an integer, not 'a'$"):
Shape("a")
with self.assertRaisesRegex(TypeError,
r"^Width of an unsigned value must be zero or a positive integer, not -1$"):
Shape(-1, signed=False)
with self.assertRaisesRegex(TypeError,
r"^Width of a signed value must be a positive integer, not 0$"):
Shape(0, signed=True)
def test_compare_non_shape(self):
self.assertNotEqual(Shape(1, True), "hi")
def test_repr(self):
self.assertEqual(repr(Shape()), "unsigned(1)")
self.assertEqual(repr(Shape(2, True)), "signed(2)")
def test_convert_tuple_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^cannot unpack non-iterable Shape object$"):
width, signed = Shape()
def test_unsigned(self):
s1 = unsigned(2)
self.assertIsInstance(s1, Shape)
self.assertEqual(s1.width, 2)
self.assertEqual(s1.signed, False)
def test_signed(self):
s1 = signed(2)
self.assertIsInstance(s1, Shape)
self.assertEqual(s1.width, 2)
self.assertEqual(s1.signed, True)
def test_cast_shape(self):
s1 = Shape.cast(unsigned(1))
self.assertEqual(s1.width, 1)
self.assertEqual(s1.signed, False)
s2 = Shape.cast(signed(3))
self.assertEqual(s2.width, 3)
self.assertEqual(s2.signed, True)
def test_cast_int(self):
s1 = Shape.cast(2)
self.assertEqual(s1.width, 2)
self.assertEqual(s1.signed, False)
def test_cast_int_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Width of an unsigned value must be zero or a positive integer, not -1$"):
Shape.cast(-1)
def test_cast_tuple_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Object \(1, True\) cannot be converted to an Amaranth shape$"):
Shape.cast((1, True))
def test_cast_range(self):
s1 = Shape.cast(range(0, 8))
self.assertEqual(s1.width, 3)
self.assertEqual(s1.signed, False)
s2 = Shape.cast(range(0, 9))
self.assertEqual(s2.width, 4)
self.assertEqual(s2.signed, False)
s3 = Shape.cast(range(-7, 8))
self.assertEqual(s3.width, 4)
self.assertEqual(s3.signed, True)
s4 = Shape.cast(range(0, 1))
self.assertEqual(s4.width, 0)
self.assertEqual(s4.signed, False)
s5 = Shape.cast(range(-1, 0))
self.assertEqual(s5.width, 1)
self.assertEqual(s5.signed, True)
s6 = Shape.cast(range(0, 0))
self.assertEqual(s6.width, 0)
self.assertEqual(s6.signed, False)
s7 = Shape.cast(range(-1, -1))
self.assertEqual(s7.width, 0)
self.assertEqual(s7.signed, False)
s8 = Shape.cast(range(0, 10, 3))
self.assertEqual(s8.width, 4)
self.assertEqual(s8.signed, False)
s9 = Shape.cast(range(0, 3, 3))
self.assertEqual(s9.width, 0)
self.assertEqual(s9.signed, False)
def test_cast_enum(self):
s1 = Shape.cast(UnsignedEnum)
self.assertEqual(s1.width, 2)
self.assertEqual(s1.signed, False)
s2 = Shape.cast(SignedEnum)
self.assertEqual(s2.width, 2)
self.assertEqual(s2.signed, True)
def test_cast_enum_bad(self):
with self.assertRaisesRegex(TypeError,
r"^Only enumerations whose members have constant-castable values can be used "
r"in Amaranth code$"):
Shape.cast(StringEnum)
def test_cast_bad(self):
with self.assertRaisesRegex(TypeError,
r"^Object 'foo' cannot be converted to an Amaranth shape$"):
Shape.cast("foo")
def test_hashable(self):
d = {
signed(2): "a",
unsigned(3): "b",
unsigned(2): "c",
}
self.assertEqual(d[signed(2)], "a")
self.assertEqual(d[unsigned(3)], "b")
self.assertEqual(d[unsigned(2)], "c")
class MockShapeCastable(ShapeCastable):
def __init__(self, dest):
self.dest = dest
def as_shape(self):
return self.dest
def __call__(self, value):
return value
def const(self, init):
return Const(init, self.dest)
def from_bits(self, bits):
return bits
class ShapeCastableTestCase(FHDLTestCase):
def test_no_override(self):
with self.assertRaisesRegex(TypeError,
r"^Class '.+\.MockShapeCastableNoOverride' deriving from 'ShapeCastable' must "
r"override the 'as_shape' method$"):
class MockShapeCastableNoOverride(ShapeCastable):
def __init__(self):
pass
def test_cast(self):
sc = MockShapeCastable(unsigned(2))
self.assertEqual(Shape.cast(sc), unsigned(2))
def test_recurse_bad(self):
sc = MockShapeCastable(None)
sc.dest = sc
with self.assertRaisesRegex(RecursionError,
r"^Shape-castable object <.+> casts to itself$"):
Shape.cast(sc)
def test_recurse(self):
sc = MockShapeCastable(MockShapeCastable(unsigned(1)))
self.assertEqual(Shape.cast(sc), unsigned(1))
def test_abstract(self):
with self.assertRaisesRegex(TypeError,
r"^Can't instantiate abstract class ShapeCastable$"):
ShapeCastable()
def test_no_from_bits(self):
with self.assertWarnsRegex(DeprecationWarning,
r"^Class '.+\.MockShapeCastableNoFromBits' deriving from 'ShapeCastable' does "
r"not override the 'from_bits' method, which will be required in Amaranth 0.6$"):
class MockShapeCastableNoFromBits(ShapeCastable):
def __init__(self, dest):
self.dest = dest
def as_shape(self):
return self.dest
def __call__(self, value):
return value
def const(self, init):
return Const(init, self.dest)
self.assertEqual(MockShapeCastableNoFromBits(unsigned(2)).from_bits(123), 123)
class ShapeLikeTestCase(FHDLTestCase):
def test_construct(self):
with self.assertRaises(TypeError):
ShapeLike()
def test_subclass(self):
self.assertTrue(issubclass(Shape, ShapeLike))
self.assertTrue(issubclass(MockShapeCastable, ShapeLike))
self.assertTrue(issubclass(int, ShapeLike))
self.assertTrue(issubclass(range, ShapeLike))
self.assertTrue(issubclass(EnumMeta, ShapeLike))
self.assertFalse(issubclass(Enum, ShapeLike))
self.assertFalse(issubclass(str, ShapeLike))
self.assertTrue(issubclass(ShapeLike, ShapeLike))
def test_isinstance(self):
self.assertTrue(isinstance(unsigned(2), ShapeLike))
self.assertTrue(isinstance(MockShapeCastable(unsigned(2)), ShapeLike))
self.assertTrue(isinstance(2, ShapeLike))
self.assertTrue(isinstance(0, ShapeLike))
self.assertFalse(isinstance(-1, ShapeLike))
self.assertTrue(isinstance(range(10), ShapeLike))
self.assertFalse(isinstance("abc", ShapeLike))
def test_isinstance_enum(self):
class EnumA(Enum):
A = 1
B = 2
class EnumB(Enum):
A = "a"
B = "b"
class EnumC(Enum):
A = Cat(Const(1, 2), Const(0, 2))
self.assertTrue(isinstance(EnumA, ShapeLike))
self.assertFalse(isinstance(EnumB, ShapeLike))
self.assertTrue(isinstance(EnumC, ShapeLike))
class ValueTestCase(FHDLTestCase):
def test_cast(self):
self.assertIsInstance(Value.cast(0), Const)
self.assertIsInstance(Value.cast(True), Const)
c = Const(0)
self.assertIs(Value.cast(c), c)
with self.assertRaisesRegex(TypeError,
r"^Object 'str' cannot be converted to an Amaranth value$"):
Value.cast("str")
def test_cast_enum(self):
e1 = Value.cast(UnsignedEnum.FOO)
self.assertIsInstance(e1, Const)
self.assertEqual(e1.shape(), unsigned(2))
e2 = Value.cast(SignedEnum.FOO)
self.assertIsInstance(e2, Const)
self.assertEqual(e2.shape(), signed(2))
def test_cast_typedenum(self):
e1 = Value.cast(TypedEnum.FOO)
self.assertIsInstance(e1, Const)
self.assertEqual(e1.shape(), unsigned(2))
def test_cast_enum_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Only enumerations whose members have constant-castable values can be used "
r"in Amaranth code$"):
Value.cast(StringEnum.FOO)
def test_bool(self):
with self.assertRaisesRegex(TypeError,
r"^Attempted to convert Amaranth value to Python boolean$"):
if Const(0):
pass
def test_len(self):
self.assertEqual(len(Const(10)), 4)
def test_getitem_int(self):
s1 = Const(10)[0]
self.assertIsInstance(s1, Slice)
self.assertEqual(s1.start, 0)
self.assertEqual(s1.stop, 1)
s2 = Const(10)[-1]
self.assertIsInstance(s2, Slice)
self.assertEqual(s2.start, 3)
self.assertEqual(s2.stop, 4)
with self.assertRaisesRegex(IndexError,
r"^Index 5 is out of bounds for a 4-bit value$"):
Const(10)[5]
def test_getitem_slice(self):
s1 = Const(10)[1:3]
self.assertIsInstance(s1, Slice)
self.assertEqual(s1.start, 1)
self.assertEqual(s1.stop, 3)
s2 = Const(10)[1:-2]
self.assertIsInstance(s2, Slice)
self.assertEqual(s2.start, 1)
self.assertEqual(s2.stop, 2)
s3 = Const(31)[::2]
self.assertIsInstance(s3, Concat)
self.assertIsInstance(s3.parts[0], Slice)
self.assertEqual(s3.parts[0].start, 0)
self.assertEqual(s3.parts[0].stop, 1)
self.assertIsInstance(s3.parts[1], Slice)
self.assertEqual(s3.parts[1].start, 2)
self.assertEqual(s3.parts[1].stop, 3)
self.assertIsInstance(s3.parts[2], Slice)
self.assertEqual(s3.parts[2].start, 4)
self.assertEqual(s3.parts[2].stop, 5)
def test_getitem_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Cannot index value with 'str'$"):
Const(31)["str"]
with self.assertRaisesRegex(TypeError,
r"^Cannot index value with a value; use Value.bit_select\(\) instead$"):
Const(31)[Signal(3)]
s = Signal(3)
with self.assertRaisesRegex(TypeError,
r"^Cannot slice value with a value; use Value.bit_select\(\) or Value.word_select\(\) instead$"):
Const(31)[s:s+3]
def test_shift_left(self):
self.assertRepr(Const(256, unsigned(9)).shift_left(0),
"(cat (const 0'd0) (const 9'd256))")
self.assertRepr(Const(256, unsigned(9)).shift_left(1),
"(cat (const 1'd0) (const 9'd256))")
self.assertRepr(Const(256, unsigned(9)).shift_left(5),
"(cat (const 5'd0) (const 9'd256))")
self.assertRepr(Const(256, signed(9)).shift_left(1),
"(s (cat (const 1'd0) (const 9'sd-256)))")
self.assertRepr(Const(256, signed(9)).shift_left(5),
"(s (cat (const 5'd0) (const 9'sd-256)))")
self.assertRepr(Const(256, unsigned(9)).shift_left(-1),
"(slice (const 9'd256) 1:9)")
self.assertRepr(Const(256, unsigned(9)).shift_left(-5),
"(slice (const 9'd256) 5:9)")
self.assertRepr(Const(256, signed(9)).shift_left(-1),
"(s (slice (const 9'sd-256) 1:9))")
self.assertRepr(Const(256, signed(9)).shift_left(-5),
"(s (slice (const 9'sd-256) 5:9))")
self.assertRepr(Const(256, signed(9)).shift_left(-15),
"(s (slice (const 9'sd-256) 8:9))")
def test_shift_left_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Shift amount must be an integer, not 'str'$"):
Const(31).shift_left("str")
def test_shift_right(self):
self.assertRepr(Const(256, unsigned(9)).shift_right(0),
"(slice (const 9'd256) 0:9)")
self.assertRepr(Const(256, unsigned(9)).shift_right(-1),
"(cat (const 1'd0) (const 9'd256))")
self.assertRepr(Const(256, unsigned(9)).shift_right(-5),
"(cat (const 5'd0) (const 9'd256))")
self.assertRepr(Const(256, signed(9)).shift_right(-1),
"(s (cat (const 1'd0) (const 9'sd-256)))")
self.assertRepr(Const(256, signed(9)).shift_right(-5),
"(s (cat (const 5'd0) (const 9'sd-256)))")
self.assertRepr(Const(256, unsigned(9)).shift_right(1),
"(slice (const 9'd256) 1:9)")
self.assertRepr(Const(256, unsigned(9)).shift_right(5),
"(slice (const 9'd256) 5:9)")
self.assertRepr(Const(256, unsigned(9)).shift_right(15),
"(slice (const 9'd256) 9:9)")
self.assertRepr(Const(256, signed(9)).shift_right(1),
"(s (slice (const 9'sd-256) 1:9))")
self.assertRepr(Const(256, signed(9)).shift_right(5),
"(s (slice (const 9'sd-256) 5:9))")
self.assertRepr(Const(256, signed(9)).shift_right(7),
"(s (slice (const 9'sd-256) 7:9))")
self.assertRepr(Const(256, signed(9)).shift_right(8),
"(s (slice (const 9'sd-256) 8:9))")
self.assertRepr(Const(256, signed(9)).shift_right(9),
"(s (slice (const 9'sd-256) 8:9))")
self.assertRepr(Const(256, signed(9)).shift_right(15),
"(s (slice (const 9'sd-256) 8:9))")
def test_shift_right_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Shift amount must be an integer, not 'str'$"):
Const(31).shift_left("str")
def test_rotate_left(self):
self.assertRepr(Const(256).rotate_left(1),
"(cat (slice (const 9'd256) 8:9) (slice (const 9'd256) 0:8))")
self.assertRepr(Const(256).rotate_left(7),
"(cat (slice (const 9'd256) 2:9) (slice (const 9'd256) 0:2))")
self.assertRepr(Const(256).rotate_left(-1),
"(cat (slice (const 9'd256) 1:9) (slice (const 9'd256) 0:1))")
self.assertRepr(Const(256).rotate_left(-7),
"(cat (slice (const 9'd256) 7:9) (slice (const 9'd256) 0:7))")
self.assertRepr(Const(0, 0).rotate_left(3),
"(cat (slice (const 0'd0) 0:0) (slice (const 0'd0) 0:0))")
self.assertRepr(Const(0, 0).rotate_left(-3),
"(cat (slice (const 0'd0) 0:0) (slice (const 0'd0) 0:0))")
def test_rotate_left_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Rotate amount must be an integer, not 'str'$"):
Const(31).rotate_left("str")
def test_rotate_right(self):
self.assertRepr(Const(256).rotate_right(1),
"(cat (slice (const 9'd256) 1:9) (slice (const 9'd256) 0:1))")
self.assertRepr(Const(256).rotate_right(7),
"(cat (slice (const 9'd256) 7:9) (slice (const 9'd256) 0:7))")
self.assertRepr(Const(256).rotate_right(-1),
"(cat (slice (const 9'd256) 8:9) (slice (const 9'd256) 0:8))")
self.assertRepr(Const(256).rotate_right(-7),
"(cat (slice (const 9'd256) 2:9) (slice (const 9'd256) 0:2))")
self.assertRepr(Const(0, 0).rotate_right(3),
"(cat (slice (const 0'd0) 0:0) (slice (const 0'd0) 0:0))")
self.assertRepr(Const(0, 0).rotate_right(-3),
"(cat (slice (const 0'd0) 0:0) (slice (const 0'd0) 0:0))")
def test_rotate_right_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Rotate amount must be an integer, not 'str'$"):
Const(31).rotate_right("str")
def test_replicate_shape(self):
s1 = Const(10).replicate(3)
self.assertEqual(s1.shape(), unsigned(12))
self.assertIsInstance(s1.shape(), Shape)
s2 = Const(10).replicate(0)
self.assertEqual(s2.shape(), unsigned(0))
def test_replicate_count_wrong(self):
with self.assertRaises(TypeError):
Const(10).replicate(-1)
with self.assertRaises(TypeError):
Const(10).replicate("str")
def test_replicate_repr(self):
s = Const(10).replicate(3)
self.assertEqual(repr(s), "(cat (const 4'd10) (const 4'd10) (const 4'd10))")
def test_format_wrong(self):
sig = Signal()
with self.assertRaisesRegex(TypeError,
r"^Value \(sig sig\) cannot be converted to string."):
f"{sig}"
class ConstTestCase(FHDLTestCase):
def test_shape(self):
self.assertEqual(Const(0).shape(), unsigned(1))
self.assertIsInstance(Const(0).shape(), Shape)
self.assertEqual(Const(1).shape(), unsigned(1))
self.assertEqual(Const(10).shape(), unsigned(4))
self.assertEqual(Const(-10).shape(), signed(5))
self.assertEqual(Const(1, 4).shape(), unsigned(4))
self.assertEqual(Const(-1, 4).shape(), signed(4))
self.assertEqual(Const(1, signed(4)).shape(), signed(4))
self.assertEqual(Const(0, unsigned(0)).shape(), unsigned(0))
def test_shape_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Width of an unsigned value must be zero or a positive integer, not -1$"):
Const(1, -1)
def test_wrong_fencepost(self):
with self.assertWarnsRegex(SyntaxWarning,
r"^Value 10 equals the non-inclusive end of the constant shape "
r"range\(0, 10\); this is likely an off-by-one error$"):
Const(10, range(10))
def test_normalization(self):
self.assertEqual(Const(0b10110, signed(5)).value, -10)
self.assertEqual(Const(0b10000, signed(4)).value, 0)
self.assertEqual(Const(-16, 4).value, 0)
def test_value(self):
self.assertEqual(Const(10).value, 10)
def test_repr(self):
self.assertEqual(repr(Const(10)), "(const 4'd10)")
self.assertEqual(repr(Const(-10)), "(const 5'sd-10)")
def test_hash(self):
with self.assertRaises(TypeError):
hash(Const(0))
def test_enum(self):
e1 = Const(UnsignedEnum.FOO)
self.assertIsInstance(e1, Const)
self.assertEqual(e1.shape(), unsigned(2))
e2 = Const(SignedEnum.FOO)
self.assertIsInstance(e2, Const)
self.assertEqual(e2.shape(), signed(2))
e3 = Const(TypedEnum.FOO)
self.assertIsInstance(e3, Const)
self.assertEqual(e3.shape(), unsigned(2))
e4 = Const(UnsignedEnum.FOO, 4)
self.assertIsInstance(e4, Const)
self.assertEqual(e4.shape(), unsigned(4))
def test_shape_castable(self):
class MockConstValue(ValueCastable):
def __init__(self, value):
self.value = value
def shape(self):
return MockConstShape()
def as_value(self):
return Const(self.value, 8)
class MockConstShape(ShapeCastable):
def as_shape(self):
return unsigned(8)
def __call__(self, value):
return value
def const(self, init):
return MockConstValue(init)
def from_bits(self, bits):
return bits
s = Const(10, MockConstShape())
self.assertIsInstance(s, MockConstValue)
self.assertEqual(s.value, 10)
class OperatorTestCase(FHDLTestCase):
def test_bool(self):
v = Const(0, 4).bool()
self.assertEqual(repr(v), "(b (const 4'd0))")
self.assertEqual(v.shape(), unsigned(1))
def test_invert(self):
v = ~Const(0, 4)
self.assertEqual(repr(v), "(~ (const 4'd0))")
self.assertEqual(v.shape(), unsigned(4))
def test_as_unsigned(self):
v = Const(-1, signed(4)).as_unsigned()
self.assertEqual(repr(v), "(u (const 4'sd-1))")
self.assertEqual(v.shape(), unsigned(4))
def test_as_signed(self):
v = Const(1, unsigned(4)).as_signed()
self.assertEqual(repr(v), "(s (const 4'd1))")
self.assertEqual(v.shape(), signed(4))
def test_as_signed_wrong(self):
with self.assertRaisesRegex(ValueError,
r"^Cannot create a 0-width signed value$"):
Const(0, 0).as_signed()
def test_pos(self):
self.assertRepr(+Const(10), "(const 4'd10)")
def test_neg(self):
v1 = -Const(0, unsigned(4))
self.assertEqual(repr(v1), "(- (const 4'd0))")
self.assertEqual(v1.shape(), signed(5))
v2 = -Const(0, signed(4))
self.assertEqual(repr(v2), "(- (const 4'sd0))")
self.assertEqual(v2.shape(), signed(5))
def test_add(self):
v1 = Const(0, unsigned(4)) + Const(0, unsigned(6))
self.assertEqual(repr(v1), "(+ (const 4'd0) (const 6'd0))")
self.assertEqual(v1.shape(), unsigned(7))
v2 = Const(0, signed(4)) + Const(0, signed(6))
self.assertEqual(v2.shape(), signed(7))
v3 = Const(0, signed(4)) + Const(0, unsigned(4))
self.assertEqual(v3.shape(), signed(6))
v4 = Const(0, unsigned(4)) + Const(0, signed(4))
self.assertEqual(v4.shape(), signed(6))
v5 = 10 + Const(0, 4)
self.assertEqual(v5.shape(), unsigned(5))
def test_sub(self):
v1 = Const(0, unsigned(4)) - Const(0, unsigned(6))
self.assertEqual(repr(v1), "(- (const 4'd0) (const 6'd0))")
self.assertEqual(v1.shape(), signed(7))
v2 = Const(0, signed(4)) - Const(0, signed(6))
self.assertEqual(v2.shape(), signed(7))
v3 = Const(0, signed(4)) - Const(0, unsigned(4))
self.assertEqual(v3.shape(), signed(6))
v4 = Const(0, unsigned(4)) - Const(0, signed(4))
self.assertEqual(v4.shape(), signed(6))
v5 = 10 - Const(0, 4)
self.assertEqual(v5.shape(), signed(5))
v6 = 1 - Const(2)
self.assertEqual(v6.shape(), signed(3))
def test_mul(self):
v1 = Const(0, unsigned(4)) * Const(0, unsigned(6))
self.assertEqual(repr(v1), "(* (const 4'd0) (const 6'd0))")
self.assertEqual(v1.shape(), unsigned(10))
v2 = Const(0, signed(4)) * Const(0, signed(6))
self.assertEqual(v2.shape(), signed(10))
v3 = Const(0, signed(4)) * Const(0, unsigned(4))
self.assertEqual(v3.shape(), signed(8))
v5 = 10 * Const(0, 4)
self.assertEqual(v5.shape(), unsigned(8))
def test_mod(self):
v1 = Const(0, unsigned(4)) % Const(0, unsigned(6))
self.assertEqual(repr(v1), "(% (const 4'd0) (const 6'd0))")
self.assertEqual(v1.shape(), unsigned(6))
v3 = Const(0, signed(4)) % Const(0, unsigned(4))
self.assertEqual(v3.shape(), unsigned(4))
v4 = Const(0, signed(4)) % Const(0, signed(6))
self.assertEqual(v4.shape(), signed(6))
v5 = 10 % Const(0, 4)
self.assertEqual(v5.shape(), unsigned(4))
def test_floordiv(self):
v1 = Const(0, unsigned(4)) // Const(0, unsigned(6))
self.assertEqual(repr(v1), "(// (const 4'd0) (const 6'd0))")
self.assertEqual(v1.shape(), unsigned(4))
v3 = Const(0, signed(4)) // Const(0, unsigned(4))
self.assertEqual(v3.shape(), signed(4))
v4 = Const(0, signed(4)) // Const(0, signed(6))
self.assertEqual(v4.shape(), signed(5))
v5 = 10 // Const(0, 4)
self.assertEqual(v5.shape(), unsigned(4))
def test_and(self):
v1 = Const(0, unsigned(4)) & Const(0, unsigned(6))
self.assertEqual(repr(v1), "(& (const 4'd0) (const 6'd0))")
self.assertEqual(v1.shape(), unsigned(6))
v2 = Const(0, signed(4)) & Const(0, signed(6))
self.assertEqual(v2.shape(), signed(6))
v3 = Const(0, signed(4)) & Const(0, unsigned(4))
self.assertEqual(v3.shape(), signed(5))
v4 = Const(0, unsigned(4)) & Const(0, signed(4))
self.assertEqual(v4.shape(), signed(5))
v5 = 10 & Const(0, 4)
self.assertEqual(v5.shape(), unsigned(4))
def test_or(self):
v1 = Const(0, unsigned(4)) | Const(0, unsigned(6))
self.assertEqual(repr(v1), "(| (const 4'd0) (const 6'd0))")
self.assertEqual(v1.shape(), unsigned(6))
v2 = Const(0, signed(4)) | Const(0, signed(6))
self.assertEqual(v2.shape(), signed(6))
v3 = Const(0, signed(4)) | Const(0, unsigned(4))
self.assertEqual(v3.shape(), signed(5))
v4 = Const(0, unsigned(4)) | Const(0, signed(4))
self.assertEqual(v4.shape(), signed(5))
v5 = 10 | Const(0, 4)
self.assertEqual(v5.shape(), unsigned(4))
def test_xor(self):
v1 = Const(0, unsigned(4)) ^ Const(0, unsigned(6))
self.assertEqual(repr(v1), "(^ (const 4'd0) (const 6'd0))")
self.assertEqual(v1.shape(), unsigned(6))
v2 = Const(0, signed(4)) ^ Const(0, signed(6))
self.assertEqual(v2.shape(), signed(6))
v3 = Const(0, signed(4)) ^ Const(0, unsigned(4))
self.assertEqual(v3.shape(), signed(5))
v4 = Const(0, unsigned(4)) ^ Const(0, signed(4))
self.assertEqual(v4.shape(), signed(5))
v5 = 10 ^ Const(0, 4)
self.assertEqual(v5.shape(), unsigned(4))
def test_shl(self):
v1 = Const(1, 4) << Const(4)
self.assertEqual(repr(v1), "(<< (const 4'd1) (const 3'd4))")
self.assertEqual(v1.shape(), unsigned(11))
def test_shl_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Shift amount must be unsigned$"):
1 << Const(0, signed(6))
with self.assertRaisesRegex(TypeError,
r"^Shift amount must be unsigned$"):
Const(1, unsigned(4)) << -1
def test_shr(self):
v1 = Const(1, 4) >> Const(4)
self.assertEqual(repr(v1), "(>> (const 4'd1) (const 3'd4))")
self.assertEqual(v1.shape(), unsigned(4))
def test_shr_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Shift amount must be unsigned$"):
1 << Const(0, signed(6))
with self.assertRaisesRegex(TypeError,
r"^Shift amount must be unsigned$"):
Const(1, unsigned(4)) << -1
def test_lt(self):
v = Const(0, 4) < Const(0, 6)
self.assertEqual(repr(v), "(< (const 4'd0) (const 6'd0))")
self.assertEqual(v.shape(), unsigned(1))
def test_le(self):
v = Const(0, 4) <= Const(0, 6)
self.assertEqual(repr(v), "(<= (const 4'd0) (const 6'd0))")
self.assertEqual(v.shape(), unsigned(1))
def test_gt(self):
v = Const(0, 4) > Const(0, 6)
self.assertEqual(repr(v), "(> (const 4'd0) (const 6'd0))")
self.assertEqual(v.shape(), unsigned(1))
def test_ge(self):
v = Const(0, 4) >= Const(0, 6)
self.assertEqual(repr(v), "(>= (const 4'd0) (const 6'd0))")
self.assertEqual(v.shape(), unsigned(1))
def test_eq(self):
v = Const(0, 4) == Const(0, 6)
self.assertEqual(repr(v), "(== (const 4'd0) (const 6'd0))")
self.assertEqual(v.shape(), unsigned(1))
def test_ne(self):
v = Const(0, 4) != Const(0, 6)
self.assertEqual(repr(v), "(!= (const 4'd0) (const 6'd0))")
self.assertEqual(v.shape(), unsigned(1))
def test_mux(self):
s = Const(0)
v1 = Mux(s, Const(0, unsigned(4)), Const(0, unsigned(6)))
self.assertEqual(repr(v1), "(switch-value (const 1'd0) (case 0 (const 6'd0)) (default (const 4'd0)))")
self.assertEqual(v1.shape(), unsigned(6))
v2 = Mux(s, Const(0, signed(4)), Const(0, signed(6)))
self.assertEqual(v2.shape(), signed(6))
v3 = Mux(s, Const(0, signed(4)), Const(0, unsigned(4)))
self.assertEqual(v3.shape(), signed(5))
v4 = Mux(s, Const(0, unsigned(4)), Const(0, signed(4)))
self.assertEqual(v4.shape(), signed(5))
def test_mux_wide(self):
s = Const(0b100)
v = Mux(s, Const(0, unsigned(4)), Const(0, unsigned(6)))
self.assertEqual(repr(v), "(switch-value (const 3'd4) (case 000 (const 6'd0)) (default (const 4'd0)))")
def test_mux_bool(self):
v = Mux(True, Const(0), Const(0))
self.assertEqual(repr(v), "(switch-value (const 1'd1) (case 0 (const 1'd0)) (default (const 1'd0)))")
def test_any(self):
v = Const(0b101).any()
self.assertEqual(repr(v), "(r| (const 3'd5))")
def test_all(self):
v = Const(0b101).all()
self.assertEqual(repr(v), "(r& (const 3'd5))")
def test_xor_value(self):
v = Const(0b101).xor()
self.assertEqual(repr(v), "(r^ (const 3'd5))")
def test_matches(self):
s = Signal(4)
self.assertRepr(s.matches(), "(const 1'd0)")
self.assertRepr(s.matches(1), """
(== (sig s) (const 1'd1))
""")
self.assertRepr(s.matches(0, 1), """
(r| (cat (== (sig s) (const 1'd0)) (== (sig s) (const 1'd1))))
""")
self.assertRepr(s.matches("10--"), """
(== (& (sig s) (const 4'd12)) (const 4'd8))
""")
self.assertRepr(s.matches("1 0--"), """
(== (& (sig s) (const 4'd12)) (const 4'd8))
""")
def test_matches_enum(self):
s = Signal(SignedEnum)
self.assertRepr(s.matches(SignedEnum.FOO), """
(== (sig s) (const 1'sd-1))
""")
def test_matches_const_castable(self):
s = Signal(4)
self.assertRepr(s.matches(Cat(C(0b10, 2), C(0b11, 2))), """
(== (sig s) (const 4'd14))
""")
def test_matches_width_wrong(self):
s = Signal(4)
with self.assertRaisesRegex(SyntaxError,
r"^Pattern '--' must have the same width as match value \(which is 4\)$"):
s.matches("--")
with self.assertWarnsRegex(SyntaxWarning,
r"^Pattern '22' \(5'10110\) is not representable in match value shape "
r"\(unsigned\(4\)\); comparison will never be true$"):
s.matches(0b10110)
with self.assertWarnsRegex(SyntaxWarning,
r"^Pattern '\(cat \(const 1'd0\) \(const 4'd11\)\)' \(5'10110\) is not "
r"representable in match value shape \(unsigned\(4\)\); comparison will never be true$"):
s.matches(Cat(0, C(0b1011, 4)))
def test_matches_bits_wrong(self):
s = Signal(4)
with self.assertRaisesRegex(SyntaxError,
r"^Pattern 'abc' must consist of 0, 1, and - \(don't care\) bits, "
r"and may include whitespace$"):
s.matches("abc")
def test_matches_pattern_wrong(self):
s = Signal(4)
with self.assertRaisesRegex(SyntaxError,
r"^Pattern must be a string or a constant-castable expression, not 1\.0$"):
s.matches(1.0)
def test_hash(self):
with self.assertRaises(TypeError):
hash(Const(0) + Const(0))
def test_abs(self):
s = Signal(4)
self.assertRepr(abs(s), """
(sig s)
""")
s = Signal(signed(4))
self.assertRepr(abs(s), """
(slice (switch-value (>= (sig s) (const 1'd0)) (case 0 (- (sig s))) (default (sig s))) 0:4)
""")
self.assertEqual(abs(s).shape(), unsigned(4))
def test_contains(self):
with self.assertRaisesRegex(TypeError,
r"^Cannot use 'in' with an Amaranth value$"):
1 in Signal(3)
class SliceTestCase(FHDLTestCase):
def test_shape(self):
s1 = Const(10)[2]
self.assertEqual(s1.shape(), unsigned(1))
self.assertIsInstance(s1.shape(), Shape)
s2 = Const(-10)[0:2]
self.assertEqual(s2.shape(), unsigned(2))
def test_start_end_negative(self):
c = Const(0, 8)
s1 = Slice(c, 0, -1)
self.assertEqual((s1.start, s1.stop), (0, 7))
s1 = Slice(c, -4, -1)
self.assertEqual((s1.start, s1.stop), (4, 7))
def test_start_end_bool(self):
c = Const(0, 8)
s = Slice(c, False, True)
self.assertIs(type(s.start), int)
self.assertIs(type(s.stop), int)
def test_start_end_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Slice start must be an integer, not 'x'$"):
Slice(0, "x", 1)
with self.assertRaisesRegex(TypeError,
r"^Slice stop must be an integer, not 'x'$"):
Slice(0, 1, "x")
def test_start_end_out_of_range(self):
c = Const(0, 8)
with self.assertRaisesRegex(IndexError,
r"^Cannot start slice 10 bits into 8-bit value$"):
Slice(c, 10, 12)
with self.assertRaisesRegex(IndexError,
r"^Cannot stop slice 12 bits into 8-bit value$"):
Slice(c, 0, 12)
with self.assertRaisesRegex(IndexError,
r"^Slice start 4 must be less than slice stop 2$"):
Slice(c, 4, 2)
with self.assertRaisesRegex(IndexError,
r"^Cannot start slice -9 bits into 8-bit value$"):
Slice(c, -9, -5)
def test_repr(self):
s1 = Const(10)[2]
self.assertEqual(repr(s1), "(slice (const 4'd10) 2:3)")
def test_const(self):
a = Const.cast(Const(0x1234, 16)[4:12])
self.assertEqual(a.value, 0x23)
self.assertEqual(a.shape(), unsigned(8))
a = Const.cast(Const(-4, signed(8))[1:6])
self.assertEqual(a.value, 0x1e)
self.assertEqual(a.shape(), unsigned(5))
class BitSelectTestCase(FHDLTestCase):
def setUp(self):
self.c = Const(0, 8)
self.s = Signal(range(len(self.c)))
def test_shape(self):
s1 = self.c.bit_select(self.s, 2)
self.assertIsInstance(s1, Part)
self.assertEqual(s1.shape(), unsigned(2))
self.assertIsInstance(s1.shape(), Shape)
s2 = self.c.bit_select(self.s, 0)
self.assertIsInstance(s2, Part)
self.assertEqual(s2.shape(), unsigned(0))
def test_stride(self):
s1 = self.c.bit_select(self.s, 2)
self.assertIsInstance(s1, Part)
self.assertEqual(s1.stride, 1)
def test_const(self):
s1 = self.c.bit_select(1, 2)
self.assertIsInstance(s1, Slice)
self.assertRepr(s1, """(slice (const 8'd0) 1:3)""")
def test_width_wrong(self):
with self.assertRaises(TypeError):
self.c.bit_select(self.s, -1)
def test_repr(self):
s = self.c.bit_select(self.s, 2)
self.assertEqual(repr(s), "(part (const 8'd0) (sig s) 2 1)")
def test_offset_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Part offset must be unsigned$"):
self.c.bit_select(self.s.as_signed(), 1)
class WordSelectTestCase(FHDLTestCase):
def setUp(self):
self.c = Const(0, 8)
self.s = Signal(range(len(self.c)))
def test_shape(self):
s1 = self.c.word_select(self.s, 2)
self.assertIsInstance(s1, Part)
self.assertEqual(s1.shape(), unsigned(2))
self.assertIsInstance(s1.shape(), Shape)
def test_stride(self):
s1 = self.c.word_select(self.s, 2)
self.assertIsInstance(s1, Part)
self.assertEqual(s1.stride, 2)
def test_const(self):
s1 = self.c.word_select(1, 2)
self.assertIsInstance(s1, Slice)
self.assertRepr(s1, """(slice (const 8'd0) 2:4)""")
def test_width_wrong(self):
with self.assertRaises(TypeError):
self.c.word_select(self.s, 0)
with self.assertRaises(TypeError):
self.c.word_select(self.s, -1)
def test_repr(self):
s = self.c.word_select(self.s, 2)
self.assertEqual(repr(s), "(part (const 8'd0) (sig s) 2 2)")
def test_offset_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Part offset must be unsigned$"):
self.c.word_select(self.s.as_signed(), 1)