src/test/scala/chipsldpc/GaussJordan/SystolicGf2SolverSpec.scala

Source from the repository checkout used to build these docs. Line numbers link to individual source lines.

  1package chipsldpc.GaussJordan
  2
  3import chisel3._
  4import chisel3.simulator.scalatest.ChiselSim
  5import chipsldpc.Gf2Reference
  6import org.scalatest.freespec.AnyFreeSpec
  7import scala.util.Random
  8
  9final class SystolicGf2SolverSpec extends AnyFreeSpec with ChiselSim {
 10  private def run(
 11      dut: SystolicGf2Solver,
 12      config: SystolicGf2SolverConfig,
 13      rows: Vector[Int],
 14      rhs: Vector[Int],
 15      activeCols: Int,
 16  ): (Option[Vector[Int]], Int) = {
 17    val expected = Gf2Reference.solve(rows, rhs, activeCols)
 18    dut.io.start.valid.poke(true.B)
 19    dut.io.start.bits.rows.poke(rows.size.U)
 20    dut.io.start.bits.activeCols.poke(activeCols.U)
 21    dut.io.start.ready.expect(true.B)
 22    dut.clock.step()
 23    dut.io.start.valid.poke(false.B)
 24    dut.io.row.valid.poke(false.B)
 25    dut.io.solution.ready.poke(true.B)
 26    dut.io.result.ready.poke(true.B)
 27    dut.clock.step()
 28
 29    rows.zip(rhs).foreach { case (row, bit) =>
 30      dut.io.row.ready.expect(true.B)
 31      dut.io.row.valid.poke(true.B)
 32      dut.io.row.bits.coefficients.poke(row.U)
 33      dut.io.row.bits.rhs.poke(bit.B)
 34      dut.clock.step()
 35    }
 36    dut.io.row.valid.poke(false.B)
 37
 38    val actual = Vector.newBuilder[Int]
 39    var waited = 0
 40    while (!dut.io.result.valid.peek().litToBoolean && waited < 8 * config.width + 8) {
 41      if (dut.io.solution.valid.peek().litToBoolean) {
 42        actual += dut.io.solution.bits.value.peek().litValue.toInt
 43      }
 44      dut.clock.step()
 45      waited += 1
 46    }
 47    dut.io.result.valid.expect(true.B)
 48    dut.io.result.bits.consistent.expect(expected.consistent.B)
 49    val cycles = dut.io.result.bits.cycles.peek().litValue.toInt
 50    assert(cycles == (if (rows.isEmpty) 0 else rows.size + 3 * config.width + 1))
 51    val observed = if (expected.consistent) Some(actual.result()) else None
 52    observed.foreach { values =>
 53      assert(rows.zip(rhs).forall { case (row, bit) =>
 54        values.indices.foldLeft(0)((sum, col) => sum ^ (((row >> col) & 1) & values(col))) == bit
 55      })
 56      assert(values == expected.bits)
 57    }
 58    dut.clock.step()
 59    (observed, cycles)
 60  }
 61
 62  "solves full-rank, rank-deficient, and inconsistent systems" in {
 63    val config = SystolicGf2SolverConfig(width = 4, maxRows = 6)
 64    simulate(new SystolicGf2Solver(config)) { dut =>
 65      dut.io.start.valid.poke(false.B)
 66      dut.io.row.valid.poke(false.B)
 67      dut.io.solution.ready.poke(true.B)
 68      dut.io.result.ready.poke(true.B)
 69
 70      run(dut, config, Vector(3, 6, 12, 8), Vector(1, 0, 1, 0), activeCols = 4)
 71      run(dut, config, Vector(3, 6, 3), Vector(1, 0, 1), activeCols = 4)
 72      run(dut, config, Vector(3, 6, 3), Vector(1, 0, 0), activeCols = 4)
 73      run(dut, config, Vector.empty, Vector.empty, activeCols = 3)
 74    }
 75  }
 76
 77  "matches GF(2) algebra for every two-column system through three rows" in {
 78    val config = SystolicGf2SolverConfig(width = 2, maxRows = 3)
 79    simulate(new SystolicGf2Solver(config)) { dut =>
 80      dut.io.start.valid.poke(false.B)
 81      dut.io.row.valid.poke(false.B)
 82      dut.io.solution.ready.poke(true.B)
 83      dut.io.result.ready.poke(true.B)
 84      for {
 85        count <- 0 to config.maxRows
 86        matrix <- 0 until (1 << (config.width * count))
 87        rhs <- 0 until (1 << count)
 88      } run(
 89        dut,
 90        config,
 91        Vector.tabulate(count)(row => matrix >> (config.width * row) & 3),
 92        Vector.tabulate(count)(row => rhs >> row & 1),
 93        activeCols = config.width,
 94      )
 95    }
 96  }
 97
 98  "matches tall random systems" in {
 99    val config = SystolicGf2SolverConfig(width = 3, maxRows = 6)
100    simulate(new SystolicGf2Solver(config)) { dut =>
101      dut.io.start.valid.poke(false.B)
102      dut.io.row.valid.poke(false.B)
103      dut.io.solution.ready.poke(true.B)
104      dut.io.result.ready.poke(true.B)
105      val random = new Random(0x6f7364L)
106      for (_ <- 0 until 256) {
107        val count = random.nextInt(config.maxRows + 1)
108        run(
109          dut,
110          config,
111          Vector.fill(count)(random.nextInt(1 << config.width)),
112          Vector.fill(count)(random.nextInt(2)),
113          activeCols = config.width,
114        )
115      }
116    }
117  }
118}