ReadyValidPriorityMux<LogicType extends Logic> constructor

ReadyValidPriorityMux<LogicType extends Logic>({
  1. required List<ReadyValidInterface<LogicType>> upstreams,
  2. required ReadyValidInterface<LogicType> downstream,
  3. String name = 'ready_valid_priority_mux',
})

Creates a lowest-index-wins ready/valid mux.

Implementation

ReadyValidPriorityMux({
  required List<ReadyValidInterface<LogicType>> upstreams,
  required ReadyValidInterface<LogicType> downstream,
  String name = 'ready_valid_priority_mux',
})  : inputCount = upstreams.length,
      super(
        definitionName: 'ReadyValidPriorityMux_N${upstreams.length}_'
            '${upstreams.isEmpty ? 'empty' : upstreams.first.data.name}',
        name: name,
      ) {
  if (upstreams.isEmpty) {
    throw ArgumentError.value(upstreams, 'upstreams', 'Must not be empty.');
  }
  for (var index = 0; index < upstreams.length; index++) {
    upstreams[index].requireProviderComposableWith(
      ReadyValidContract.validThenReady,
      consumerName: '$name upstream $index',
    );
  }
  downstream.requireConsumerComposableWith(
    ReadyValidContract.readyAndValid,
    providerName: name,
  );

  this.upstreams = UnmodifiableListView([
    for (var index = 0; index < inputCount; index++)
      addPairInterface(
        upstreams[index],
        PairRole.consumer,
        uniquify: (original) => 'upstream_${index}_$original',
      ),
  ]);
  this.downstream = addPairInterface(
    downstream,
    PairRole.provider,
    uniquify: (original) => 'downstream_$original',
  );
  addOutput('grant', width: inputCount);

  final arbiter = PriorityArbiter(
    this.upstreams.map((upstream) => upstream.valid).toList(),
    name: '${name}_arbiter',
  );
  grant <= arbiter.grants.rconcat();
  this.downstream.valid <= grant.or();

  Logic selectedData = this.upstreams.last.data;
  for (var index = inputCount - 2; index >= 0; index--) {
    selectedData = mux(
      arbiter.grants[index],
      this.upstreams[index].data,
      selectedData,
    ).named('${name}_selected_data_$index');
  }
  this.downstream.data <= selectedData;

  for (var index = 0; index < inputCount; index++) {
    this.upstreams[index].ready <=
        arbiter.grants[index] & this.downstream.ready;
  }
}