FilterBank constructor

FilterBank(
  1. Logic clk,
  2. Logic reset,
  3. Logic start,
  4. List<FilterSample> samples,
  5. Logic inputDone, {
  6. required int numTaps,
  7. required int dataWidth,
  8. required List<List<int>> coefficients,
  9. int numChannels = 2,
  10. LogicNet? dataBus,
  11. Logic? writeEnable,
  12. String name = 'FilterBank',
  13. String? definitionName,
})

Creates a FilterBank with numChannels channels (default 2).

Each channel has numTaps FIR taps at dataWidth bits. coefficients is a list of per-channel coefficient lists — coefficients[i] supplies the tap weights for channel i. samples is a LogicArray with one element per channel. inputDone when the input stream is complete.

Optionally pass dataBus (a LogicNet) and writeEnable to attach a bidirectional shared data bus via SharedDataBus. The bus latches external data when writeEnable is low and drives storedValue output.

Implementation

FilterBank(
  Logic clk,
  Logic reset,
  Logic start,
  List<FilterSample> samples,
  Logic inputDone, {
  required this.numTaps,
  required this.dataWidth,
  required List<List<int>> coefficients,
  this.numChannels = 2,
  LogicNet? dataBus,
  Logic? writeEnable,
  super.name = 'FilterBank',
  String? definitionName,
}) : super(definitionName: definitionName ?? 'FilterBank') {
  if (coefficients.length != numChannels) {
    throw Exception(
      'coefficients must have $numChannels entries (one per channel).',
    );
  }

  // ── Register ports ──
  clk = addInput('clk', clk);
  reset = addInput('reset', reset);
  start = addInput('start', start);
  inputDone = addInput('inputDone', inputDone);

  // One typed FilterSample input port per channel.
  final inPorts = <FilterSample>[];
  for (var ch = 0; ch < numChannels; ch++) {
    inPorts.add(addTypedInput('sample$ch', samples[ch]));
  }

  final channelOut = addTypedOutput<LogicArray>(
    'channelOut',
    ({name = 'channelOut'}) =>
        LogicArray([numChannels], dataWidth, name: name),
  );
  final validOut = addOutput('validOut');
  final done = addOutput('done');
  final state = addOutput('state', width: 3);

  // ── Controller FSM ──
  // Drain cycles: numTaps cycles per accumulation + pipeline depth (2) + 1
  final controller = FilterController(
    clk,
    reset,
    start,
    inPorts[0].valid, // valid is shared across channels
    inputDone,
    drainCycles: numTaps + 3,
    name: 'controller',
  );

  final filterEnable = controller.filterEnable;

  // ── Per-channel filter instantiation ──
  final srcIntfs = <FilterDataInterface>[];
  for (var ch = 0; ch < numChannels; ch++) {
    final srcIntf = FilterDataInterface(dataWidth: dataWidth);
    srcIntf.sampleIn <= inPorts[ch].data;
    srcIntf.validIn <= inPorts[ch].valid;

    FilterChannel(
      srcIntf,
      clk,
      reset,
      filterEnable,
      numTaps: numTaps,
      dataWidth: dataWidth,
      coefficients: coefficients[ch],
      name: 'ch$ch',
    );

    srcIntfs.add(srcIntf);
  }

  // ── Connect outputs ──
  for (var ch = 0; ch < numChannels; ch++) {
    channelOut.elements[ch] <= srcIntfs[ch].dataOut;
  }
  validOut <= srcIntfs[0].validOut;
  done <= controller.doneFlag;
  state <= controller.state;

  // ── Optional shared data bus (inOut port) ──
  if (dataBus != null && writeEnable != null) {
    final busPort = addInOut('dataBus', dataBus, width: dataWidth);
    writeEnable = addInput('writeEnable', writeEnable);
    final storedValue = addOutput('storedValue', width: dataWidth);

    final sharedBus = SharedDataBus(
      LogicNet(name: 'busNet', width: dataWidth)..gets(busPort),
      writeEnable,
      clk,
      reset,
      dataWidth: dataWidth,
    );
    storedValue <= sharedBus.storedValue;
  }
}