ReadyValidOrderedChannelGate<LogicType extends Logic> constructor

ReadyValidOrderedChannelGate<LogicType extends Logic>({
  1. required Logic clk,
  2. required Logic reset,
  3. required Logic commandValid,
  4. required Logic commandChannel,
  5. required List<ReadyValidInterface<LogicType>> upstreams,
  6. required ReadyValidInterface<LogicType> downstream,
  7. required int tokenDepth,
  8. TransactionCompletionPredicate<LogicType> followOnCompletion = completesTransactionOnEveryBeat,
  9. String name = 'ready_valid_ordered_channel_gate',
})

Creates an ordered follow-on channel gate.

Implementation

ReadyValidOrderedChannelGate(
    {required Logic clk,
    required Logic reset,
    required Logic commandValid,
    required Logic commandChannel,
    required List<ReadyValidInterface<LogicType>> upstreams,
    required ReadyValidInterface<LogicType> downstream,
    required this.tokenDepth,
    TransactionCompletionPredicate<LogicType> followOnCompletion =
        completesTransactionOnEveryBeat,
    String name = 'ready_valid_ordered_channel_gate'})
    : channelCount = upstreams.length,
      super(
          definitionName: 'ReadyValidOrderedChannelGate_N${upstreams.length}_'
              'D${tokenDepth}_'
              '${upstreams.isEmpty ? 'empty' : upstreams.first.data.name}',
          name: name) {
  if (channelCount < 1) {
    throw ArgumentError.value(upstreams, 'upstreams', 'Must not be empty.');
  }
  if (tokenDepth < 1) {
    throw ArgumentError.value(tokenDepth, 'tokenDepth', 'Must be positive.');
  }
  final channelWidth = max(1, channelCount.log2Ceil);
  if (commandChannel.width < channelWidth) {
    throw ArgumentError.value(commandChannel, 'commandChannel',
        'Must represent every configured channel.');
  }
  for (var index = 0; index < channelCount; index++) {
    upstreams[index].requireProviderComposableWith(
        ReadyValidContract.validThenReady,
        consumerName: '$name upstream $index');
  }
  downstream.requireConsumerComposableWith(ReadyValidContract.readyAndValid,
      providerName: name);

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  final localCommandValid = addInput('commandValid', commandValid);
  final localCommandChannel =
      addInput('commandChannel', commandChannel, width: commandChannel.width);
  this.upstreams = UnmodifiableListView([
    for (var index = 0; index < channelCount; index++)
      addPairInterface(upstreams[index], PairRole.consumer,
          uniquify: (original) => 'upstream_${index}_$original')
  ]);
  this.downstream = addPairInterface(downstream, PairRole.provider,
      uniquify: (original) => 'downstream_$original');
  addOutput('commandReady');
  addOutput('commandAccepted');
  addOutput('invalidCommandChannel');
  addOutput('empty');
  addOutput('full');
  addOutput('headChannel', width: commandChannel.width);

  final commandMatches = List.generate(
      channelCount,
      (index) => localCommandChannel
          .eq(Const(index, width: localCommandChannel.width))
          .named('${name}_command_matches_$index'));
  final commandChannelValid =
      commandMatches.rconcat().or().named('${name}_command_channel_valid');
  final followOnAccepted = Logic(name: '${name}_follow_on_accepted');
  final followOnComplete = followOnCompletion(this.downstream.data)
      .named('${name}_follow_on_complete');
  if (followOnComplete.width != 1) {
    throw ArgumentError.value(followOnCompletion, 'followOnCompletion',
        'Must produce a one-bit completion predicate.');
  }
  final followOnCompleted = (followOnAccepted & followOnComplete)
      .named('${name}_follow_on_completed');
  final localCommandReady = Logic(name: '${name}_command_ready');
  final localCommandAccepted = Logic(name: '${name}_command_accepted');
  final tokenFifo = Fifo(localClk, localReset,
      writeEnable: localCommandAccepted,
      writeData: localCommandChannel,
      readEnable: followOnCompleted,
      depth: tokenDepth,
      name: '${name}_token_fifo');
  final headMatches = List.generate(
      channelCount,
      (index) => (~tokenFifo.empty &
              tokenFifo.readData
                  .eq(Const(index, width: tokenFifo.readData.width)))
          .named('${name}_head_matches_$index'));

  localCommandReady <=
      (~tokenFifo.full | followOnCompleted) & commandChannelValid;
  localCommandAccepted <= localCommandValid & localCommandReady;
  commandReady <= localCommandReady;
  commandAccepted <= localCommandAccepted;
  invalidCommandChannel <= localCommandValid & ~commandChannelValid;
  empty <= tokenFifo.empty;
  full <= tokenFifo.full;
  headChannel <= tokenFifo.readData;

  this.downstream.valid <=
      List.generate(channelCount,
              (index) => headMatches[index] & this.upstreams[index].valid)
          .rconcat()
          .or();
  Logic selectedData = this.upstreams.last.data;
  for (var index = channelCount - 2; index >= 0; index--) {
    selectedData =
        mux(headMatches[index], this.upstreams[index].data, selectedData)
            .named('${name}_selected_data_$index');
  }
  this.downstream.data <= selectedData;
  for (var index = 0; index < channelCount; index++) {
    this.upstreams[index].ready <= headMatches[index] & this.downstream.ready;
  }
  followOnAccepted <= this.downstream.valid & this.downstream.ready;
}