summaryrefslogtreecommitdiff
path: root/src/main/scala/Chisel/util/Conditional.scala
diff options
context:
space:
mode:
authorducky2015-10-26 17:30:15 -0700
committerPalmer Dabbelt2015-11-02 13:04:21 -0800
commit6c34949c45cf7ddfdabafb749de2cb9e439b381e (patch)
treeb4bcdd586123b571a2f53b6726e501003dd46119 /src/main/scala/Chisel/util/Conditional.scala
parent178f5c564e9ab0594656185e2e0a5bcc029d5743 (diff)
Break out Utils.scala into smaller portions
Diffstat (limited to 'src/main/scala/Chisel/util/Conditional.scala')
-rw-r--r--src/main/scala/Chisel/util/Conditional.scala69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/main/scala/Chisel/util/Conditional.scala b/src/main/scala/Chisel/util/Conditional.scala
new file mode 100644
index 00000000..b0ac4491
--- /dev/null
+++ b/src/main/scala/Chisel/util/Conditional.scala
@@ -0,0 +1,69 @@
+// See LICENSE for license details.
+
+/** Conditional blocks.
+ */
+
+package Chisel
+
+import scala.language.reflectiveCalls
+import scala.language.experimental.macros
+import scala.reflect.runtime.universe._
+import scala.reflect.macros.blackbox._
+
+/** This is identical to [[Chisel.when when]] with the condition inverted */
+object unless { // scalastyle:ignore object.name
+ def apply(c: Bool)(block: => Unit) {
+ when (!c) { block }
+ }
+}
+
+class SwitchContext[T <: Bits](cond: T) {
+ def is(v: Iterable[T])(block: => Unit) {
+ if (!v.isEmpty) when (v.map(_.asUInt === cond.asUInt).reduce(_||_)) { block }
+ }
+ def is(v: T)(block: => Unit) { is(Seq(v))(block) }
+ def is(v: T, vr: T*)(block: => Unit) { is(v :: vr.toList)(block) }
+}
+
+/** An object for separate cases in [[Chisel.switch switch]]
+ * It is equivalent to a [[Chisel.when$ when]] block comparing to the condition
+ * Use outside of a switch statement is illegal */
+object is { // scalastyle:ignore object.name
+ // Begin deprecation of non-type-parameterized is statements.
+ def apply(v: Iterable[Bits])(block: => Unit) {
+ Builder.error("The 'is' keyword may not be used outside of a switch.")
+ }
+
+ def apply(v: Bits)(block: => Unit) {
+ Builder.error("The 'is' keyword may not be used outside of a switch.")
+ }
+
+ def apply(v: Bits, vr: Bits*)(block: => Unit) {
+ Builder.error("The 'is' keyword may not be used outside of a switch.")
+ }
+}
+
+/** Conditional logic to form a switch block
+ * @example
+ * {{{ ... // default values here
+ * switch ( myState ) {
+ * is( state1 ) {
+ * ... // some logic here
+ * }
+ * is( state2 ) {
+ * ... // some logic here
+ * }
+ * } }}}*/
+object switch { // scalastyle:ignore object.name
+ def apply[T <: Bits](cond: T)(x: => Unit): Unit = macro impl
+ def impl(c: Context)(cond: c.Tree)(x: c.Tree): c.Tree = { import c.universe._
+ val sc = c.universe.internal.reificationSupport.freshTermName("sc")
+ def extractIsStatement(tree: Tree): List[c.universe.Tree] = tree match {
+ case q"Chisel.is.apply( ..$params )( ..$body )" => List(q"$sc.is( ..$params )( ..$body )")
+ case b => throw new Exception(s"Cannot include blocks that do not begin with is() in switch.")
+ }
+ val q"..$body" = x
+ val ises = body.flatMap(extractIsStatement(_))
+ q"""{ val $sc = new SwitchContext($cond); ..$ises }"""
+ }
+}