Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/scala/chisel3/util/BitPat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package chisel3.util

import scala.language.experimental.macros
import chisel3._
import chisel3.experimental.EnumType
import chisel3.internal.sourceinfo.{SourceInfo, SourceInfoTransform}


Expand Down Expand Up @@ -100,6 +101,11 @@ object BitPat {
apply("b" + x.litValue.toString(2).reverse.padTo(len, "0").reverse.mkString)
}

/**
* Allows [[ChiselEnum]] to be used where a BitPat is expected.
*/
def apply(x: EnumType): BitPat = apply(x.litValue.U((x.getWidth).W))

implicit class fromUIntToBitPatComparable(x: UInt) extends SourceInfoDoc {
import internal.sourceinfo.{SourceInfo, SourceInfoTransform}

Expand Down
11 changes: 11 additions & 0 deletions src/test/scala/chiselTests/util/BitPatSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
package chiselTests.util

import chisel3.util.BitPat
import chisel3.experimental.ChiselEnum
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

object Enum extends ChiselEnum {
val VAL1, VAL2, VAL3 = Value
}

class BitPatSpec extends AnyFlatSpec with Matchers {
behavior of classOf[BitPat].toString
Expand Down Expand Up @@ -49,4 +53,11 @@ class BitPatSpec extends AnyFlatSpec with Matchers {
b(4, 3) should be(BitPat("b01"))
b(6, 6) should be(BitPat("b1"))
}

it should "convert to BitPat from ChiselEnum" in {
val b = BitPat(Enum.VAL1)
val c = BitPat(Enum.VAL3)
b should be(BitPat("b00"))
c should be(BitPat("b10"))
}
}