src/test/scala/chipsldpc/GaussJordan/TrapezoidMeshSpec.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 org.scalatest.freespec.AnyFreeSpec
6
7import scala.util.Random
8
9private object TrapezoidMeshSpec {
10 final case class Inputs(
11 top: BigInt = 0,
12 reduce: Boolean = false,
13 enable: Boolean = true,
14 reset: Boolean = false,
15 )
16
17 final case class Outputs(
18 bottom: BigInt,
19 diagData: BigInt,
20 diagReduce: BigInt,
21 aState: BigInt,
22 bState: BigInt,
23 )
24}
25
26final class TrapezoidMeshSpec extends AnyFreeSpec with ChiselSim {
27 import TrapezoidMeshSpec._
28
29 private object Op {
30 val Pass = 0
31 val Swap = 1
32 val Add = 2
33 val Lock = 3
34 }
35
36 /** Cycle oracle for the registered vertical, horizontal, and reduce paths. */
37 private final class MeshOracle(cfg: TrapezoidMeshConfig) {
38 private val rows = cfg.n
39 private val cols = cfg.n + cfg.liftedCols
40 private val depth = cfg.reduceHopDelay
41 private var state = Array.fill(rows, cols)(false)
42 private var data = Array.fill(rows, cols)(false)
43 private var op = Array.fill(rows, cols)(Op.Pass)
44 private var reducePipe = Array.fill(rows, depth)(false)
45
46 private def bit(word: BigInt, index: Int): Boolean = word.testBit(index)
47
48 private def reduceInputs(external: Boolean): Array[Boolean] =
49 Array.tabulate(rows)(row => if (row == 0) external else reducePipe(row)(depth - 1))
50
51 def step(in: Inputs): Unit = {
52 if (in.reset) {
53 state = Array.fill(rows, cols)(false)
54 data = Array.fill(rows, cols)(false)
55 op = Array.fill(rows, cols)(Op.Pass)
56 reducePipe = Array.fill(rows, depth)(false)
57 } else if (in.enable) {
58 val reduceIn = reduceInputs(in.reduce)
59 val nextState = state.map(_.clone())
60 val nextData = data.map(_.clone())
61 val nextOp = op.map(_.clone())
62 val nextReducePipe = reducePipe.map(_.clone())
63
64 for (row <- 0 until rows; col <- row until cols) {
65 val dataIn = if (row == 0) bit(in.top, col) else data(row - 1)(col)
66 if (col == row) {
67 if (reduceIn(row)) {
68 nextData(row)(col) = dataIn
69 nextOp(row)(col) = if (state(row)(col)) Op.Swap else Op.Pass
70 } else if (!dataIn) {
71 nextData(row)(col) = false
72 nextOp(row)(col) = Op.Pass
73 } else if (!state(row)(col)) {
74 nextState(row)(col) = true
75 nextData(row)(col) = false
76 nextOp(row)(col) = Op.Lock
77 } else {
78 nextData(row)(col) = false
79 nextOp(row)(col) = Op.Add
80 }
81 } else {
82 val opIn = op(row)(col - 1)
83 nextOp(row)(col) = opIn
84 opIn match {
85 case Op.Add => nextData(row)(col) = state(row)(col) ^ dataIn
86 case Op.Swap | Op.Lock =>
87 nextState(row)(col) = dataIn
88 nextData(row)(col) = state(row)(col)
89 case _ => nextData(row)(col) = dataIn
90 }
91 }
92 }
93
94 for (row <- 1 until rows) {
95 nextReducePipe(row)(0) = reduceIn(row - 1)
96 for (stage <- 1 until depth)
97 nextReducePipe(row)(stage) = reducePipe(row)(stage - 1)
98 }
99
100 state = nextState
101 data = nextData
102 op = nextOp
103 reducePipe = nextReducePipe
104 }
105 }
106
107 private def pack(width: Int)(valueAt: Int => Boolean): BigInt =
108 (0 until width).foldLeft(BigInt(0)) { (word, index) =>
109 if (valueAt(index)) word.setBit(index) else word
110 }
111
112 def outputs(reduce: Boolean): Outputs = {
113 val a = pack(rows * rows) { index =>
114 val row = index / rows
115 val col = index % rows
116 col >= row && state(row)(col)
117 }
118 val b = pack(rows * cfg.liftedCols) { index =>
119 val row = index / cfg.liftedCols
120 val col = index % cfg.liftedCols
121 state(row)(rows + col)
122 }
123 Outputs(
124 bottom = pack(cfg.liftedCols)(col => data(rows - 1)(rows + col)),
125 diagData = pack(rows)(row => data(row)(row)),
126 diagReduce = pack(rows)(row => if (row == 0) reduce else reducePipe(row)(depth - 1)),
127 aState = a,
128 bState = b,
129 )
130 }
131 }
132
133 private def expect(dut: TrapezoidMesh, expected: Outputs): Unit = {
134 dut.io.data_bottom_o.expect(expected.bottom.U)
135 dut.io.diag_data_out_o.expect(expected.diagData.U)
136 dut.io.diag_reduce_in_o.expect(expected.diagReduce.U)
137 dut.io.a_regs_flat_o.expect(expected.aState.U)
138 dut.io.b_regs_flat_o.expect(expected.bState.U)
139 }
140
141 private def cycle(dut: TrapezoidMesh, oracle: MeshOracle, in: Inputs): Unit = {
142 dut.io.rst.poke(in.reset.B)
143 dut.io.en_i.poke(in.enable.B)
144 dut.io.reduce_i.poke(in.reduce.B)
145 dut.io.data_top_i.poke(in.top.U)
146 dut.io.clk.step()
147 oracle.step(in)
148 expect(dut, oracle.outputs(in.reduce))
149 }
150
151 private def reset(dut: TrapezoidMesh, oracle: MeshOracle): Unit =
152 cycle(dut, oracle, Inputs(reset = true))
153
154 private def pack(bits: Seq[Int]): BigInt =
155 bits.zipWithIndex.foldLeft(BigInt(0)) { case (word, (bit, index)) =>
156 if (bit == 1) word.setBit(index) else word
157 }
158
159 "rejects empty dimensions and zero reduce delay" in {
160 assertThrows[IllegalArgumentException](TrapezoidMeshConfig(0, 1, 1))
161 assertThrows[IllegalArgumentException](TrapezoidMeshConfig(1, 0, 1))
162 assertThrows[IllegalArgumentException](TrapezoidMeshConfig(1, 1, 0))
163 }
164
165 "N=1 has a direct diagonal and lifted boundary" in {
166 val cfg = TrapezoidMeshConfig(n = 1, liftedCols = 1, reduceHopDelay = 1)
167 simulateRaw(new TrapezoidMesh(cfg)) { dut =>
168 val oracle = new MeshOracle(cfg)
169 reset(dut, oracle)
170
171 cycle(dut, oracle, Inputs(top = 1)) // Lock A[0,0].
172 dut.io.a_regs_flat_o.expect(1.U)
173 cycle(dut, oracle, Inputs(top = 2)) // Lock B[0,0] one cycle later.
174 dut.io.b_regs_flat_o.expect(1.U)
175 cycle(dut, oracle, Inputs(top = 1, reduce = true))
176 dut.io.diag_data_out_o.expect(1.U)
177 dut.io.diag_reduce_in_o.expect(1.U)
178 }
179 }
180
181 "matches an independent oracle for deterministic and random traffic" in {
182 val cfg = TrapezoidMeshConfig(n = 4, liftedCols = 3, reduceHopDelay = 2)
183 simulateRaw(new TrapezoidMesh(cfg)) { dut =>
184 val oracle = new MeshOracle(cfg)
185 reset(dut, oracle)
186
187 Seq(0x01, 0x52, 0x7f, 0x24, 0x68, 0x13).foreach { top =>
188 cycle(dut, oracle, Inputs(top = BigInt(top)))
189 }
190
191 val random = new Random(0x5eedL)
192 for (index <- 0 until 80) {
193 val top = (0 until (cfg.n + cfg.liftedCols)).foldLeft(BigInt(0)) { (word, bit) =>
194 if (random.nextBoolean()) word.setBit(bit) else word
195 }
196 cycle(
197 dut,
198 oracle,
199 Inputs(top, reduce = index % 13 == 4, enable = index % 9 != 2),
200 )
201 }
202 }
203 }
204
205 "packs A and B state row-major with coordinate zero in the LSB" in {
206 val cfg = TrapezoidMeshConfig(n = 3, liftedCols = 2, reduceHopDelay = 2)
207 val inputs = Seq(
208 Seq(1, 0, 1, 1, 0),
209 Seq(0, 1, 1, 0, 1),
210 Seq(1, 1, 0, 1, 1),
211 Seq(0, 0, 1, 1, 0),
212 Seq(1, 0, 0, 0, 1),
213 Seq(0, 1, 0, 1, 0),
214 )
215 simulateRaw(new TrapezoidMesh(cfg)) { dut =>
216 val oracle = new MeshOracle(cfg)
217 reset(dut, oracle)
218 inputs.foreach(bits => cycle(dut, oracle, Inputs(top = pack(bits))))
219
220 // A rows: 011, 110, 100; B rows: 11, 00, 10 (listed low bit first).
221 dut.io.a_regs_flat_o.expect(307.U) // 0b100110011
222 dut.io.b_regs_flat_o.expect(35.U) // 0b100011
223 }
224 }
225
226 "holds on disable and gives synchronous reset priority over enable" in {
227 val cfg = TrapezoidMeshConfig(n = 2, liftedCols = 2, reduceHopDelay = 2)
228 simulateRaw(new TrapezoidMesh(cfg)) { dut =>
229 val oracle = new MeshOracle(cfg)
230 reset(dut, oracle)
231 cycle(dut, oracle, Inputs(top = 0xd))
232 cycle(dut, oracle, Inputs(top = 0xa))
233 val held = oracle.outputs(reduce = false)
234
235 cycle(dut, oracle, Inputs(top = 0xf, reduce = true, enable = false))
236 dut.io.a_regs_flat_o.expect(held.aState.U)
237 dut.io.b_regs_flat_o.expect(held.bState.U)
238 dut.io.data_bottom_o.expect(held.bottom.U)
239 dut.io.diag_data_out_o.expect(held.diagData.U)
240
241 // Merely asserting synchronous reset does not alter registered outputs.
242 dut.io.rst.poke(true.B)
243 dut.io.en_i.poke(false.B)
244 dut.io.reduce_i.poke(false.B)
245 expect(dut, held)
246 dut.io.clk.step()
247 oracle.step(Inputs(enable = false, reset = true))
248 expect(dut, oracle.outputs(reduce = false))
249 }
250 }
251
252 "propagates one reduce pulse through delays one, two, and three" in {
253 for (delay <- 1 to 3) {
254 val cfg = TrapezoidMeshConfig(n = 4, liftedCols = 1, reduceHopDelay = delay)
255 simulateRaw(new TrapezoidMesh(cfg)) { dut =>
256 val oracle = new MeshOracle(cfg)
257 reset(dut, oracle)
258 val lastArrival = ((cfg.n - 1) * delay) - 1
259
260 for (cycleIndex <- 0 to lastArrival + 1) {
261 val pulse = cycleIndex == 0
262 cycle(dut, oracle, Inputs(reduce = pulse))
263 val expected = (0 until cfg.n).foldLeft(BigInt(0)) { (mask, row) =>
264 val visible = if (row == 0) pulse else cycleIndex == (row * delay) - 1
265 if (visible) mask.setBit(row) else mask
266 }
267 dut.io.diag_reduce_in_o.expect(expected.U)
268 }
269 }
270 }
271 }
272}