summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorAndrew Waterman2016-04-14 23:36:46 -0700
committerAndrew Waterman2016-04-14 23:36:46 -0700
commit09ce36e79ba5a999d0e47ec482bbf06857cf7043 (patch)
tree098e0cc5fa975508c2f31fe9d46ae1745051cb4c /src/main
parent8368c10da3442f5c74ca4c90f5442a1a97b57ea5 (diff)
Disallow Counters with negative n
One could make an argument for disallowing n=0, too, but HW generators will benefit from our leniency. Closes #107. Thanks @jackkoenig
Diffstat (limited to 'src/main')
-rw-r--r--src/main/scala/Chisel/util/Counter.scala9
1 files changed, 5 insertions, 4 deletions
diff --git a/src/main/scala/Chisel/util/Counter.scala b/src/main/scala/Chisel/util/Counter.scala
index 14f51ec4..3a9db309 100644
--- a/src/main/scala/Chisel/util/Counter.scala
+++ b/src/main/scala/Chisel/util/Counter.scala
@@ -7,20 +7,21 @@ package Chisel
* maximum output value of the counter), need not be a power of two
*/
class Counter(val n: Int) {
- val value = if (n == 1) UInt(0) else Reg(init=UInt(0, log2Up(n)))
+ 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) {
- Bool(true)
- } else {
+ 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)
}
}
}