src/test/scala/chipsldpc/RelayCoefficientsSpec.scala
Source from the repository checkout used to build these docs. Line numbers link to individual source lines.
1package chipsldpc
2
3import chisel3._
4import chisel3.simulator.scalatest.ChiselSim
5import org.scalatest.freespec.AnyFreeSpec
6
7final class RelayCoefficientsSpec extends AnyFreeSpec with ChiselSim {
8 private val stateCount = 65535
9
10 private def seed(offset: Int, lane: Int): Int =
11 1 + ((BigInt(offset) + BigInt(lane) * 32768) % stateCount).toInt
12
13 private def step(state: Int): Int =
14 (state >>> 1) ^ (if ((state & 1) != 0) 0xb400 else 0)
15
16 private def expectBeta(dut: LfsrRelayCoefficients, expected: Seq[Int]): Unit =
17 dut.io.beta.zip(expected).foreach { case (actual, value) => actual.expect(value.U) }
18
19 private def reset(dut: LfsrRelayCoefficients): Unit = {
20 dut.io.frameStart.poke(false.B)
21 dut.io.advanceLeg.poke(false.B)
22 dut.reset.poke(true.B)
23 dut.clock.step()
24 dut.reset.poke(false.B)
25 }
26
27 "lane seeds independently match the specified nonzero permutation" in {
28 val lanes = 8784
29 val offsets = Seq(0, 1, 12345, stateCount - 1)
30 offsets.foreach { offset =>
31 val expected = Vector.tabulate(lanes)(seed(offset, _))
32 val actual = Vector.tabulate(lanes)(LfsrRelayCoefficients.laneSeed(offset, _))
33 assert(actual == expected)
34 assert(actual.forall(value => value >= 1 && value <= stateCount))
35 assert(actual.distinct.size == lanes)
36 }
37 }
38
39 "each advance matches an independent LFSR sequence and stays in the beta range" in {
40 val lanes = 9
41 val offset = 12345
42 simulate(new LfsrRelayCoefficients(lanes, offset)) { dut =>
43 reset(dut)
44 expectBeta(dut, Vector.fill(lanes)(7))
45
46 var states = Vector.tabulate(lanes)(seed(offset, _))
47 for (_ <- 0 until 128) {
48 states = states.map(step)
49 val expected = states.map(state => 3 + (state & 7))
50 dut.io.advanceLeg.poke(true.B)
51 dut.clock.step()
52 dut.io.advanceLeg.poke(false.B)
53 expectBeta(dut, expected)
54 assert(expected.forall(value => value >= 3 && value <= 10))
55 }
56 }
57 }
58
59 "coefficients hold between legs and every frame restarts leg zero" in {
60 val lanes = 5
61 val offset = 29
62 val firstStates = Vector.tabulate(lanes)(lane => step(seed(offset, lane)))
63 val firstBeta = firstStates.map(state => 3 + (state & 7))
64
65 simulate(new LfsrRelayCoefficients(lanes, offset)) { dut =>
66 reset(dut)
67
68 dut.io.frameStart.poke(true.B)
69 dut.io.advanceLeg.poke(true.B)
70 dut.clock.step()
71 dut.io.frameStart.poke(false.B)
72 dut.io.advanceLeg.poke(false.B)
73 expectBeta(dut, Vector.fill(lanes)(7))
74
75 for (_ <- 0 until 4) {
76 dut.clock.step()
77 expectBeta(dut, Vector.fill(lanes)(7))
78 }
79
80 dut.io.advanceLeg.poke(true.B)
81 dut.clock.step()
82 dut.io.advanceLeg.poke(false.B)
83 expectBeta(dut, firstBeta)
84
85 for (_ <- 0 until 4) {
86 dut.clock.step()
87 expectBeta(dut, firstBeta)
88 }
89
90 dut.io.frameStart.poke(true.B)
91 dut.clock.step()
92 dut.io.frameStart.poke(false.B)
93 expectBeta(dut, Vector.fill(lanes)(7))
94
95 dut.io.advanceLeg.poke(true.B)
96 dut.clock.step()
97 dut.io.advanceLeg.poke(false.B)
98 expectBeta(dut, firstBeta)
99 }
100 }
101}