ReadyValidSelectiveFork<LogicType extends Logic> constructor

ReadyValidSelectiveFork<LogicType extends Logic>({
  1. required Logic clk,
  2. required Logic reset,
  3. required ReadyValidInterface<LogicType> upstream,
  4. required List<ReadyValidInterface<LogicType>> downstreams,
  5. required Logic selection,
  6. String name = 'ready_valid_selective_fork',
})

Creates a selective fork with per-output completion tracking.

Implementation

ReadyValidSelectiveFork({
  required Logic clk,
  required Logic reset,
  required ReadyValidInterface<LogicType> upstream,
  required List<ReadyValidInterface<LogicType>> downstreams,
  required Logic selection,
  String name = 'ready_valid_selective_fork',
})  : outputCount = downstreams.length,
      super(
        definitionName: 'ReadyValidSelectiveFork_selective_fork_core_'
            'N${downstreams.length}_'
            '${_routingPayloadIdentity([
              upstream.data,
              for (final downstream in downstreams) downstream.data,
            ])}',
        name: name,
      ) {
  if (downstreams.isEmpty) {
    throw ArgumentError.value(
      downstreams,
      'downstreams',
      'Must not be empty.',
    );
  }
  if (selection.width != downstreams.length) {
    throw ArgumentError.value(
      selection,
      'selection',
      'Must have one bit for each downstream interface.',
    );
  }
  _validateFanoutPayloads(
      upstream.data, downstreams.map((downstream) => downstream.data));
  upstream.requireProviderComposableWith(
    ReadyValidContract.readyAndValid,
    consumerName: name,
  );
  for (var index = 0; index < downstreams.length; index++) {
    downstreams[index].requireConsumerComposableWith(
      ReadyValidContract.irrevocable,
      providerName: name,
    );
  }

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  this.upstream = addPairInterface(
    upstream,
    PairRole.consumer,
    uniquify: (original) => 'upstream_$original',
  );
  this.selection = addInput('selection', selection, width: selection.width);
  this.downstreams = UnmodifiableListView([
    for (var index = 0; index < outputCount; index++)
      addPairInterfaceFrom(
        IrrevocableReadyValidInterface<LogicType>(
          downstreams[index].data.cloneTyped(),
        ),
        downstreams[index],
        PairRole.provider,
        uniquify: (original) => 'downstream_${index}_$original',
      ),
  ]);
  addOutput('occupied');

  final load = Logic(name: '${name}_load');
  final advance = Logic(name: '${name}_advance', width: outputCount);
  final payload = this.upstream.data.cloneTyped();
  final coreFunctor = SelectiveForkCoreFunctor<LogicType>(
    outputCount: outputCount,
  );
  final binding = coreFunctor.construct(
    SelectiveForkCoreRequest<LogicType>(
      clk: localClk,
      reset: localReset,
      payload: payload,
      selection: this.selection,
      load: load,
      advance: advance,
    ),
    name: '${name}_core',
  );

  payload <= this.upstream.data;
  load <= this.upstream.valid & binding.canLoad;
  advance <=
      binding.outputValidMask &
          this
              .downstreams
              .map((downstream) => downstream.ready)
              .toList()
              .rconcat();
  occupied <= binding.occupied;
  this.upstream.ready <= binding.canLoad;
  for (var index = 0; index < outputCount; index++) {
    this.downstreams[index].valid <= binding.outputValidMask[index];
    this.downstreams[index].data <= binding.retainedPayload;
  }
}