TreeOfTwoInputModules constructor

TreeOfTwoInputModules(
  1. List<Logic> seq,
  2. Logic op(
    1. Logic a,
    2. Logic b
    )
)

Creates a tree that reduces seq using op.

Recursively splits seq in half until single elements remain, then combines them pair-wise with the supplied operation.

Implementation

TreeOfTwoInputModules(List<Logic> seq, this.op)
    : super(
        name: 'tree_of_two_input_modules',
        definitionName: 'TreeMax_N${seq.length}',
      ) {
  if (seq.isEmpty) {
    throw Exception("Don't use TreeOfTwoInputModules with an empty sequence");
  }

  for (var i = 0; i < seq.length; i++) {
    _seq.add(addInput('seq$i', seq[i], width: seq[i].width));
  }
  addOutput('out', width: seq[0].width);

  if (_seq.length == 1) {
    out <= _seq[0];
  } else {
    final a = TreeOfTwoInputModules(
      _seq.getRange(0, _seq.length ~/ 2).toList(),
      op,
    ).out;
    final b = TreeOfTwoInputModules(
      _seq.getRange(_seq.length ~/ 2, _seq.length).toList(),
      op,
    ).out;
    out <= op(a, b);
  }
}