学习chisel(2): chisel基础
1. 第一个模块
1 // Chisel Code: Declare a new module definition 2 class Passthrough extends Module { 3 val io = IO(new Bundle { 4 val in = Input(UInt(4.W)) 5 val out = Output(UInt(4.W)) 6 }) 7 io.out := io.in 8 }
我们一行一行看,
第一行 class Passthrough extends Module 。Module是一个内嵌的chisel类,所有的硬件模块都必须扩展它。
第二行 val io = IO(...) 我们会在io常量中声明我们的输入输出端口,需要这种类型
IO(_instantiated_bundle_)
.
new Bundle {
val in = Input(...)
val out = Output(...)
}
声明一个新的硬件结构体 Bundle(集束),包含一些被命名的信号 in 和 out ,它们的方向是 Input 和 Output
UInt(4.W)
我们声明一个信号的硬件类型。在这个例子中,这是一个无符号整数,宽度为4。
io.out := io.in
在这个模块中,我们把 io.out 和 io.in 连接。
Note that the `:=` operator is a ***Chisel*** operator that indicates that the right-hand signal drives the left-hand signal. It is a directioned operator.
hardware construction languages (HCLs)
接着我们可以使用scala把chisel模块翻译成verilog模块,这个过程叫elaboration(细化)
println(getVerilog(new Passthrough))
1 // Chisel Code, but pass in a parameter to set widths of ports 2 class PassthroughGenerator(width: Int) extends Module { 3 val io = IO(new Bundle { 4 val in = Input(UInt(width.W)) 5 val out = Output(UInt(width.W)) 6 }) 7 io.out := io.in 8 } 9 10 // Let's now generate modules with different widths 11 println(getVerilog(new PassthroughGenerator(10))) 12 println(getVerilog(new PassthroughGenerator(20)))
可以看到, chisel模块实际上就是用scala类实现的,可以通过参数来动态修改端口的大小
2. 组合逻辑
1 是scala整数,而1.U 是一根通电值为“1”的线(wire),这两者是不一样的。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库