RamLifo<LogicType extends Logic> constructor
- required Logic clk,
- required Logic reset,
- required Logic push,
- required LogicType pushData,
- required Logic pop,
- required int depth,
- Logic? replaceTop,
- LogicType? replaceTopData,
- SynchronousMemoryFunctor? memoryFunctor,
- MemoryConfiguration? memoryConfiguration,
- SynchronousRamReadUnderWrite readUnderWrite = SynchronousRamReadUnderWrite.readFirst,
- int memoryReadLatency = 1,
- String name = 'ram_lifo',
Creates a memory-backed stack driven by push, pop, and the optional
replaceTop actions.
memoryFunctor binds the backing memory directly. Otherwise
memoryConfiguration, or the scoped memory configuration, selects it.
replaceTop and replaceTopData must be supplied together.
Implementation
RamLifo({
required Logic clk,
required Logic reset,
required Logic push,
required LogicType pushData,
required Logic pop,
required this.depth,
Logic? replaceTop,
LogicType? replaceTopData,
SynchronousMemoryFunctor? memoryFunctor,
MemoryConfiguration? memoryConfiguration,
this.readUnderWrite = SynchronousRamReadUnderWrite.readFirst,
this.memoryReadLatency = 1,
super.name = 'ram_lifo',
}) : memoryFunctor = memoryFunctor ??
selectMemoryFunctor(
memoryConfiguration,
dataWidth: pushData.width,
depth: depth,
memoryReadLatency: memoryReadLatency,
readUnderWrite: readUnderWrite,
),
super(
definitionName: 'RamLifo_D${depth}_'
'L${memoryReadLatency}_U${readUnderWrite.name}_'
'S${(memoryFunctor ?? selectMemoryFunctor(
memoryConfiguration,
dataWidth: pushData.width,
depth: depth,
memoryReadLatency: memoryReadLatency,
readUnderWrite: readUnderWrite,
)).identity}_'
'${payloadShapesIdentity([
pushData,
if (replaceTopData != null) replaceTopData,
])}_R${replaceTop == null ? 0 : 1}',
) {
if (depth < 1 || memoryReadLatency < 1) {
throw ArgumentError('depth and memoryReadLatency must be positive.');
}
if ((replaceTop == null) != (replaceTopData == null)) {
throw ArgumentError(
'replaceTop and replaceTopData must be supplied together.');
}
if (replaceTopData != null &&
!rohdPayloadsCompatible([pushData, replaceTopData],
ignoreConstantNames: true)) {
throw ArgumentError('Push and replacement payloads must be compatible.');
}
final localClk = addInput('clk', clk);
final localReset = addInput('reset', reset);
final localPush = addInput('push', push);
final localPushData = addTypedInput('pushData', pushData);
final localPop = addInput('pop', pop);
final localReplaceTop =
replaceTop == null ? null : addInput('replaceTop', replaceTop);
final addressWidth = max(1, depth.log2Ceil);
final countWidth = max(1, (depth + 1).log2Ceil);
final cachedTop = localPushData.cloneTyped(name: '${name}_top_data');
final cachedTopValid = Logic(name: '${name}_top_valid');
final localReplaceTopData = replaceTopData == null
? cachedTop
: addTypedInput('replaceTopData', replaceTopData);
addOutput('canPush');
addOutput('topValid') <= cachedTopValid;
topData = addTypedPayloadOutput(this, 'topData', localPushData);
topData <= cachedTop;
addOutput('occupancy', width: countWidth);
addOutput('empty');
addOutput('full');
empty <= occupancy.eq(0);
full <= occupancy.eq(depth);
final write = DataPortInterface(localPushData.width, addressWidth);
final read = SynchronousReadDataPortInterface(
localPushData.width,
addressWidth,
);
final refill = ReadyAndValidInterface<Logic>(
Logic(width: localPushData.width, name: '${name}_refill'),
);
ReadResponseBuffer<Logic>(
clk: localClk,
reset: localReset,
responseValid: read.valid,
responseData: read.data,
downstream: refill,
name: '${name}_refill_buffer',
);
final refillData = localPushData.cloneTyped(name: '${name}_refill_data')
..gets(refill.data);
refill.ready <= ~cachedTopValid;
final pushTaken = (localPush & canPush).named('${name}_push_taken');
final popTaken = (localPop & cachedTopValid).named('${name}_pop_taken');
final moreThanOneEntry = occupancy
.gt(Const(1, width: countWidth))
.named('${name}_more_than_one_entry');
final needsRefill = (popTaken & ~pushTaken & moreThanOneEntry)
.named('${name}_needs_refill');
final readLimiter = OutstandingRequestLimiter(
clk: localClk,
reset: localReset,
requestRequested: needsRefill,
responseValid: read.valid,
maximumOutstanding: 1,
name: '${name}_read_limiter',
);
// Entries remain but none is cached: the next top is still on its way.
final refilling = (~cachedTopValid & ~empty).named('${name}_refilling');
canPush <=
readLimiter.requestReady &
~refilling &
(occupancy.lt(depth) | popTaken);
Logic replaceTaken = Const(0);
if (localReplaceTop != null) {
final canReplace = addOutput('canReplaceTop')
..gets(
cachedTopValid & readLimiter.requestReady & ~pushTaken & ~popTaken);
replaceTaken =
(localReplaceTop & canReplace).named('${name}_replace_top_taken');
}
// A push over a retained top spills that top to the slot beneath it.
write.en <= pushTaken & cachedTopValid & ~popTaken;
write.addr <=
(occupancy - Const(1, width: countWidth)).slice(addressWidth - 1, 0);
write.data <= cachedTop;
read.en <= readLimiter.requestIssued;
read.addr <=
(occupancy - Const(2, width: countWidth)).slice(addressWidth - 1, 0);
memoryBinding = this.memoryFunctor(
clk: localClk,
reset: localReset,
writePorts: [write],
readPorts: [read],
entryCount: depth,
readLatency: memoryReadLatency,
readUnderWrite: readUnderWrite,
name: '${name}_memory',
);
Sequential(localClk, reset: localReset, resetValues: {
occupancy: 0,
cachedTopValid: 0,
cachedTop: 0,
}, [
If(pushTaken & ~popTaken, then: [occupancy < occupancy + 1]),
If(popTaken & ~pushTaken, then: [occupancy < occupancy - 1]),
If(pushTaken, then: [
cachedTopValid < 1,
cachedTop < localPushData,
], orElse: [
If(replaceTaken, then: [
cachedTop < localReplaceTopData,
], orElse: [
If(popTaken, then: [cachedTopValid < 0]),
If(refill.accepted, then: [
cachedTopValid < 1,
cachedTop < refillData,
]),
]),
]),
]);
}