src/main/scala/chipsldpc/RelayCoefficients.scala
Source from the repository checkout used to build these docs. Line numbers link to individual source lines.
1package chipsldpc
2
3import chisel3._
4
5/** Common controller-facing interface for replaceable Relay coefficient sources. */
6final class RelayCoefficientSourceIO(variableCount: Int, betaBits: Int) extends Bundle {
7 val frameStart = Input(Bool())
8 val advanceLeg = Input(Bool())
9 val beta = Output(Vec(variableCount, UInt(betaBits.W)))
10}
11
12abstract class RelayCoefficientSource(val variableCount: Int, val betaBits: Int) extends Module {
13 val io = IO(new RelayCoefficientSourceIO(variableCount, betaBits))
14}
15
16object LfsrRelayCoefficients {
17 val StateBits = 16
18 val BetaBits = 4
19 val InitialBeta = 7
20
21 private val StateCount = (BigInt(1) << StateBits) - 1
22 private val LaneStride = BigInt(32768)
23
24 private[chipsldpc] def laneSeed(seedOffset: Int, lane: Int): Int = {
25 require(seedOffset >= 0 && BigInt(seedOffset) < StateCount)
26 require(lane >= 0)
27 (1 + (BigInt(seedOffset) + BigInt(lane) * LaneStride) % StateCount).toInt
28 }
29}
30
31/**
32 * One deterministic 16-bit Galois LFSR per variable.
33 *
34 * `frameStart` and `advanceLeg` are one-cycle controller commands. A frame
35 * starts at beta 7 without consuming an LFSR value. Each subsequent leg
36 * consumes exactly one value, and coefficients remain stable otherwise.
37 */
38final class LfsrRelayCoefficients(variableCount: Int, seedOffset: Int = 0)
39 extends RelayCoefficientSource(variableCount, LfsrRelayCoefficients.BetaBits) {
40 import LfsrRelayCoefficients._
41
42 require(variableCount > 0 && variableCount <= (1 << StateBits) - 1)
43 require(seedOffset >= 0 && seedOffset < (1 << StateBits) - 1)
44
45 private val seeds = Vector.tabulate(variableCount)(laneSeed(seedOffset, _))
46 private val states = RegInit(VecInit(seeds.map(_.U(StateBits.W))))
47 private val coefficients = RegInit(VecInit(Seq.fill(variableCount)(InitialBeta.U(BetaBits.W))))
48
49 io.beta := coefficients
50
51 when(io.frameStart) {
52 states.zip(seeds).foreach { case (state, seed) => state := seed.U(StateBits.W) }
53 coefficients.foreach(_ := InitialBeta.U)
54 }.elsewhen(io.advanceLeg) {
55 states.zip(coefficients).foreach { case (state, coefficient) =>
56 val feedback = Mux(state(0), "hB400".U(StateBits.W), 0.U(StateBits.W))
57 val nextState = (state >> 1) ^ feedback
58 state := nextState
59 coefficient := 3.U(BetaBits.W) + nextState(2, 0)
60 }
61 }
62}