risc-v中文社区

 找回密码
 立即注册
查看: 1052|回复: 0

[讨论] Reg变量判断和赋值顺序

[复制链接]

347

主题

564

帖子

2237

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2237
发表于 2021-9-15 08:51:44 | 显示全部楼层 |阅读模式
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这二句互换位置,有没有影响???

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



Archiver|手机版|小黑屋|risc-v中文社区

GMT+8, 2024-4-30 16:33 , Processed in 0.017917 second(s), 17 queries .

risc-v中文社区论坛 官方网站

Copyright © 2018-2021, risc-v open source

快速回复 返回顶部 返回列表