TreePseudoLruReplacement constructor
- required Logic clk,
- required Logic reset,
- required Logic validMask,
- required Logic eligibleMask,
- required List<
ReplacementAccessInterface> accesses, - required List<
ReplacementAllocationInterface> allocations, - required int entryCount,
- List<
ReplacementCommitInterface> commits = const [], - String name = 'tree_pseudo_lru_replacement',
Creates a tree pseudo-LRU policy.
Implementation
TreePseudoLruReplacement({
required super.clk,
required super.reset,
required super.validMask,
required super.eligibleMask,
required super.accesses,
required super.allocations,
required super.entryCount,
super.commits,
super.name = 'tree_pseudo_lru_replacement',
}) : super(
definitionName: 'TreePseudoLruReplacement_E${entryCount}_'
'H${accesses.length}_A${allocations.length}'
'${commits.isEmpty ? '' : '_C${commits.length}'}') {
if ((entryCount & (entryCount - 1)) != 0) {
throw ArgumentError.value(
entryCount, 'entryCount', 'Must be a power of two.');
}
Logic update(Logic state, Logic index) {
if (entryCount == 1) {
return state;
}
final nodes = List<({int start, int end})?>.filled(entryCount - 1, null);
void visit(int node, int start, int end) {
if (end - start == 1) {
return;
}
nodes[node] = (start: start, end: end);
final middle = (start + end) ~/ 2;
visit(node * 2 + 1, start, middle);
visit(node * 2 + 2, middle, end);
}
visit(0, 0, entryCount);
final extended = index.zeroExtend(indexWidth + 1);
return [
for (var node = 0; node < nodes.length; node++)
mux<Logic>(
extended.gte(Const(nodes[node]!.start, width: indexWidth + 1)) &
extended.lt(Const(nodes[node]!.end, width: indexWidth + 1)),
mux(
extended.lt(Const((nodes[node]!.start + nodes[node]!.end) ~/ 2,
width: indexWidth + 1)),
Const(1),
Const(0)),
state[node],
)
].rconcat();
}
ReplacementSelection select(Logic state, Logic candidates) {
ReplacementSelection visit(int node, int start, int end) {
if (end - start == 1) {
return (
valid: candidates[start],
index: Const(start, width: indexWidth)
);
}
final middle = (start + end) ~/ 2;
final left = visit(node * 2 + 1, start, middle);
final right = visit(node * 2 + 2, middle, end);
final chooseRight =
mux(state[node], right.valid, ~left.valid & right.valid);
return (
valid: left.valid | right.valid,
index: mux(chooseRight, right.index, left.index)
);
}
return visit(0, 0, entryCount);
}
buildPolicy(
stateWidth: entryCount == 1 ? 1 : entryCount - 1,
onAccess: update,
select: select,
onAllocation: update,
);
}