当前位置:网站首页>吃透Chisel语言.12.Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest
吃透Chisel语言.12.Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest
2022-07-04 12:49:00 【github-3rr0r】
Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest
上一篇文章我们介绍了ScalaTest,它是Scala和Java的测试工具。而现在Chisel模块最新的标准测试工具是ChiselTest,它是基于ScalaTest的,允许我们用于Chisel测试。为了使用ChiselTest,我们同样需要在build.sbt里面包含chiseltest的库:
libraryDependencies += "edu.berkeley.cs" %% "chiseltest" % "0.5.1" % "test"
或者这样:
libraryDependencies ++= Seq(
"edu.berkeley.cs" %% "chisel3" % chiselVersion,
"edu.berkeley.cs" %% "chiseltest" % "0.5.1" % "test"
),
Chisel-template的build.sbt里面是默认包含了chiseltest库的,所以我们可以不修改。用这种方法包含ChiselTest会自动包含对应版本的ScalaTest,所以上一篇文章中说的包含ScalaTest也是不需要的。
要使用ChiselTest的话,我们需要导入下面的这些包:
import chisel3._
import chiseltest._
import org.scalatest.flatspec.AnyFlatSpec
对电路进行测试需要包含至少两部分,一个是DUT(待测件),另一个是测试逻辑,也叫testbench。启动测试和ScalaTest是一样的,运行sbt test命令就行了,不需要有个包含主函数的对象作为程序入口。
下面的代码是一个简单的DUT,它有两个输入端口和一个输出端口,都是二位无符号数信号,实现的功能是对输入a和b执行按位与操作,然后把结果输出到out上:
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
}
虽然还没介绍模块的写法,但是这个代码也是很好懂的。现在我们给出这个DUT的testbench,它是从AnyFlatSpec和ChiselScalatestTester拓展来的,因此是具有ChiselTest功能的ScalaTest。而调用test()方法时,以新创建的DUT的一个实例为参数,以测试代码为函数字面量(function literal)。代码如下:
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的实例的输入输出端口通过dut.io来访问。我们可以通过在端口上调用poke来给端口赋值,它接受的是端口对应的Chisel类型的值。而在输出端口上调用peek可以把端口的输出给读取出来,返回值也是该端口对应的Chisel类型的值,再在该值上调用toString可以将其转换为字符串,进而可以调用println()函数在命令行输出。测试中调用dut.clock.step()可以让时钟前进一个周期,以此让仿真前进。要是想要前进多个周期,我们可以给step()提供一个参数。
运行sbt test或者:
sbt "testOnly SimpleTest"
就可以在终端看到测试的结果:

结果表明,0和1按位与的结果为0,3和2按位与的结果为2。当然信息不止有结果,我们还可以看到toString方法还输出了结果的Chisel类型UInt<2>,即二位无符号整数。
那我们人工核对输出结果肯定是不好的,但确实已经成功一半了。要想自动完成和预期结果的比较,我们可以通过在输出端口上调用expect(value)来在testbench中给出预期值,预期值就是expect(value)的参数value。修改之后的测试如下:
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)
}
}
}
现在运行这个测试:
sbt "testOnly SimpleTestExpect"
这个测试不会输出任何来自硬件的结果,它自动在测试内完成了结果与预期值的比较,输出如下:

如果测试失败的话,比如DUT或者testbench里面有错误,导致实际结果和预期值不符,那就回生成一条错误信息来描述两者的差异。比如我们把testbench中最后一行的dut.io.out.expect(2.U)改成dut.io.out.expect(1.U),输出结果就是这样的:

一片红色的报错,同时也给出了测试未通过的理由和具体差异在哪里,在调试的时候这些信息非常有用,所以说测试很重要。
结语
这一篇文章讲了如何用ChiselTest做简单的测试,可以看到很基础,同时也很方便。然而,更精髓的内容还没有体现出来,目前写的测试还没有发挥Scala的强大能力。比如说,上面的测试里面我们都是手动给出测试的输入和输出预期值的,而Scala可以用于写一个参考模型,也就是在另一个系列中说的黄金模型,这样测试就可以更自动化、更全面了。不过我们暂时还不会展开讲,得先把几个测试方法都讲完先,下一篇我们就讲讲用波形进行测试。
边栏推荐
- 华昊中天冲刺科创板:年亏2.8亿拟募资15亿 贝达药业是股东
- 分布式BASE理论
- Secretary of Homeland Security of the United States: domestic violent extremism is one of the biggest terrorist threats facing the United States at present
- C语言程序设计
- E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
- 2022危险化学品经营单位主要负责人练习题及模拟考试
- 读取 Excel 表数据
- 程序员的焦虑
- CA: efficient coordinate attention mechanism for mobile terminals | CVPR 2021
- 免费、好用、强大的轻量级笔记软件评测:Drafts、Apple 备忘录、Flomo、Keep、FlowUs、Agenda、SideNote、Workflowy
猜你喜欢

如何在 2022 年为 Web 应用程序选择技术堆栈

【Antd踩坑】Antd Form 配合Input.Group时出现Form.Item所占据的高度不对

Summary of recent days (non-technical article)

高质量软件架构的唯一核心指标

30:第三章:开发通行证服务:13:开发【更改/完善用户信息,接口】;(使用***BO类承接参数,并使用了参数校验)

OpenHarmony应用开发之如何创建DAYU200预览器

OPPO Find N2产品形态首曝:补齐各项短板

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

Redis - how to install redis and configuration (how to quickly install redis on ubuntu18.04 and centos7.6 Linux systems)

Flet tutorial 03 basic introduction to filledbutton (tutorial includes source code) (tutorial includes source code)
随机推荐
CVPR 2022 | 大幅减少零样本学习所需的人工标注,提出富含视觉信息的类别语义嵌入(源代码下载)...
remount of the / superblock failed: Permission denied
[C question set] of VII
華昊中天沖刺科創板:年虧2.8億擬募資15億 貝達藥業是股東
Introduction to XML III
2022 hoisting machinery command examination simulation 100 questions simulation examination platform operation
golang fmt.printf()(转)
Five "potential errors" in embedded programming
205. 同构字符串
博士申请 | 西湖大学学习与推理系统实验室招收博后/博士/研究实习等
Optional values and functions of the itemized contenttype parameter in the request header
2022g3 boiler water treatment examination question simulation examination question bank and simulation examination
C foundation in-depth study I
js中的变量提升和函数提升
C语言程序设计选题参考
C language dormitory management query software
XML入门二
动画与过渡效果
高质量软件架构的唯一核心指标
Gorm 读写分离(转)