SystolicMatrixTranspose<T extends Logic, V> constructor

SystolicMatrixTranspose<T extends Logic, V>({
  1. required Logic clk,
  2. required Logic reset,
  3. required Logic step,
  4. required TypedLogicArray<T, V> dataIn,
  5. String name = 'systolic_matrix_transpose',
})

Creates a transposer for a nonempty, one-dimensional typed row.

Implementation

SystolicMatrixTranspose({
  required Logic clk,
  required Logic reset,
  required Logic step,
  required TypedLogicArray<T, V> dataIn,
  super.name = 'systolic_matrix_transpose',
})  : dimension =
          dataIn.dimensions.length == 1 ? dataIn.dimensions.single : 0,
      elementWidth = dataIn.elementWidth,
      super(
        definitionName:
            'SystolicMatrixTranspose_${payloadShapeIdentity(dataIn)}',
      ) {
  if (dimension < 1 || elementWidth < 1) {
    throw ArgumentError('Matrix input must be a nonempty typed row.');
  }
  clk = addInput('clk', clk);
  reset = addInput('reset', reset);
  step = addInput('step', step);
  dataIn = addTypedInput('dataIn', dataIn);
  dataOut = addTypedPayloadOutput(this, 'dataOut', dataIn);
  final direction = Logic(name: 'direction');
  final counter = Logic(name: 'counter', width: max(1, dimension.log2Ceil));
  final filled = Logic(name: 'filled');
  final cells = TypedLogicArray<T, V>(
    [dimension, dimension],
    dataIn.arrayElements.first.cloneTyped,
    valueCodec: dataIn.valueCodec,
    name: 'cells',
  );
  T cell(int row, int column) =>
      cells.arrayElements[row * dimension + column];
  for (var row = 0; row < dimension; row++) {
    for (var column = 0; column < dimension; column++) {
      final fromRight = column == dimension - 1
          ? dataIn.arrayElements[row]
          : cell(row, column + 1);
      final fromBelow = row == dimension - 1
          ? dataIn.arrayElements[column]
          : cell(row + 1, column);
      cell(row, column) <=
          ShiftRegister<T>(
            mux<T>(direction, fromBelow, fromRight),
            clk: clk,
            enable: step & ~reset,
            depth: 1,
            name: '${name}_${row}_$column',
            definitionName:
                'SystolicTransposeCell_${payloadShapeIdentity(fromRight)}',
          ).dataOut;
    }
  }
  for (var index = 0; index < dimension; index++) {
    dataOut.arrayElements[index] <=
        mux<T>(direction, cell(0, index), cell(index, 0));
  }
  addOutput('primed') <= filled & ~reset;
  Sequential(clk, reset: reset, resetValues: {
    counter: 0,
    direction: 0,
    filled: 0,
  }, [
    If(step, then: [
      If(counter.eq(dimension - 1), then: [
        counter < 0,
        direction < ~direction,
        filled < 1,
      ], orElse: [
        counter < counter + 1,
      ]),
    ]),
  ]);
}