问题
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 assignmentif (condition) { body }
is a standard conditional
- Chisel operations
this := that
connects one Chisel type to another Chisel typewhen (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