ExternallyAllocatedLoadStoreState<MetadataType extends Logic> constructor

ExternallyAllocatedLoadStoreState<MetadataType extends Logic>({
  1. required Logic clk,
  2. required Logic reset,
  3. required Logic quiesce,
  4. required Logic allocate,
  5. required AllocatedMemoryOperation<MetadataType> allocation,
  6. required Logic updateLoadAddress,
  7. required Tagged<MemoryAccess> loadAddressUpdate,
  8. required Logic updateStoreAddress,
  9. required Tagged<MemoryAccess> storeAddressUpdate,
  10. required Logic updateStoreData,
  11. required Tagged<Logic> storeDataUpdate,
  12. required Logic updateLoadExecution,
  13. required Tagged<Logic> loadExecutionUpdate,
  14. required Logic releaseLoad,
  15. required TagRelease loadRelease,
  16. required Logic commitStore,
  17. required TagRelease storeCommit,
  18. required Logic releaseStore,
  19. required TagRelease storeRelease,
  20. required Logic cancelLoads,
  21. required Logic loadCancellationMask,
  22. required Logic cancelStores,
  23. required Logic storeCancellationMask,
  24. required int loadCount,
  25. required int storeCount,
  26. String name = 'externally_allocated_load_store_state',
})

Creates externally allocated load/store state driven by explicit actions.

Implementation

