CacheTransactionAdapter<T extends Logic, M extends Logic, C extends Logic, R extends Logic> constructor
- required Logic clk,
- required Logic reset,
- required ReadyValidInterface<
CacheTransactionRequest< requests,T, M, C> > - required ReadyValidInterface<
CacheTransactionResponse< responses,T, M, C, R> > - required ReadyValidInterface<
CacheTransactionEviction< evictions,T, M, C> > - required CacheTransactionPort<
T, M, C, R> cache, - required int maxOutstanding,
- String name = 'cache_transaction_adapter',
Creates a bounded adapter. The supplied channels must be irrevocable.
Implementation
CacheTransactionAdapter({
required Logic clk,
required Logic reset,
required this.requests,
required this.responses,
required this.evictions,
required this.cache,
required this.maxOutstanding,
super.name = 'cache_transaction_adapter',
}) : super(
definitionName: 'CacheTransactionAdapter_N${maxOutstanding}_'
'${payloadShapeIdentity(requests.data)}_'
'${payloadShapeIdentity(responses.data)}_'
'${payloadShapeIdentity(evictions.data)}',
) {
if (clk.width != 1 || reset.width != 1 || maxOutstanding < 1) {
throw ArgumentError('Invalid adapter clock, reset, or capacity.');
}
if (!requests.contract.satisfies(ReadyValidContract.irrevocable) ||
!responses.contract.satisfies(ReadyValidContract.irrevocable) ||
!evictions.contract.satisfies(ReadyValidContract.irrevocable) ||
!sameRohdPayloadShape(requests.data, cache.requests.data) ||
!sameRohdPayloadShape(responses.data, cache.responses.data) ||
!sameRohdPayloadShape(evictions.data, cache.evictions.data)) {
throw ArgumentError('Adapter channels must have matching payloads and '
'irrevocable contracts.');
}
final localClk = addInput('clk', clk);
final localReset = addInput('reset', reset);
final inputRequests = addInterfacePorts(requests,
inputTags: [PairDirection.fromProvider],
outputTags: [PairDirection.fromConsumer],
uniquify: (signal) => 'input_request_$signal');
final outputResponses = addInterfacePorts(responses,
inputTags: [PairDirection.fromConsumer],
outputTags: [PairDirection.fromProvider],
uniquify: (signal) => 'output_response_$signal');
final outputEvictions = addInterfacePorts(evictions,
inputTags: [PairDirection.fromConsumer],
outputTags: [PairDirection.fromProvider],
uniquify: (signal) => 'output_eviction_$signal');
final localCache = addInterfacePorts(cache,
inputTags: [PairDirection.fromConsumer],
outputTags: [PairDirection.fromProvider],
uniquify: (signal) => 'cache_$signal');
final cacheRequests = localCache.requests;
final cacheResponses = localCache.responses;
final cacheEvictions = localCache.evictions;
final occupied = [for (var i = 0; i < maxOutstanding; i++) Logic()];
final tokens = [
for (var i = 0; i < maxOutstanding; i++)
Logic(width: requests.data.token.width)
];
Logic combine(List<Logic> values) =>
values.reduce((left, right) => left | right);
Logic matches(Logic token) => combine([
for (var i = 0; i < maxOutstanding; i++)
occupied[i] & tokens[i].eq(token)
]);
final duplicate = matches(inputRequests.data.token);
final free = combine([for (final entry in occupied) ~entry]);
final staged = Logic(name: 'staged');
final stagedData = inputRequests.data.cloneTyped(name: 'staged_data');
inputRequests.ready <= ~localReset & ~staged & free & ~duplicate;
final accept = inputRequests.valid & inputRequests.ready;
cacheRequests.valid <= ~localReset & staged;
cacheRequests.data <= stagedData;
final issue = cacheRequests.valid & cacheRequests.ready;
staged <=
flop(localClk, mux(accept, Const(1), mux(issue, Const(0), staged)),
reset: localReset);
stagedData <=
flop<CacheTransactionRequest<T, M, C>>(
localClk,
mux<CacheTransactionRequest<T, M, C>>(
accept, inputRequests.data, stagedData),
reset: localReset);
final responseMatch = matches(cacheResponses.data.token);
outputResponses.valid <= ~localReset & cacheResponses.valid & responseMatch;
outputResponses.data <= cacheResponses.data;
cacheResponses.ready <= ~localReset & outputResponses.ready & responseMatch;
final finish = cacheResponses.valid & cacheResponses.ready;
final evictionMatch = matches(cacheEvictions.data.token);
outputEvictions.valid <= ~localReset & cacheEvictions.valid & evictionMatch;
outputEvictions.data <= cacheEvictions.data;
cacheEvictions.ready <= ~localReset & outputEvictions.ready & evictionMatch;
addOutput('duplicateRequest') <=
~localReset & inputRequests.valid & duplicate;
addOutput('invalidResponse') <=
~localReset & cacheResponses.valid & ~responseMatch;
addOutput('invalidEviction') <=
~localReset & cacheEvictions.valid & ~evictionMatch;
Logic earlierFree = Const(0);
for (var i = 0; i < maxOutstanding; i++) {
final allocate = accept & ~occupied[i] & ~earlierFree;
earlierFree = earlierFree | ~occupied[i];
final release =
finish & occupied[i] & tokens[i].eq(cacheResponses.data.token);
occupied[i] <=
flop(localClk,
mux(allocate, Const(1), mux(release, Const(0), occupied[i])),
reset: localReset);
tokens[i] <=
flop(localClk, mux(allocate, inputRequests.data.token, tokens[i]),
reset: localReset);
}
}