我想知道使用Mux语句与使用when- there语句之间是否有根本的区别?
例如:
when(read){
dataReg := inValue
}.otherwise{
dataReg := 0.U
}或
dataReg := Mux (read, inValue, 0.U)他们中的一个应该有什么偏好吗?
发布于 2020-12-04 00:47:20
您可以将when-otherwise看作是一个较高级别的构造,它被降为一个或多个Muxes。Mux对于简单的情况很有用(特别是当您希望代码适合1行时),但是有很多东西更便于用when-otherwise来表达。
例如,可以使用when-otherwise以Mux不允许的方便方式表示双向连接。
val producer_1 = IO(Flipped(Decoupled(UInt(8.W))))
val producer_2 = IO(Flipped(Decoupled(UInt(8.W))))
val select = IO(Input(Bool()))
val consumer = IO(Decoupled(UInt(8.W)))
when(select) {
consumer <> producer_1
producer_2.ready := false.B
} .otherwise {
consumer <> producer_2
producer_1.ready := false.B
}这就产生了:
assign producer_1_ready = select & consumer_ready;
assign producer_2_ready = select ? 1'h0 : consumer_ready;
assign consumer_valid = select ? producer_1_valid : producer_2_valid;
assign consumer_bits = select ? producer_1_bits : producer_2_bits;(可执行示例链接:https://scastie.scala-lang.org/GVH1zA2MTQ2fhm4yENijbg)
此外,when-otherwise还可用于同时连接多个事物:
val a = IO(Input(Bool()))
val b = IO(Input(Bool()))
val foo = IO(Input(UInt(8.W)))
val bar = IO(Input(UInt(8.W)))
val out = IO(Output(UInt(8.W)))
val reg1 = RegInit(0.U(8.W))
val reg2 = RegInit(0.U(8.W))
out := reg1
when (a) {
reg1 := foo
} .elsewhen (b) {
reg1 := bar
reg2 := foo
} .otherwise {
out := reg2
}(可执行示例链接:https://scastie.scala-lang.org/q9WNZVDoSpufRCyBeukCEg)
请注意,在不同的路径上都有到reg1、reg2和out的连接,甚至在每个路径中都没有一个连接。
https://stackoverflow.com/questions/65135886
复制相似问题