diff options
| author | Jack | 2022-01-12 04:27:19 +0000 |
|---|---|---|
| committer | Jack | 2022-01-12 04:27:19 +0000 |
| commit | 29df513e348cc809876893f650af8180f0190496 (patch) | |
| tree | 06daaea954b4e5af7113f06e4bdbb78b33515cb3 /src/test/scala/examples/VendingMachineGenerator.scala | |
| parent | 5242ce90659decb9058ee75db56e5c188029fbf9 (diff) | |
| parent | 747d16311bdf185d2e98e452b14cb5d8ccca004c (diff) | |
Merge branch 'master' into 3.5-release
Diffstat (limited to 'src/test/scala/examples/VendingMachineGenerator.scala')
| -rw-r--r-- | src/test/scala/examples/VendingMachineGenerator.scala | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/src/test/scala/examples/VendingMachineGenerator.scala b/src/test/scala/examples/VendingMachineGenerator.scala index 72bfdf53..4adae987 100644 --- a/src/test/scala/examples/VendingMachineGenerator.scala +++ b/src/test/scala/examples/VendingMachineGenerator.scala @@ -12,12 +12,14 @@ import VendingMachineUtils._ class VendingMachineIO(val legalCoins: Seq[Coin]) extends Bundle { require(legalCoins.size >= 1, "The vending machine must accept at least 1 coin!") // Order of coins by value - val coins: Seq[Coin] = legalCoins sortBy (_.value) + val coins: Seq[Coin] = legalCoins.sortBy(_.value) // Map of coin names to their relative position in value (ie. index in inputs) val indexMap: Map[String, Int] = coins.map(_.name).zipWithIndex.toMap - require(coins map (_.value % coins.head.value == 0) reduce (_ && _), - "All coins must be a multiple of the lowest value coin!") + require( + coins.map(_.value % coins.head.value == 0).reduce(_ && _), + "All coins must be a multiple of the lowest value coin!" + ) val inputs = Input(Vec(legalCoins.size, Bool())) val dispense = Output(Bool()) @@ -34,17 +36,17 @@ abstract class ParameterizedVendingMachine(legalCoins: Seq[Coin], val sodaCost: // Enforce one hot if (io.inputs.size > 1) { for (input <- io.inputs) { - when (input) { - assert(io.inputs.filterNot(_ == input).map(!_).reduce(_ && _), - "Only 1 coin can be input in a given cycle!") + when(input) { + assert(io.inputs.filterNot(_ == input).map(!_).reduce(_ && _), "Only 1 coin can be input in a given cycle!") } } } } class VendingMachineGenerator( - legalCoins: Seq[Coin], - sodaCost: Int) extends ParameterizedVendingMachine(legalCoins, sodaCost) { + legalCoins: Seq[Coin], + sodaCost: Int) + extends ParameterizedVendingMachine(legalCoins, sodaCost) { require(sodaCost > 0, "Sodas must actually cost something!") // All coin values are normalized to a multiple of the minimum coin value @@ -57,21 +59,22 @@ class VendingMachineGenerator( val incValue = WireDefault(0.asUInt(width)) val doDispense = value >= (sodaCost / minCoin).U - when (doDispense) { + when(doDispense) { value := 0.U // No change given - } .otherwise { + }.otherwise { value := value + incValue } for ((coin, index) <- io.coins.zipWithIndex) { - when (io.inputs(index)) { incValue := (coin.value / minCoin).U } + when(io.inputs(index)) { incValue := (coin.value / minCoin).U } } io.dispense := doDispense } class ParameterizedVendingMachineTester( - mod: => ParameterizedVendingMachine, - testLength: Int) extends BasicTester { + mod: => ParameterizedVendingMachine, + testLength: Int) + extends BasicTester { require(testLength > 0, "Test length must be positive!") // Construct the module @@ -81,24 +84,24 @@ class ParameterizedVendingMachineTester( // Inputs and expected results // Do random testing private val _rand = scala.util.Random - val inputs: Seq[Option[Coin]] = Seq.fill(testLength)(coins.lift(_rand.nextInt(coins.size + 1))) + val inputs: Seq[Option[Coin]] = Seq.fill(testLength)(coins.lift(_rand.nextInt(coins.size + 1))) val expected: Seq[Boolean] = getExpectedResults(inputs, dut.sodaCost) - val inputVec: Vec[UInt] = VecInit(inputs map { + val inputVec: Vec[UInt] = VecInit(inputs.map { case Some(coin) => (1 << dut.io.indexMap(coin.name)).asUInt(coins.size.W) - case None => 0.asUInt(coins.size.W) + case None => 0.asUInt(coins.size.W) }) - val expectedVec: Vec[Bool] = VecInit(expected map (_.B)) + val expectedVec: Vec[Bool] = VecInit(expected.map(_.B)) val (idx, done) = Counter(true.B, testLength + 1) - when (done) { stop(); stop() } // Two stops for Verilator + when(done) { stop(); stop() } // Two stops for Verilator dut.io.inputs := inputVec(idx).asBools assert(dut.io.dispense === expectedVec(idx)) } class VendingMachineGeneratorSpec extends ChiselFlatSpec { - behavior of "The vending machine generator" + behavior.of("The vending machine generator") it should "generate a vending machine that accepts only nickels and dimes and costs $0.20" in { val coins = Seq(Nickel, Dime) |
