From 09ce36e79ba5a999d0e47ec482bbf06857cf7043 Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Thu, 14 Apr 2016 23:36:46 -0700 Subject: 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 --- src/main/scala/Chisel/util/Counter.scala | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/main') 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) } } } -- cgit v1.2.3