RoundRobinSharedPoolAdapter<LogicType extends Logic> constructor
- required Logic clk,
- required Logic reset,
- required List<
ReadyValidInterface< sources,PoolRequest< >LogicType> > - required List<
ReadyValidInterface< downstreams,LogicType> > - required int totalCapacity,
- SharedPoolGrantGenerator grantGenerator = roundRobinSharedPoolGrantGenerator,
- String grantGeneratorName = 'roundRobin',
- String name = 'round_robin_shared_pool_adapter',
Creates a shared-pool ingress adapter with an injectable grant policy.
Custom grantGenerator implementations require a distinct, stable
SystemVerilog-safe grantGeneratorName.
Implementation
RoundRobinSharedPoolAdapter(
{required Logic clk,
required Logic reset,
required List<ReadyValidInterface<PoolRequest<LogicType>>> sources,
required List<ReadyValidInterface<LogicType>> downstreams,
required this.totalCapacity,
SharedPoolGrantGenerator grantGenerator =
roundRobinSharedPoolGrantGenerator,
String grantGeneratorName = 'roundRobin',
String name = 'round_robin_shared_pool_adapter'})
: sourceCount = sources.length,
poolCount = downstreams.length,
super(
definitionName: 'RoundRobinSharedPoolAdapter_S${sources.length}'
'_P${downstreams.length}_C${totalCapacity}_'
'G${grantGeneratorName}_'
'${payloadShapesIdentity([
for (final source in sources) source.data.data,
for (final downstream in downstreams) downstream.data,
])}',
name: name) {
if (sourceCount < 1 || poolCount < 1 || totalCapacity < 1) {
throw ArgumentError('Sources, pools, and capacity must be positive.');
}
if (sources.any((source) => source.data.poolCount != poolCount)) {
throw ArgumentError('Every source must select exactly $poolCount pools.');
}
if (!RegExp(r'^[A-Za-z][A-Za-z0-9_]*$').hasMatch(grantGeneratorName)) {
throw ArgumentError.value(grantGeneratorName, 'grantGeneratorName',
'Must be a valid SystemVerilog identifier fragment.');
}
if (!identical(grantGenerator, roundRobinSharedPoolGrantGenerator) &&
grantGeneratorName == 'roundRobin') {
throw ArgumentError.value(grantGeneratorName, 'grantGeneratorName',
'A custom grant generator requires a distinct policy name.');
}
final payloads = [for (final source in sources) source.data.data];
if (!rohdPayloadsCompatible(payloads, ignoreConstantNames: true) ||
downstreams.any((downstream) => payloads.any((payload) =>
!rohdPayloadsCompatible([downstream.data, payload],
ignoreConstantNames: true)))) {
throw ArgumentError(
'Source and destination payloads must be compatible.');
}
final localClk = addInput('clk', clk);
final localReset = addInput('reset', reset);
final occupancyWidth = max(1, (totalCapacity + 1).log2Ceil);
addOutput('creditReturn', width: sourceCount);
addOutput('grant', width: sourceCount);
addOutput('occupancy', width: occupancyWidth);
this.sources = UnmodifiableListView([
for (var source = 0; source < sourceCount; source++)
addPairInterface(sources[source], PairRole.consumer,
uniquify: (original) => 'source_${source}_$original')
]);
this.downstreams = UnmodifiableListView([
for (var pool = 0; pool < poolCount; pool++)
addPairInterfaceFrom(
IrrevocableReadyValidInterface<LogicType>(
downstreams[pool].data.cloneTyped()),
downstreams[pool],
PairRole.provider,
uniquify: (original) => 'downstream_${pool}_$original',
)
]);
final ingressAccepted = Logic(name: 'ingress_accepted');
final hasRequest = this.sources.map((source) => source.valid).toList();
final grantedSources = grantGenerator(
requests: hasRequest,
accepted: ingressAccepted,
clk: localClk,
reset: localReset,
name: name);
if (grantedSources.length != sourceCount ||
grantedSources.any((grant) => grant.width != 1)) {
throw ArgumentError.value(grantedSources, 'grantGenerator',
'Must produce one grant bit for each source.');
}
final selected = List<Logic>.generate(sourceCount,
(source) => grantedSources[source].named('${name}_grant_$source'));
final occupancies = <Logic>[];
final returned = List<Logic>.generate(sourceCount, (_) => Const(0));
final selectedPool =
List.generate(poolCount, (pool) => Logic(name: 'pool_$pool'));
final occupancySum = Logic(name: 'occupancy_sum', width: occupancyWidth);
final capacityAvailable = occupancySum
.lt(Const(totalCapacity, width: occupancyWidth))
.named('${name}_capacity_available');
ingressAccepted <= selected.rconcat().or() & capacityAvailable;
for (var pool = 0; pool < poolCount; pool++) {
Logic poolRequest = Const(0);
for (var source = 0; source < sourceCount; source++) {
final sourceTargetsPool =
(selected[source] & this.sources[source].data.pool.eq(pool))
.named('${name}_source_${source}_targets_pool_$pool');
poolRequest = (poolRequest | sourceTargetsPool)
.named('${name}_pool_request_${pool}_through_source_$source');
}
selectedPool[pool] <= poolRequest;
}
for (var source = 0; source < sourceCount; source++) {
this.sources[source].ready <= selected[source] & capacityAvailable;
}
for (var pool = 0; pool < poolCount; pool++) {
final item = _SharedPoolItem<LogicType>(
this.sources.first.data.data.cloneTyped(),
sourceCount: sourceCount);
final writeEnable = Logic(name: 'write_$pool');
final readEnable = Logic(name: 'read_$pool');
final fifo = Fifo<_SharedPoolItem<LogicType>>(localClk, localReset,
writeEnable: writeEnable,
readEnable: readEnable,
writeData: item,
depth: totalCapacity,
generateOccupancy: true,
name: '${name}_fifo_$pool');
Logic selectedData = this.sources.last.data.data;
Logic selectedSource =
Const(sourceCount - 1, width: max(1, sourceCount.log2Ceil));
for (var source = sourceCount - 2; source >= 0; source--) {
selectedData =
mux(selected[source], this.sources[source].data.data, selectedData)
.named('${name}_selected_data_${pool}_through_source_$source');
selectedSource = mux(selected[source],
Const(source, width: selectedSource.width), selectedSource)
.named('${name}_selected_source_${pool}_through_source_$source');
}
item.data <= selectedData.named('${name}_selected_data_$pool');
item.source <= selectedSource.named('${name}_selected_source_$pool');
writeEnable <= selectedPool[pool] & capacityAvailable;
readEnable <= this.downstreams[pool].ready & ~fifo.empty;
this.downstreams[pool].valid <= ~fifo.empty;
this.downstreams[pool].data <= fifo.readData.data;
for (var source = 0; source < sourceCount; source++) {
returned[source] =
returned[source] | (readEnable & fifo.readData.source.eq(source));
}
occupancies.add(fifo.occupancy!.zeroExtend(occupancyWidth));
}
occupancySum <=
occupancies
.reduce((sum, value) => (sum + value).zeroExtend(occupancyWidth));
occupancy <= occupancySum;
creditReturn <= returned.rconcat();
grant <= selected.rconcat();
}