summaryrefslogtreecommitdiff
path: root/src/main/scala
diff options
context:
space:
mode:
authorJack Koenig2020-08-11 10:28:15 -0700
committerGitHub2020-08-11 10:28:15 -0700
commit3668532fabc4ba4eaf70cf0ad1a55522aa33cdb3 (patch)
tree48a18bfa974e71845ec91140c32d0b0d4fcc73b4 /src/main/scala
parentf103ce0e468e106f4758f7b6a0088d909cc5c152 (diff)
Restore Counter.n API (#1546)
Includes special case support for Counter(0) which has identical behavior to Counter(1) except for the value of n.
Diffstat (limited to 'src/main/scala')
-rw-r--r--src/main/scala/chisel3/util/Counter.scala26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/main/scala/chisel3/util/Counter.scala b/src/main/scala/chisel3/util/Counter.scala
index 11daa515..fa1bb80c 100644
--- a/src/main/scala/chisel3/util/Counter.scala
+++ b/src/main/scala/chisel3/util/Counter.scala
@@ -19,20 +19,42 @@ import chisel3.internal.naming.chiselName // can't use chisel3_ version because
* ...
* }
* }}}
+ * @example {{{
+ * // Using Scala Range API
+ * val (counterValue, counterWrap) = Counter(0 until 10 by 2)
+ * when (counterValue === 4.U) {
+ * ...
+ * }
+ * }}}
*/
@chiselName
-class Counter private (r: Range) {
+class Counter private (r: Range, oldN: Option[Int] = None) {
require(r.length > 0, s"Counter range cannot be empty, got: $r")
require(r.start >= 0 && r.end >= 0, s"Counter range must be positive, got: $r")
private lazy val delta = math.abs(r.step)
private lazy val width = math.max(log2Up(r.last + 1), log2Up(r.head + 1))
+ /** Number of counts before the counter resets
+ *
+ * @note Only defined for ranges starting at zero with steps of size 1. Use [[range]] for other
+ * use cases.
+ */
+ def n: Int = oldN match {
+ case Some(x) => x
+ case None =>
+ // Reasonable for typical ranges
+ require(r.start == 0 && r.step == 1,
+ s"Counter.n only defined on ranges starting at 0 with step == 1, got $r. " +
+ "Use underlying range.")
+ r.last + 1
+ }
+
/** Creates a counter with the specified number of steps.
*
* @param n number of steps before the counter resets
*/
- def this(n: Int) { this(0 until math.max(1, n)) }
+ 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