ReadyValidFramedResourceOwner<LogicType extends Logic> constructor

ReadyValidFramedResourceOwner<LogicType extends Logic>({
  1. required Logic clk,
  2. required Logic reset,
  3. required IrrevocableReadyValidInterface<LogicType> upstream,
  4. required ReadyValidInterface<LogicType> downstream,
  5. required IrrevocableReadyValidInterface<Logic> allocation,
  6. required ReadyValidInterface<TagRelease> release,
  7. required int resourceCount,
  8. required TransactionStartPredicate<LogicType> isFirst,
  9. required TransactionCompletionPredicate<LogicType> isFinal,
  10. Logic? cancel,
  11. Logic? completion,
  12. String name = 'ready_valid_framed_resource_owner',
})

Creates packet-scoped ownership around an external resource allocator.

Implementation

ReadyValidFramedResourceOwner({
  required Logic clk,
  required Logic reset,
  required IrrevocableReadyValidInterface<LogicType> upstream,
  required ReadyValidInterface<LogicType> downstream,
  required IrrevocableReadyValidInterface<Logic> allocation,
  required ReadyValidInterface<TagRelease> release,
  required this.resourceCount,
  required TransactionStartPredicate<LogicType> isFirst,
  required TransactionCompletionPredicate<LogicType> isFinal,
  Logic? cancel,
  Logic? completion,
  super.name = 'ready_valid_framed_resource_owner',
})  : resourceWidth = max(1, resourceCount.log2Ceil),
      super(
        definitionName: 'ReadyValidFramedResourceOwner_R${resourceCount}_'
            '${upstream.data.name}_'
            '${cancel == null ? 'NoCancel' : 'Cancel'}_'
            '${completion == null ? 'Immediate' : 'Deferred'}_'
            '${payloadShapesIdentity([
              upstream.data,
              downstream.data,
              allocation.data,
              release.data
            ])}',
      ) {
  if (allocation.data.width != resourceWidth ||
      release.data.tag.width != resourceWidth) {
    throw ArgumentError(
        'Allocation and release resource widths must be $resourceWidth.');
  }
  if (!rohdPayloadsCompatible([upstream.data, downstream.data],
      ignoreConstantNames: true)) {
    throw ArgumentError(
        'Framed input and output payloads must be compatible.');
  }
  downstream.requireConsumerComposableWith(ReadyValidContract.irrevocable,
      providerName: name);
  release.requireConsumerComposableWith(ReadyValidContract.irrevocable,
      providerName: name);

  this.clk = addInput('clk', clk);
  this.reset = addInput('reset', reset);
  this.cancel = cancel == null ? Const(0) : addInput('cancel', cancel);
  this.completion =
      completion == null ? Const(0) : addInput('completion', completion);
  this.upstream = addPairInterface(
    upstream,
    PairRole.consumer,
    uniquify: (original) => 'upstream_$original',
  );
  this.downstream = addPairInterfaceFrom(
    IrrevocableReadyValidInterface<LogicType>(
        cloneInterfacePayload(downstream.data)),
    downstream,
    PairRole.provider,
    uniquify: (original) => 'downstream_$original',
  );
  this.allocation = addPairInterface(
    allocation,
    PairRole.consumer,
    uniquify: (original) => 'allocation_$original',
  );
  this.release = addPairInterfaceFrom(
    IrrevocableReadyValidInterface<TagRelease>(release.data.cloneTyped()),
    release,
    PairRole.provider,
    uniquify: (original) => 'release_$original',
  );

  final beatValid = Logic(name: '${name}_beat_valid');
  final beat = this.upstream.data.cloneTyped(name: '${name}_beat');
  final allocate = Logic(name: '${name}_allocate');
  final allocatedResource =
      this.allocation.data.cloneTyped(name: '${name}_allocated_resource');
  final transfer = Logic(name: '${name}_transfer');
  final takeRelease = Logic(name: '${name}_take_release');
  final core = FramedResourceOwner<LogicType>(
    clk: this.clk,
    reset: this.reset,
    beatValid: beatValid,
    beat: beat,
    transfer: transfer,
    allocate: allocate,
    allocatedResource: allocatedResource,
    takeRelease: takeRelease,
    resourceCount: resourceCount,
    isFirst: isFirst,
    isFinal: isFinal,
    cancel: cancel == null ? null : this.cancel,
    completion: completion == null ? null : this.completion,
    name: '${name}_core',
  );

  // A beat moves from upstream to downstream in one cycle, so the upstream
  // and allocation handshakes also wait for the downstream to be ready. The
  // core sees the raw beat and the raw allocation offer: it takes the
  // allocation only together with the first beat.
  ReadyValidIngressAttachmentPolicy<LogicType>(
    supportsSameCycleReplacement: false,
    coreGatesAccept: true,
  ).attach(
    upstream: this.upstream,
    canAccept: this.downstream.ready & core.canTransfer,
    accept: beatValid,
    payload: beat,
  );
  ReadyValidIngressAttachmentPolicy<Logic>(
    supportsSameCycleReplacement: false,
    coreGatesAccept: true,
  ).attach(
    upstream: this.allocation,
    canAccept: core.canAllocate & this.downstream.ready,
    accept: allocate,
    payload: allocatedResource,
  );
  // The downstream payload may be a packed or renamed bridge of the
  // upstream schema.
  final forwarded = this.downstream.data.cloneTyped(name: '${name}_forwarded')
    ..gets(this.upstream.data);
  ReadyValidEgressAttachmentPolicy<LogicType>().attach(
    downstream: this.downstream,
    offerValid: core.transferValid,
    payload: forwarded,
    transfer: transfer,
  );
  final released = this.release.data.cloneTyped(name: '${name}_released');
  released.tag <= core.releaseResource;
  ReadyValidEgressAttachmentPolicy<TagRelease>().attach(
    downstream: this.release,
    offerValid: core.releaseValid,
    payload: released,
    transfer: takeRelease,
  );

  for (final status in [
    core.resource,
    core.ownershipActive,
    core.acquireAccepted,
    core.finalAccepted,
    core.releaseAccepted,
    core.cancelled,
    core.bodyWithoutOwnership,
    core.nestedFirst,
  ]) {
    addOutput(status.name, width: status.width) <= status;
  }
}