src/main/scala/chipsldpc/GaussJordan/TrapezoidMesh.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.Cat
  5
  6final case class TrapezoidMeshConfig(
  7    n: Int,
  8    liftedCols: Int,
  9    reduceHopDelay: Int,
 10    exposeFullState: Boolean = true,
 11) {
 12  require(n > 0, "n must be positive")
 13  require(liftedCols > 0, "liftedCols must be positive")
 14  require(reduceHopDelay > 0, "reduceHopDelay must be positive")
 15
 16  val totalCols: Int = n + liftedCols
 17}
 18
 19final class TrapezoidMeshIO(config: TrapezoidMeshConfig) extends Bundle {
 20  val clk              = Input(Clock())
 21  val rst              = Input(Bool())
 22  val en_i             = Input(Bool())
 23  val reduce_i         = Input(Bool())
 24  val data_top_i       = Input(UInt(config.totalCols.W))
 25  val data_bottom_o    = Output(UInt(config.liftedCols.W))
 26  val diag_data_out_o  = Output(UInt(config.n.W))
 27  val diag_reduce_in_o = Output(UInt(config.n.W))
 28  val diag_state_o     = Output(UInt(config.n.W))
 29  val a_regs_flat_o    = Output(UInt((if (config.exposeFullState) config.n * config.n else 1).W))
 30  val b_regs_flat_o    = Output(UInt((if (config.exposeFullState) config.n * config.liftedCols else 1).W))
 31}
 32
 33/** Structural binary Gauss-Jordan mesh with the legacy flat SystemVerilog ABI. */
 34final class TrapezoidMesh(config: TrapezoidMeshConfig) extends RawModule {
 35  override def desiredName: String = "trapeziod_mesh"
 36
 37  val io = FlatIO(new TrapezoidMeshIO(config))
 38
 39  private val dataDown = Wire(Vec(config.n, Vec(config.totalCols, Bool())))
 40  private val opBus = Wire(Vec(config.n, Vec(config.totalCols, GjOpcode())))
 41  private val states = if (config.exposeFullState)
 42    Some(Wire(Vec(config.n, Vec(config.totalCols, Bool())))) else None
 43  private val diagState = Wire(Vec(config.n, Bool()))
 44  private val reduceIn = Wire(Vec(config.n, Bool()))
 45  private val reduceOut = Wire(Vec(config.n, Bool()))
 46
 47  for (row <- 0 until config.n; col <- 0 until config.totalCols) {
 48    if (col < row) {
 49      dataDown(row)(col) := false.B
 50      opBus(row)(col) := GjOpcode.Pass
 51      states.foreach(_(row)(col) := false.B)
 52    } else if (col == row) {
 53      val cell = Module(new PeDiag)
 54      cell.clk := io.clk
 55      cell.rst := io.rst
 56      cell.en_i := io.en_i
 57      cell.data_i := (if (row == 0) io.data_top_i(col) else dataDown(row - 1)(col))
 58      cell.reduce_sig_i := reduceIn(row)
 59      dataDown(row)(col) := cell.data_o
 60      opBus(row)(col) := cell.op_o
 61      states.foreach(_(row)(col) := cell.state_o)
 62      diagState(row) := cell.state_o
 63      reduceOut(row) := cell.reduce_sig_o
 64    } else {
 65      val cell = Module(new PeCol)
 66      cell.clk := io.clk
 67      cell.rst := io.rst
 68      cell.en_i := io.en_i
 69      cell.data_i := (if (row == 0) io.data_top_i(col) else dataDown(row - 1)(col))
 70      cell.op_i := opBus(row)(col - 1)
 71      dataDown(row)(col) := cell.data_o
 72      opBus(row)(col) := cell.op_o
 73      states.foreach(_(row)(col) := cell.state_o)
 74    }
 75  }
 76
 77  reduceIn(0) := io.reduce_i
 78  for (row <- 1 until config.n) {
 79    val pipe = withClockAndReset(io.clk, io.rst) {
 80      RegInit(VecInit(Seq.fill(config.reduceHopDelay)(false.B)))
 81    }
 82    when(io.en_i) {
 83      pipe(0) := reduceOut(row - 1)
 84      for (stage <- 1 until config.reduceHopDelay) {
 85        pipe(stage) := pipe(stage - 1)
 86      }
 87    }
 88    reduceIn(row) := pipe.last
 89  }
 90
 91  private def pack(bits: Seq[Bool]): UInt = Cat(bits.reverse)
 92
 93  io.data_bottom_o := pack((0 until config.liftedCols).map(col => dataDown(config.n - 1)(config.n + col)))
 94  io.diag_data_out_o := pack((0 until config.n).map(row => dataDown(row)(row)))
 95  io.diag_reduce_in_o := pack(reduceIn.toSeq)
 96  io.diag_state_o := pack(diagState.toSeq)
 97  io.a_regs_flat_o := states.fold(0.U)(grid =>
 98    pack(for (row <- 0 until config.n; col <- 0 until config.n) yield grid(row)(col)))
 99  io.b_regs_flat_o := states.fold(0.U)(grid => pack(
100    for (row <- 0 until config.n; col <- 0 until config.liftedCols) yield grid(row)(config.n + col)
101  ))
102}