diff options
| author | Josh Bassett | 2020-07-30 15:11:33 +1000 |
|---|---|---|
| committer | GitHub | 2020-07-30 05:11:33 +0000 |
| commit | fca89f6c8544a1e0b42e1cec34207f74bf238e8a (patch) | |
| tree | b6b6ed8c10bb11364ffca807ee41241ca1dbc697 /src/test/scala/chiselTests | |
| parent | 164490c8fbf132ca65644d05d6ff8d0d7a3beb20 (diff) | |
Allow a counter to be instantiated using a Scala range (#1515)
* Add positive range generator
* Allow the Counter module to be instantiated with a Scala range
* Use head/last to determine counter width
Co-authored-by: Jack Koenig <jack.koenig3@gmail.com>
* Let counter overflow naturally when appropriate
We only need to explicitly wrap counters that don't start at zero, or end on a power of two. Otherwise we just let the counter overflow naturally to avoid wasting an extra mux.
* Require counter range to be non-empty
Co-authored-by: Jack Koenig <jack.koenig3@gmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Diffstat (limited to 'src/test/scala/chiselTests')
| -rw-r--r-- | src/test/scala/chiselTests/ChiselSpec.scala | 14 | ||||
| -rw-r--r-- | src/test/scala/chiselTests/Counter.scala | 19 |
2 files changed, 33 insertions, 0 deletions
diff --git a/src/test/scala/chiselTests/ChiselSpec.scala b/src/test/scala/chiselTests/ChiselSpec.scala index 7980e772..9518fb5c 100644 --- a/src/test/scala/chiselTests/ChiselSpec.scala +++ b/src/test/scala/chiselTests/ChiselSpec.scala @@ -150,6 +150,20 @@ class ChiselPropSpec extends PropSpec with ChiselRunners with ScalaCheckProperty // Generator for small positive integers. val smallPosInts = Gen.choose(1, 4) + // Generator for positive (ascending or descending) ranges. + def posRange: Gen[Range] = for { + dir <- Gen.oneOf(true, false) + step <- Gen.choose(1, 3) + m <- Gen.choose(1, 10) + n <- Gen.choose(1, 10) + } yield { + if (dir) { + Range(m, (m+n)*step, step) + } else { + Range((m+n)*step, m, -step) + } + } + // Generator for widths considered "safe". val safeUIntWidth = Gen.choose(1, 30) diff --git a/src/test/scala/chiselTests/Counter.scala b/src/test/scala/chiselTests/Counter.scala index 34fdec8e..31bfe7eb 100644 --- a/src/test/scala/chiselTests/Counter.scala +++ b/src/test/scala/chiselTests/Counter.scala @@ -35,6 +35,19 @@ class WrapTester(max: Int) extends BasicTester { } } +class RangeTester(r: Range) extends BasicTester { + val (cnt, wrap) = Counter(r) + val checkWrap = RegInit(false.B) + + when(checkWrap) { + assert(cnt === r.head.U) + stop() + }.elsewhen(wrap) { + assert(cnt === r.last.U) + checkWrap := true.B + } +} + class CounterSpec extends ChiselPropSpec { property("Counter should count up") { forAll(smallPosInts) { (max: Int) => assertTesterPasses{ new CountTester(max) } } @@ -47,4 +60,10 @@ class CounterSpec extends ChiselPropSpec { property("Counter should wrap") { forAll(smallPosInts) { (max: Int) => assertTesterPasses{ new WrapTester(max) } } } + + property("Counter should handle a range") { + forAll(posRange) { (r: Range) => + assertTesterPasses{ new RangeTester(r) } + } + } } |
