Chisel3 - Tutorial - ByteSelector

https://mp.weixin.qq.com/s/RQg2ca1rwfVHx_QG-IOV-w

 
字节选择器。
 
参考链接:
https://github.com/ucb-bar/chisel-tutorial/blob/release/src/main/scala/examples/ByteSelector.scala
 
1. 引入Chisel3
 
 
2. 继承自Module类
 
 
3. 定义输入输出接口
 
创建各项输入输出接口。
 
这些接口都是无符号整型数:val in = Input(UInt(32.W))
a. 使用32.W表示位宽为32位;
b. 使用UInt创建无符号整型数;
c. 使用Input/Output表示接口方向;
d. val 关键字表明定义的变量是所属匿名Bundle子类的数据成员;
 
4. 内部连接
 
 
使用when/elsewhen/otherwise表达判断逻辑。
 
1) when()()可以理解为一个有三个参数列表的函数
 
函数签名如下,返回一个WhenContext对象:
 
Scala中,允许使用{}代替()来包围参数列表。所以when()()可以改成when(){}的形式。
 
最后一个参数列表忽略。
 
2) elsewhen()()
 
 
elsewhen/otherwise都是WhenContext的方法。所以可以直接调用。elsewhen如同when,接收两个参数列表。
 
3) otherwise()
 
最后一个判断分支。
 
5. 生成Verilog
 
 
可以直接点运行符号运行。
 
也可以使用sbt shell执行:
 
生成Verilog如下:
 
6. 测试
 
 
 
7. 附录
 
ByteSelector.scala:
import chisel3._
 
class ByteSelector extends Module {
val io = IO(new Bundle {
val in = Input(UInt(32.W))
val offset = Input(UInt(2.W))
val out = Output(UInt(8.W))
})
io.out := 0.U(8.W)
when (io.offset === 0.U(2.W)) {
io.out := io.in(7, 0)
}.elsewhen(io.offset === 1.U) {
io.out := io.in(15, 8)
}.elsewhen(io.offset === 2.U) {
io.out := io.in(23, 16)
}.otherwise {
io.out := io.in(31, 24)
}
}
 
object ByteSelectorMain {
def main(args: Array[String]): Unit = {
chisel3.Driver.execute(Array("--target-dir", "generated/ByteSelector"), () => new ByteSelector)
}
}

猜你喜欢

转载自www.cnblogs.com/wjcdx/p/10093502.html