Skip to content

Instantly share code, notes, and snippets.

@diningyo diningyo/Top.scala
Created Feb 23, 2019

Embed
What would you like to do?
ChiselのRTL生成&テストの実装サンプル
import chisel3._
import chisel3.iotesters
import chisel3.iotesters.PeekPokeTester
class Top(in0Bits: Int, in1Bits: Int) extends Module {
val io = IO(new Bundle {
val in0 = Input(UInt(in0Bits.W))
val in1 = Input(UInt(in0Bits.W))
val out = Output(UInt((in0Bits+1).W))
})
// 入力のビット幅は最大で32bit
require(in0Bits <= 32)
require(in1Bits <= 32)
io.out := RegNext(io.in0 +& io.in1)
}
object Elaborate extends App {
Driver.execute(args, () => new Top(32, 32))
}
class TopTester(c: Top, in0bits: Int, in1bits: Int) extends PeekPokeTester(c) {
import scala.util.Random
// データを入力
def feedData(in0: BigInt, in1: BigInt): Unit = {
poke(c.io.in0, in0)
poke(c.io.in1, in1)
}
def getData(): (BigInt, BigInt, BigInt) = {
val in0Mask = (BigInt(1) << in0bits) - 1
val in1Mask = (BigInt(1) << in1bits) - 1
val in0 = BigInt(r.nextLong()) & in0Mask
val in1 = BigInt(r.nextLong()) & in1Mask
val eMask = (BigInt(1) << (in0bits + 1)) - 1
val e = (in0 + in1) & eMask
(in0, in1, e)
}
val r = new Random
for (i <- 0 until 100) {
println(s"- ${i} -")
val (in0, in1, exp) = getData()
feedData(in0, in1)
step(1)
println(s"io.in0 = 0x${peek(c.io.in0).toLong.toHexString}")
println(s"io.in1 = 0x${peek(c.io.in1).toLong.toHexString}")
println(s"io.out = 0x${peek(c.io.out).toLong.toHexString}")
println(s"exp = 0x${exp.toLong.toHexString}")
expect(c.io.out, exp)
}
}
object Test extends App {
val (in0Bits, in1Bits) = (32, 32)
iotesters.Driver.execute(args, () => new Top(in0Bits, in1Bits)) {
c => new TopTester(c, in0Bits, in1Bits)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.