InferredSram<T extends Logic> constructor
- Logic clk,
- Logic reset,
- List<
DataPortInterface< writePorts,T> > - List<
DataPortInterface< readPorts, {T> > - required int entryCount,
- String name = 'inferred_sram',
Creates an SRAM with one read port and one write port.
Implementation
InferredSram(super.clk, super.reset, super.writePorts, super.readPorts,
{required this.entryCount, super.name = 'inferred_sram'})
: super(definitionName: _sramDefinition(entryCount, writePorts)) {
if (numWrites != 1 ||
numReads != 1 ||
entryCount < 1 ||
BigInt.from(entryCount) > (BigInt.one << addrWidth) ||
wrPorts.single is MaskedDataPortInterface<T> ||
rdPorts.single is MaskedDataPortInterface<T>) {
throw ArgumentError('An inferred SRAM needs one unmasked read and write '
'port with valid geometry.');
}
_storage = SparseMemoryStorage(addrWidth: addrWidth, dataWidth: dataWidth);
_raw = MemoryStorageState(_storage);
_usesPackedCell = _hasUnpackedPayload(wrPorts.single.data) ||
_hasUnpackedPayload(rdPorts.single.data);
if (_usesPackedCell) {
final cell = _PackedSramCell(clk, reset, wrPorts.single, rdPorts.single,
entryCount: entryCount);
_readValue = cell.readData;
rdPorts.single.data <= _readValue;
} else {
_readValue = rdPorts.single.data;
}
putPackedLogicValue(_readValue, LogicValue.filled(dataWidth, LogicValue.x));
clk.posedge.listen((_) {
if (reset.previousValue == LogicValue.one) {
_resetSeen = true;
putPackedLogicValue(
_readValue, LogicValue.filled(dataWidth, LogicValue.zero));
return;
}
final write = wrPorts.single;
if (write.en.previousValue == LogicValue.one) {
final address = write.addr.previousValue!;
if (address.isValid && address.toBigInt() < BigInt.from(entryCount)) {
_storage.setData(
address, normalizeStoredValue(write.data.previousValue!));
}
} else {
final read = rdPorts.single;
if (read.en.previousValue == LogicValue.one) {
final address = read.addr.previousValue!;
final value =
address.isValid && address.toBigInt() < BigInt.from(entryCount)
? _storage.getData(address)
: null;
putPackedLogicValue(
_readValue, value ?? LogicValue.filled(dataWidth, LogicValue.x));
}
}
});
}