BchSingleErrorDecoder constructor

BchSingleErrorDecoder(
  1. Logic codeword, {
  2. required BchCode code,
  3. String name = 'bch_single_error_decoder',
})

Creates a combinational single-error BCH decoder.

Implementation

BchSingleErrorDecoder(
  Logic codeword, {
  required BchCode code,
  super.name = 'bch_single_error_decoder',
}) : super(
        definitionName:
            'BchSingleErrorDecoder_N${code.length}_D${code.designedDistance}',
      ) {
  if (codeword.width != code.length) {
    throw PortWidthMismatchException(codeword, code.length);
  }
  final localCodeword = addInput('codeword', codeword, width: code.length);
  final syndromeCalculator = BchSyndromeCalculator(localCodeword, code: code);
  final packedSyndromes = syndromeCalculator.syndromes;
  final syndromeNonzero = Logic(name: 'syndromeNonzero');
  syndromeNonzero <= packedSyndromes.or();

  Logic correctionMask = Const(0, width: code.length);
  Logic matchedPosition = Const(0, width: code.length.bitLength);
  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 = packedSyndromes.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)),
        );
    matchedPosition = mux(
      positionMatch,
      Const(position, width: code.length.bitLength),
      matchedPosition,
    );
  }

  final correctedWord = localCodeword ^ correctionMask;
  addOutput('correctedCodeword', width: code.length) <= correctedWord;
  addOutput('message', width: code.messageBits) <=
      correctedWord.slice(code.length - 1, code.parityBits);
  addOutput('errorDetected') <= syndromeNonzero;
  addOutput('corrected') <= matched & syndromeNonzero;
  addOutput('uncorrectable') <= syndromeNonzero & ~matched;
  addOutput('errorPosition', width: code.length.bitLength) <= matchedPosition;
}