summaryrefslogtreecommitdiff
path: root/chiselFrontend/src/main/scala/Chisel/When.scala
diff options
context:
space:
mode:
authorducky2016-05-05 11:50:39 -0700
committerducky2016-05-05 11:50:39 -0700
commit9036d96bb032c19de31131f2296120e708cbc3dc (patch)
treecf17173fab309b09670ca7529680e09d61341451 /chiselFrontend/src/main/scala/Chisel/When.scala
parent623a301df1f5a1954f8e4a64ef97c99c3900da28 (diff)
Move Chisel API into separate chiselFrontend compilation unit in preparation for source locator macros
Diffstat (limited to 'chiselFrontend/src/main/scala/Chisel/When.scala')
-rw-r--r--chiselFrontend/src/main/scala/Chisel/When.scala56
1 files changed, 56 insertions, 0 deletions
diff --git a/chiselFrontend/src/main/scala/Chisel/When.scala b/chiselFrontend/src/main/scala/Chisel/When.scala
new file mode 100644
index 00000000..5f6b02c5
--- /dev/null
+++ b/chiselFrontend/src/main/scala/Chisel/When.scala
@@ -0,0 +1,56 @@
+// See LICENSE for license details.
+
+package Chisel
+
+import internal._
+import internal.Builder.pushCommand
+import internal.firrtl._
+
+object when { // scalastyle:ignore object.name
+ /** Create a `when` condition block, where whether a block of logic is
+ * executed or not depends on the conditional.
+ *
+ * @param cond condition to execute upon
+ * @param block logic that runs only if `cond` is true
+ *
+ * @example
+ * {{{
+ * when ( myData === UInt(3) ) {
+ * // Some logic to run when myData equals 3.
+ * } .elsewhen ( myData === UInt(1) ) {
+ * // Some logic to run when myData equals 1.
+ * } .otherwise {
+ * // Some logic to run when myData is neither 3 nor 1.
+ * }
+ * }}}
+ */
+ def apply(cond: Bool)(block: => Unit): WhenContext = {
+ new WhenContext(cond, !cond)(block)
+ }
+}
+
+/** Internal mechanism for generating a when. Because of the way FIRRTL
+ * commands are emitted, generating a FIRRTL elsewhen or nested whens inside
+ * elses would be difficult. Instead, this keeps track of the negative of the
+ * previous conditions, so when an elsewhen or otherwise is used, it checks
+ * that both the condition is true and all the previous conditions have been
+ * false.
+ */
+class WhenContext(cond: Bool, prevCond: => Bool)(block: => Unit) {
+ /** This block of logic gets executed if above conditions have been false
+ * and this condition is true.
+ */
+ def elsewhen (elseCond: Bool)(block: => Unit): WhenContext = {
+ new WhenContext(prevCond && elseCond, prevCond && !elseCond)(block)
+ }
+
+ /** This block of logic gets executed only if the above conditions were all
+ * false. No additional logic blocks may be appended past the `otherwise`.
+ */
+ def otherwise(block: => Unit): Unit =
+ new WhenContext(prevCond, null)(block)
+
+ pushCommand(WhenBegin(cond.ref))
+ block
+ pushCommand(WhenEnd())
+}