calculateCode method

  1. @override
  2. @protected
Logic calculateCode()
override

Generates the code to be provided by the transmitter, to be implemented by implementations.

Implementation

@override
@protected
Logic calculateCode() {
  final parityBits = List<Logic?>.generate(code.width, (index) => null);
  final dataBits = List<Logic>.generate(
      data.width, (index) => Logic(name: 'd${index + 1}')..gets(data[index]));

  final hammingCodeWidth = code.width - hammingType._extraParityBits;

  var dataIdx = 0;
  for (var i = 1;
      i <= transmission.width - hammingType._extraParityBits;
      i++) {
    if (!_isPowerOfTwo(i)) {
      final ilv = LogicValue.ofInt(i, hammingCodeWidth);

      for (var p = 0; p < hammingCodeWidth; p++) {
        if (ilv[p].toBool()) {
          if (parityBits[p] == null) {
            parityBits[p] = dataBits[dataIdx];
          } else {
            parityBits[p] = parityBits[p]! ^ dataBits[dataIdx];
          }
        }
      }
      dataIdx++;
    }
  }

  var calculatedCode = [
    for (var i = 0; i < hammingCodeWidth; i++)
      Logic(name: 'p${1 << i}')..gets(parityBits[i]!),
  ].rswizzle();

  if (hammingType.hasExtraParityBit) {
    // extra parity bit is calculated by calculating parity across entire rest
    // of the transmission
    final pExtra = Logic(name: 'pExtra')
      ..gets(ParityTransmitter([calculatedCode, data].swizzle()).code);
    calculatedCode = [pExtra, calculatedCode].swizzle();
  }

  return calculatedCode;
}