当前位置:网站首页>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 .
边栏推荐
猜你喜欢

MySQL 45 lecture - learn the actual combat notes of MySQL in Geek time 45 lecture - 06 | global lock and table lock_ Why are there so many obstacles in adding a field to the table

Huahao Zhongtian rushes to the scientific and Technological Innovation Board: the annual loss is 280million, and it is proposed to raise 1.5 billion. Beida pharmaceutical is a shareholder

Five "potential errors" in embedded programming

MySQL45讲——学习极客时间MySQL实战45讲笔记—— 06 | 全局锁和表锁_给表加个字段怎么有这么多阻碍

Unity shader learning (3) try to draw a circle

小程序直播 + 电商,想做新零售电商就用它吧!

1200. Minimum absolute difference

Summary of recent days (non-technical article)

面试官:Redis中哈希数据类型的内部实现方式是什么?

Redis —— How To Install Redis And Configuration(如何快速在 Ubuntu18.04 与 CentOS7.6 Linux 系统上安装 Redis)
随机推荐
xshell/bash/zsh 等终端鼠标滚轮乱码问题(转)
程序员的焦虑
Secretary of Homeland Security of the United States: domestic violent extremism is one of the biggest terrorist threats facing the United States at present
2022年山东省安全员C证考试题库及在线模拟考试
奇妙秘境 码蹄集
Dgraph: large scale dynamic graph dataset
苹果5G芯片研发失败:继续依赖高通,还要担心被起诉?
[antd step pit] antd form cooperates with input Form The height occupied by item is incorrect
OpenHarmony应用开发之如何创建DAYU200预览器
吃透Chisel语言.04.Chisel基础(一)——信号类型和常量
php 日志调试
IP 实验室月复盘 · 第 5 期
WS2818M是CPC8封装,是三通道LED驱动控制专用电路外置IC全彩双信号5V32灯可编程led灯带户外工程
Understand chisel language thoroughly 11. Chisel project construction, operation and test (III) -- scalatest of chisel test
【Antd】Antd 如何在 Form.Item 中有 Input.Gourp 时获取 Input.Gourp 的每一个 Input 的value
Getting started with the go language is simple: go implements the Caesar password
Apple 5g chip research and development failure: continue to rely on Qualcomm, but also worry about being prosecuted?
Node の MongoDB安装
源码编译安装MySQL
Understand chisel language thoroughly 03. Write to the developer of Verilog to chisel (you can also see it without Verilog Foundation)