From d9903de3357336c4bd158a02318b9cafea7bdf46 Mon Sep 17 00:00:00 2001 From: Josh Bassett Date: Fri, 14 Aug 2020 06:37:44 +1000 Subject: Allow counters to be reset manually (#1527) Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>--- src/main/scala/chisel3/util/Counter.scala | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/main') 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) } } -- cgit v1.2.3