diff options
| author | ducky | 2016-01-16 18:03:11 -0800 |
|---|---|---|
| committer | ducky | 2016-01-16 18:03:11 -0800 |
| commit | ccb532af5fc9ae96c011a9efd852f5e153b12613 (patch) | |
| tree | 728d534192dc18480778697292f058587760456b | |
| parent | 2d7bf7a5fdb5ee722009d4816bb8aa355ead59fc (diff) | |
Add When test and fix when / elsewhen / otherwise behavior
| -rw-r--r-- | src/main/scala/Chisel/When.scala | 26 | ||||
| -rw-r--r-- | src/test/scala/chiselTests/When.scala | 60 |
2 files changed, 73 insertions, 13 deletions
diff --git a/src/main/scala/Chisel/When.scala b/src/main/scala/Chisel/When.scala index 7b8d60c2..506661b9 100644 --- a/src/main/scala/Chisel/When.scala +++ b/src/main/scala/Chisel/When.scala @@ -25,31 +25,31 @@ object when { // scalastyle:ignore object.name * }}} */ def apply(cond: => Bool)(block: => Unit): WhenContext = { - new WhenContext(cond)(block) + new WhenContext(cond, Bool(true))(block) } } -class WhenContext(cond: => Bool)(block: => Unit) { +/** 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 (cond: => Bool)(block: => Unit): WhenContext = - doOtherwise(when(cond)(block)) + def elsewhen (elseCond: => Bool)(block: => Unit): WhenContext = + new WhenContext(elseCond, prevCond && !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) + new WhenContext(Bool(true), prevCond && !cond)(block) - pushCommand(WhenBegin(cond.ref)) + pushCommand(WhenBegin((cond && prevCond).ref)) block pushCommand(WhenEnd()) - - private def doOtherwise[T](block: => T): T = { - pushCommand(WhenElse()) - val res = block - pushCommand(WhenEnd()) - res - } } diff --git a/src/test/scala/chiselTests/When.scala b/src/test/scala/chiselTests/When.scala new file mode 100644 index 00000000..b78e1c5a --- /dev/null +++ b/src/test/scala/chiselTests/When.scala @@ -0,0 +1,60 @@ +// See LICENSE for license details. + +package chiselTests + +import org.scalatest._ +import Chisel._ +import Chisel.testers.BasicTester + +class WhenTester() extends BasicTester { + val cnt = Counter(4) + when(Bool(true)) { cnt.inc() } + + val out = Wire(UInt(width=3)) + when(cnt.value === UInt(0)) { + out := UInt(1) + } .elsewhen (cnt.value === UInt(1)) { + out := UInt(2) + } .elsewhen (cnt.value === UInt(2)) { + out := UInt(3) + } .otherwise { + out := UInt(0) + } + + assert(out === cnt.value + UInt(1)) + + when(cnt.value === UInt(3)) { + stop() + } +} + +class OverlappedWhenTester() extends BasicTester { + val cnt = Counter(4) + when(Bool(true)) { cnt.inc() } + + val out = Wire(UInt(width=3)) + when(cnt.value <= UInt(0)) { + out := UInt(1) + } .elsewhen (cnt.value <= UInt(1)) { + out := UInt(2) + } .elsewhen (cnt.value <= UInt(2)) { + out := UInt(3) + } .otherwise { + out := UInt(0) + } + + assert(out === cnt.value + UInt(1)) + + when(cnt.value === UInt(3)) { + stop() + } +} + +class WhenSpec extends ChiselFlatSpec { + "When, elsewhen, and otherwise with orthogonal conditions" should "work" in { + assert(execute{ new WhenTester }) + } + "When, elsewhen, and otherwise with overlapped conditions" should "work" in { + assert(execute{ new OverlappedWhenTester }) + } +} |
