When we should use “:=” not “=” in chisel3, same case is “when” and “if”

最后都变了- 提交于 2020-01-14 14:10:25

问题


Recently I am leraning chisel3, and I have below questions: When we should use " := " not " = " Same case is "when" and "if". Or could you kindly offer some general rules for these cases ?

Another question is about "Wire", what's rule it should be used or not when we declare one val ?

Thanks a lot! Bibo


回答1:


The fundamental difference here is certain operations are Scala operations and certain operations are Chisel operations. Scala operations are evaluated statically to build hardware generators. Chisel operations are used to describe how hardware components are connected and interact. For your two provided examples:

  • Scala operations
    • this = that does Scala assignment
    • if (condition) { body } is a standard conditional
  • Chisel operations
    • this := that connects one Chisel type to another Chisel type
    • when (condition) { body } is a hardware conditional

To concretize this, consider the following Chisel Module that mixes Scala operations and Chisel operations.

import chisel3._
/* param is a Scala Boolean that will change the internals of Foo */
class Foo(param: Boolean) extends Module {
  /* We do Scala assignment of a Chisel type (a Bundle) to value "io" */
  val io = IO(new Bundle{})

  /* We use a Scala conditional to change whether or not "x" will be 
   * a 1-bit wire or a 2-bit wire */
  val x = if (param) { Wire(UInt(1.W)) }
          else       { Wire(UInt(2.W)) }

  /* Finally, we do a Chisel assignment (hardware connection) of x 
   * to a literal 0 */
  x := 0.U
}

Behind the scenes, Chisel "operations" are really methods that are defined for Chisel types that use names that look familiar, like := is Chisel connect and === is Chisel hardware equality. These must be different from their underlying Scala operations, e.g., = is Scala assign and == is Scala equality.


A Wire is needed if you want to describe actual hardware that you can do hardware operations on. Same for a Reg. A bare Chisel type, like UInt will only generally be used in describing a Bundle or in some io. To do hardware connections/operations with this Chisel type, you will need to wrap it in a Reg() or a Wire().



来源:https://stackoverflow.com/questions/51644857/when-we-should-use-not-in-chisel3-same-case-is-when-and-if

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!