InferredSram<T extends Logic> constructor

InferredSram<T extends Logic>(
  1. Logic clk,
  2. Logic reset,
  3. List<DataPortInterface<T>> writePorts,
  4. List<DataPortInterface<T>> readPorts, {
  5. required int entryCount,
  6. 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));
      }
    }
  });
}