ReplacementPolicyModel constructor
- required Logic clk,
- required Logic reset,
- required Logic validMask,
- required Logic eligibleMask,
- required List<
ReplacementAccessInterface> accesses, - required List<
ReplacementAllocationInterface> allocations, - required int entryCount,
- required ReplacementPolicyModelKind kind,
- List<
ReplacementCommitInterface> commits = const [], - String name = 'behavioral_replacement',
Creates a software implementation of the replacement port protocol.
Implementation
ReplacementPolicyModel({
required super.clk,
required super.reset,
required super.validMask,
required super.eligibleMask,
required super.accesses,
required super.allocations,
required super.entryCount,
required this.kind,
super.commits,
super.name = 'behavioral_replacement',
}) : super(
definitionName: 'BehavioralReplacement_${kind.name}_E${entryCount}_'
'H${accesses.length}_A${allocations.length}_'
'C${commits.length}') {
if (kind == ReplacementPolicyModelKind.treePseudoLru &&
(entryCount & (entryCount - 1)) != 0) {
throw ArgumentError.value(
entryCount, 'entryCount', 'Tree pseudo-LRU requires a power of two.');
}
description = ReplacementPolicyModelDescription(
replacementKind: kind,
entryCount: entryCount,
accessCount: localAccesses.length,
commitCount: localCommits.length,
allocationCount: localAllocations.length);
final stateWidth = kind == ReplacementPolicyModelKind.roundRobin
? indexWidth
: entryCount == 1
? 1
: entryCount - 1;
_setState(LogicValue.filled(stateWidth, LogicValue.x));
LogicValue value(Logic signal, {required bool previous}) =>
previous ? signal.previousValue! : signal.value;
_FastEvaluation? evaluateFast({required bool previous}) {
var pending = _stateBits;
if (pending == null) {
return null;
}
int? known(Logic signal) {
final current = value(signal, previous: previous);
return current.isValid ? current.toInt() : null;
}
final valid = known(validMask);
final eligible = known(eligibleMask);
if (valid == null || eligible == null) {
return null;
}
var invalidHit = false;
var badCommit = false;
for (final access in localAccesses) {
final enable = known(access.en);
final index = known(access.index);
if (enable == null || index == null) {
return null;
}
if (enable == 1) {
if (index < entryCount) {
pending = _onAccessBits(pending!, index);
} else {
invalidHit = true;
}
}
}
for (final commit in localCommits) {
final enable = known(commit.en);
final index = known(commit.index);
if (enable == null || index == null) {
return null;
}
if (enable == 1) {
if (index < entryCount) {
pending = _onAllocationBits(pending!, index);
} else {
badCommit = true;
}
}
}
var reserved = 0;
final results = <_FastAllocation>[];
for (final allocation in localAllocations) {
final enable = known(allocation.en);
final commit = known(allocation.commit);
if (enable == null || commit == null) {
return null;
}
final available = eligible & ~reserved;
final unused = available & ~valid;
final choice = _selectBits(pending!, unused != 0 ? unused : available);
final granted = enable == 1 && choice != null;
badCommit |= commit == 1 && !granted;
results.add((
valid: granted,
index: granted ? choice : 0,
evict: granted && (valid >> choice) & 1 == 1,
));
if (granted) {
reserved |= 1 << choice;
if (commit == 1) {
pending = _onAllocationBits(pending, choice);
}
}
}
return (
nextState: pending!,
invalidAccess: invalidHit,
invalidCommit: badCommit,
allocations: results,
);
}
_Evaluation evaluate({required bool previous}) {
var pending = _state;
var invalidHit = LogicValue.zero;
var badCommit = LogicValue.zero;
final valid = value(validMask, previous: previous);
final eligible = value(eligibleMask, previous: previous);
final limit = LogicValue.ofInt(entryCount, indexWidth + 1);
for (final access in localAccesses) {
final index = value(access.index, previous: previous);
final enable = value(access.en, previous: previous);
final inRange = index.zeroExtend(indexWidth + 1) < limit;
invalidHit |= enable & ~inRange;
pending = selectLogicValue(
enable & inRange, _onAccess(pending, index), pending);
}
for (final commit in localCommits) {
final index = value(commit.index, previous: previous);
final enable = value(commit.en, previous: previous);
final inRange = index.zeroExtend(indexWidth + 1) < limit;
badCommit |= enable & ~inRange;
pending = selectLogicValue(
enable & inRange, _onAllocation(pending, index), pending);
}
var reserved = LogicValue.filled(entryCount, LogicValue.zero);
final results = <_Allocation>[];
for (final allocation in localAllocations) {
final enable = value(allocation.en, previous: previous);
final commit = value(allocation.commit, previous: previous);
final available = eligible & ~reserved;
final unused = available & ~valid;
final candidates = selectLogicValue(unused.or(), unused, available);
final choice = _select(pending, candidates);
final granted = enable & choice.valid;
final committed = granted & commit;
badCommit |= commit & ~granted;
var selectedValid = LogicValue.zero;
final reservationBits = <LogicValue>[];
for (var index = 0; index < entryCount; index++) {
final matches = storedValueIndexMatches(choice.index, index);
selectedValid |= valid[index] & matches;
reservationBits.add(granted & matches);
}
results.add((
valid: granted,
index: selectLogicValue(granted, choice.index,
LogicValue.filled(indexWidth, LogicValue.zero)),
evict: granted & selectedValid,
));
reserved |= LogicValue.of(reservationBits);
pending = selectLogicValue(
committed, _onAllocation(pending, choice.index), pending);
}
return (
nextState: pending,
invalidAccess: invalidHit,
invalidCommit: badCommit,
allocations: results,
);
}
final driven = <Logic, LogicValue>{};
void drive(Logic port, LogicValue value) {
if (driven[port] != value) {
driven[port] = value;
port.put(value);
}
}
void refresh() {
final fast = evaluateFast(previous: false);
if (fast != null) {
drive(invalidAccess, LogicValue.ofBool(fast.invalidAccess));
drive(invalidCommit, LogicValue.ofBool(fast.invalidCommit));
for (final (index, port) in localAllocations.indexed) {
final choice = fast.allocations[index];
drive(port.valid, LogicValue.ofBool(choice.valid));
drive(port.index, LogicValue.ofInt(choice.index, indexWidth));
drive(port.evict, LogicValue.ofBool(choice.evict));
}
return;
}
final result = evaluate(previous: false);
drive(invalidAccess, result.invalidAccess);
drive(invalidCommit, result.invalidCommit);
for (final (index, port) in localAllocations.indexed) {
final choice = result.allocations[index];
drive(port.valid, choice.valid);
drive(port.index, choice.index);
drive(port.evict, choice.evict);
}
}
_refresh = refresh;
for (final signal in [
validMask,
eligibleMask,
for (final port in localAccesses) ...[port.en, port.index],
for (final port in localCommits) ...[port.en, port.index],
for (final port in localAllocations) ...[port.en, port.commit],
]) {
signal.glitch.listen((_) => refresh());
}
clk.posedge.listen((_) {
final resetValue = reset.previousValue!;
if (resetValue == LogicValue.one) {
_setState(LogicValue.filled(stateWidth, LogicValue.zero));
} else {
final fast =
resetValue == LogicValue.zero ? evaluateFast(previous: true) : null;
_setState(fast != null
? LogicValue.ofInt(fast.nextState, stateWidth)
: selectLogicValue(
resetValue,
LogicValue.filled(stateWidth, LogicValue.zero),
evaluate(previous: true).nextState));
}
refresh();
});
Simulator.injectAction(refresh);
}