目的
这次的目的是利用chisel test完成对硬件代码的测试。相当于Verilog写Testbench测试硬件代码的过程。因为chisel-bootcamp和chisel-book用的chisel test都过老,和直接clone下来的chisel-templete的chisel test不符,所以这里在下还是搞了很久…
主要参考:
https://github.com/ucb-bar/chisel-testers2
和chisel-templete中test中的gcd测试
流程
在之前的MyPacket.scala中添加AND硬件代码
class AND 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
}
在test下创建mypack对应的MyPacket.scala文件
其中代码如下所示:
package mypack
//表明是mypack的包
import chisel3._
import chisel3.tester._
//import chiseltest._
import org.scalatest._
import scala.util._
class ANDSpec extends FreeSpec with ChiselScalatestTester with Matchers{
"AND should " in {
test(new AND) { c =>
c.io.a.poke(0.U) // Set our input to value 0
c.io.b.poke(0.U) // Set our input to value 0
c.io.out.expect(0.U) // Assert that the output correctly has 0
c.io.a.poke(1.U) // Set our input to value 1
c.io.out.expect(0.U) // Assert that the output correctly has 1
c.io.b.poke(1.U) // Set our input to value 2
c.io.out.expect(1.U) // Assert that the output correctly has 2
}
}
}
其中 extends FreeSpec with ChiselScalatestTester with Matchers 这些是取自scala test的东西,chisel-test2似乎将之前的PeekPokeTester与Scala test合并,所以相对来说很像chisel-book中提到的scala test的测试方法。
这里要注意的是和bootcamp与book不同的是,这里的test一定要在前面写个“字符串” in {},在大括号里放置test才可以,据大佬说是scala test框架的锅。
然后在终端输入
sbt test
即可验证。若想单独验证,输入如下代码即可
sbt "testOnly mypack.ANDSpec"