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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
|
// SPDX-License-Identifier: Apache-2.0
package chisel3
import chisel3.internal._
import chisel3.internal.Builder.pushOp
import chisel3.internal.firrtl._
import chisel3.internal.firrtl.PrimOp._
import _root_.firrtl.{ir => firrtlir}
import _root_.firrtl.{constraint => firrtlconstraint}
/** Exists to unify common interfaces of [[Bits]] and [[Reset]].
*
* @note This is a workaround because macros cannot override abstract methods.
*/
private[chisel3] sealed trait ToBoolable extends Element {
def asBool: Bool
}
/** A data type for values represented by a single bitvector. This provides basic bitwise operations.
*
* @groupdesc Bitwise Bitwise hardware operators
* @define coll [[Bits]]
* @define sumWidthInt @note The width of the returned $coll is `width of this` + `that`.
* @define sumWidth @note The width of the returned $coll is `width of this` + `width of that`.
* @define unchangedWidth @note The width of the returned $coll is unchanged, i.e., the `width of this`.
*/
sealed abstract class Bits(private[chisel3] val width: Width) extends Element with ToBoolable {
// TODO: perhaps make this concrete?
// Arguments for: self-checking code (can't do arithmetic on bits)
// Arguments against: generates down to a FIRRTL UInt anyways
// Only used for in a few cases, hopefully to be removed
private[chisel3] def cloneTypeWidth(width: Width): this.type
def cloneType: this.type = cloneTypeWidth(width)
/** Tail operator
*
* @param n the number of bits to remove
* @return This $coll with the `n` most significant bits removed.
* @group Bitwise
*/
/** Head operator
*
* @param n the number of bits to take
* @return The `n` most significant bits of this $coll
* @group Bitwise
*/
def tail(n: Int): UInt = {
val w = width match {
case KnownWidth(x) =>
require(x >= n, s"Can't tail($n) for width $x < $n")
Width(x - n)
case UnknownWidth() => Width()
}
binop(UInt(width = w), TailOp, n)
}
def head(n: Int): UInt = {
width match {
case KnownWidth(x) => require(x >= n, s"Can't head($n) for width $x < $n")
case UnknownWidth() =>
}
binop(UInt(Width(n)), HeadOp, n)
}
/** Returns the specified bit on this $coll as a [[Bool]], statically addressed.
*
* @param x an index
* @return the specified bit
*/
final def extract(x: BigInt): Bool = {
if (x < 0) {
Builder.error(s"Negative bit indices are illegal (got $x)")
}
// This preserves old behavior while a more more consistent API is under debate
// See https://github.com/freechipsproject/chisel3/issues/867
litOption.map { value =>
(((value >> castToInt(x, "Index")) & 1) == 1).asBool
}.getOrElse {
requireIsHardware(this, "bits to be indexed")
widthOption match {
case Some(w) if x >= w => Builder.error(s"High index $x is out of range [0, ${w - 1}]")
case _ =>
}
pushOp(DefPrim(Bool(), BitsExtractOp, this.ref, ILit(x), ILit(x)))
}
}
/** Returns the specified bit on this $coll as a [[Bool]], statically addressed.
*
* @param x an index
* @return the specified bit
*/
final def apply(x: BigInt): Bool =
extract(x)
/** Returns the specified bit on this $coll as a [[Bool]], statically addressed.
*
* @param x an index
* @return the specified bit
*/
final def apply(x: Int): Bool =
extract(BigInt(x))
/** Returns the specified bit on this wire as a [[Bool]], dynamically addressed.
*
* @param x a hardware component whose value will be used for dynamic addressing
* @return the specified bit
*/
final def extract(x: UInt): Bool = {
val theBits = this >> x
theBits(0)
}
/** Returns the specified bit on this wire as a [[Bool]], dynamically addressed.
*
* @param x a hardware component whose value will be used for dynamic addressing
* @return the specified bit
*/
final def apply(x: UInt): Bool =
extract(x)
/** Returns a subset of bits on this $coll from `hi` to `lo` (inclusive), statically addressed.
*
* @example
* {{{
* myBits = 0x5 = 0b101
* myBits(1,0) => 0b01 // extracts the two least significant bits
* }}}
* @param x the high bit
* @param y the low bit
* @return a hardware component contain the requested bits
*/
final def apply(x: Int, y: Int): UInt = {
if (x < y || y < 0) {
Builder.error(s"Invalid bit range ($x,$y)")
}
val w = x - y + 1
// This preserves old behavior while a more more consistent API is under debate
// See https://github.com/freechipsproject/chisel3/issues/867
litOption.map { value =>
((value >> y) & ((BigInt(1) << w) - 1)).asUInt(w.W)
}.getOrElse {
requireIsHardware(this, "bits to be sliced")
widthOption match {
case Some(w) if y >= w => Builder.error(s"High and low indices $x and $y are both out of range [0, ${w - 1}]")
case Some(w) if x >= w => Builder.error(s"High index $x is out of range [0, ${w - 1}]")
case _ =>
}
pushOp(DefPrim(UInt(Width(w)), BitsExtractOp, this.ref, ILit(x), ILit(y)))
}
}
// REVIEW TODO: again, is this necessary? Or just have this and use implicits?
/** Returns a subset of bits on this $coll from `hi` to `lo` (inclusive), statically addressed.
*
* @example
* {{{
* myBits = 0x5 = 0b101
* myBits(1,0) => 0b01 // extracts the two least significant bits
* }}}
* @param x the high bit
* @param y the low bit
* @return a hardware component contain the requested bits
*/
final def apply(x: BigInt, y: BigInt): UInt =
apply(castToInt(x, "High index"), castToInt(y, "Low index"))
private[chisel3] def unop[T <: Data](dest: T, op: PrimOp): T = {
requireIsHardware(this, "bits operated on")
pushOp(DefPrim(dest, op, this.ref))
}
private[chisel3] def binop[T <: Data](dest: T, op: PrimOp, other: BigInt): T = {
requireIsHardware(this, "bits operated on")
pushOp(DefPrim(dest, op, this.ref, ILit(other)))
}
private[chisel3] def binop[T <: Data](dest: T, op: PrimOp, other: Bits): T = {
requireIsHardware(this, "bits operated on")
requireIsHardware(other, "bits operated on")
pushOp(DefPrim(dest, op, this.ref, other.ref))
}
private[chisel3] def compop(op: PrimOp, other: Bits): Bool = {
requireIsHardware(this, "bits operated on")
requireIsHardware(other, "bits operated on")
pushOp(DefPrim(Bool(), op, this.ref, other.ref))
}
private[chisel3] def redop(op: PrimOp): Bool = {
requireIsHardware(this, "bits operated on")
pushOp(DefPrim(Bool(), op, this.ref))
}
/** Pad operator
*
* @param that the width to pad to
* @return this @coll zero padded up to width `that`. If `that` is less than the width of the original component,
* this method returns the original component.
* @note For [[SInt]]s only, this will do sign extension.
* @group Bitwise
*/
def pad(that: Int): this.type = this.width match {
case KnownWidth(w) if w >= that => this
case _ => binop(cloneTypeWidth(this.width.max(Width(that))), PadOp, that)
}
/** Bitwise inversion operator
*
* @return this $coll with each bit inverted
* @group Bitwise
*/
def unary_~ : Bits
/** Static left shift operator
*
* @param that an amount to shift by
* @return this $coll with `that` many zeros concatenated to its least significant end
* $sumWidthInt
* @group Bitwise
*/
def <<(that: BigInt): Bits
/** Static left shift operator
*
* @param that an amount to shift by
* @return this $coll with `that` many zeros concatenated to its least significant end
* $sumWidthInt
* @group Bitwise
*/
def <<(that: Int): Bits
/** Dynamic left shift operator
*
* @param that a hardware component
* @return this $coll dynamically shifted left by `that` many places, shifting in zeros from the right
* @note The width of the returned $coll is `width of this + pow(2, width of that) - 1`.
* @group Bitwise
*/
def <<(that: UInt): Bits
/** Static right shift operator
*
* @param that an amount to shift by
* @return this $coll with `that` many least significant bits truncated
* $unchangedWidth
* @group Bitwise
*/
def >>(that: BigInt): Bits
/** Static right shift operator
*
* @param that an amount to shift by
* @return this $coll with `that` many least significant bits truncated
* $unchangedWidth
* @group Bitwise
*/
def >>(that: Int): Bits
/** Dynamic right shift operator
*
* @param that a hardware component
* @return this $coll dynamically shifted right by the value of `that` component, inserting zeros into the most
* significant bits.
* $unchangedWidth
* @group Bitwise
*/
def >>(that: UInt): Bits
/** Returns the contents of this wire as a [[scala.collection.Seq]] of [[Bool]]. */
def asBools: Seq[Bool] =
Seq.tabulate(this.getWidth)(i => this(i))
/** Reinterpret this $coll as an [[SInt]]
*
* @note The arithmetic value is not preserved if the most-significant bit is set. For example, a [[UInt]] of
* width 3 and value 7 (0b111) would become an [[SInt]] of width 3 and value -1.
*/
def asSInt: SInt
final def asBool: Bool = {
width match {
case KnownWidth(1) => this(0)
case _ => throwException(s"can't covert ${this.getClass.getSimpleName}$width to Bool")
}
}
/** Concatenation operator
*
* @param that a hardware component
* @return this $coll concatenated to the most significant end of `that`
* $sumWidth
* @group Bitwise
*/
def ##(that: Bits): UInt = {
val w = this.width + that.width
pushOp(DefPrim(UInt(w), ConcatOp, this.ref, that.ref))
}
/** Default print as [[Decimal]] */
final def toPrintable: Printable = Decimal(this)
protected final def validateShiftAmount(x: Int): Int = {
if (x < 0)
Builder.error(s"Negative shift amounts are illegal (got $x)")
x
}
}
object Bits {
def apply(): UInt = apply(Width())
/** Create a UInt port with specified width. */
def apply(width: Width): UInt = new UInt(width)
/** Create a UInt literal with specified width. */
protected[chisel3] def Lit(value: BigInt, width: Width): UInt = {
val lit = ULit(value, width)
val result = new UInt(lit.width)
// Bind result to being an Literal
lit.bindLitArg(result)
}
}
/** A data type for unsigned integers, represented as a binary bitvector. Defines arithmetic operations between other
* integer types.
*
* @define coll [[UInt]]
* @define numType $coll
* @define expandingWidth @note The width of the returned $coll is `width of this` + `1`.
* @define constantWidth @note The width of the returned $coll is unchanged, i.e., `width of this`.
*/
sealed class UInt private[chisel3] (width: Width) extends Bits(width) with Num[UInt] {
override def toString: String = {
litOption match {
case Some(value) => s"UInt$width($value)"
case _ => stringAccessor(s"UInt$width")
}
}
private[chisel3] override def typeEquivalent(that: Data): Boolean =
that.isInstanceOf[UInt] && this.width == that.width
private[chisel3] override def cloneTypeWidth(w: Width): this.type =
new UInt(w).asInstanceOf[this.type]
// TODO: refactor to share documentation with Num or add independent scaladoc
/** Unary negation (expanding width)
*
* @return a $coll equal to zero minus this $coll
* $constantWidth
* @group Arithmetic
*/
@deprecated(
"Calling this function with an empty argument list is invalid in Scala 3. Use the form without parentheses instead",
"Chisel 3.5"
)
/** Unary negation (constant width)
*
* @return a $coll equal to zero minus this $coll shifted right by one.
* $constantWidth
* @group Arithmetic
*/
def unary_- : UInt = 0.U - this
def unary_-% : UInt = 0.U -% this
override def +(that: UInt): UInt = this +% that
override def -(that: UInt): UInt = this -% that
override def /(that: UInt): UInt =
binop(UInt(this.width), DivideOp, that)
override def %(that: UInt): UInt =
binop(UInt(this.width.min(that.width)), RemOp, that)
override def *(that: UInt): UInt =
binop(UInt(this.width + that.width), TimesOp, that)
/** Multiplication operator
*
* @param that a hardware [[SInt]]
* @return the product of this $coll and `that`
* $sumWidth
* $singleCycleMul
* @group Arithmetic
*/
def *(that: SInt): SInt = that * this
/** Addition operator (expanding width)
*
* @param that a hardware $coll
* @return the sum of this $coll and `that`
* $maxWidthPlusOne
* @group Arithmetic
*/
/** Addition operator (constant width)
*
* @param that a hardware $coll
* @return the sum of this $coll and `that`
* $maxWidth
* @group Arithmetic
*/
/** Subtraction operator (increasing width)
*
* @param that a hardware $coll
* @return the difference of this $coll less `that`
* $maxWidthPlusOne
* @group Arithmetic
*/
/** Subtraction operator (constant width)
*
* @param that a hardware $coll
* @return the difference of this $coll less `that`
* $maxWidth
* @group Arithmetic
*/
def +&(that: UInt): UInt =
binop(UInt((this.width.max(that.width)) + 1), AddOp, that)
def +%(that: UInt): UInt =
(this +& that).tail(1)
def -&(that: UInt): UInt =
(this.subtractAsSInt(that)).asUInt
def -%(that: UInt): UInt =
(this.subtractAsSInt(that)).tail(1)
/** Bitwise and operator
*
* @param that a hardware $coll
* @return the bitwise and of this $coll and `that`
* $maxWidth
* @group Bitwise
*/
/** Bitwise or operator
*
* @param that a hardware $coll
* @return the bitwise or of this $coll and `that`
* $maxWidth
* @group Bitwise
*/
/** Bitwise exclusive or (xor) operator
*
* @param that a hardware $coll
* @return the bitwise xor of this $coll and `that`
* $maxWidth
* @group Bitwise
*/
def abs: UInt = this
def &(that: UInt): UInt =
binop(UInt(this.width.max(that.width)), BitAndOp, that)
def |(that: UInt): UInt =
binop(UInt(this.width.max(that.width)), BitOrOp, that)
def ^(that: UInt): UInt =
binop(UInt(this.width.max(that.width)), BitXorOp, that)
def unary_~ : UInt =
unop(UInt(width = width), BitNotOp)
// REVIEW TODO: Can these be defined on Bits?
/** Or reduction operator
*
* @return a hardware [[Bool]] resulting from every bit of this $coll or'd together
* @group Bitwise
*/
@deprecated(
"Calling this function with an empty argument list is invalid in Scala 3. Use the form without parentheses instead",
"Chisel 3.5"
)
/** And reduction operator
*
* @return a hardware [[Bool]] resulting from every bit of this $coll and'd together
* @group Bitwise
*/
@deprecated(
"Calling this function with an empty argument list is invalid in Scala 3. Use the form without parentheses instead",
"Chisel 3.5"
)
/** Exclusive or (xor) reduction operator
*
* @return a hardware [[Bool]] resulting from every bit of this $coll xor'd together
* @group Bitwise
*/
@deprecated(
"Calling this function with an empty argument list is invalid in Scala 3. Use the form without parentheses instead",
"Chisel 3.5"
)
def orR: Bool = redop(OrReduceOp)
def andR: Bool = redop(AndReduceOp)
def xorR: Bool = redop(XorReduceOp)
override def <(that: UInt): Bool =
compop(LessOp, that)
override def >(that: UInt): Bool =
compop(GreaterOp, that)
override def <=(that: UInt): Bool =
compop(LessEqOp, that)
override def >=(that: UInt): Bool =
compop(GreaterEqOp, that)
/** Dynamic not equals operator
*
* @param that a hardware $coll
* @return a hardware [[Bool]] asserted if this $coll is not equal to `that`
* @group Comparison
*/
/** Dynamic equals operator
*
* @param that a hardware $coll
* @return a hardware [[Bool]] asserted if this $coll is equal to `that`
* @group Comparison
*/
def =/=(that: UInt): Bool =
compop(NotEqualOp, that)
def ===(that: UInt): Bool =
compop(EqualOp, that)
/** Unary not
*
* @return a hardware [[Bool]] asserted if this $coll equals zero
* @group Bitwise
*/
@deprecated(
"Calling this function with an empty argument list is invalid in Scala 3. Use the form without parentheses instead",
"Chisel 3.5"
)
def unary_! : Bool = this === 0.U(1.W)
override def <<(that: Int): UInt =
binop(UInt(this.width + that), ShiftLeftOp, validateShiftAmount(that))
override def <<(that: BigInt): UInt =
this << castToInt(that, "Shift amount")
override def <<(that: UInt): UInt =
binop(UInt(this.width.dynamicShiftLeft(that.width)), DynamicShiftLeftOp, that)
override def >>(that: Int): UInt =
binop(UInt(this.width.shiftRight(that)), ShiftRightOp, validateShiftAmount(that))
override def >>(that: BigInt): UInt =
this >> castToInt(that, "Shift amount")
override def >>(that: UInt): UInt =
binop(UInt(this.width), DynamicShiftRightOp, that)
/**
* Circular shift to the left
* @param that number of bits to rotate
* @return UInt of same width rotated left n bits
*/
def rotateLeft(n: Int): UInt = width match {
case _ if (n == 0) => this
case KnownWidth(w) if (w <= 1) => this
case KnownWidth(w) if n >= w => rotateLeft(n % w)
case _ if (n < 0) => rotateRight(-n)
case _ => tail(n) ## head(n)
}
/**
* Circular shift to the right
* @param that number of bits to rotate
* @return UInt of same width rotated right n bits
*/
def rotateRight(n: Int): UInt = width match {
case _ if (n <= 0) => rotateLeft(-n)
case KnownWidth(w) if (w <= 1) => this
case KnownWidth(w) if n >= w => rotateRight(n % w)
case _ => this(n - 1, 0) ## (this >> n)
}
// private def dynamicShift(
// n: UInt,
// staticShift: (UInt, Int) => UInt
// ): UInt =
// n.asBools().zipWithIndex.foldLeft(this) {
// case (in, (en, sh)) => Mux(en, staticShift(in, 1 << sh), in)
// }
// def rotateRight(n: UInt): UInt =
// dynamicShift(n, _ rotateRight _)
// def rotateLeft(n: UInt): UInt =
// dynamicShift(n, _ rotateLeft _)
/** Conditionally set or clear a bit
*
* @param off a dynamic offset
* @param dat set if true, clear if false
* @return a hrdware $coll with bit `off` set or cleared based on the value of `dat`
* $unchangedWidth
*/
// def bitSet(off: UInt, dat: Bool): UInt = {
// val bit = 1.U(1.W) << off
// Mux(dat, this | bit, ~(~(this()) | bit))
// }
// TODO: this eventually will be renamed as toSInt, once the existing toSInt
// completes its deprecation phase.
/** Zero extend as [[SInt]]
*
* @return an [[SInt]] equal to this $coll with an additional zero in its most significant bit
* @note The width of the returned [[SInt]] is `width of this` + `1`.
*/
@deprecated(
"Calling this function with an empty argument list is invalid in Scala 3. Use the form without parentheses instead",
"Chisel 3.5"
)
def zext: SInt =
pushOp(DefPrim(SInt(width + 1), ConvertOp, ref))
override def asSInt: SInt =
pushOp(DefPrim(SInt(width), AsSIntOp, ref))
override def asUInt: UInt = this
private[chisel3] override def connectFromBits(
that: Bits
): Unit = {
this := that.asUInt
}
private def subtractAsSInt(that: UInt): SInt =
binop(SInt((this.width.max(that.width)) + 1), SubOp, that)
}
object UInt {
def apply(): UInt = apply(Width())
/** Create a UInt port with specified width. */
def apply(width: Width): UInt = new UInt(width)
/** Create a UInt literal with specified width. */
protected[chisel3] def Lit(value: BigInt, width: Width): UInt = {
val lit = ULit(value, width)
val result = new UInt(lit.width)
// Bind result to being an Literal
lit.bindLitArg(result)
}
}
/** A data type for signed integers, represented as a binary bitvector. Defines arithmetic operations between other
* integer types.
*
* @define coll [[SInt]]
* @define numType $coll
* @define expandingWidth @note The width of the returned $coll is `width of this` + `1`.
* @define constantWidth @note The width of the returned $coll is unchanged, i.e., `width of this`.
*/
sealed class SInt private[chisel3] (width: Width) extends Bits(width) with Num[SInt] {
override def toString: String = {
litOption match {
case Some(value) => s"SInt$width($value)"
case _ => stringAccessor(s"SInt$width")
}
}
private[chisel3] override def typeEquivalent(that: Data): Boolean =
this.getClass == that.getClass && this.width == that.width // TODO: should this be true for unspecified widths?
private[chisel3] override def cloneTypeWidth(w: Width): this.type =
new SInt(w).asInstanceOf[this.type]
/** Unary negation (constant width)
*
* @return a hardware $coll equal to zero minus this $coll
* $constantWidth
* @group Arithmetic
*/
@deprecated(
"Calling this function with an empty argument list is invalid in Scala 3. Use the form without parentheses instead",
"Chisel 3.5"
)
/** Unary negation (constant width)
*
* @return a hardware $coll equal to zero minus `this` shifted right by one
* $constantWidth
* @group Arithmetic
*/
@deprecated(
"Calling this function with an empty argument list is invalid in Scala 3. Use the form without parentheses instead",
"Chisel 3.5"
)
def unary_- : SInt = 0.S - this
def unary_-% : SInt = 0.S -% this
/** add (default - no growth) operator */
override def +(that: SInt): SInt =
this +% that
/** subtract (default - no growth) operator */
override def -(that: SInt): SInt =
this -% that
override def *(that: SInt): SInt =
binop(SInt(this.width + that.width), TimesOp, that)
override def /(that: SInt): SInt =
binop(SInt(this.width + 1), DivideOp, that)
override def %(that: SInt): SInt =
binop(SInt(this.width.min(that.width)), RemOp, that)
/** Multiplication operator
*
* @param that a hardware $coll
* @return the product of this $coll and `that`
* $sumWidth
* $singleCycleMul
* @group Arithmetic
*/
def *(that: UInt): SInt = {
val thatToSInt = that.zext
val result = binop(SInt(this.width + thatToSInt.width), TimesOp, thatToSInt)
result.tail(1).asSInt
}
/** Addition operator (expanding width)
*
* @param that a hardware $coll
* @return the sum of this $coll and `that`
* $maxWidthPlusOne
* @group Arithmetic
*/
/** Addition operator (constant width)
*
* @param that a hardware $coll
* @return the sum of this $coll and `that` shifted right by one
* $maxWidth
* @group Arithmetic
*/
/** Subtraction operator (increasing width)
*
* @param that a hardware $coll
* @return the difference of this $coll less `that`
* $maxWidthPlusOne
* @group Arithmetic
*/
/** Subtraction operator (constant width)
*
* @param that a hardware $coll
* @return the difference of this $coll less `that` shifted right by one
* $maxWidth
* @group Arithmetic
*/
def +&(that: SInt): SInt =
binop(SInt((this.width.max(that.width)) + 1), AddOp, that)
def +%(that: SInt): SInt =
(this +& that).tail(1).asSInt
def -&(that: SInt): SInt =
binop(SInt((this.width.max(that.width)) + 1), SubOp, that)
def -%(that: SInt): SInt =
(this -& that).tail(1).asSInt
/** Bitwise and operator
*
* @param that a hardware $coll
* @return the bitwise and of this $coll and `that`
* $maxWidth
* @group Bitwise
*/
/** Bitwise or operator
*
* @param that a hardware $coll
* @return the bitwise or of this $coll and `that`
* $maxWidth
* @group Bitwise
*/
/** Bitwise exclusive or (xor) operator
*
* @param that a hardware $coll
* @return the bitwise xor of this $coll and `that`
* $maxWidth
* @group Bitwise
*/
def &(that: SInt): SInt =
binop(UInt(this.width.max(that.width)), BitAndOp, that).asSInt
def |(that: SInt): SInt =
binop(UInt(this.width.max(that.width)), BitOrOp, that).asSInt
def ^(that: SInt): SInt =
binop(UInt(this.width.max(that.width)), BitXorOp, that).asSInt
def unary_~ : SInt =
unop(UInt(width = width), BitNotOp).asSInt
override def <(that: SInt): Bool =
compop(LessOp, that)
override def >(that: SInt): Bool =
compop(GreaterOp, that)
override def <=(that: SInt): Bool =
compop(LessEqOp, that)
override def >=(that: SInt): Bool =
compop(GreaterEqOp, that)
/** Dynamic not equals operator
*
* @param that a hardware $coll
* @return a hardware [[Bool]] asserted if this $coll is not equal to `that`
* @group Comparison
*/
/** Dynamic equals operator
*
* @param that a hardware $coll
* @return a hardware [[Bool]] asserted if this $coll is equal to `that`
* @group Comparison
*/
def =/=(that: SInt): Bool =
compop(NotEqualOp, that)
def ===(that: SInt): Bool =
compop(EqualOp, that)
def abs: SInt = {
Mux(this < 0.S, -this, this)
}
override def <<(that: Int): SInt =
binop(SInt(this.width + that), ShiftLeftOp, validateShiftAmount(that))
override def <<(that: BigInt): SInt =
this << castToInt(that, "Shift amount")
override def <<(that: UInt): SInt =
binop(SInt(this.width.dynamicShiftLeft(that.width)), DynamicShiftLeftOp, that)
override def >>(that: Int): SInt =
binop(SInt(this.width.shiftRight(that)), ShiftRightOp, validateShiftAmount(that))
override def >>(that: BigInt): SInt =
this >> castToInt(that, "Shift amount")
override def >>(that: UInt): SInt =
binop(SInt(this.width), DynamicShiftRightOp, that)
override def asUInt: UInt = pushOp(
DefPrim(UInt(this.width), AsUIntOp, ref)
)
override def asSInt: SInt = this
private[chisel3] override def connectFromBits(
that: Bits
) = {
this := that.asSInt
}
}
object SInt {
/** Create an SInt type with inferred width. */
def apply(): SInt = apply(Width())
/** Create a SInt type or port with fixed width. */
def apply(width: Width): SInt = new SInt(width)
/** Create an SInt literal with specified width. */
protected[chisel3] def Lit(value: BigInt, width: Width): SInt = {
val lit = SLit(value, width)
val result = new SInt(lit.width)
lit.bindLitArg(result)
}
}
sealed trait Reset extends Element with ToBoolable {
/** Casts this $coll to an [[AsyncReset]] */
@deprecated(
"Calling this function with an empty argument list is invalid in Scala 3. Use the form without parentheses instead",
"Chisel 3.5"
)
def asAsyncReset: AsyncReset
}
object Reset {
def apply(): Reset = new ResetType
}
/** "Abstract" Reset Type inferred in FIRRTL to either [[AsyncReset]] or [[Bool]]
*
* @note This shares a common interface with [[AsyncReset]] and [[Bool]] but is not their actual
* super type due to Bool inheriting from abstract class UInt
*/
final class ResetType(private[chisel3] val width: Width = Width(1)) extends Element with Reset {
override def toString: String = stringAccessor("Reset")
def cloneType: this.type = Reset().asInstanceOf[this.type]
private[chisel3] def typeEquivalent(that: Data): Boolean =
this.getClass == that.getClass
override def litOption = None
/** Not really supported */
def toPrintable: Printable = PString("Reset")
def asUInt: UInt = pushOp(
DefPrim(UInt(this.width), AsUIntOp, ref)
)
private[chisel3] override def connectFromBits(
that: Bits
): Unit = {
this := that
}
def asAsyncReset: AsyncReset =
pushOp(DefPrim(AsyncReset(), AsAsyncResetOp, ref))
def asBool: Bool =
pushOp(DefPrim(Bool(), AsUIntOp, ref))
def toBool: Bool = asBool
}
object AsyncReset {
def apply(): AsyncReset = new AsyncReset
}
/** Data type representing asynchronous reset signals
*
* These signals are similar to [[Clock]]s in that they must be glitch-free for proper circuit
* operation. [[Reg]]s defined with the implicit reset being an [[AsyncReset]] will be
* asychronously reset registers.
*/
sealed class AsyncReset(private[chisel3] val width: Width = Width(1)) extends Element with Reset {
override def toString: String = stringAccessor("AsyncReset")
def cloneType: this.type = AsyncReset().asInstanceOf[this.type]
private[chisel3] def typeEquivalent(that: Data): Boolean =
this.getClass == that.getClass
override def litOption = None
/** Not really supported */
def toPrintable: Printable = PString("AsyncReset")
override def asUInt: UInt = pushOp(
DefPrim(UInt(this.width), AsUIntOp, ref)
)
// TODO Is this right?
private[chisel3] override def connectFromBits(
that: Bits
): Unit = {
this := that.asBool.asAsyncReset
}
def asAsyncReset: AsyncReset = this
def asBool: Bool =
pushOp(DefPrim(Bool(), AsUIntOp, ref))
def toBool: Bool = asBool
}
// REVIEW TODO: Why does this extend UInt and not Bits? Does defining airth
// operations on a Bool make sense?
/** A data type for booleans, defined as a single bit indicating true or false.
*
* @define coll [[Bool]]
* @define numType $coll
*/
sealed class Bool() extends UInt(1.W) with Reset {
override def toString: String = {
litToBooleanOption match {
case Some(value) => s"Bool($value)"
case _ => stringAccessor("Bool")
}
}
private[chisel3] override def cloneTypeWidth(w: Width): this.type = {
require(!w.known || w.get == 1)
new Bool().asInstanceOf[this.type]
}
/** Convert to a [[scala.Option]] of [[scala.Boolean]] */
def litToBooleanOption: Option[Boolean] = litOption.map {
case intVal if intVal == 1 => true
case intVal if intVal == 0 => false
case intVal => throwException(s"Boolean with unexpected literal value $intVal")
}
/** Convert to a [[scala.Boolean]] */
def litToBoolean: Boolean = litToBooleanOption.get
// REVIEW TODO: Why does this need to exist and have different conventions
// than Bits?
/** Bitwise and operator
*
* @param that a hardware $coll
* @return the bitwise and of this $coll and `that`
* @group Bitwise
*/
/** Bitwise or operator
*
* @param that a hardware $coll
* @return the bitwise or of this $coll and `that`
* @group Bitwise
*/
/** Bitwise exclusive or (xor) operator
*
* @param that a hardware $coll
* @return the bitwise xor of this $coll and `that`
* @group Bitwise
*/
def &(that: Bool): Bool =
binop(Bool(), BitAndOp, that)
def |(that: Bool): Bool =
binop(Bool(), BitOrOp, that)
def ^(that: Bool): Bool =
binop(Bool(), BitXorOp, that)
override def unary_~ : Bool =
unop(Bool(), BitNotOp)
/** Logical or operator
*
* @param that a hardware $coll
* @return the logical or of this $coll and `that`
* @note this is equivalent to [[Bool!.|(that:chisel3\.Bool)* Bool.|)]]
* @group Logical
*/
def ||(that: Bool): Bool = this | that
/** Logical and operator
*
* @param that a hardware $coll
* @return the logical and of this $coll and `that`
* @note this is equivalent to [[Bool!.&(that:chisel3\.Bool)* Bool.&]]
* @group Logical
*/
def &&(that: Bool): Bool = this & that
/** Reinterprets this $coll as a clock */
@deprecated(
"Calling this function with an empty argument list is invalid in Scala 3. Use the form without parentheses instead",
"Chisel 3.5"
)
def asClock: Clock = pushOp(
DefPrim(Clock(), AsClockOp, ref)
)
def asAsyncReset: AsyncReset =
pushOp(DefPrim(AsyncReset(), AsAsyncResetOp, ref))
}
object Bool {
def Lit(x: Boolean): Bool = {
val result = new Bool()
val lit = ULit(if (x) 1 else 0, Width(1))
lit.bindLitArg(result)
}
def apply(): Bool = new Bool()
}
package experimental {
import chisel3.internal.firrtl.BinaryPoint
/** Chisel types that have binary points support retrieving
* literal values as `Double` or `BigDecimal`
*/
trait HasBinaryPoint { self: Bits =>
def binaryPoint: BinaryPoint
/** Return the [[Double]] value of this instance if it is a Literal
* @note this method may throw an exception if the literal value won't fit in a Double
*/
def litToDoubleOption: Option[Double] = {
litOption match {
case Some(bigInt: BigInt) =>
Some(Num.toDouble(bigInt, binaryPoint))
case _ => None
}
}
/** Return the double value of this instance assuming it is a literal (convenience method)
*/
def litToDouble: Double = litToDoubleOption.get
/** Return the [[BigDecimal]] value of this instance if it is a Literal
* @note this method may throw an exception if the literal value won't fit in a BigDecimal
*/
def litToBigDecimalOption: Option[BigDecimal] = {
litOption match {
case Some(bigInt: BigInt) =>
Some(Num.toBigDecimal(bigInt, binaryPoint))
case _ => None
}
}
/** Return the [[BigDecimal]] value of this instance assuming it is a literal (convenience method)
* @return
*/
def litToBigDecimal: BigDecimal = litToBigDecimalOption.get
}
}
|