QcLdpcLayeredMinSumDecoderCore<Input extends Logic, Output extends Logic> constructor

QcLdpcLayeredMinSumDecoderCore<Input extends Logic, Output extends Logic>({
  1. required Logic clk,
  2. required Logic reset,
  3. required QcLdpcCode code,
  4. required QcLdpcLayerSchedule schedule,
  5. required int llrWidth,
  6. required int maxIterations,
  7. required Logic accept,
  8. required CodecFrame<Input> inputFrame,
  9. required CodecFrame<Output> resultSchema,
  10. StaticOrRuntimeParameter? consumeResult,
  11. int normalizationNumerator = 1,
  12. int normalizationShift = 0,
  13. int offset = 0,
  14. String name = 'qc_ldpc_layered_min_sum_decoder',
})

Creates a partially parallel layered decoder.

Implementation

QcLdpcLayeredMinSumDecoderCore({
  required Logic clk,
  required Logic reset,
  required this.code,
  required this.schedule,
  required this.llrWidth,
  required this.maxIterations,
  required super.accept,
  required super.inputFrame,
  required super.resultSchema,
  super.consumeResult,
  this.normalizationNumerator = 1,
  this.normalizationShift = 0,
  this.offset = 0,
  super.name = 'qc_ldpc_layered_min_sum_decoder',
}) : super(
        definitionName: schemaQualifiedDefinitionName(
          'QcLdpcLayeredMinSumDecoderCore_'
          'N${code.length}_K${code.messageBits}_Z${code.liftingSize}_'
          'P${schedule.parallelism}_Q${llrWidth}_I${maxIterations}_'
          'A${normalizationNumerator}S${normalizationShift}_O${offset}_'
          '${code.configurationSignature}_',
          [inputFrame, resultSchema],
        ),
      ) {
  if (!identical(schedule.code, code)) {
    throw ArgumentError(
      'The QC schedule must have been constructed from this code object.',
    );
  }
  validateLdpcMinSumConfiguration(
    llrWidth: llrWidth,
    maxIterations: maxIterations,
    normalizationNumerator: normalizationNumerator,
    normalizationShift: normalizationShift,
    offset: offset,
  );
  validateLdpcDecoderPayloads(inputFrame.data, result.data,
      code: code, llrWidth: llrWidth);

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  final stepWidth = max(1, schedule.stepCount.log2Ceil);
  final iterationWidth = max(1, (maxIterations + 1).log2Ceil);
  addOutput('decodedCodeword', width: code.length);
  addOutput('syndrome', width: code.parityBits);
  addOutput('busy');
  addOutput('scheduleStep', width: stepWidth);
  addOutput('currentIteration', width: iterationWidth);
  status = addTypedOutput(
    'status',
    CodecDecodeStatus(iterationWidth: iterationWidth).clone,
  );

  final processing = Logic(name: '${name}_processing');
  final outputValid = Logic(name: '${name}_output_valid');
  final step = Logic(name: '${name}_step', width: stepWidth);
  final iteration = Logic(name: '${name}_iteration', width: iterationWidth);
  final inputHadError = Logic(name: '${name}_input_had_error');
  final beliefs = [
    for (var variable = 0; variable < code.length; variable++)
      Logic(name: '${name}_belief_$variable', width: llrWidth),
  ];
  final checkMessages = [
    for (var edge = 0; edge < code.edgeCount; edge++)
      Logic(name: '${name}_check_message_$edge', width: llrWidth),
  ];
  final outputMessage =
      Logic(name: '${name}_output_message', width: code.messageBits);
  final outputCodeword =
      Logic(name: '${name}_output_codeword', width: code.length);
  final outputSyndrome =
      Logic(name: '${name}_output_syndrome', width: code.parityBits);
  final outputStart = Logic(name: '${name}_output_start');
  final outputLast = Logic(name: '${name}_output_last');
  final outputErrorDetected = Logic(name: '${name}_output_error_detected');
  final outputCorrected = Logic(name: '${name}_output_corrected');
  final outputUncorrectable = Logic(name: '${name}_output_uncorrectable');
  final outputIterations =
      Logic(name: '${name}_output_iterations', width: iterationWidth);

  final packedInput = inputFrame.data.packed;
  final inputLlrs = [
    for (var variable = 0; variable < code.length; variable++)
      packedInput.getRange(variable * llrWidth, (variable + 1) * llrWidth),
  ];
  final inputHardCodeword = _packHardDecisions(inputLlrs);
  final inputSyndrome = BinaryLinearTransform(
    inputHardCodeword,
    matrix: code.parityCheckMatrix,
    name: '${name}_input_syndrome',
  ).result;

  final maximumDegree = code.checkDegrees.reduce(max);
  final edgeWidth = max(1, code.edgeCount.log2Ceil);
  final variableWidth = max(1, code.length.log2Ceil);
  final selectedEdges = <List<Logic>>[];
  final selectedVariables = <List<Logic>>[];
  final selectedValids = <List<Logic>>[];
  final updatedMessages = <List<Logic>>[];
  final updatedBeliefs = <List<Logic>>[];

  for (var lane = 0; lane < schedule.parallelism; lane++) {
    final laneEdges = <Logic>[];
    final laneVariables = <Logic>[];
    final laneValids = <Logic>[];
    final extrinsic = <Logic>[];
    for (var slot = 0; slot < maximumDegree; slot++) {
      final validByStep = <int>[];
      final edgeByStep = <int>[];
      final variableByStep = <int>[];
      for (final scheduledStep in schedule.steps) {
        final edges = scheduledStep.laneEdges[lane];
        final slotValid = slot < edges.length;
        validByStep.add(slotValid ? 1 : 0);
        edgeByStep.add(slotValid ? edges[slot].edgeIndex : 0);
        variableByStep.add(slotValid ? edges[slot].variableIndex : 0);
      }
      final selectedValid = _selectStepConstant(
        step,
        validByStep,
        width: 1,
        name: '${name}_lane_${lane}_slot_${slot}_valid',
      );
      final selectedEdge = _selectStepConstant(
        step,
        edgeByStep,
        width: edgeWidth,
        name: '${name}_lane_${lane}_slot_${slot}_edge',
      );
      final selectedVariable = _selectStepConstant(
        step,
        variableByStep,
        width: variableWidth,
        name: '${name}_lane_${lane}_slot_${slot}_variable',
      );
      laneValids.add(selectedValid);
      laneEdges.add(selectedEdge);
      laneVariables.add(selectedVariable);
      extrinsic.add(
        saturatingLdpcSignedDifference(
          beliefs.selectIndex(selectedVariable),
          checkMessages.selectIndex(selectedEdge),
          width: llrWidth,
          name: '${name}_lane_${lane}_slot_${slot}_extrinsic',
        ),
      );
    }
    final node = LdpcMinSumCheckNode(
      extrinsic,
      activeMask: laneValids.rconcat(),
      normalizationNumerator: normalizationNumerator,
      normalizationShift: normalizationShift,
      offset: offset,
      name: '${name}_lane_${lane}_check',
    );
    selectedEdges.add(laneEdges);
    selectedVariables.add(laneVariables);
    selectedValids.add(laneValids);
    updatedMessages.add(node.messages);
    updatedBeliefs.add([
      for (var slot = 0; slot < maximumDegree; slot++)
        saturatingLdpcSignedSum(
          [extrinsic[slot], node.messages[slot]],
          width: llrWidth,
          name: '${name}_lane_${lane}_slot_${slot}_belief',
        ),
    ]);
  }

  final nextBeliefs = <Logic>[];
  for (var variable = 0; variable < code.length; variable++) {
    var selected = beliefs[variable];
    for (var lane = 0; lane < schedule.parallelism; lane++) {
      for (var slot = 0; slot < maximumDegree; slot++) {
        final matches = selectedValids[lane][slot] &
            selectedVariables[lane][slot].eq(variable);
        selected = mux(matches, updatedBeliefs[lane][slot], selected);
      }
    }
    nextBeliefs.add(selected.named('${name}_next_belief_$variable'));
  }
  final nextCheckMessages = <Logic>[];
  for (var edge = 0; edge < code.edgeCount; edge++) {
    var selected = checkMessages[edge];
    for (var lane = 0; lane < schedule.parallelism; lane++) {
      for (var slot = 0; slot < maximumDegree; slot++) {
        final matches =
            selectedValids[lane][slot] & selectedEdges[lane][slot].eq(edge);
        selected = mux(matches, updatedMessages[lane][slot], selected);
      }
    }
    nextCheckMessages.add(
      selected.named('${name}_next_check_message_$edge'),
    );
  }

  final nextHardCodeword = _packHardDecisions(nextBeliefs);
  final nextSyndrome = BinaryLinearTransform(
    nextHardCodeword,
    matrix: code.parityCheckMatrix,
    name: '${name}_next_syndrome',
  ).result;
  final inputMessage = _extractMessage(inputHardCodeword, code);
  final nextMessage = _extractMessage(nextHardCodeword, code);
  final inputAccepted = accepted;
  final outputAccepted = outputValid & resultReady;
  final inputClean = ~inputSyndrome.or();
  final nextConverged = ~nextSyndrome.or();
  final lastStep = step.eq(schedule.stepCount - 1);
  final nextIteration = iteration + Const(1, width: iterationWidth);
  final finalIteration = nextIteration.eq(maxIterations);

  canAccept <= ~processing & (~outputValid | resultReady);
  resultValid <= outputValid;
  connectPackedPayload(result.data, outputMessage);
  result.start <= outputStart;
  result.last <= outputLast;
  decodedCodeword <= outputCodeword;
  syndrome <= outputSyndrome;
  busy <= processing;
  scheduleStep <= step;
  currentIteration <= iteration;
  status.errorDetected <= outputErrorDetected;
  status.corrected <= outputCorrected;
  status.uncorrectable <= outputUncorrectable;
  status.frameDone <= outputValid & outputLast;
  status.iterationCount <= outputIterations;

  Sequential(localClk, reset: localReset, resetValues: {
    processing: 0,
    outputValid: 0,
    step: 0,
    iteration: 0,
    inputHadError: 0,
    outputMessage: 0,
    outputCodeword: 0,
    outputSyndrome: 0,
    outputStart: 0,
    outputLast: 0,
    outputErrorDetected: 0,
    outputCorrected: 0,
    outputUncorrectable: 0,
    outputIterations: 0,
    for (final belief in beliefs) belief: 0,
    for (final message in checkMessages) message: 0,
  }, [
    If(outputAccepted, then: [outputValid < 0]),
    If(inputAccepted, then: [
      outputStart < inputFrame.start,
      outputLast < inputFrame.last,
      inputHadError < ~inputClean,
      If(inputClean, then: [
        processing < 0,
        outputValid < 1,
        outputMessage < inputMessage,
        outputCodeword < inputHardCodeword,
        outputSyndrome < inputSyndrome,
        outputErrorDetected < 0,
        outputCorrected < 0,
        outputUncorrectable < 0,
        outputIterations < 0,
      ], orElse: [
        processing < 1,
        step < 0,
        iteration < 0,
        for (var variable = 0; variable < code.length; variable++)
          beliefs[variable] < inputLlrs[variable],
        for (final message in checkMessages) message < 0,
      ]),
    ]),
    If(processing, then: [
      for (var variable = 0; variable < code.length; variable++)
        beliefs[variable] < nextBeliefs[variable],
      for (var edge = 0; edge < code.edgeCount; edge++)
        checkMessages[edge] < nextCheckMessages[edge],
      If(lastStep, then: [
        iteration < nextIteration,
        If(nextConverged | finalIteration, then: [
          processing < 0,
          outputValid < 1,
          outputMessage < nextMessage,
          outputCodeword < nextHardCodeword,
          outputSyndrome < nextSyndrome,
          outputErrorDetected < inputHadError,
          outputCorrected < inputHadError & nextConverged,
          outputUncorrectable < ~nextConverged,
          outputIterations < nextIteration,
        ], orElse: [
          step < 0,
        ]),
      ], orElse: [
        step < step + 1,
      ]),
    ]),
  ]);
}