diff options
| author | Josh Bassett | 2020-08-14 06:37:44 +1000 |
|---|---|---|
| committer | GitHub | 2020-08-13 20:37:44 +0000 |
| commit | d9903de3357336c4bd158a02318b9cafea7bdf46 (patch) | |
| tree | fab01f11d33df0bb8ec3984736950367a0f49995 /src/main | |
| parent | a66d2d152897fa979aea07aa067c27ba455ca054 (diff) | |
Allow counters to be reset manually (#1527)
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/scala/chisel3/util/Counter.scala | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/src/main/scala/chisel3/util/Counter.scala b/src/main/scala/chisel3/util/Counter.scala index fa1bb80c..603ebeb6 100644 --- a/src/main/scala/chisel3/util/Counter.scala +++ b/src/main/scala/chisel3/util/Counter.scala @@ -57,7 +57,7 @@ class Counter private (r: Range, oldN: Option[Int] = None) { def this(n: Int) { this(0 until math.max(1, n), Some(n)) } /** The current value of the counter. */ - val value = if (r.length > 1) RegInit(r.head.U(width.W)) else r.head.U + val value = if (r.length > 1) RegInit(r.head.U(width.W)) else WireInit(r.head.U) /** The range of the counter values. */ def range: Range = r @@ -91,6 +91,11 @@ class Counter private (r: Range, oldN: Option[Int] = None) { true.B } } + + /** Resets the counter to its initial value */ + def reset(): Unit = { + value := r.head.U + } } object Counter @@ -118,14 +123,21 @@ object Counter * * @param r the range of counter values * @param enable controls whether the counter increments this cycle + * @param reset resets the counter to its initial value during this cycle * @return tuple of the counter value and whether the counter will wrap (the value is at * maximum and the condition is true). */ @chiselName - def apply(r: Range, enable: Bool = true.B): (UInt, Bool) = { + def apply(r: Range, enable: Bool = true.B, reset: Bool = false.B): (UInt, Bool) = { val c = new Counter(r) val wrap = WireInit(false.B) - when (enable) { wrap := c.inc() } + + when(reset) { + c.reset() + }.elsewhen(enable) { + wrap := c.inc() + } + (c.value, wrap) } } |
