summaryrefslogtreecommitdiff
path: root/src/main/scala/Chisel/When.scala
diff options
context:
space:
mode:
authorducky2015-10-26 18:08:47 -0700
committerducky2015-10-26 18:08:47 -0700
commit4b51975ec7a543e165660d654fa84eaa9b9b3b3e (patch)
tree40db3dbf3f7cbb4f1a7753840ddac72fee52fee2 /src/main/scala/Chisel/When.scala
parent9430600381d52b10a6f5aad7140f355c3abf963c (diff)
Break Core.scala into bite-sized pieces
Diffstat (limited to 'src/main/scala/Chisel/When.scala')
-rw-r--r--src/main/scala/Chisel/When.scala52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/main/scala/Chisel/When.scala b/src/main/scala/Chisel/When.scala
new file mode 100644
index 00000000..a051e52c
--- /dev/null
+++ b/src/main/scala/Chisel/When.scala
@@ -0,0 +1,52 @@
+// See LICENSE for license details.
+
+package Chisel
+import Builder.pushCommand
+
+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)(block)
+ }
+}
+
+class WhenContext(cond: => Bool)(block: => Unit) {
+ /** This block of logic gets executed if above conditions have been false
+ * and this condition is true.
+ */
+ def elsewhen (cond: => Bool)(block: => Unit): WhenContext =
+ doOtherwise(when(cond)(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 =
+ doOtherwise(block)
+
+ pushCommand(WhenBegin(cond.ref))
+ block
+ pushCommand(WhenEnd())
+
+ private def doOtherwise[T](block: => T): T = {
+ pushCommand(WhenElse())
+ val res = block
+ pushCommand(WhenEnd())
+ res
+ }
+}