Skip to content

Using the PeekPokeTester

Chick Markley edited this page Sep 20, 2016 · 8 revisions

How to build a test using the PeekPokeTester

This will be a brief walk-through of the PeekPokeTester. We will look at the src/test/scala/examples/GCDSpec.scala example.

A couple of imports

import chisel3._
import chisel3.util._
import chisel3.iotesters._
import org.scalatest.{Matchers, FlatSpec}

The device under test.

For the purposes of this discussion we are focused on the IO ports of the circuit or device under test (DUT).

object RealGCD2 {
  val num_width = 16
}

class RealGCD2Input extends Bundle {
  val a = Bits(width = RealGCD2.num_width)
  val b = Bits(width = RealGCD2.num_width)
}

class RealGCD2 extends Module {
  val io  = new Bundle {
    val in  = Decoupled(new RealGCD2Input()).flip()
    val out = Valid(UInt(width = RealGCD2.num_width))
  }
...

The Test Harness

The test harness API allows 4 interactions with the DUT

  1. To set the DUT'S inputs: poke
  2. To look at the DUT'S outputs: peek
  3. To test one of the DUT's outputs: expect
  4. To advance the clock of the DUT: step

The tester is constructed by subclassing PeekPokeTester

class GCDPeekPokeTester(c: RealGCD2) extends PeekPokeTester(c)  {
  for {
    i <- 1 to 10
    j <- 1 to 10
  } {
    val (gcd_value, cycles) = GCDCalculator.computeGcdResultsAndCycles(i, j)

    poke(c.io.in.bits.a, i)
    poke(c.io.in.bits.b, j)
    poke(c.io.in.valid, 1)

    var count = 0
    while(peek(c.io.out.valid) == BigInt(0) && count < 20) {
      step(1)
      count += 1
    }
    if(count > 30) {
      println(s"Waited $count cycles on gcd inputs $i, $j, giving up")
      System.exit(0)
    }
    expect(c.io.out.bits, gcd_value)
    step(1)
  }
}
Clone this wiki locally