PhysicalRegisterState<LogicType extends Logic> constructor

PhysicalRegisterState<LogicType extends Logic>({
  1. required Logic clk,
  2. required Logic reset,
  3. required List<Logic> markBusy,
  4. required List<Logic> busyTags,
  5. required List<Logic> writebacks,
  6. required List<ExecutionWritebackEvent<LogicType>> writebackData,
  7. required List<Logic> readTags,
  8. required int capacity,
  9. required int valueWidth,
  10. Logic wakeupValue(
    1. ExecutionWritebackEvent<LogicType> writeback
    )?,
  11. List<StaticOrRuntimeParameter?> wakeupTransfers = const [],
  12. Logic? restore,
  13. Logic? restoreBusyResources,
  14. Logic writebackPredicate(
    1. ExecutionWritebackEvent<LogicType> writeback
    )?,
  15. Logic writebackValue(
    1. ExecutionWritebackEvent<LogicType> writeback
    )?,
  16. DataPortInterface<Logic> writePortFactory({
    1. required String name,
    2. required int tagWidth,
    3. required int valueWidth,
    })?,
  17. void writePortConfigurer({
    1. required DataPortInterface<Logic> port,
    2. required ExecutionWritebackEvent<LogicType> writeback,
    })?,
  18. RegisterFileFunctor registerFileFunctor = flopRegisterFileFunctor,
  19. String policyIdentity = 'default',
  20. String? definitionNameOverride,
  21. String name = 'physical_register_state',
})

Creates physical-register state driven by explicit actions.

writebackPredicate selects the writebacks that change state; by default those without an exception or replay. writebackValue and wakeupValue extract the stored and wakeup values; a non-null wakeupValue generates the wakeup offers. The extracted writeback values and write-port payloads must be pairwise compatible because they target the same register file. writePortFactory and writePortConfigurer shape the register-file write ports built by registerFileFunctor. These callbacks change the generated hardware, so distinguish them with policyIdentity or definitionNameOverride. Supply restore and restoreBusyResources together to generate the atomic busy-bitmap replacement.

Implementation

