Count constructor

Count(
  1. Logic bus, {
  2. bool countOne = true,
  3. String name = 'count',
  4. bool reserveName = false,
  5. bool reserveDefinitionName = false,
  6. String? definitionName,
})

Count 1 or 0.

Takes in bus of type Logic. by default performs countOne (1) if countOne is false will count 0

Implementation

Count(Logic bus,
    {bool countOne = true,
    super.name = 'count',
    super.reserveName,
    super.reserveDefinitionName,
    String? definitionName})
    : super(definitionName: definitionName ?? 'Count_W${bus.width}') {
  bus = addInput('bus', bus, width: bus.width);
  Logic count = Const(0, width: max(1, log2Ceil(bus.width + 1)));
  for (var i = 0; i < bus.width; i++) {
    count = (count + (countOne ? bus[i] : ~bus[i]).zeroExtend(count.width))
        .named('count_$i');
  }
  _output =
      addOutput('count${countOne ? "One" : "Zero"}', width: count.width);

  _output <= count;
}