BchTwoErrorPipelinedDecoderCore<Input extends Logic, Output extends Logic> constructor

BchTwoErrorPipelinedDecoderCore<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 = 3,
  9. String name = 'bch_two_error_pipelined_decoder',
})

Creates a pipelined bounded two-error decoder.

Implementation

BchTwoErrorPipelinedDecoderCore({
  required Logic clk,
  required Logic reset,
  required BchCode code,
  required super.accept,
  required super.inputFrame,
  required super.resultSchema,
  super.consumeResult,
  this.pipelineDepth = 3,
  super.name = 'bch_two_error_pipelined_decoder',
}) : super(
        definitionName: schemaQualifiedDefinitionName(
          'BchTwoErrorPipelinedDecoderCore_${code.configurationSignature}_'
          'P${pipelineDepth}_',
          [inputFrame, resultSchema],
        ),
      ) {
  if (pipelineDepth < 3) {
    throw ArgumentError.value(
      pipelineDepth,
      'pipelineDepth',
      'Must be at least three.',
    );
  }
  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 locator = Logic(
    width: code.field.degree * 3,
    name: 'locator',
  );
  final locatorDegree = Logic(width: 3, name: 'locatorDegree');
  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 locatorStage = BchBerlekampMassey(
        p.get(syndromes),
        code: code,
        correctionCapacity: 2,
      );
      return [
        p.get(locator) < locatorStage.locator,
        p.get(locatorDegree) < locatorStage.locatorDegree,
      ];
    },
    (p) {
      final correctionStage = BchCorrectionStage(
        p.get(codeword),
        p.get(locator),
        p.get(locatorDegree),
        code: code,
        correctionCapacity: 2,
      );
      return [
        p.get(correctedCodeword) < correctionStage.correctedCodeword,
        p.get(message) < correctionStage.message,
        p.get(errorDetected) < p.get(syndromes).or(),
        p.get(corrected) < p.get(syndromes).or() & correctionStage.valid,
        p.get(uncorrectable) <
            p.get(syndromes).or() & correctionStage.uncorrectable,
      ];
    },
  ];
  for (var index = 3; index < pipelineDepth; index++) {
    stageOperations.add((p) => []);
  }

  final pipeline = ReadyValidPipeline(
    localClk,
    accept,
    resultReady,
    reset: localReset,
    signals: [
      codeword,
      syndromes,
      inputFrame.start,
      inputFrame.last,
      locator,
      locatorDegree,
      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);
}