InvalidatingAssociativeCache<T extends Logic> constructor

InvalidatingAssociativeCache<T extends Logic>({
  1. required Logic clk,
  2. required Logic reset,
  3. required Logic lookupEnable,
  4. required Logic lookupKey,
  5. required Logic fillEnable,
  6. required Logic fillValid,
  7. required Logic fillKey,
  8. required T fillData,
  9. required int ways,
  10. Logic? lookupInvalidate,
  11. CacheReplacement? replacement,
  12. MemoryConfiguration? memoryConfiguration,
  13. String name = 'invalidating_associative_cache',
})

Creates a cache with ways entries.

Implementation

InvalidatingAssociativeCache({
  required Logic clk,
  required Logic reset,
  required Logic lookupEnable,
  required Logic lookupKey,
  required Logic fillEnable,
  required Logic fillValid,
  required Logic fillKey,
  required T fillData,
  required this.ways,
  Logic? lookupInvalidate,
  CacheReplacement? replacement,
  MemoryConfiguration? memoryConfiguration,
  super.name = 'invalidating_associative_cache',
})  : replacement = _resolveReplacement(replacement, memoryConfiguration),
      super(
          definitionName: 'InvalidatingAssociativeCache_W${ways}_'
              'K${lookupKey.width}_${payloadShapeIdentity(fillData)}_'
              '${_replacementIdentity(replacement, memoryConfiguration)}'
              '${lookupInvalidate == null ? '' : '_LI'}_'
              '${_backingIdentity(memoryConfiguration)}') {
  if (ways < 1) {
    throw ArgumentError.value(ways, 'ways', 'Must be positive.');
  }
  if (lookupKey.width != fillKey.width || lookupKey.width < 1) {
    throw ArgumentError('Lookup and fill keys must have the same width.');
  }
  for (final signal in [
    clk,
    reset,
    lookupEnable,
    fillEnable,
    fillValid,
    if (lookupInvalidate != null) lookupInvalidate,
  ]) {
    if (signal.width != 1) {
      throw ArgumentError('Clock, reset, and controls must be bits.');
    }
  }

  final keyWidth = lookupKey.width;
  final indexWidth = max(1, (ways - 1).bitLength);
  clk = addInput('clk', clk);
  reset = addInput('reset', reset);
  lookupEnable = addInput('lookupEnable', lookupEnable);
  lookupKey = addInput('lookupKey', lookupKey, width: keyWidth);
  final localInvalidate = lookupInvalidate == null
      ? Const(0)
      : addInput('lookupInvalidate', lookupInvalidate);
  fillEnable = addInput('fillEnable', fillEnable);
  fillValid = addInput('fillValid', fillValid);
  fillKey = addInput('fillKey', fillKey, width: keyWidth);
  fillData = addTypedInput('fillData', fillData);

  addOutput('lookupHit');
  addOutput('lookupIndex', width: indexWidth);
  lookupData = addTypedPayloadOutput<T>(this, 'lookupData', fillData);
  addOutput('fillIndex', width: indexWidth);
  addOutput('fillEvicts');
  addOutput('occupancy', width: (ways + 1).log2Ceil);
  addOutput('full');
  addOutput('empty');

  final lookupTag = AssociativeInvalidateLookupPort(
      indexWidth: indexWidth, keyWidth: keyWidth);
  final fillTag = AssociativeInvalidateLookupPort(
      indexWidth: indexWidth, keyWidth: keyWidth);
  final tagWrite = DataPortInterface(keyWidth, indexWidth);
  final tags = AssociativeInvalidateMemory(
      clk, reset, [tagWrite], [lookupTag, fillTag],
      entryCount: ways,
      enableValidTracking: true,
      memoryConfiguration: memoryConfiguration,
      name: 'tags');
  lookupTag.key <= lookupKey;
  fillTag.key <= fillKey;

  final lookupMatch = (lookupEnable & lookupTag.hit).named('lookupMatch');
  final lookupRemoves =
      (lookupMatch & localInvalidate).named('lookupRemoves');
  final fillMatch = (fillEnable & fillTag.hit).named('fillMatch');
  final fillUpdate = (fillMatch & fillValid).named('fillUpdate');
  final fillAllocate =
      (fillEnable & fillValid & ~fillTag.hit).named('fillAllocate');
  final fillRemove = (fillMatch & ~fillValid).named('fillRemove');

  final valid = Logic(name: 'valid', width: ways);
  final lookupFreed = [
    for (var i = 0; i < ways; i++) lookupRemoves & lookupTag.index.eq(i),
  ].rconcat().named('lookupFreed');

  final lookupAccess = ReplacementAccessInterface(ways);
  lookupAccess.en <= lookupMatch;
  lookupAccess.index <= lookupTag.index;
  final fillAccess = ReplacementAccessInterface(ways);
  fillAccess.en <= fillUpdate;
  fillAccess.index <= fillTag.index;
  final allocation = ReplacementAllocationInterface(ways);
  allocation.en <= fillAllocate;
  allocation.commit <= fillAllocate;
  this.replacement.build(
        clk: clk,
        reset: reset,
        validMask: (valid & ~lookupFreed).named('allocatableValid'),
        eligibleMask: Const(1, width: ways, fill: true),
        accesses: [lookupAccess, fillAccess],
        allocations: [allocation],
        entryCount: ways,
        name: 'replacement',
      );

  final writeIndex =
      mux(fillTag.hit, fillTag.index, allocation.index).named('writeIndex');
  final fillWrites = (fillUpdate | fillAllocate).named('fillWrites');
  final lookupInvalidates =
      (lookupRemoves & ~(fillWrites & writeIndex.eq(lookupTag.index)))
          .named('lookupInvalidates');
  lookupTag.invalidate <= lookupInvalidates;
  fillTag.invalidate <= fillRemove;
  tagWrite.en <= fillAllocate;
  tagWrite.addr <= allocation.index;
  tagWrite.data <= fillKey;

  valid <=
      [
        for (var i = 0; i < ways; i++)
          flop(
              clk,
              (fillWrites & writeIndex.eq(i)) |
                  (valid[i] &
                      ~(lookupInvalidates & lookupTag.index.eq(i)) &
                      ~(fillRemove & fillTag.index.eq(i))),
              reset: reset),
      ].rconcat();

  final dataWrite = DataPortInterface(fillData.width, indexWidth);
  dataWrite.en <= fillWrites;
  dataWrite.addr <= writeIndex;
  dataWrite.data <= fillData;
  final dataRead = DataPortInterface(fillData.width, indexWidth);
  dataRead.en <= lookupMatch;
  dataRead.addr <= lookupTag.index;
  configuredRegisterFile(clk, reset, [dataWrite], [dataRead],
      entryCount: ways,
      memoryConfiguration: memoryConfiguration,
      name: 'data');

  lookupHit <= lookupMatch;
  lookupIndex <=
      mux(lookupMatch, lookupTag.index, Const(0, width: indexWidth));
  lookupData <= dataRead.data;
  fillIndex <= writeIndex;
  fillEvicts <= fillAllocate & allocation.evict;
  occupancy <= tags.validCount!;
  full <= tags.full!;
  empty <= tags.empty!;
}