summaryrefslogtreecommitdiff
path: root/core/src/main/scala/chisel3
diff options
context:
space:
mode:
authorVerneri Hirvonen2021-07-08 18:59:12 +0300
committerGitHub2021-07-08 15:59:12 +0000
commitbb520b8573328fda5f7b3c3892e6995fbe1b4239 (patch)
tree3c2280100553ddab64b23e9e41baa0c34ff7950a /core/src/main/scala/chisel3
parentf1e37900170124254f3cf4599a45e7a485c17a91 (diff)
Add `isOneOf` method to `ChiselEnum` (#1966)
* Add @ekiwi's code as a starting point * Add test for ChiselEnum isOneOf method * Make isOneOfTester naming consistent with other testers * Add scaladoc comments for isOneOf * Add isOneOf tests that use the method that takes variable number of args * Add guide level documentation example for isOneOf
Diffstat (limited to 'core/src/main/scala/chisel3')
-rw-r--r--core/src/main/scala/chisel3/StrongEnum.scala18
1 files changed, 18 insertions, 0 deletions
diff --git a/core/src/main/scala/chisel3/StrongEnum.scala b/core/src/main/scala/chisel3/StrongEnum.scala
index 1d0e04d3..b3d7cf7d 100644
--- a/core/src/main/scala/chisel3/StrongEnum.scala
+++ b/core/src/main/scala/chisel3/StrongEnum.scala
@@ -135,6 +135,24 @@ abstract class EnumType(private val factory: EnumFactory, selfAnnotating: Boolea
}
}
+ /** Test if this enumeration is equal to any of the values in a given sequence
+ *
+ * @param s a [[scala.collection.Seq$ Seq]] of enumeration values to look for
+ * @return a hardware [[Bool]] that indicates if this value matches any of the given values
+ */
+ final def isOneOf(s: Seq[EnumType])(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool = {
+ VecInit(s.map(this === _)).asUInt().orR()
+ }
+
+ /** Test if this enumeration is equal to any of the values given as arguments
+ *
+ * @param u1 the first value to look for
+ * @param u2 zero or more additional values to look for
+ * @return a hardware [[Bool]] that indicates if this value matches any of the given values
+ */
+ final def isOneOf(u1: EnumType, u2: EnumType*)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool
+ = isOneOf(u1 +: u2.toSeq)
+
def next(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): this.type = {
if (litOption.isDefined) {
val index = factory.all.indexOf(this)