Skip to content

Commit

Permalink
Require counter range to be non-empty
Browse files Browse the repository at this point in the history
  • Loading branch information
nullobject committed Jul 29, 2020
1 parent fd4fd06 commit 0fc6c93
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/main/scala/chisel3/util/Counter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ import chisel3.internal.naming.chiselName // can't use chisel3_ version because
*/
@chiselName
class Counter private (r: Range) {
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 val delta = math.abs(r.step)
private val width = math.max(log2Up(r.last + 1), log2Up(r.head + 1))
private lazy val delta = math.abs(r.step)
private lazy val width = math.max(log2Up(r.last + 1), log2Up(r.head + 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 n) }
def this(n: Int) { this(0 until math.max(1, n)) }

/** The current value of the counter. */
val value = if (r.length > 1) RegInit(r.head.U(width.W)) else r.head.U
Expand Down

0 comments on commit 0fc6c93

Please sign in to comment.