src/main/scala/chipsldpc/StaticTannerDatapath.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.util.{Decoupled, RegEnable, Valid}
  5import chipsldpc.graph.TannerNodeGraphs
  6
  7final class StaticDecoderInput(checks: Int, variables: Int, q: Quantization) extends Bundle {
  8  val syndrome = Vec(checks, Bool())
  9  val prior = Vec(variables, UInt(q.magnitudeBits.W))
 10}
 11
 12final class StaticDecoderResult(checks: Int, variables: Int, q: Quantization) extends Bundle {
 13  val marginal = Vec(variables, SInt(q.accumulatorBits.W))
 14  val decision = Vec(variables, Bool())
 15  val residual = Vec(checks, Bool())
 16  val converged = Bool()
 17}
 18
 19private[chipsldpc] final class StaticCheckStage(config: CheckConfig) extends Module {
 20  val io = IO(new Bundle {
 21    val load = Input(Bool())
 22    val enable = Input(Bool())
 23    val input = Input(Vec(config.degree, new SignMag(config.q.magnitudeBits)))
 24    val syndromeIn = Input(Bool())
 25    val scaleControl = Input(UInt(config.scale.controlBits.W))
 26    val syndrome = Output(Bool())
 27    val result = Output(new CheckResult(config))
 28  })
 29
 30  private val syndrome = RegEnable(io.syndromeIn, io.load)
 31  private val core = Module(new CheckNode(config))
 32  core.inputs := io.input
 33  core.syndrome := syndrome
 34  core.scaleShift.foreach(_ := io.scaleControl)
 35
 36  io.syndrome := syndrome
 37  io.result := RegEnable(core.result, io.enable)
 38}
 39
 40private[chipsldpc] final class StaticVariableStage(config: VariableConfig) extends Module {
 41  val io = IO(new Bundle {
 42    val load = Input(Bool())
 43    val enable = Input(Bool())
 44    val restartMessages = Input(Bool())
 45    val input = Input(Vec(config.degree, new CheckMessage(config.q.magnitudeBits)))
 46    val priorIn = Input(UInt(config.q.magnitudeBits.W))
 47    val bias = Input(SInt(config.q.accumulatorBits.W))
 48    val prior = Output(UInt(config.q.magnitudeBits.W))
 49    val next = Output(new VariableResult(config))
 50    val result = Output(new VariableResult(config))
 51  })
 52
 53  private val priorIn = io.priorIn.pad(config.q.accumulatorBits).asSInt
 54  private val prior = RegEnable(io.priorIn, io.load)
 55  private val core = Module(new VariableNode(config))
 56  core.inputs := io.input
 57  core.prior := io.bias
 58
 59  private val state = Reg(new VariableResult(config))
 60  when(io.load) {
 61    state.marginal := priorIn
 62    state.decision := false.B
 63    state.extrinsic.foreach { message =>
 64      message.sign := false.B
 65      message.magnitude := io.priorIn
 66    }
 67  }.elsewhen(io.enable) {
 68    state.marginal := core.result.marginal
 69    state.decision := core.result.decision
 70    state.extrinsic.zip(core.result.extrinsic).foreach { case (message, next) =>
 71      when(io.restartMessages) {
 72        message.sign := false.B
 73        message.magnitude := prior
 74      }.otherwise {
 75        message := next
 76      }
 77    }
 78  }
 79  io.prior := prior
 80  io.next := core.result
 81  io.result := state
 82}
 83
 84/** Shared statically wired Tanner core: one CNU cycle, then one VNU cycle. */
 85private[chipsldpc] final class StaticTannerCore(
 86    nodes: TannerNodeGraphs,
 87    q: Quantization = RelayDefaults.q,
 88    scale: CheckScale = RelayDefaults.scale,
 89) extends Module {
 90  private val graph = nodes.graph
 91  require(nodes.checks.forall(_.degree >= 2), "check degrees must be at least two")
 92
 93  val io = IO(new Bundle {
 94    val load = Flipped(Decoupled(new StaticDecoderInput(graph.checkCount, graph.variableCount, q)))
 95    val step = Flipped(Decoupled(UInt(scale.controlBits.W)))
 96    val bias = Input(Vec(graph.variableCount, SInt(q.accumulatorBits.W)))
 97    val restartMessages = Input(Bool())
 98    val prior = Output(Vec(graph.variableCount, SInt(q.accumulatorBits.W)))
 99    val commit = Valid(new StaticDecoderResult(graph.checkCount, graph.variableCount, q))
100    val result = Valid(new StaticDecoderResult(graph.checkCount, graph.variableCount, q))
101  })
102
103  private val loaded = RegInit(false.B)
104  private val vnuPhase = RegInit(false.B)
105  private val resultValid = RegInit(false.B)
106  io.load.ready := !vnuPhase
107  io.step.ready := loaded && !vnuPhase && !io.load.valid
108  when(io.load.fire) { loaded := true.B }
109  vnuPhase := io.step.fire
110  resultValid := vnuPhase
111
112  private val checks = nodes.checks.map { node =>
113    val stage = Module(new StaticCheckStage(CheckConfig(node.degree, q, scale)))
114    stage.suggestName(s"cnu_${node.checkId}")
115    stage.io.load := io.load.fire
116    stage.io.enable := io.step.fire
117    stage.io.syndromeIn := io.load.bits.syndrome(node.checkId)
118    stage.io.scaleControl := io.step.bits
119    stage
120  }
121  private val variables = nodes.variables.map { node =>
122    val stage = Module(new StaticVariableStage(VariableConfig(node.degree, q)))
123    stage.suggestName(s"vnu_${node.variableId}")
124    stage.io.load := io.load.fire
125    stage.io.enable := vnuPhase
126    stage.io.restartMessages := io.restartMessages
127    stage.io.priorIn := io.load.bits.prior(node.variableId)
128    stage.io.bias := io.bias(node.variableId)
129    stage
130  }
131
132  nodes.connections.foreach { edge =>
133    val check = checks(edge.checkId).io
134    val variable = variables(edge.variableId).io
135    check.input(edge.checkPort) := variable.result.extrinsic(edge.variablePort)
136    variable.input(edge.variablePort).minima := check.result.minima
137    variable.input(edge.variablePort).sign := check.result.edges(edge.checkPort).sign
138    variable.input(edge.variablePort).useSecond := check.result.edges(edge.checkPort).useSecond
139  }
140
141  private def connectVariables(output: StaticDecoderResult, values: Seq[VariableResult]): Unit = {
142    values.zipWithIndex.foreach { case (variable, id) =>
143      output.marginal(id) := variable.marginal
144      output.decision(id) := variable.decision
145    }
146  }
147
148  variables.zipWithIndex.foreach { case (variable, id) =>
149    io.prior(id) := variable.io.prior.pad(q.accumulatorBits).asSInt
150  }
151  private val convergence = Module(new ConvergenceChecker(graph))
152  convergence.estimate := VecInit(variables.map(_.io.next.decision))
153  convergence.syndrome := VecInit(checks.map(_.io.syndrome))
154  io.commit.valid := vnuPhase
155  connectVariables(io.commit.bits, variables.map(_.io.next))
156  io.commit.bits.residual := convergence.residual
157  io.commit.bits.converged := convergence.converged
158
159  private val resultResidual = RegEnable(convergence.residual, vnuPhase)
160  private val resultConverged = RegEnable(convergence.converged, vnuPhase)
161  io.result.valid := resultValid
162  connectVariables(io.result.bits, variables.map(_.io.result))
163  io.result.bits.residual := resultResidual
164  io.result.bits.converged := resultConverged
165}
166
167/** Compatibility shell exposing the original manually stepped vanilla datapath. */
168final class StaticTannerDatapath(
169    nodes: TannerNodeGraphs,
170    q: Quantization = RelayDefaults.q,
171    scale: CheckScale = RelayDefaults.scale,
172) extends Module {
173  private val graph = nodes.graph
174  val io = IO(new Bundle {
175    val load = Flipped(Decoupled(new StaticDecoderInput(graph.checkCount, graph.variableCount, q)))
176    val step = Flipped(Decoupled(UInt(scale.controlBits.W)))
177    val result = Valid(new StaticDecoderResult(graph.checkCount, graph.variableCount, q))
178  })
179
180  private val core = Module(new StaticTannerCore(nodes, q, scale))
181  core.io.load <> io.load
182  core.io.step <> io.step
183  core.io.bias := core.io.prior
184  core.io.restartMessages := false.B
185  io.result := core.io.result
186}