ThermometerToBinary constructor

ThermometerToBinary(
  1. Logic thermometer, {
  2. String name = 'thermometer_to_binary',
  3. bool reserveName = false,
  4. bool reserveDefinitionName = false,
  5. String? definitionName,
})

Creates a thermometer-to-binary decoder.

Implementation

ThermometerToBinary(
  Logic thermometer, {
  super.name = 'thermometer_to_binary',
  super.reserveName,
  super.reserveDefinitionName,
  String? definitionName,
}) : super(
        definitionName:
            definitionName ?? 'ThermometerToBinary_W${thermometer.width}',
      ) {
  if (thermometer.width < 1) {
    throw ArgumentError.value(
        thermometer.width, 'thermometer.width', 'Must be positive.');
  }
  thermometer =
      addInput('thermometer', thermometer, width: thermometer.width);
  final countWidth = thermometer.width.bitLength;
  Logic count = Const(0, width: countWidth);
  for (final bit in thermometer.elements) {
    count = count + bit.zeroExtend(countWidth);
  }
  addOutput('binary', width: countWidth) <= count;
  final invalid = [
    for (var bit = 1; bit < thermometer.width; bit++)
      thermometer[bit] & ~thermometer[bit - 1],
  ];
  addOutput('valid') <= (invalid.isEmpty ? Const(1) : ~invalid.concat().or());
}