EdgeDetector constructor

EdgeDetector(
  1. Logic signal, {
  2. required Logic clk,
  3. Logic? reset,
  4. dynamic resetValue,
  5. Edge edgeType = Edge.pos,
  6. String? name,
  7. bool reserveName = false,
  8. bool reserveDefinitionName = false,
  9. String? definitionName,
})

Creates an edge detector which flags an edge when signal changes relative to its value in the previous cycle.

signal must be 1-bit.

If a reset is provided, then the first cycle after reset is deasserted, signal will be compared to resetValue (or 0, if not provided).

Implementation

EdgeDetector(Logic signal,
    {required Logic clk,
    Logic? reset,
    dynamic resetValue,
    this.edgeType = Edge.pos,
    String? name,
    super.reserveName,
    super.reserveDefinitionName,
    String? definitionName})
    : super(
          name: name ?? '${edgeType.name}_edge_detector',
          definitionName:
              definitionName ?? 'EdgeDetector_T${edgeType.name}') {
  if (signal.width != 1 ||
      (resetValue is Logic && resetValue.width != 1) ||
      (resetValue is LogicValue && resetValue.width != 1)) {
    throw RohdHclException('Can only detect edges on 1-bit signals.');
  }

  if (reset == null && resetValue != null) {
    throw RohdHclException(
        'If no reset is provided, then a resetValue cannot be provided.');
  }

  clk = addInput('clk', clk);
  signal = addInput('signal', signal);

  if (reset != null) {
    reset = addInput('reset', reset);
  }

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

  addOutput(_edgeName);

  final previousValue = Logic(name: 'previousValue')
    ..gets(
      flop(clk, reset: reset, resetValue: resetValue, signal),
    );

  edge <=
      [
        if (edgeType case Edge.pos || Edge.any) ~previousValue & signal,
        if (edgeType case Edge.neg || Edge.any) previousValue & ~signal,
      ].swizzle().or();
}