PolicyArbiter constructor

PolicyArbiter(
  1. List<Logic> requests, {
  2. required Logic clk,
  3. required Logic reset,
  4. required ArbitrationPolicy policy,
  5. ArbitrationAdvanceMode advanceMode = ArbitrationAdvanceMode.onGrant,
  6. ArbitrationRetentionPolicy retentionPolicy = ArbitrationRetentionPolicy.none,
  7. Logic? accepted,
  8. Logic? complete,
  9. Logic? captureOwner,
  10. Logic? releaseOwner,
  11. List<Logic> policyInputs = const [],
  12. String name = 'policy_arbiter',
  13. bool reserveName = false,
  14. bool reserveDefinitionName = false,
  15. String? definitionName,
})

Creates a composed policy arbiter.

Implementation

PolicyArbiter(
  super.requests, {
  required super.clk,
  required super.reset,
  required this.policy,
  this.advanceMode = ArbitrationAdvanceMode.onGrant,
  this.retentionPolicy = ArbitrationRetentionPolicy.none,
  Logic? accepted,
  Logic? complete,
  Logic? captureOwner,
  Logic? releaseOwner,
  List<Logic> policyInputs = const [],
  super.name = 'policy_arbiter',
  super.reserveName,
  super.reserveDefinitionName,
  String? definitionName,
})  : policyInputCount = policyInputs.length,
      super(
        definitionName: definitionName ??
            'PolicyArbiter_W${requests.length}_P${policy.identity}_'
                'A${advanceMode.name}_R${retentionPolicy.name}_'
                'Q${accepted == null ? 0 : 1}_'
                'I${policyInputs.map((input) => input.width).join('_')}',
      ) {
  if (requests.isEmpty) {
    throw ArgumentError.value(requests, 'requests', 'Must not be empty.');
  }
  for (final control in {
    'accepted': accepted,
    'complete': complete,
    'captureOwner': captureOwner,
    'releaseOwner': releaseOwner,
  }.entries) {
    if (control.value != null && control.value!.width != 1) {
      throw ArgumentError.value(
        control.value,
        control.key,
        'Arbitration controls must be one bit.',
      );
    }
  }
  if ((advanceMode == ArbitrationAdvanceMode.onAccepted ||
          retentionPolicy == ArbitrationRetentionPolicy.holdUntilAccepted) &&
      accepted == null) {
    throw ArgumentError(
      'Accepted advancement and hold retention require an accepted '
      'qualifier.',
    );
  }
  if (advanceMode == ArbitrationAdvanceMode.onCompletion &&
      complete == null) {
    throw ArgumentError(
      'onCompletion advancement requires a complete event.',
    );
  }
  if (advanceMode != ArbitrationAdvanceMode.onCompletion &&
      complete != null) {
    throw ArgumentError(
      'A complete event is valid only with onCompletion advancement.',
    );
  }
  if (retentionPolicy == ArbitrationRetentionPolicy.lockUntilRelease &&
      (captureOwner == null || releaseOwner == null)) {
    throw ArgumentError(
      'lockUntilRelease retention requires capture and release controls.',
    );
  }
  if (retentionPolicy != ArbitrationRetentionPolicy.lockUntilRelease &&
      (captureOwner != null || releaseOwner != null)) {
    throw ArgumentError(
      'Capture and release controls are valid only with lockUntilRelease '
      'retention.',
    );
  }

  final acceptedQualifier =
      accepted == null ? Const(1) : addInput('accepted', accepted);
  final completionEvent = advanceMode == ArbitrationAdvanceMode.onCompletion
      ? addInput('complete', complete!)
      : Const(0);
  final captureRequest =
      retentionPolicy == ArbitrationRetentionPolicy.lockUntilRelease
          ? addInput('captureOwner', captureOwner!)
          : Const(0);
  final releaseRequest =
      retentionPolicy == ArbitrationRetentionPolicy.lockUntilRelease
          ? addInput('releaseOwner', releaseOwner!)
          : Const(0);
  final localPolicyInputs = [
    for (var index = 0; index < policyInputs.length; index++)
      addInput(
        'policyInput_$index',
        policyInputs[index],
        width: policyInputs[index].width,
      ),
  ];

  addOutput('candidate', width: count);
  addOutput('selection', width: count);
  addOutput('retainedOwner', width: count);
  addOutput('acceptedGrant', width: count);
  addOutput('completedGrant', width: count);
  addOutput('locked');

  final completedGrantFeedback =
      Logic(name: '${name}_completed_grant_feedback', width: count);
  final candidateSelection = policy.build(
    requests: requests,
    completedGrant: completedGrantFeedback,
    policyInputs: localPolicyInputs,
    clk: clk,
    reset: reset,
    name: '${name}_${policy.identity}',
  );
  candidate <= candidateSelection;

  late final Logic captureControl;
  late final Logic releaseControl;
  late final Logic ownerSelection;
  late final Logic registeredOwner;
  late final Logic activeGrant;
  late final Logic retentionLocked;
  if (retentionPolicy == ArbitrationRetentionPolicy.none) {
    captureControl = Const(0);
    releaseControl = Const(0);
    ownerSelection = candidateSelection;
    registeredOwner = Const(0, width: count);
    activeGrant = candidateSelection & requests.rconcat();
    retentionLocked = Const(0);
  } else {
    captureControl = Logic(name: '${name}_capture_control');
    releaseControl = Logic(name: '${name}_release_control');
    final retainer = GrantRetainer(
      clk: clk,
      reset: reset,
      candidate: candidateSelection,
      requests: requests.rconcat(),
      capture: captureControl,
      release: releaseControl,
      name: '${name}_retainer',
    );
    ownerSelection = retainer.selection;
    registeredOwner = retainer.retainedOwner;
    activeGrant = retainer.grant;
    retentionLocked = retainer.locked;
  }
  selection <= ownerSelection;
  retainedOwner <= registeredOwner;
  locked <= retentionLocked;
  for (var index = 0; index < count; index++) {
    grants[index] <= activeGrant[index];
  }

  final acceptedBits = [
    for (var index = 0; index < count; index++)
      (activeGrant[index] & acceptedQualifier)
          .named('${name}_accepted_$index'),
  ].rconcat();
  acceptedGrant <= acceptedBits;

  switch (retentionPolicy) {
    case ArbitrationRetentionPolicy.none:
      break;
    case ArbitrationRetentionPolicy.holdUntilAccepted:
      captureControl <=
          ~retentionLocked & activeGrant.or() & ~acceptedQualifier;
      releaseControl <=
          retentionLocked & activeGrant.or() & acceptedQualifier;
    case ArbitrationRetentionPolicy.lockUntilRelease:
      captureControl <= captureRequest;
      releaseControl <= retentionLocked & releaseRequest;
  }
  switch (advanceMode) {
    case ArbitrationAdvanceMode.onGrant:
      completedGrantFeedback <= activeGrant;
    case ArbitrationAdvanceMode.onAccepted:
      completedGrantFeedback <= acceptedBits;
    case ArbitrationAdvanceMode.onCompletion:
      completedGrantFeedback <=
          ownerSelection & completionEvent.replicate(count);
  }
  completedGrant <= completedGrantFeedback;
}