CaseOneHotToBinary constructor

CaseOneHotToBinary(
  1. Logic onehot, {
  2. bool generateError = false,
  3. String name = 'one_hot_to_binary',
})

Constructs a Module which decodes a one-hot number onehot into a 2s complement number binary by encoding the position of the '1' using a Case block.

It is not recommended to use this for very large-width onehots since it will create many CaseItems. If the width is more than ~8, try using the TreeOneHotToBinary. The implementation does not support widths exceeding the maximum width of an int.

Implementation

CaseOneHotToBinary(super.onehot,
    {super.generateError = false, super.name = 'one_hot_to_binary'})
    : super.base() {
  if (onehot.width >= 32) {
    throw RohdHclException('Should not be used for large widths.');
  }
  Combinational([
    Case(onehot, conditionalType: ConditionalType.unique, [
      for (var i = 0; i < onehot.width; i++)
        CaseItem(
          Const(BigInt.from(1) << i, width: onehot.width),
          [
            binary < Const(i, width: binary.width),
            if (generateError) error! < 0,
          ],
        )
    ], defaultItem: [
      binary < Const(0, width: binary.width),
      if (generateError) error! < 1,
    ])
  ]);
}