MacUnit constructor

MacUnit(
  1. Logic sampleIn,
  2. Logic coeffIn,
  3. Logic accumIn,
  4. Logic clk,
  5. Logic reset,
  6. Logic enable, {
  7. required int dataWidth,
  8. String name = 'MacUnit',
})

Creates a MacUnit that multiplies sampleIn by coeffIn in stage 0 and adds the product to accumIn in stage 1.

clk, reset, and enable control the pipeline registers.

Implementation

MacUnit(
  Logic sampleIn,
  Logic coeffIn,
  Logic accumIn,
  Logic clk,
  Logic reset,
  Logic enable, {
  required this.dataWidth,
  super.name = 'MacUnit',
}) : super(definitionName: 'MacUnit_W$dataWidth') {
  sampleIn = addInput('sampleIn', sampleIn, width: dataWidth);
  coeffIn = addInput('coeffIn', coeffIn, width: dataWidth);
  accumIn = addInput('accumIn', accumIn, width: dataWidth);
  clk = addInput('clk', clk);
  reset = addInput('reset', reset);
  enable = addInput('enable', enable);
  final result = addOutput('result', width: dataWidth);
  final stall = (~enable).named('stall', naming: Naming.mergeable);

  // A 2-stage pipeline: multiply, then accumulate
  final pipe = Pipeline(
    clk,
    reset: reset,
    stalls: [stall, stall],
    stages: [
      // Stage 0: multiply
      (p) => [
            // Product = sample * coefficient (truncated to dataWidth)
            p.get(sampleIn) <
                (p.get(sampleIn) * p.get(coeffIn)).named('product'),
          ],
      // Stage 1: accumulate
      (p) => [
            p.get(sampleIn) <
                (p.get(sampleIn) + p.get(accumIn)).named('macSum'),
          ],
    ],
    signals: [sampleIn, coeffIn, accumIn],
  );

  result <= pipe.get(sampleIn);
}