ToggleGate constructor

ToggleGate({
  1. required Logic enable,
  2. required Logic data,
  3. required Logic clk,
  4. required Logic reset,
  5. dynamic resetValue,
  6. ClockGateControlInterface? clockGateControlIntf,
  7. String name = 'toggle_gate',
})

Constructs a ToggleGate to reduce power consumption by reducing toggling of the gatedData signal when enable is not asserted.

When enable is high, the gatedData signal will be the same as the data signal. When enable is low, the gatedData signal will be the same as the last value of data when enable was high.

If resetValue is provided, then the gatedData signal will be set to that when reset.

If no clockGateControlIntf is provided (left null), then a default clock gating implementation will be included for the internal sequential elements.

Implementation

ToggleGate({
  required Logic enable,
  required Logic data,
  required Logic clk,
  required Logic reset,
  dynamic resetValue,
  ClockGateControlInterface? clockGateControlIntf,
  super.name = 'toggle_gate',
}) {
  enable = addInput('enable', enable);
  data = addInput('data', data, width: data.width);
  clk = addInput('clk', clk);
  reset = addInput('reset', reset);

  if (resetValue != null && resetValue is Logic) {
    resetValue = addInput('resetValue', resetValue, width: resetValue.width);
  }

  if (clockGateControlIntf != null) {
    clockGateControlIntf =
        ClockGateControlInterface.clone(clockGateControlIntf)
          ..pairConnectIO(this, clockGateControlIntf, PairRole.consumer);
  }

  addOutput('gatedData', width: data.width);

  final lastData = Logic(name: 'lastData', width: data.width);

  final gateEnable = enable & (lastData.neq(data));

  final clkGate = ClockGate(
    clk,
    enable: gateEnable,
    reset: reset,
    controlIntf: clockGateControlIntf,
  );

  lastData <=
      flop(
          clkGate.gatedClk,
          en: clkGate.isPresent ? null : gateEnable,
          reset: reset,
          resetValue: resetValue,
          data);

  gatedData <=
      mux(
        enable,
        data,
        lastData,
      );
}