PhysicalRegisterState({
  required Logic clk,
  required Logic reset,
  required List<Logic> markBusy,
  required List<Logic> busyTags,
  required List<Logic> writebacks,
  required List<ExecutionWritebackEvent<LogicType>> writebackData,
  required List<Logic> readTags,
  required this.capacity,
  required this.valueWidth,
  Logic Function(ExecutionWritebackEvent<LogicType> writeback)? wakeupValue,
  List<StaticOrRuntimeParameter?> wakeupTransfers = const [],
  Logic? restore,
  Logic? restoreBusyResources,
  Logic Function(ExecutionWritebackEvent<LogicType> writeback)?
      writebackPredicate,
  Logic Function(ExecutionWritebackEvent<LogicType> writeback)?
      writebackValue,
  DataPortInterface Function({
    required int valueWidth,
    required int tagWidth,
    required String name,
  })? writePortFactory,
  void Function({
    required DataPortInterface port,
    required ExecutionWritebackEvent<LogicType> writeback,
  })? writePortConfigurer,
  RegisterFileFunctor registerFileFunctor = flopRegisterFileFunctor,
  String policyIdentity = 'default',
  String? definitionNameOverride,
  super.name = 'physical_register_state',
})  : tagWidth = capacity < 1 ? 0 : max(1, capacity.log2Ceil),
      super(
        definitionName: definitionNameOverride ??
            'PhysicalRegisterState_'
                'C${capacity}_V${valueWidth}_B${markBusy.length}_'
                'W${writebacks.length}_R${readTags.length}_'
                'U${wakeupValue == null ? 0 : writebacks.length}'
                '${_scheduledWakeupSuffix(
              wakeupValue != null,
              writebacks.length,
              wakeupTransfers,
            )}_'
                '${restore == null ? '' : 'Restore_'}'
                '${registerFileFunctor.identity}_${policyIdentity}_'
                '${payloadShapesIdentity(writebackData)}',
      ) {
  if (definitionNameOverride == null &&
      (policyIdentity.trim().isEmpty ||
          (policyIdentity == 'default' &&
              (wakeupValue != null ||
                  writebackPredicate != null ||
                  writebackValue != null ||
                  writePortFactory != null ||
                  writePortConfigurer != null)))) {
    throw ArgumentError.value(
        policyIdentity,
        'policyIdentity',
        'Custom physical-register callbacks require a distinct policy '
            'identity or definitionNameOverride.');
  }
  if (capacity < 1 ||
      valueWidth < 1 ||
      markBusy.isEmpty ||
      writebacks.isEmpty ||
      readTags.isEmpty ||
      (wakeupTransfers.isNotEmpty &&
          wakeupTransfers.length != writebacks.length) ||
      (restoreBusyResources != null &&
          restoreBusyResources.width != capacity)) {
    throw ArgumentError(
        'Physical-state geometry and lane counts must be positive and '
        'match.');
  }
  if (busyTags.length != markBusy.length ||
      writebackData.length != writebacks.length ||
      (restore == null) != (restoreBusyResources == null)) {
    throw ArgumentError(
        'Every busy, writeback, and restore action requires its payload.');
  }
  if (busyTags.any((tag) => tag.width != tagWidth) ||
      writebackData.any(
        (writeback) => writeback.destinationWidth != tagWidth,
      ) ||
      readTags.any((tag) => tag.width != tagWidth)) {
    throw ArgumentError(
        'Busy, writeback, wakeup, read, value, and tag geometry must match.');
  }
  if (wakeupValue == null && wakeupTransfers.isNotEmpty) {
    throw ArgumentError('wakeupValue is required when wakeups are supplied.');
  }

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  final localMarkBusy = [
    for (var lane = 0; lane < markBusy.length; lane++)
      addInput('markBusy$lane', markBusy[lane]),
  ];
  final localBusyTags = [
    for (var lane = 0; lane < markBusy.length; lane++)
      addInput('busyTag$lane', busyTags[lane], width: tagWidth),
  ];
  final localWritebacks = [
    for (var lane = 0; lane < writebacks.length; lane++)
      addInput('writeback$lane', writebacks[lane]),
  ];
  final localWritebackData = [
    for (var lane = 0; lane < writebacks.length; lane++)
      addTypedInput('writebackData$lane', writebackData[lane]),
  ];
  final localWakeupTransfers = <Logic?>[
    for (var lane = 0; lane < writebacks.length; lane++)
      if (wakeupValue == null)
        null
      else
        resolveTransferControl(
                wakeupTransfers.isEmpty ? null : wakeupTransfers[lane],
                name: 'wakeupTransfer$lane')
            .getRuntimeInput(this),
  ];
  final localReadTags = [
    for (var port = 0; port < readTags.length; port++)
      addInput('readTag$port', readTags[port], width: tagWidth),
  ];
  final localRestore = restore == null ? null : addInput('restore', restore);
  final localRestoreBusyResources = restoreBusyResources == null
      ? null
      : addInput('restoreBusyResources', restoreBusyResources,
          width: capacity);

  final wakeupValues = [
    if (wakeupValue != null)
      for (final event in localWritebackData) wakeupValue(event),
  ];
  if (wakeupValues.any((value) => value.width != valueWidth)) {
    throw ArgumentError('The wakeup value must match valueWidth.');
  }
  canWriteback = [
    for (var lane = 0; lane < writebacks.length; lane++)
      addOutput('canWriteback$lane'),
  ];
  wakeupValid = [
    if (wakeupValue != null)
      for (var lane = 0; lane < writebacks.length; lane++)
        addOutput('wakeupValid$lane'),
  ];
  wakeupData = [
    for (final (lane, value) in wakeupValues.indexed)
      addTypedPayloadOutput(
          this,
          'wakeupData$lane',
          WakeupEvent(cloneInterfacePayload(value),
              tagWidth: tagWidth, name: 'wakeupData$lane')),
  ];
  queryBusy = [
    for (var port = 0; port < localReadTags.length; port++)
      addOutput('queryBusy$port'),
  ];
  readValues = [
    for (var port = 0; port < localReadTags.length; port++)
      addOutput('readValue$port', width: valueWidth),
  ];
  addOutput('suppressedWritebacks', width: writebacks.length);
  addOutput('busyResources', width: capacity);

  final readyUpdates = <ReadyValidInterface<TagRelease>>[];
  final writePorts = <DataPortInterface>[];
  final writePayloads = <Logic>[];
  final suppressed = <Logic>[];
  final predicate = writebackPredicate ?? _defaultWritebackPredicate;
  final valueExtractor = writebackValue ?? _defaultWritebackValue;

  for (var lane = 0; lane < localWritebacks.length; lane++) {
    final event = localWritebackData[lane];
    final hasSideEffect = predicate(event);
    final wakeupTransfer = localWakeupTransfers[lane];

    canWriteback[lane] <=
        (wakeupTransfer == null
            ? Const(1)
            : mux(hasSideEffect, wakeupTransfer, Const(1)));
    if (wakeupValue != null) {
      wakeupValid[lane] <= localWritebacks[lane] & hasSideEffect;
      wakeupData[lane].data <= wakeupValues[lane];
      wakeupData[lane].tag <= event.destination;
      wakeupData[lane].speculative <= Const(0);
    }

    final accepted = (canWriteback[lane] & localWritebacks[lane])
        .named('${name}_writeback_${lane}_accepted');
    final acceptedSideEffect = accepted & hasSideEffect;
    final readyUpdate = ReadyThenValidInterface(
      TagRelease(tagWidth: tagWidth, name: 'readyUpdate$lane'),
    );
    readyUpdate.valid <= acceptedSideEffect;
    readyUpdate.data.tag <= event.destination;
    readyUpdates.add(readyUpdate);

    final writePort = writePortFactory == null
        ? DataPortInterface(valueWidth, tagWidth)
        : writePortFactory(
            valueWidth: valueWidth,
            tagWidth: tagWidth,
            name: '${name}_write_$lane',
          );
    if (writePort.dataWidth != valueWidth ||
        writePort.addrWidth != tagWidth) {
      throw ArgumentError(
          'writePortFactory must match physical-state data and tag widths.');
    }
    writePort.en <= acceptedSideEffect;
    writePort.addr <= event.destination;
    final extractedValue = valueExtractor(event);
    if (extractedValue.width != valueWidth) {
      throw ArgumentError('The writeback value must match valueWidth.');
    }
    writePort.data <= extractedValue;
    writePortConfigurer?.call(
      port: writePort,
      writeback: event,
    );
    writePayloads.addAll([extractedValue, writePort.data]);
    writePorts.add(writePort);
    suppressed.add(accepted & ~hasSideEffect);
  }
  if (!rohdPayloadsCompatible(writePayloads, allowRenamedClones: true)) {
    throw ArgumentError('Writeback values and register-file write ports '
        'must have compatible payloads.');
  }
  suppressedWritebacks <= suppressed.rconcat();

  // The scoreboard is always ready, so each action is its update's valid.
  final busyUpdates = [
    for (var lane = 0; lane < localMarkBusy.length; lane++)
      ReadyAndValidInterface(Logic(name: 'busyUpdate$lane', width: tagWidth))
        ..valid.gets(localMarkBusy[lane])
        ..data.gets(localBusyTags[lane]),
  ];
  final restoreUpdate = localRestore == null ||
          localRestoreBusyResources == null
      ? null
      : (ReadyAndValidInterface(Logic(name: 'restoreUpdate', width: capacity))
        ..valid.gets(localRestore)
        ..data.gets(localRestoreBusyResources));
  scoreboard = ReadyValidMultiScoreboard(
    clk: localClk,
    reset: localReset,
    markBusy: busyUpdates,
    markReady: readyUpdates,
    queryTags: localReadTags,
    capacity: capacity,
    restore: restoreUpdate,
    name: '${name}_scoreboard',
  );
  for (var port = 0; port < queryBusy.length; port++) {
    queryBusy[port] <= scoreboard.queryBusy[port];
  }
  busyResources <= scoreboard.busyResources;

  readPorts = [
    for (var port = 0; port < localReadTags.length; port++)
      DataPortInterface(valueWidth, tagWidth),
  ];
  for (var port = 0; port < readPorts.length; port++) {
    readPorts[port].en <= Const(1);
    readPorts[port].addr <= localReadTags[port];
    readValues[port] <= readPorts[port].data;
  }
  registerFile = registerFileFunctor(
    clk: localClk,
    reset: localReset,
    writePorts: writePorts,
    readPorts: readPorts,
    entryCount: capacity,
    readUnderWrite: RegisterFileReadUnderWrite.writeFirst,
    resetValue: 0,
    name: '${name}_register_file',
  );
}