summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/Tbl.scala
blob: 003554c8845b4ea82024a44a53fa9eb9ea0ffbb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// See LICENSE for license details.

package chiselTests

import Chisel._
import org.scalatest._
import org.scalatest.prop._
import Chisel.testers.BasicTester

class Tbl(w: Int, n: Int) extends Module {
  val io = new Bundle {
    val wi  = UInt(INPUT, log2Up(n))
    val ri  = UInt(INPUT, log2Up(n))
    val we  = Bool(INPUT)
    val  d  = UInt(INPUT, w)
    val  o  = UInt(OUTPUT, w)
  }
  val m = Mem(UInt(width = w), n)
  io.o := m(io.ri)
  when (io.we) {
    m(io.wi) := io.d
    when(io.ri === io.wi) {
      io.o := io.d
    }
  }
}

class TblTester(w: Int, n: Int, idxs: List[Int], values: List[Int]) extends BasicTester {
  val (cnt, wrap) = Counter(Bool(true), idxs.size)
  val dut = Module(new Tbl(w, n))
  val vvalues = Vec(values.map(UInt(_)))
  val vidxs = Vec(idxs.map(UInt(_)))
  val prev_idx = vidxs(cnt - UInt(1))
  val prev_value = vvalues(cnt - UInt(1))
  dut.io.wi := vidxs(cnt)
  dut.io.ri := prev_idx
  dut.io.we := Bool(true) //TODO enSequence
  dut.io.d := vvalues(cnt)
  when (cnt > UInt(0)) {
    when (prev_idx === vidxs(cnt)) {
      assert(dut.io.o === vvalues(cnt))
    } .otherwise {
      assert(dut.io.o === prev_value)
    }
  }
  when(wrap) {
    stop()
  }
}

class TblSpec extends ChiselPropSpec {
  property("All table reads should return the previous write") {
    forAll(safeUIntPairN(8)) { case(w: Int, pairs: List[(Int, Int)]) =>
      require(w > 0)
      val (idxs, values) = pairs.unzip
      assert(execute{ new TblTester(w, 1 << w, idxs, values) })
    }
  }
}