当前位置:网站首页>Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test
Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test
2022-07-04 14:09:00 【github-3rr0r】
Chisel The project build 、 Run and test ( Four )——Chisel Tested ChiselTest
Last article we introduced ScalaTest, It is Scala and Java Test tools . And now Chisel The latest standard test tool for modules is ChiselTest, It is based on ScalaTest Of , Allow us to use Chisel test . In order to use ChiselTest, We also need to build.sbt It contains chiseltest The library of :
libraryDependencies += "edu.berkeley.cs" %% "chiseltest" % "0.5.1" % "test"
Or so :
libraryDependencies ++= Seq(
"edu.berkeley.cs" %% "chisel3" % chiselVersion,
"edu.berkeley.cs" %% "chiseltest" % "0.5.1" % "test"
),
Chisel-template Of build.sbt It contains by default chiseltest Library , So we can not modify . This method includes ChiselTest Will automatically include the corresponding version ScalaTest, So what I said in the last article includes ScalaTest It's not necessary .
To use ChiselTest Words , We need to import the following packages :
import chisel3._
import chiseltest._
import org.scalatest.flatspec.AnyFlatSpec
Testing the circuit needs to include at least two parts , One is DUT( Parts to be tested ), The other is test logic , Also called testbench. Start the test and ScalaTest It's the same , function sbt test Just order , There is no need to have an object containing the main function as the program entry .
The following code is a simple DUT, It has two input ports and one output port , Are two unsigned number signals , The function realized is to input a and b perform Bitwise AND operation , Then output the results to out On :
class DeviceUnderTest extends Module {
val io = IO(new Bundle {
val a = Input(UInt(2.W))
val b = Input(UInt(2.W))
val out = Output(UInt(2.W))
})
io.out := io.a & io.b
}
Although the writing method of the module has not been introduced , But this code is also very easy to understand . Now let's give this DUT Of testbench, It's from AnyFlatSpec and ChiselScalatestTester Extended , Therefore, it has ChiselTest Functional ScalaTest. And call test() When the method is used , With the newly created DUT An example of is the parameter , Take the test code as the function number face quantity (function literal). The code is as follows :
class SimpleTest extends AnyFlatSpec with ChiselScalatestTester {
"DUT" should "pass" in {
test(new DeviceUnderTest) {
dut =>
dut.io.a.poke(0.U)
dut.io.b.poke(1.U)
dut.clock.step()
println("Result is: " + dut.io.out.peek().toString)
dut.io.a.poke(3.U)
dut.io.b.poke(2.U)
dut.clock.step()
println("Result is: " + dut.io.out.peek().toString)
}
}
}
DUT The input and output ports of the instance of pass through dut.io To visit . We can call poke To assign values to ports , It accepts the corresponding port Chisel Type value . Call on the output port peek The output of the port can be read out , The return value is also corresponding to the port Chisel Type value , Then call on this value toString You can convert it to a string , Then you can call println() Function output on the command line . Call in the test dut.clock.step() It can advance the clock by one cycle , So that the simulation can move forward . If you want to advance multiple cycles , We can give step() Provide a parameter .
function sbt test perhaps :
sbt "testOnly SimpleTest"
You can see the test results on the terminal :

It turns out that ,0 and 1 The result of bitwise and is 0,3 and 2 The result of bitwise and is 2. Of course, information has more than results , We can still see that toString Method also outputs the result Chisel type UInt<2>, That is, two unsigned integers .
It must be bad for us to check the output manually , But it's half done . To automatically complete the comparison with the expected results , We can call on the output port expect(value) Come on testbench The expected value is given in , The expected value is expect(value) Parameters of value. The modified test is as follows :
class SimpleTestExpect extends AnyFlatSpec with ChiselScalatestTester {
"DUT" should "pass" in {
test(new DeviceUnderTest) {
dut =>
dut.io.a.poke(0.U)
dut.io.b.poke(1.U)
dut.clock.step()
dut.io.out.expect(0.U)
dut.io.a.poke(3.U)
dut.io.b.poke(2.U)
dut.clock.step()
dut.io.out.expect(2.U)
}
}
}
Now run this test :
sbt "testOnly SimpleTestExpect"
This test will not output any results from hardware , It automatically compares the result with the expected value in the test , Output is as follows :

If the test fails , such as DUT perhaps testbench There are mistakes in it , The actual result is inconsistent with the expected value , Then generate an error message to describe the difference between the two . For example, we put testbench In the last line of dut.io.out.expect(2.U) Change to dut.io.out.expect(1.U), The output is like this :

A red error report , At the same time, it also gives the reasons why the test failed and the specific differences , This information is very useful when debugging , So testing is very important .
Conclusion
This article talks about how to use ChiselTest Do a simple test , You can see that it is very basic , It's also very convenient . However , The more essential content has not been reflected , At present, the test written has not yet played Scala The power of . for instance , In the above tests, we manually give the expected input and output values of the test , and Scala It can be used to write a reference model , That is, the golden model in another series , In this way, the test can be more automated 、 More comprehensive . But we won't talk about it for the time being , I have to finish talking about several test methods first , Next we will talk about testing with waveforms .
边栏推荐
- 博士申请 | 西湖大学学习与推理系统实验室招收博后/博士/研究实习等
- 苹果5G芯片研发失败:继续依赖高通,还要担心被起诉?
- Interview disassembly: how to check the soaring usage of CPU after the system goes online?
- 动画与过渡效果
- 英视睿达冲刺科创板:年营收4.5亿 拟募资9.79亿
- Understand chisel language thoroughly 03. Write to the developer of Verilog to chisel (you can also see it without Verilog Foundation)
- php 日志调试
- 分布式BASE理论
- 吃透Chisel语言.06.Chisel基础(三)——寄存器和计数器
- IP 实验室月复盘 · 第 5 期
猜你喜欢

吃透Chisel语言.10.Chisel项目构建、运行和测试(二)——Chisel中生成Verilog代码&Chisel开发流程

Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder

2022g3 boiler water treatment examination question simulation examination question bank and simulation examination

Test evaluation of software testing

国内酒店交易DDD应用与实践——代码篇

.Net之延迟队列
![30: Chapter 3: develop Passport Service: 13: develop [change / improve user information, interface]; (use * * * Bo class to accept parameters, and use parameter verification)](/img/89/aabf79ca02bf587e93885cadd5f98a.png)
30: Chapter 3: develop Passport Service: 13: develop [change / improve user information, interface]; (use * * * Bo class to accept parameters, and use parameter verification)

DGraph: 大规模动态图数据集

吃透Chisel语言.05.Chisel基础(二)——组合电路与运算符

MySQL 5 installation and modification free
随机推荐
2022kdd pre lecture | 11 first-class scholars take you to unlock excellent papers in advance
1200. Minimum absolute difference
好博医疗冲刺科创板:年营收2.6亿 万永钢和沈智群为实控人
Node の MongoDB安装
近日小结(非技术文)
2022年起重机械指挥考试模拟100题模拟考试平台操作
Summary of recent days (non-technical article)
程序员的焦虑
Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder
[C question set] of VII
Gorm 读写分离(转)
自主工业软件的创新与发展
程序员转方向
Unity shader learning (3) try to draw a circle
2022年山东省安全员C证考试题库及在线模拟考试
PHP log debugging
Programmer anxiety
MySQL 5 installation and modification free
Source code compilation and installation of MySQL
Hardware Basics - diode Basics