src/main/scala/chipsldpc/sort/NeighbourSorter.scala
Source from the repository checkout used to build these docs. Line numbers link to individual source lines.
1package chipsldpc.sort
2
3import chisel3._
4import chisel3.util.{Decoupled, Enum, log2Ceil}
5
6sealed trait NeighbourOrder
7case object MagnitudeAscending extends NeighbourOrder
8case object SignedAscending extends NeighbourOrder
9
10final case class NeighbourSorterConfig(
11 size: Int,
12 softBits: Int,
13 threshold: BigInt,
14 order: NeighbourOrder = MagnitudeAscending,
15) {
16 require(size > 0, "size must be positive")
17 require(softBits > 1, "softBits must exceed one")
18 private val limit = BigInt(1) << (softBits - 1)
19 order match {
20 case MagnitudeAscending => require(threshold >= 0 && threshold <= limit + 1, "threshold is out of range")
21 case SignedAscending => require(threshold >= -limit && threshold < limit, "threshold is out of range")
22 }
23
24 val pairs: Int = (size + 1) / 2
25 val indexBits: Int = math.max(1, log2Ceil(size))
26 val countBits: Int = math.max(1, log2Ceil(size + 1))
27}
28
29final class NeighbourSorterInput(config: NeighbourSorterConfig) extends Bundle {
30 val soft = Vec(config.size, SInt(config.softBits.W))
31 val inScopeValid = Bool()
32 val inScope = Vec(config.size, Bool())
33}
34
35final class NeighbourSorterOutput(config: NeighbourSorterConfig) extends Bundle {
36 val soft = SInt(config.softBits.W)
37 val index = UInt(config.indexBits.W)
38 val last = Bool()
39}
40
41private final class Entry(config: NeighbourSorterConfig) extends Bundle {
42 val soft = SInt(config.softBits.W)
43 val magnitude = UInt(config.softBits.W)
44 val index = UInt(config.indexBits.W)
45}
46
47/** Section 3.1 sorter for one parallel BP-soft frame and an optional runtime scope. */
48final class NeighbourSorter(config: NeighbourSorterConfig) extends Module {
49 val io = IO(new Bundle {
50 val in = Flipped(Decoupled(new NeighbourSorterInput(config)))
51 val out = Decoupled(new NeighbourSorterOutput(config))
52 val done = Output(Bool())
53 })
54
55 private val sIdle :: sFill :: sDrain :: Nil = Enum(3)
56 private val state = RegInit(sIdle)
57 private val soft = Reg(Vec(config.size, SInt(config.softBits.W)))
58 private val inScopeValid = Reg(Bool())
59 private val inScope = Reg(Vec(config.size, Bool()))
60 private val scan = RegInit(0.U(config.indexBits.W))
61 private val count = RegInit(0.U(config.countBits.W))
62 private val remaining = RegInit(0.U(config.countBits.W))
63 private val left = Reg(Vec(config.pairs, new Entry(config)))
64 private val right = Reg(Vec(config.pairs, new Entry(config)))
65 private val leftValid = RegInit(VecInit(Seq.fill(config.pairs)(false.B)))
66 private val rightValid = RegInit(VecInit(Seq.fill(config.pairs)(false.B)))
67 private val done = RegInit(false.B)
68
69 private def included(value: SInt): Bool = config.order match {
70 case MagnitudeAscending => value.abs.asUInt < config.threshold.U
71 case SignedAscending => value < config.threshold.S(config.softBits.W)
72 }
73
74 private def precedes(a: Entry, b: Entry): Bool = {
75 val less = config.order match {
76 case MagnitudeAscending => a.magnitude < b.magnitude
77 case SignedAscending => a.soft < b.soft
78 }
79 val equal = config.order match {
80 case MagnitudeAscending => a.magnitude === b.magnitude
81 case SignedAscending => a.soft === b.soft
82 }
83 less || (equal && a.index <= b.index)
84 }
85
86 io.in.ready := state === sIdle
87 io.out.valid := state === sDrain && leftValid.head
88 io.out.bits.soft := left.head.soft
89 io.out.bits.index := left.head.index
90 io.out.bits.last := remaining === 1.U
91 io.done := done
92 done := false.B
93
94 when(io.in.fire) {
95 val firstSoft = io.in.bits.soft(config.size - 1)
96 val firstMagnitude = firstSoft.abs.asUInt
97 val firstInclude = included(firstSoft) &&
98 (!io.in.bits.inScopeValid || io.in.bits.inScope(config.size - 1))
99 for (i <- 0 until config.size) {
100 soft(i) := io.in.bits.soft(i)
101 inScope(i) := io.in.bits.inScope(i)
102 }
103 inScopeValid := io.in.bits.inScopeValid
104 leftValid.foreach(_ := false.B)
105 rightValid.foreach(_ := false.B)
106 when(firstInclude) {
107 left.head.soft := firstSoft
108 left.head.magnitude := firstMagnitude
109 left.head.index := (config.size - 1).U
110 leftValid.head := true.B
111 }
112 count := firstInclude.asUInt
113 if (config.size == 1) {
114 when(firstInclude) {
115 remaining := 1.U
116 state := sDrain
117 }.otherwise {
118 done := true.B
119 state := sIdle
120 }
121 } else {
122 scan := (config.size - 2).U
123 state := sFill
124 }
125 }.elsewhen(state === sFill) {
126 val currentSoft = if (config.size == 1) soft.head else soft(scan)
127 val currentScope = if (config.size == 1) inScope.head else inScope(scan)
128 val magnitude = currentSoft.abs.asUInt
129 val include = included(currentSoft) && (!inScopeValid || currentScope)
130 val nextCount = count + include.asUInt
131 val candidate = Wire(new Entry(config))
132 candidate.soft := currentSoft
133 candidate.magnitude := magnitude
134 candidate.index := scan
135
136 when(include) {
137 for (i <- 0 until config.pairs) {
138 val carry = if (i == 0) candidate else right(i - 1)
139 val carryValid = if (i == 0) true.B else rightValid(i - 1)
140 rightValid(i) := false.B
141 when(carryValid) {
142 when(!leftValid(i) || precedes(carry, left(i))) {
143 left(i) := carry
144 leftValid(i) := true.B
145 right(i) := left(i)
146 rightValid(i) := leftValid(i)
147 }.otherwise {
148 right(i) := carry
149 rightValid(i) := true.B
150 }
151 }
152 }
153 }
154
155 count := nextCount
156 when(scan === 0.U) {
157 when(nextCount === 0.U) {
158 done := true.B
159 state := sIdle
160 }.otherwise {
161 remaining := nextCount
162 state := sDrain
163 }
164 }.otherwise {
165 scan := scan - 1.U
166 }
167 }.elsewhen(io.out.fire) {
168 for (i <- 0 until config.pairs) {
169 val carry = if (i + 1 < config.pairs) left(i + 1) else 0.U.asTypeOf(new Entry(config))
170 val carryValid = if (i + 1 < config.pairs) leftValid(i + 1) else false.B
171 leftValid(i) := false.B
172 when(!rightValid(i)) {
173 right(i) := carry
174 rightValid(i) := carryValid
175 }.elsewhen(!carryValid) {
176 left(i) := right(i)
177 leftValid(i) := true.B
178 rightValid(i) := false.B
179 }.elsewhen(precedes(right(i), carry)) {
180 left(i) := right(i)
181 leftValid(i) := true.B
182 right(i) := carry
183 }.otherwise {
184 left(i) := carry
185 leftValid(i) := true.B
186 }
187 }
188
189 when(remaining === 1.U) {
190 remaining := 0.U
191 done := true.B
192 state := sIdle
193 }.otherwise {
194 remaining := remaining - 1.U
195 }
196 }
197}