Skip to content

Commit

Permalink
Adding saturating counter
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Lowe-Power <[email protected]>
  • Loading branch information
powerjg committed Feb 26, 2019
1 parent 4699e67 commit c00c1a4
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/test/scala/components/SaturatingCounterTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Unit tests for the saturating counter

package dinocpu

import chisel3._

import chisel3.iotesters
import chisel3.iotesters.{ChiselFlatSpec, Driver, PeekPokeTester}

class Wrapper(width: Int) extends Module {
val io = IO(new Bundle {
val incr = Input(Bool())
val decr = Input(Bool())

val value = Output(UInt(width.W))
val up = Output(Bool())
val down = Output(Bool())
})
io := DontCare

val counter = RegInit(SaturatingCounter.initup(width))

when (io.incr) {
counter := SaturatingCounter.incr(counter)
}

when (io.decr) {
//counter.decr()
}

io.value := counter
//io.up := counter.up()
//io.down := counter.down()
}

class SaturatingCounterUnitTester(c: Wrapper, width: Int) extends PeekPokeTester(c) {
val max = 1 << width
val d = 1 << (width - 1)
expect(c.io.value, d)

step(1)
val v = d + 1
expect(c.io.value, if (v < max) v else max)

}

/**
* This is a trivial example of how to run this Specification
* From within sbt use:
* {{{
* testOnly dinocpu.SaturatingCounterTester
* }}}
* From a terminal shell use:
* {{{
* sbt 'testOnly dinocpu.SaturatingCounterTester'
* }}}
*/
class SaturatingCounterTester extends ChiselFlatSpec {
"SaturatingCounter" should s"match expectations for width 2" in {
Driver(() => new Wrapper(2)) {
c => new SaturatingCounterUnitTester(c, 2)
} should be (true)
}
"SaturatingCounter" should s"match expectations for width 4" in {
Driver(() => new Wrapper(4)) {
c => new SaturatingCounterUnitTester(c, 4)
} should be (true)
}
}

0 comments on commit c00c1a4

Please sign in to comment.