PhysicalRegisterState<LogicType extends Logic> constructor
- required Logic clk,
- required Logic reset,
- required List<
Logic> markBusy, - required List<
Logic> busyTags, - required List<
Logic> writebacks, - required List<
ExecutionWritebackEvent< writebackData,LogicType> > - required List<
Logic> readTags, - required int capacity,
- required int valueWidth,
- Logic wakeupValue(
- ExecutionWritebackEvent<
LogicType> writeback
- ExecutionWritebackEvent<
- List<
StaticOrRuntimeParameter?> wakeupTransfers = const [], - Logic? restore,
- Logic? restoreBusyResources,
- Logic writebackPredicate(
- ExecutionWritebackEvent<
LogicType> writeback
- ExecutionWritebackEvent<
- Logic writebackValue(
- ExecutionWritebackEvent<
LogicType> writeback
- ExecutionWritebackEvent<
- DataPortInterface<
Logic> writePortFactory({})?, - void writePortConfigurer({
- required DataPortInterface<
Logic> port, - required ExecutionWritebackEvent<
LogicType> writeback,
- required DataPortInterface<
- RegisterFileFunctor registerFileFunctor = flopRegisterFileFunctor,
- String policyIdentity = 'default',
- String? definitionNameOverride,
- 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',
);
}