joe 发表于 2021-9-15 08:51:44

Reg变量判断和赋值顺序

chisel中Counter的源码如下,
class Counter(val n: Int) {
require(n >= 0, s"Counter value must be nonnegative, got: $n")
val value = if (n > 1) RegInit(0.U(log2Ceil(n).W)) else 0.U

/** Increment the counter
    *
    * @note The incremented value is registered and will be visible on the next clock cycle
    * @return whether the counter will wrap to zero on the next cycle
    */
def inc(): Bool = {
    if (n > 1) {
      val wrap = value === (n-1).U
      value := value + 1.U
      if (!isPow2(n)) {
      when (wrap) { value := 0.U }
      }
      wrap
    } else {
      true.B
    }
}
}
提一个问题:如果将val wrap = value === (n-1).U和value := value + 1.U这二句互换位置,有没有影响???

页: [1]
查看完整版本: Reg变量判断和赋值顺序