diff options
| author | Jim Lawson | 2015-05-11 13:02:03 -0700 |
|---|---|---|
| committer | Jim Lawson | 2015-07-24 15:50:53 -0700 |
| commit | 2ae50411cbc5e2cd5fdc9ca4069b9c5f64919bc4 (patch) | |
| tree | a656e44d86a68a7c53b159fe6c74d328a126126d /src/test/scala/ChiselTests/VendingMachine.scala | |
| parent | b208bfb5691c7b5921dd47d0b599726872acd1cd (diff) | |
Incorporate chisel3-tests; update Makefile.
Diffstat (limited to 'src/test/scala/ChiselTests/VendingMachine.scala')
| -rw-r--r-- | src/test/scala/ChiselTests/VendingMachine.scala | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/test/scala/ChiselTests/VendingMachine.scala b/src/test/scala/ChiselTests/VendingMachine.scala new file mode 100644 index 00000000..787f7e33 --- /dev/null +++ b/src/test/scala/ChiselTests/VendingMachine.scala @@ -0,0 +1,55 @@ +package ChiselTests +import Chisel._ + +class VendingMachine extends Module { + val io = new Bundle { + val nickel = Bool(dir = INPUT) + val dime = Bool(dir = INPUT) + val valid = Bool(dir = OUTPUT) } + val c = Lit(5, 3){ UInt() } + val sIdle :: s5 :: s10 :: s15 :: sOk :: Nil = Enum(UInt(), 5) + val state = Reg(init = sIdle) + when (state === sIdle) { + when (io.nickel) { state := s5 } + when (io.dime) { state := s10 } + } + when (state === s5) { + when (io.nickel) { state := s10 } + when (io.dime) { state := s15 } + } + when (state === s10) { + when (io.nickel) { state := s15 } + when (io.dime) { state := sOk } + } + when (state === s15) { + when (io.nickel) { state := sOk } + when (io.dime) { state := sOk } + } + when (state === sOk) { + state := sIdle + } + io.valid := (state === sOk) +} + + +class VendingMachineTester(c: VendingMachine) extends Tester(c) { + var money = 0 + var isValid = false + for (t <- 0 until 20) { + val coin = rnd.nextInt(3)*5 + val isNickel = coin == 5 + val isDime = coin == 10 + + // Advance circuit + poke(c.io.nickel, int(isNickel)) + poke(c.io.dime, int(isDime)) + step(1) + + // Advance model + money = if (isValid) 0 else (money + coin) + isValid = money >= 20 + + // Compare + expect(c.io.valid, int(isValid)) + } +} |
