chisel - when-otherwise 语句与 Mux 语句
问题描述
我想知道使用 Mux 语句与使用 when-otherwise 语句之间是否有任何根本区别?
例如:
when(read){
dataReg := inValue
}.otherwise{
dataReg := 0.U
}
或者
dataReg := Mux (read, inValue, 0.U)
是否应该对其中之一有任何偏好?
解决方案
您可以将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
推荐阅读
- angular7 - 如何更正 Angular 上的错误无法加载 http://localhost/api/notification/create.php:预检响应没有 HTTP ok 状态
- android - 以编程方式反应本机重新加载包
- grails - 如何通过 Grails 应用程序中的 Whatsapp 发送文本消息和图像
- javascript - 使用多个单选按钮验证多个选项卡
- android - Android - 具有相似 URL 的深度链接
- excel - 从 Outlook VBA 激活工作表
- java - Spring Boot 从 Java 启动 Zookeeper 和 Kafka Server
- elasticsearch - Elapsed Plugin 创建一个带有日志详细信息的新事件
- python-3.x - PermissionError:[Errno 13] 权限被拒绝:'./img/'
- android - java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“boolean android.view.WindowInsets.isConsumed()”