buildPolicy method
Builds ordered access and allocation processing around a policy state.
Implementation
@protected
void buildPolicy({
required int stateWidth,
required Logic Function(Logic state, Logic index) onAccess,
required ReplacementSelection Function(Logic state, Logic candidates)
select,
required Logic Function(Logic state, Logic index) onAllocation,
}) {
final state = Logic(name: 'replacementState', width: stateWidth);
final nextState = Logic(name: 'replacementNextState', width: stateWidth);
state <= flop(clk, mux(reset, Const(0, width: stateWidth), nextState));
var pending = state;
Logic invalid = Const(0);
for (final access in localAccesses) {
final inRange = access.index
.zeroExtend(indexWidth + 1)
.lt(Const(entryCount, width: indexWidth + 1));
invalid = invalid | (access.en & ~inRange);
pending =
mux(access.en & inRange, onAccess(pending, access.index), pending);
}
invalidAccess <= invalid;
Logic badCommit = Const(0);
for (final commit in localCommits) {
final inRange = commit.index
.zeroExtend(indexWidth + 1)
.lt(Const(entryCount, width: indexWidth + 1));
badCommit = badCommit | (commit.en & ~inRange);
pending = mux(
commit.en & inRange, onAllocation(pending, commit.index), pending);
}
Logic reserved = Const(0, width: entryCount);
for (final allocation in localAllocations) {
final available = eligibleMask & ~reserved;
final unused = available & ~validMask;
final candidates = mux(unused.or(), unused, available);
final choice = select(pending, candidates);
final granted = allocation.en & choice.valid;
final committed = granted & allocation.commit;
badCommit = badCommit | (allocation.commit & ~granted);
allocation.valid <= granted;
allocation.index <=
mux(granted, choice.index, Const(0, width: indexWidth));
final selectedValid = [
for (var index = 0; index < entryCount; index++)
validMask[index] & storedIndexMatches(choice.index, index)
].reduce((left, right) => left | right);
allocation.evict <= granted & selectedValid;
reserved = reserved |
[
for (var index = 0; index < entryCount; index++)
granted & storedIndexMatches(choice.index, index)
].rconcat();
pending = mux(committed, onAllocation(pending, choice.index), pending);
}
invalidCommit <= badCommit;
nextState <= pending;
}