ExternallyAllocatedLoadStoreState({
  required Logic clk,
  required Logic reset,
  required Logic quiesce,
  required Logic allocate,
  required AllocatedMemoryOperation<MetadataType> allocation,
  required Logic updateLoadAddress,
  required Tagged<MemoryAccess> loadAddressUpdate,
  required Logic updateStoreAddress,
  required Tagged<MemoryAccess> storeAddressUpdate,
  required Logic updateStoreData,
  required Tagged<Logic> storeDataUpdate,
  required Logic updateLoadExecution,
  required Tagged<Logic> loadExecutionUpdate,
  required Logic releaseLoad,
  required TagRelease loadRelease,
  required Logic commitStore,
  required TagRelease storeCommit,
  required Logic releaseStore,
  required TagRelease storeRelease,
  required Logic cancelLoads,
  required Logic loadCancellationMask,
  required Logic cancelStores,
  required Logic storeCancellationMask,
  required this.loadCount,
  required this.storeCount,
  super.name = 'externally_allocated_load_store_state',
}) : super(
        definitionName: 'ExternallyAllocatedLoadStoreState_'
            'L${loadCount}_S${storeCount}_'
            'Q${allocation.sequenceWidth}_'
            '${payloadShapesIdentity([
              allocation,
              loadAddressUpdate,
              storeAddressUpdate,
              storeDataUpdate,
              loadExecutionUpdate
            ])}',
      ) {
  if (loadCount < 1 || storeCount < 1) {
    throw ArgumentError('Load and store counts must be positive.');
  }
  final request = allocation.request;
  final loadTagWidth = max(1, loadCount.log2Ceil);
  final storeTagWidth = max(1, storeCount.log2Ceil);
  final requiredQueueTagWidth = max(loadTagWidth, storeTagWidth);
  if (allocation.queueTagWidth < requiredQueueTagWidth ||
      loadAddressUpdate.tagWidth != loadTagWidth ||
      loadAddressUpdate.data.address.width != request.access.address.width ||
      loadAddressUpdate.data.byteEnable.width !=
          request.access.byteEnable.width ||
      storeAddressUpdate.tagWidth != storeTagWidth ||
      storeAddressUpdate.data.address.width != request.access.address.width ||
      storeAddressUpdate.data.byteEnable.width !=
          request.access.byteEnable.width ||
      storeDataUpdate.tagWidth != storeTagWidth ||
      storeDataUpdate.data.width != request.data.width ||
      loadExecutionUpdate.tagWidth != loadTagWidth ||
      loadExecutionUpdate.data.width != 2 ||
      loadRelease.tagWidth != loadTagWidth ||
      storeCommit.tagWidth != storeTagWidth ||
      storeRelease.tagWidth != storeTagWidth ||
      loadCancellationMask.width != loadCount ||
      storeCancellationMask.width != storeCount) {
    throw ArgumentError('Queue transaction shapes must match capacities.');
  }

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  final localQuiesce = addInput('quiesce', quiesce);
  final localAllocate = addInput('allocate', allocate);
  final localAllocation = addTypedInput('allocation', allocation);
  final localUpdateLoadAddress =
      addInput('updateLoadAddress', updateLoadAddress);
  final localLoadAddressUpdate =
      addTypedInput('loadAddressUpdate', loadAddressUpdate);
  final localUpdateStoreAddress =
      addInput('updateStoreAddress', updateStoreAddress);
  final localStoreAddressUpdate =
      addTypedInput('storeAddressUpdate', storeAddressUpdate);
  final localUpdateStoreData = addInput('updateStoreData', updateStoreData);
  final localStoreDataUpdate =
      addTypedInput('storeDataUpdate', storeDataUpdate);
  final localUpdateLoadExecution =
      addInput('updateLoadExecution', updateLoadExecution);
  final localLoadExecutionUpdate =
      addTypedInput('loadExecutionUpdate', loadExecutionUpdate);
  final localReleaseLoad = addInput('releaseLoad', releaseLoad);
  final localLoadRelease = addTypedInput('loadRelease', loadRelease);
  final localCommitStore = addInput('commitStore', commitStore);
  final localStoreCommit = addTypedInput('storeCommit', storeCommit);
  final localReleaseStore = addInput('releaseStore', releaseStore);
  final localStoreRelease = addTypedInput('storeRelease', storeRelease);
  final localCancelLoads = addInput('cancelLoads', cancelLoads);
  final localLoadCancellationMask = addInput(
      'loadCancellationMask', loadCancellationMask,
      width: loadCount);
  final localCancelStores = addInput('cancelStores', cancelStores);
  final localStoreCancellationMask = addInput(
      'storeCancellationMask', storeCancellationMask,
      width: storeCount);
  final localRequest = localAllocation.request;

  final loadPrototype = LoadQueueEntry<MetadataType>(
    localRequest.metadata.cloneTyped(),
    addressWidth: localRequest.access.address.width,
    byteCount: localRequest.access.byteEnable.width,
    memorySequenceWidth: localAllocation.sequenceWidth,
    loadSequenceWidth: localAllocation.sequenceWidth,
    destinationTagWidth: localRequest.destinationTagWidth,
  );
  final storePrototype = StoreQueueEntry<MetadataType>(
    localRequest.metadata.cloneTyped(),
    dataWidth: localRequest.data.width,
    addressWidth: localRequest.access.address.width,
    byteCount: localRequest.access.byteEnable.width,
    memorySequenceWidth: localAllocation.sequenceWidth,
    storeSequenceWidth: localAllocation.sequenceWidth,
  );
  final storedLoads = [
    for (var slot = 0; slot < loadCount; slot++)
      loadPrototype.cloneTyped(name: '${name}_load_$slot'),
  ];
  final storedStores = [
    for (var slot = 0; slot < storeCount; slot++)
      storePrototype.cloneTyped(name: '${name}_store_$slot'),
  ];
  loadEntries = [
    for (var slot = 0; slot < loadCount; slot++)
      addTypedPayloadOutput(this, 'loadEntry$slot', loadPrototype),
  ];
  storeEntries = [
    for (var slot = 0; slot < storeCount; slot++)
      addTypedPayloadOutput(this, 'storeEntry$slot', storePrototype),
  ];
  for (var slot = 0; slot < loadCount; slot++) {
    loadEntries[slot] <= storedLoads[slot];
  }
  for (var slot = 0; slot < storeCount; slot++) {
    storeEntries[slot] <= storedStores[slot];
  }

  final loadLiveBits = [
    for (var slot = 0; slot < loadCount; slot++)
      Logic(name: '${name}_load_${slot}_live'),
  ];
  final storeLiveBits = [
    for (var slot = 0; slot < storeCount; slot++)
      Logic(name: '${name}_store_${slot}_live'),
  ];
  final liveLoads = loadLiveBits.rconcat().named('${name}_live_loads');
  final liveStores = storeLiveBits.rconcat().named('${name}_live_stores');
  addOutput('occupiedLoads', width: loadCount) <= liveLoads;
  addOutput('occupiedStores', width: storeCount) <= liveStores;
  addOutput('invalidAllocation');
  addOutput('invalidMutation');
  addOutput('cancelInactiveLoads', width: loadCount);
  addOutput('cancelInactiveStores', width: storeCount);
  addOutput('canAllocate') <= ~localQuiesce;
  addOutput('canCancel') <= localQuiesce;
  addOutput('quiesced') <= localQuiesce;

  final allocationAccepted =
      (localAllocate & canAllocate).named('${name}_allocation_accepted');
  final loadCancellationAccepted = (localCancelLoads & canCancel)
      .named('${name}_load_cancellation_accepted');
  final storeCancellationAccepted = (localCancelStores & canCancel)
      .named('${name}_store_cancellation_accepted');

  Logic inRange(Logic tag, int count) => count.bitLength > tag.width
      ? Const(1)
      : tag.lt(Const(count, width: tag.width));
  Logic selected(Logic mask, Logic tag) {
    var value = mask[mask.width - 1];
    for (var slot = mask.width - 2; slot >= 0; slot--) {
      value = mux(tag.eq(slot), mask[slot], value);
    }
    return value;
  }

  final allocationTag = localAllocation.queueTag;
  Logic matchesAllocationTag(Logic tag) =>
      allocationTag.eq(tag.zeroExtend(allocationTag.width));
  final validLoadRelease = localReleaseLoad &
      inRange(localLoadRelease.tag, loadCount) &
      selected(liveLoads, localLoadRelease.tag);
  final validStoreRelease = localReleaseStore &
      inRange(localStoreRelease.tag, storeCount) &
      selected(liveStores, localStoreRelease.tag);
  final allocatingStore = allocationAccepted & localRequest.isStore;
  final allocatingLoad = allocationAccepted & ~localRequest.isStore;
  final loadAllocationInRange = inRange(allocationTag, loadCount);
  final storeAllocationInRange = inRange(allocationTag, storeCount);
  final replacingReleasedLoad =
      validLoadRelease & matchesAllocationTag(localLoadRelease.tag);
  final replacingReleasedStore =
      validStoreRelease & matchesAllocationTag(localStoreRelease.tag);
  final loadTagOccupied =
      selected(liveLoads, allocationTag) & ~replacingReleasedLoad;
  final storeTagOccupied =
      selected(liveStores, allocationTag) & ~replacingReleasedStore;
  final validLoadAllocation =
      allocatingLoad & loadAllocationInRange & ~loadTagOccupied;
  final validStoreAllocation =
      allocatingStore & storeAllocationInRange & ~storeTagOccupied;
  invalidAllocation <=
      (allocatingLoad & (~loadAllocationInRange | loadTagOccupied)) |
          (allocatingStore & (~storeAllocationInRange | storeTagOccupied));

  Logic loadLiveAfterAllocation(Logic tag) =>
      (selected(liveLoads, tag) &
          ~(validLoadRelease & localLoadRelease.tag.eq(tag))) |
      (validLoadAllocation & matchesAllocationTag(tag));
  Logic storeLiveAfterAllocation(Logic tag) =>
      (selected(liveStores, tag) &
          ~(validStoreRelease & localStoreRelease.tag.eq(tag))) |
      (validStoreAllocation & matchesAllocationTag(tag));
  final validLoadAddressUpdate = localUpdateLoadAddress &
      inRange(localLoadAddressUpdate.tag, loadCount) &
      loadLiveAfterAllocation(localLoadAddressUpdate.tag);
  final validLoadExecutionUpdate = localUpdateLoadExecution &
      inRange(localLoadExecutionUpdate.tag, loadCount) &
      loadLiveAfterAllocation(localLoadExecutionUpdate.tag);
  final validStoreAddressUpdate = localUpdateStoreAddress &
      inRange(localStoreAddressUpdate.tag, storeCount) &
      storeLiveAfterAllocation(localStoreAddressUpdate.tag);
  final validStoreDataUpdate = localUpdateStoreData &
      inRange(localStoreDataUpdate.tag, storeCount) &
      storeLiveAfterAllocation(localStoreDataUpdate.tag);
  final validStoreCommit = localCommitStore &
      inRange(localStoreCommit.tag, storeCount) &
      storeLiveAfterAllocation(localStoreCommit.tag);
  invalidMutation <=
      (localUpdateLoadAddress & ~validLoadAddressUpdate) |
          (localUpdateLoadExecution & ~validLoadExecutionUpdate) |
          (localReleaseLoad & ~validLoadRelease) |
          (localUpdateStoreAddress & ~validStoreAddressUpdate) |
          (localUpdateStoreData & ~validStoreDataUpdate) |
          (localCommitStore & ~validStoreCommit) |
          (localReleaseStore & ~validStoreRelease);
  cancelInactiveLoads <=
      mux(loadCancellationAccepted, localLoadCancellationMask & ~liveLoads,
          Const(0, width: loadCount));
  cancelInactiveStores <=
      mux(storeCancellationAccepted, localStoreCancellationMask & ~liveStores,
          Const(0, width: storeCount));

  final proposedLoad =
      loadPrototype.cloneTyped(name: '${name}_proposed_load');
  proposedLoad.metadata <= localRequest.metadata;
  proposedLoad.address <= localRequest.access.address;
  proposedLoad.byteEnable <= localRequest.access.byteEnable;
  proposedLoad.memorySequence <= localAllocation.memorySequence;
  proposedLoad.loadSequence <= localAllocation.queueSequence;
  proposedLoad.destinationTag <= localRequest.destinationTag;
  proposedLoad.addressValid <= localRequest.addressValid;
  proposedLoad.executed <= Const(0);
  proposedLoad.waitingForMiss <= Const(0);
  final proposedStore =
      storePrototype.cloneTyped(name: '${name}_proposed_store');
  proposedStore.metadata <= localRequest.metadata;
  proposedStore.data <= localRequest.data;
  proposedStore.address <= localRequest.access.address;
  proposedStore.byteEnable <= localRequest.access.byteEnable;
  proposedStore.memorySequence <= localAllocation.memorySequence;
  proposedStore.storeSequence <= localAllocation.queueSequence;
  proposedStore.addressValid <= localRequest.addressValid;
  proposedStore.dataValid <= localRequest.dataValid;
  proposedStore.committed <= Const(0);
  proposedStore.draining <= Const(0);

  Sequential(localClk, reset: localReset, resetValues: {
    for (final live in loadLiveBits) live: 0,
    for (final live in storeLiveBits) live: 0,
    for (final load in storedLoads) load: 0,
    for (final store in storedStores) store: 0,
  }, [
    for (var load = 0; load < loadCount; load++) ...[
      If(validLoadRelease & localLoadRelease.tag.eq(load),
          then: [loadLiveBits[load] < 0, storedLoads[load] < 0]),
      If(validLoadAllocation & allocationTag.eq(load),
          then: [loadLiveBits[load] < 1, storedLoads[load] < proposedLoad]),
      If(validLoadAddressUpdate & localLoadAddressUpdate.tag.eq(load), then: [
        storedLoads[load].address < localLoadAddressUpdate.data.address,
        storedLoads[load].byteEnable < localLoadAddressUpdate.data.byteEnable,
        storedLoads[load].addressValid < 1,
      ]),
      If(validLoadExecutionUpdate & localLoadExecutionUpdate.tag.eq(load),
          then: [
            storedLoads[load].executed < localLoadExecutionUpdate.data[0],
            storedLoads[load].waitingForMiss <
                localLoadExecutionUpdate.data[1],
          ]),
      If(loadCancellationAccepted & localLoadCancellationMask[load],
          then: [loadLiveBits[load] < 0, storedLoads[load] < 0]),
    ],
    for (var store = 0; store < storeCount; store++) ...[
      If(validStoreRelease & localStoreRelease.tag.eq(store),
          then: [storeLiveBits[store] < 0, storedStores[store] < 0]),
      If(validStoreAllocation & allocationTag.eq(store), then: [
        storeLiveBits[store] < 1,
        storedStores[store] < proposedStore,
      ]),
      If(validStoreAddressUpdate & localStoreAddressUpdate.tag.eq(store),
          then: [
            storedStores[store].address <
                localStoreAddressUpdate.data.address,
            storedStores[store].byteEnable <
                localStoreAddressUpdate.data.byteEnable,
            storedStores[store].addressValid < 1,
          ]),
      If(validStoreDataUpdate & localStoreDataUpdate.tag.eq(store), then: [
        storedStores[store].data < localStoreDataUpdate.data,
        storedStores[store].dataValid < 1,
      ]),
      If(validStoreCommit & localStoreCommit.tag.eq(store),
          then: [storedStores[store].committed < 1]),
      If(storeCancellationAccepted & localStoreCancellationMask[store],
          then: [storeLiveBits[store] < 0, storedStores[store] < 0]),
    ],
  ]);
}