Chisel三
参考boot-camp2.1
一、导入库和当中包含的类
根据Chisel(一)配置好环境后建立scala文件,导入需要的chisel库中的类
-
import chisel3._
-
import chisel3.util._
-
import chisel3.tester._
-
import chisel3.tester.RawTester.test
-
import dotvisualizer._
二、建立第一个模块
-
/ Chisel Code: Declare a new module definition
-
class Passthrough extends Module {
-
val io = IO(new Bundle {
-
val in = Input(UInt(4.W))
-
val out = Output(UInt(4.W))
-
})
-
io.out := io.in
-
}
class Passthrough extends Module { --声明模块名称为Passthrough,Module是Chisel的内置类,所以所有的硬件模块都必须extends
val io = IO(...) -- 声明可输入输出端口,表现形式为IO(_instantiated_bundle_)
new Bundle { val in = Input(...) val out = Output(...) } --声明新的硬件结构类型Bundle,包含两个val型变量in,out,分别为输入、输出类型
UInt(4.W) -- 声明一个硬件类型,这是无符号整数,宽度为4
io.out := io.in --连接输入和输出端口,io.in驱动了io.out,:=是Chisel的操作符,意思是右边的信号驱动了左边的信号,这是有方向的操作符。
三、细化(elaborati)
HCLs中我们可以使用底层编程语言作为脚本语言,比如我们可以使用Scala来调用Chisel编译器编译Chisel的Passthrough模块为verilog类型,这个过程叫做细化(elaboration)
-
package mypack
-
//定义包
-
import chisel3._
-
import chisel3.util._
-
import chisel3.tester._
-
import chisel3.tester.RawTester.test
-
//调用chisel包
-
-
class PassthroughGenerator(width: Int) extends Module {
-
val io = IO(new Bundle {
-
val in = Input(UInt(width.W))
-
val out = Output(UInt(width.W))
-
})
-
io.out := io.in
-
}
-
object PassthroughGenerator extends App {
-
println(getVerilogString(new PassthroughGenerator(10)))
-
}
因为可以通过PassthroughGenerator生成定义位宽的模块,所以PassthroughGenerator被称为生成器,效果如下:
四、测试硬件模块
Chisel内置了测试模块,下面的测试模块
以下示例是一个 Chisel 测试工具,它将值传递给 Passthrough 的输入端口 in 的实例,并检查在输出端口 out 上是否看到相同的值。
-
import chisel3._
-
import chiseltest._
-
import org.scalatest.flatspec.AnyFlatSpec
-
import mypack._
-
-
-
class PassthroughTest extends AnyFlatSpec with ChiselScalatestTester {
-
behavior of "PassthroughGenerator"
-
it should "pass through bits" in {
-
test(new PassthroughGenerator(20)) { c =>
-
c.io.in.poke(0.U) // Set our input to value 0
-
c.io.out.expect(0.U) // Assert that the output correctly has 0
-
c.io.in.poke(10.U) // Set our input to value 10
-
c.io.out.expect(10.U) // Assert that the output correctly has 10
-
c.io.in.poke(20.U) // Set our input to value 20
-
c.io.out.expect(20.U) // Assert that the output correctly has 20
-
}
-
println("SUCCESS!!") // Scala Code: if we get here, our tests passed!
-
}
-
}
测试输出:
使用sbt test会将文件夹中所有的test编译
五、printf与println的区别
println 是一个内置的 Scala 函数,可以打印到控制台。它不能在电路仿真期间用于打印,因为生成的电路是 FIRRTL 或 Verilog,而不是 Scala。
-
package mypack
-
//定义包
-
import chisel3._
-
import chisel3.util._
-
import chisel3.tester._
-
import chisel3.tester.RawTester.test
-
//调用chisel包
-
-
class PassthroughGenerator(width: Int) extends Module {
-
val io = IO(new Bundle {
-
val in = Input(UInt(width.W))
-
val out = Output(UInt(width.W))
-
})
-
io.out := io.in
-
printf("Print during simulation: Input is %d\n", io.in)
-
// chisel printf has its own string interpolator too
-
printf(p"Print during simulation: IO is $io\n")
-
-
println(s"Print during generation: Input is ${io.in}")
-
}
-
object PassthroughGenerator extends App {
-
println(getVerilogString(new PassthroughGenerator(10)))
-
}
测试结果如下:
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgkgijh
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13