src/main/scala/chipsldpc/GaussJordan/SystolicGf2Solver.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.util.{Decoupled, Enum, log2Ceil}
5
6final case class SystolicGf2SolverConfig(width: Int, maxRows: Int) {
7 require(width > 0, "width must be positive")
8 require(maxRows > 0, "maxRows must be positive")
9
10 val rowCountBits: Int = math.max(1, log2Ceil(maxRows + 1))
11 val slotBits: Int = math.max(1, log2Ceil(width))
12 val activeCountBits: Int = math.max(1, log2Ceil(width + 1))
13 val cycleBits: Int = math.max(1, log2Ceil(maxRows + 3 * width + 3))
14}
15
16final class SystolicGf2Start(config: SystolicGf2SolverConfig) extends Bundle {
17 val rows = UInt(config.rowCountBits.W)
18 val activeCols = UInt(config.activeCountBits.W)
19}
20
21final class SystolicGf2Row(config: SystolicGf2SolverConfig) extends Bundle {
22 val coefficients = UInt(config.width.W)
23 val rhs = Bool()
24}
25
26final class SystolicGf2Bit(config: SystolicGf2SolverConfig) extends Bundle {
27 val slot = UInt(config.slotBits.W)
28 val value = Bool()
29 val last = Bool()
30}
31
32final class SystolicGf2Result(config: SystolicGf2SolverConfig) extends Bundle {
33 val consistent = Bool()
34 val cycles = UInt(config.cycleBits.W)
35}
36
37/** Framed row-stream wrapper around the binary trapezoid mesh. */
38final class SystolicGf2Solver(config: SystolicGf2SolverConfig) extends Module {
39 val io = IO(new Bundle {
40 val start = Flipped(Decoupled(new SystolicGf2Start(config)))
41 val row = Flipped(Decoupled(new SystolicGf2Row(config)))
42 val solution = Decoupled(new SystolicGf2Bit(config))
43 val result = Decoupled(new SystolicGf2Result(config))
44 })
45
46 private val sIdle :: sClear :: sFeed :: sRun :: sOutput :: sResult :: Nil = Enum(6)
47 private val state = RegInit(sIdle)
48 private val rowCount = Reg(UInt(config.rowCountBits.W))
49 private val activeCols = Reg(UInt(config.activeCountBits.W))
50 private val rowsSeen = RegInit(0.U(config.rowCountBits.W))
51 private val postCycle = RegInit(0.U(log2Ceil(3 * config.width + 2).W))
52 private val cycles = RegInit(0.U(config.cycleBits.W))
53 private val inconsistent = RegInit(false.B)
54 private val solution = RegInit(VecInit(Seq.fill(config.width)(false.B)))
55 private val outputSlot = RegInit(0.U(config.slotBits.W))
56
57 private val mesh = Module(new TrapezoidMesh(
58 TrapezoidMeshConfig(
59 config.width, liftedCols = 1, reduceHopDelay = 3, exposeFullState = false,
60 ),
61 ))
62 mesh.io.clk := clock
63 mesh.io.rst := reset.asBool || state === sClear
64 mesh.io.en_i := state =/= sIdle
65 mesh.io.reduce_i := state === sRun && postCycle === 1.U
66
67 private val inputBits = Wire(Vec(config.width + 1, Bool()))
68 for (col <- 0 until config.width) {
69 val source = activeCols - 1.U - col.U
70 inputBits(col) := col.U < activeCols &&
71 io.row.bits.coefficients(source(config.slotBits - 1, 0)) && io.row.fire
72 }
73 inputBits(config.width) := io.row.bits.rhs && io.row.fire
74
75 private val staggered = Seq.tabulate(config.width + 1) { col =>
76 RegInit(VecInit(Seq.fill(col + 1)(false.B)))
77 }
78 for ((pipe, col) <- staggered.zipWithIndex) {
79 when(state === sClear) {
80 pipe.foreach(_ := false.B)
81 }.otherwise {
82 pipe.head := inputBits(col)
83 for (stage <- 1 until pipe.length)
84 pipe(stage) := pipe(stage - 1)
85 }
86 }
87 mesh.io.data_top_i := VecInit(staggered.map(_.last)).asUInt
88
89 io.start.ready := state === sIdle
90 io.row.ready := state === sFeed
91 io.solution.valid := state === sOutput && !inconsistent && activeCols =/= 0.U
92 io.solution.bits.slot := outputSlot
93 io.solution.bits.value := solution((activeCols - 1.U - outputSlot)(config.slotBits - 1, 0))
94 io.solution.bits.last := outputSlot === activeCols - 1.U
95 io.result.valid := state === sResult
96 io.result.bits.consistent := !inconsistent
97 io.result.bits.cycles := cycles
98
99 when(io.start.fire) {
100 assert(io.start.bits.rows <= config.maxRows.U)
101 assert(io.start.bits.activeCols <= config.width.U)
102 rowCount := io.start.bits.rows
103 activeCols := io.start.bits.activeCols
104 rowsSeen := 0.U
105 postCycle := 0.U
106 cycles := 0.U
107 inconsistent := false.B
108 solution.foreach(_ := false.B)
109 outputSlot := 0.U
110 state := sClear
111 }.elsewhen(state === sClear) {
112 state := Mux(rowCount === 0.U, sOutput, sFeed)
113 }.elsewhen(state === sFeed) {
114 cycles := cycles + 1.U
115 when(mesh.io.data_bottom_o(0)) {
116 inconsistent := true.B
117 }
118 when(io.row.fire) {
119 rowsSeen := rowsSeen + 1.U
120 when(rowsSeen + 1.U === rowCount) {
121 postCycle := 0.U
122 state := sRun
123 }
124 }
125 }.elsewhen(state === sRun) {
126 cycles := cycles + 1.U
127 when(postCycle <= (2 * config.width).U && mesh.io.data_bottom_o(0)) {
128 inconsistent := true.B
129 }
130 when(postCycle > (2 * config.width).U) {
131 val slot = (postCycle - (2 * config.width + 1).U)(config.slotBits - 1, 0)
132 val pivot = mesh.io.diag_state_o(slot)
133 solution(slot) := mesh.io.data_bottom_o(0) && pivot
134 }
135 when(postCycle === (3 * config.width).U) {
136 outputSlot := 0.U
137 state := sOutput
138 }.otherwise {
139 postCycle := postCycle + 1.U
140 }
141 }.elsewhen(state === sOutput) {
142 when(inconsistent || activeCols === 0.U) {
143 state := sResult
144 }.elsewhen(io.solution.fire) {
145 when(io.solution.bits.last) {
146 state := sResult
147 }.otherwise {
148 outputSlot := outputSlot + 1.U
149 }
150 }
151 }.elsewhen(io.result.fire) {
152 state := sIdle
153 }
154}