BchPipelinedDecoderCore<Input extends Logic, Output extends Logic> constructor

BchPipelinedDecoderCore<Input extends Logic, Output extends Logic>({
  1. required Logic clk,
  2. required Logic reset,
  3. required BchCode code,
  4. required Logic accept,
  5. required CodecFrame<Input> inputFrame,
  6. required CodecFrame<Output> resultSchema,
  7. StaticOrRuntimeParameter? consumeResult,
  8. int pipelineDepth = 2,
  9. String name = 'bch_pipelined_decoder',
})

Creates a pipelined decoder.

Implementation

BchPipelinedDecoderCore({
  required Logic clk,
  required Logic reset,
  required BchCode code,
  required super.accept,
  required super.inputFrame,
  required super.resultSchema,
  super.consumeResult,
  this.pipelineDepth = 2,
  super.name = 'bch_pipelined_decoder',
}) : super(
        definitionName: schemaQualifiedDefinitionName(
          'BchPipelinedDecoderCore_${code.configurationSignature}_'
          'P${pipelineDepth}_',
          [inputFrame, resultSchema],
        ),
      ) {
  if (pipelineDepth < 2) {
    throw ArgumentError.value(
      pipelineDepth,
      'pipelineDepth',
      'Must be at least two.',
    );
  }
  if (inputFrame.data.width != code.length) {
    throw PortWidthMismatchException(inputFrame.data, code.length);
  }
  if (result.data.width != code.messageBits) {
    throw PortWidthMismatchException(
      result.data,
      code.messageBits,
    );
  }

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  final codeword = inputFrame.data.packed;
  final syndromeCalculator = BchSyndromeCalculator(codeword, code: code);
  final syndromes = syndromeCalculator.syndromes;
  final correctedCodeword =
      Logic(width: code.length, name: 'correctedCodeword');
  final message = Logic(width: code.messageBits, name: 'message');
  final errorDetected = Logic(name: 'errorDetected');
  final corrected = Logic(name: 'corrected');
  final uncorrectable = Logic(name: 'uncorrectable');

  final stageOperations = <List<Conditional> Function(PipelineStageInfo)>[
    (p) => [
          p.get(syndromes) < syndromeCalculator.syndromes,
        ],
    (p) {
      final syndromeNonzero = p.get(syndromes).or();
      Logic correctionMask = Const(0, width: code.length);
      Logic matched = Const(0);
      for (var position = 0; position < code.length; position++) {
        Logic positionMatch = Const(1);
        for (var index = 0;
            index < syndromeCalculator.syndromeCount;
            index++) {
          final syndrome = p.get(syndromes).slice(
                (index + 1) * syndromeCalculator.syndromeWidth - 1,
                index * syndromeCalculator.syndromeWidth,
              );
          final expected = code.field.x.pow(
            BigInt.from(
                ((code.firstRoot + index) * position) % code.fullLength),
          );
          positionMatch = positionMatch &
              syndrome.eq(Const(expected.logicValue, preferredRadix: 2));
        }
        matched = matched | positionMatch;
        correctionMask = correctionMask |
            mux(
              positionMatch,
              Logic.from(Const(BigInt.one << position, width: code.length)),
              Logic.from(Const(0, width: code.length)),
            );
      }
      final correctedValue = p.get(codeword) ^ correctionMask;
      return [
        p.get(correctedCodeword) < correctedValue,
        p.get(message) <
            correctedValue.slice(code.length - 1, code.parityBits),
        p.get(errorDetected) < syndromeNonzero,
        p.get(corrected) < matched & syndromeNonzero,
        p.get(uncorrectable) < syndromeNonzero & ~matched,
      ];
    },
  ];
  for (var index = 2; index < pipelineDepth; index++) {
    stageOperations.add((p) => []);
  }

  final pipeline = ReadyValidPipeline(
    localClk,
    accept,
    resultReady,
    reset: localReset,
    signals: [
      codeword,
      syndromes,
      inputFrame.start,
      inputFrame.last,
      correctedCodeword,
      message,
      errorDetected,
      corrected,
      uncorrectable,
    ],
    stages: stageOperations,
  );

  canAccept <= pipeline.readyPipeIn;
  resultValid <= pipeline.validPipeOut;
  connectPackedPayload(result.data, pipeline.get(message));
  result.start <= pipeline.get(inputFrame.start);
  result.last <= pipeline.get(inputFrame.last);

  status = addTypedOutput(
    'status',
    CodecDecodeStatus(iterationWidth: pipelineDepth.bitLength).clone,
  );
  status.errorDetected <= pipeline.get(errorDetected);
  status.corrected <= pipeline.get(corrected);
  status.uncorrectable <= pipeline.get(uncorrectable);
  status.frameDone <= pipeline.get(inputFrame.last);
  status.iterationCount <=
      Const(pipelineDepth, width: status.iterationCount.width);
}