summaryrefslogtreecommitdiff
path: root/src/main/scala/chisel3/util/BitPat.scala
diff options
context:
space:
mode:
authorJack Koenig2021-09-17 21:01:26 -0700
committerJack Koenig2021-09-17 21:01:26 -0700
commit5c8c19345e6711279594cf1f9ddab33623c8eba7 (patch)
treed9d6ced3934aa4a8be3dec19ddcefe50a7a93d5a /src/main/scala/chisel3/util/BitPat.scala
parente63b9667d89768e0ec6dc8a9153335cb48a213a7 (diff)
parent958904cb2f2f65d02b2ab3ec6d9ec2e06d04e482 (diff)
Merge branch 'master' into 3.5-release
Diffstat (limited to 'src/main/scala/chisel3/util/BitPat.scala')
-rw-r--r--src/main/scala/chisel3/util/BitPat.scala65
1 files changed, 52 insertions, 13 deletions
diff --git a/src/main/scala/chisel3/util/BitPat.scala b/src/main/scala/chisel3/util/BitPat.scala
index 40563e23..0dcb2466 100644
--- a/src/main/scala/chisel3/util/BitPat.scala
+++ b/src/main/scala/chisel3/util/BitPat.scala
@@ -23,6 +23,7 @@ object BitPat {
// If ? parsing is to be exposed, the return API needs further scrutiny
// (especially with things like mask polarity).
require(x.head == 'b', "BitPats must be in binary and be prefixed with 'b'")
+ require(x.length > 1, "BitPat width cannot be 0.")
var bits = BigInt(0)
var mask = BigInt(0)
var count = 0
@@ -57,6 +58,22 @@ object BitPat {
*/
def dontCare(width: Int): BitPat = BitPat("b" + ("?" * width))
+ /** Creates a [[BitPat]] of all 1 of the specified bitwidth.
+ *
+ * @example {{{
+ * val myY = BitPat.Y(4) // equivalent to BitPat("b1111")
+ * }}}
+ */
+ def Y(width: Int = 1): BitPat = BitPat("b" + ("1" * width))
+
+ /** Creates a [[BitPat]] of all 0 of the specified bitwidth.
+ *
+ * @example {{{
+ * val myN = BitPat.N(4) // equivalent to BitPat("b0000")
+ * }}}
+ */
+ def N(width: Int = 1): BitPat = BitPat("b" + ("0" * width))
+
/** Allows BitPats to be used where a UInt is expected.
*
* @note the BitPat must not have don't care bits (will error out otherwise)
@@ -91,12 +108,6 @@ object BitPat {
/** @group SourceInfoTransformMacro */
def do_=/= (that: BitPat)
(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool = that =/= x
-
- final def != (that: BitPat): Bool = macro SourceInfoTransform.thatArg
- @chiselRuntimeDeprecated
- @deprecated("Use '=/=', which avoids potential precedence problems", "3.0")
- def do_!= (that: BitPat)
- (implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool = that != x
}
}
@@ -111,8 +122,29 @@ object BitPat {
*/
sealed class BitPat(val value: BigInt, val mask: BigInt, width: Int) extends SourceInfoDoc {
def getWidth: Int = width
+ def apply(x: Int): BitPat = macro SourceInfoTransform.xArg
+ def apply(x: Int, y: Int): BitPat = macro SourceInfoTransform.xyArg
def === (that: UInt): Bool = macro SourceInfoTransform.thatArg
def =/= (that: UInt): Bool = macro SourceInfoTransform.thatArg
+ def ## (that: BitPat): BitPat = macro SourceInfoTransform.thatArg
+ override def equals(obj: Any): Boolean = {
+ obj match {
+ case y: BitPat => value == y.value && mask == y.mask && getWidth == y.getWidth
+ case _ => false
+ }
+ }
+
+ /** @group SourceInfoTransformMacro */
+ def do_apply(x: Int)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): BitPat = {
+ do_apply(x, x)
+ }
+
+ /** @group SourceInfoTransformMacro */
+ def do_apply(x: Int, y: Int)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): BitPat = {
+ require(width > x && y >= 0, s"Invalid bit range ($x, $y), index should be bounded by (${width - 1}, 0)")
+ require(x >= y, s"Invalid bit range ($x, $y), x should be greater or equal to y.")
+ BitPat(s"b${rawString.slice(width - x - 1, width - y)}")
+ }
/** @group SourceInfoTransformMacro */
def do_=== (that: UInt)
@@ -124,12 +156,19 @@ sealed class BitPat(val value: BigInt, val mask: BigInt, width: Int) extends Sou
(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool = {
!(this === that)
}
-
- def != (that: UInt): Bool = macro SourceInfoTransform.thatArg
- @chiselRuntimeDeprecated
- @deprecated("Use '=/=', which avoids potential precedence problems", "3.0")
- def do_!= (that: UInt)
- (implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool = {
- this =/= that
+ /** @group SourceInfoTransformMacro */
+ def do_##(that: BitPat)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): BitPat = {
+ new BitPat((value << that.getWidth) + that.value, (mask << that.getWidth) + that.mask, this.width + that.getWidth)
}
+
+ /** Generate raw string of a BitPat. */
+ def rawString: String = Seq.tabulate(width) { i =>
+ (value.testBit(width - i - 1), mask.testBit(width - i - 1)) match {
+ case (true, true) => "1"
+ case (false, true) => "0"
+ case (_, false) => "?"
+ }
+ }.mkString
+
+ override def toString = s"BitPat($rawString)"
}