summaryrefslogtreecommitdiff
path: root/src/main/scala/chisel3/util/Counter.scala
diff options
context:
space:
mode:
authorJim Lawson2016-10-06 08:57:10 -0700
committerJim Lawson2016-10-06 08:57:10 -0700
commit82625071405672eb4a19363d6f73f359ac28a7f5 (patch)
treedee5beff0e7333fa86c1cdcdb79c0d111114b8c9 /src/main/scala/chisel3/util/Counter.scala
parentb7c6e0d1a2098b545938a5a8dfce2b1d9294532f (diff)
parent7de30c2b893a3f24d43f2e131557430eb64f6bc8 (diff)
Merge branch 'master' into tobits-deprecation
Diffstat (limited to 'src/main/scala/chisel3/util/Counter.scala')
-rw-r--r--src/main/scala/chisel3/util/Counter.scala62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/main/scala/chisel3/util/Counter.scala b/src/main/scala/chisel3/util/Counter.scala
new file mode 100644
index 00000000..ba66d667
--- /dev/null
+++ b/src/main/scala/chisel3/util/Counter.scala
@@ -0,0 +1,62 @@
+// See LICENSE for license details.
+
+package chisel3.util
+
+import chisel3._
+//import chisel3.core.ExplicitCompileOptions.Strict
+
+/** 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.
+ */
+ def inc(): Bool = {
+ if (n > 1) {
+ val wrap = value === UInt(n-1)
+ value := value + UInt(1)
+ if (!isPow2(n)) {
+ when (wrap) { value := UInt(0) }
+ }
+ wrap
+ } else {
+ Bool(true)
+ }
+ }
+}
+
+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
+ when (cond) { wrap = c.inc() }
+ (c.value, cond && wrap)
+ }
+}