IndexedMemoryWriteBypass<LogicType extends Logic> constructor

IndexedMemoryWriteBypass<LogicType extends Logic>({
  1. required Logic readAddress,
  2. required LogicType backingData,
  3. required Logic backingValid,
  4. required List<MemoryWriteRequest<LogicType>> pendingWrites,
  5. required Logic pendingValidMask,
  6. AssociativeMatchSelection matchSelection = AssociativeMatchSelection.highestIndex,
  7. String name = 'indexed_memory_write_bypass',
})

Creates an indexed-memory pending-write bypass selector.

Implementation

IndexedMemoryWriteBypass({
  required Logic readAddress,
  required LogicType backingData,
  required Logic backingValid,
  required List<MemoryWriteRequest<LogicType>> pendingWrites,
  required Logic pendingValidMask,
  this.matchSelection = AssociativeMatchSelection.highestIndex,
  super.name = 'indexed_memory_write_bypass',
})  : pendingWriteCount = pendingWrites.length,
      super(
        definitionName: 'IndexedMemoryWriteBypass_'
            'P${pendingWrites.length}_A${readAddress.width}_'
            'D${backingData.width}_${matchSelection.name}',
      ) {
  if (pendingWrites.isEmpty ||
      pendingValidMask.width != pendingWrites.length) {
    throw ArgumentError(
        'A validity bit is required for every pending write.');
  }
  if (pendingWrites.any((write) => write.addr.width != readAddress.width)) {
    throw ArgumentError('Pending write address widths must match the read.');
  }
  if (!rohdPayloadsCompatible([
    backingData,
    ...pendingWrites.map((write) => write.data),
  ])) {
    throw ArgumentError(
        'Pending write data and backing data must share one payload schema.');
  }

  final localReadAddress =
      addInput('readAddress', readAddress, width: readAddress.width);
  final localBackingData = addTypedInput('backingData', backingData);
  final localBackingValid = addInput('backingValid', backingValid);
  final localPendingValidMask = addInput('pendingValidMask', pendingValidMask,
      width: pendingWrites.length);
  final localPendingWrites = [
    for (var index = 0; index < pendingWrites.length; index++)
      addTypedInput('pendingWrite$index', pendingWrites[index])
  ];

  final matcher = typed.AssociativeMatcher<Logic>(
    query: localReadAddress,
    storedKeys: [
      for (final write in localPendingWrites) write.addr,
    ],
    validMask: localPendingValidMask,
    name: '${name}_matcher',
  );
  // The compatibility selector supplies implicit index values.
  // ignore: deprecated_member_use_from_same_package
  final selector = AssociativeMatchSelector(
    matchVector: matcher.matchVector,
    preference: matchSelection == AssociativeMatchSelection.lowestIndex
        ? lowerAssociativeSelectionValue
        : higherAssociativeSelectionValue,
    selectionName: matchSelection.name,
    name: '${name}_selector',
  );
  Logic selectedData = Const(0, width: backingData.width);
  for (var index = pendingWrites.length - 1; index >= 0; index--) {
    selectedData = mux(
      selector.selectedIndex.eq(index),
      localPendingWrites[index].data.packed,
      selectedData,
    ).named('${name}_selected_data_$index');
  }

  addOutput('dataValid') <= matcher.hit | localBackingValid;
  connectPackedPayload(addTypedPayloadOutput(this, 'data', localBackingData),
      mux(matcher.hit, selectedData, localBackingData.packed));
  addOutput('bypassed') <= matcher.hit;
  addOutput('pendingIndex',
          width: max(1, (pendingWrites.length - 1).bitLength)) <=
      mux(
        matcher.hit,
        selector.selectedIndex,
        Const(0, width: selector.selectedIndex.width),
      );
  addOutput('matchVector', width: pendingWrites.length) <=
      matcher.matchVector;
}