summaryrefslogtreecommitdiff
path: root/src/main/scala/chisel3/util/Counter.scala
diff options
context:
space:
mode:
authorJim Lawson2016-09-23 16:50:39 -0700
committerJim Lawson2016-09-23 16:50:39 -0700
commit3e174cc55be350a06e6e95ac6505a77167bd5a29 (patch)
tree01813d93be83432a7c13fed6b1f56d9b9b942ca0 /src/main/scala/chisel3/util/Counter.scala
parent9c88d767e04ac25ab72380c39f30e39c83abf563 (diff)
parent785620b1403d827986bf60c2a001d8d6f71eed72 (diff)
Merge branch 'master' into gsdt
Diffstat (limited to 'src/main/scala/chisel3/util/Counter.scala')
-rw-r--r--src/main/scala/chisel3/util/Counter.scala25
1 files changed, 20 insertions, 5 deletions
diff --git a/src/main/scala/chisel3/util/Counter.scala b/src/main/scala/chisel3/util/Counter.scala
index 441f5c5b..5d9b8c3c 100644
--- a/src/main/scala/chisel3/util/Counter.scala
+++ b/src/main/scala/chisel3/util/Counter.scala
@@ -6,12 +6,14 @@ import chisel3._
import chisel3.Strict.CompileOptions
/** A counter module
+ *
* @param n number of counts before the counter resets (or one more than the
* maximum output value of the counter), need not be a power of two
*/
class Counter(val n: Int) {
require(n >= 0)
val value = if (n > 1) Reg(init=UInt(0, log2Up(n))) else UInt(0)
+
/** Increment the counter, returning whether the counter currently is at the
* maximum and will wrap. The incremented value is registered and will be
* visible on the next cycle.
@@ -30,14 +32,27 @@ class Counter(val n: Int) {
}
}
-/** Counter Object
- * Example Usage:
- * {{{ val countOn = Bool(true) // increment counter every clock cycle
- * val (myCounterValue, myCounterWrap) = Counter(countOn, n)
- * when ( myCounterValue === UInt(3) ) { ... } }}}*/
object Counter
{
+ /** Instantiate a [[Counter! counter]] with the specified number of counts.
+ */
def apply(n: Int): Counter = new Counter(n)
+
+ /** Instantiate a [[Counter! counter]] with the specified number of counts and a gate.
+ *
+ * @param cond condition that controls whether the counter increments this cycle
+ * @param n number of counts before the counter resets
+ * @return tuple of the counter value and whether the counter will wrap (the value is at
+ * maximum and the condition is true).
+ *
+ * @example {{{
+ * val countOn = Bool(true) // increment counter every clock cycle
+ * val (counterValue, counterWrap) = Counter(countOn, 4)
+ * when (counterValue === UInt(3)) {
+ * ...
+ * }
+ * }}}
+ */
def apply(cond: Bool, n: Int): (UInt, Bool) = {
val c = new Counter(n)
var wrap: Bool = null