MultiMetadataTable<LogicType extends Logic> constructor

MultiMetadataTable<LogicType extends Logic>({
  1. required Logic clk,
  2. required Logic reset,
  3. required List<Logic> writes,
  4. required List<Tagged<LogicType>> writeData,
  5. required List<Logic> lookupTags,
  6. required int capacity,
  7. List<LogicValue>? writeMasks,
  8. LogicValue? resetValue,
  9. RegisterFileFunctor? storageImplementation,
  10. String name = 'multi_metadata_table',
})

Creates typed, ordered, direct-indexed metadata storage driven by explicit write actions.

The first writeData entry declares the stored schema; every other lane must be compatible with it. storageImplementation selects the entry backing; the default is synthesizable flop state.

Implementation

MultiMetadataTable({
  required Logic clk,
  required Logic reset,
  required List<Logic> writes,
  required List<Tagged<LogicType>> writeData,
  required List<Logic> lookupTags,
  required this.capacity,
  List<LogicValue>? writeMasks,
  LogicValue? resetValue,
  RegisterFileFunctor? storageImplementation,
  super.name = 'multi_metadata_table',
})  : tagWidth = writeData.isEmpty ? 0 : writeData.first.tag.width,
      writeLaneCount = writes.length,
      lookupCount = lookupTags.length,
      writeMasks = writeMasks ??
          List<LogicValue>.filled(
            writes.length,
            LogicValue.filled(
              writeData.isEmpty ? 0 : writeData.first.data.width,
              LogicValue.one,
            ),
          ),
      resetValue = resetValue ??
          LogicValue.filled(
            writeData.isEmpty ? 0 : writeData.first.data.width,
            LogicValue.zero,
          ),
      super(
        definitionName: _multiMetadataCoreDefinitionName(
          capacity,
          writeData,
          lookupTags.length,
          writeMasks,
          resetValue,
          storageImplementation,
        ),
      ) {
  if (capacity < 1 || writeLaneCount < 1 || lookupCount < 1) {
    throw ArgumentError(
        'Capacity, write lanes, and lookup ports must be positive.');
  }
  if (writeData.length != writeLaneCount) {
    throw ArgumentError('Every write action requires its tagged payload.');
  }
  final minimumTagWidth = max(1, capacity.log2Ceil);
  final dataWidth = writeData.first.data.width;
  if (tagWidth < minimumTagWidth ||
      writeData.any((write) =>
          write.tag.width != tagWidth || write.data.width != dataWidth) ||
      this.writeMasks.length != writeLaneCount ||
      this.writeMasks.any((mask) => mask.width != dataWidth) ||
      this.resetValue.width != dataWidth ||
      lookupTags.any((tag) => tag.width != tagWidth)) {
    throw ArgumentError(
        'Write tags, metadata widths, masks, reset value, and lookup tags '
        'must match capacity.');
  }
  if (!rohdPayloadsCompatible(writeData.map((write) => write.data),
      ignoreConstantNames: true)) {
    throw ArgumentError('Metadata write payloads must be compatible.');
  }

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  final localLookupTags = [
    for (var port = 0; port < lookupCount; port++)
      addInput('lookupTag$port', lookupTags[port], width: tagWidth),
  ];
  final localWrites = [
    for (var lane = 0; lane < writeLaneCount; lane++)
      addInput('write$lane', writes[lane]),
  ];
  final localWriteData = [
    for (var lane = 0; lane < writeLaneCount; lane++)
      addTypedInput('writeData$lane', writeData[lane]),
  ];
  final schema = localWriteData.first.data;

  entryData = [
    for (var index = 0; index < capacity; index++)
      addTypedPayloadOutput(this, 'entryData$index', schema),
  ];
  registeredEntryData = [
    for (var index = 0; index < capacity; index++)
      addTypedPayloadOutput(this, 'registeredEntryData$index', schema),
  ];
  lookupData = [
    for (var port = 0; port < lookupCount; port++)
      addTypedPayloadOutput(this, 'lookupData$port', schema),
  ];
  registeredLookupData = [
    for (var port = 0; port < lookupCount; port++)
      addTypedPayloadOutput(this, 'registeredLookupData$port', schema),
  ];
  addOutput('invalidWrites', width: writeLaneCount);
  addOutput('invalidLookups', width: lookupCount);

  final stored = [
    for (var index = 0; index < capacity; index++)
      schema.cloneTyped(name: '${name}_metadata_$index'),
  ];
  final fillsTagSpace = capacity == (1 << tagWidth);
  Logic inRange(Logic tag, String operation) => fillsTagSpace
      ? Const(1)
      : tag
          .lt(Const(capacity, width: tagWidth))
          .named('${name}_${operation}_in_range');

  final acceptedWrites = [
    for (var lane = 0; lane < writeLaneCount; lane++)
      (localWrites[lane] & inRange(localWriteData[lane].tag, 'write_$lane'))
          .named('${name}_write_${lane}_accepted'),
  ];
  var updatedStored = <Logic>[...stored];
  for (var lane = 0; lane < writeLaneCount; lane++) {
    final mask = Const(this.writeMasks[lane]);
    updatedStored = [
      for (var index = 0; index < capacity; index++)
        mux(
          acceptedWrites[lane] & localWriteData[lane].tag.eq(index),
          (localWriteData[lane].data & mask) | (updatedStored[index] & ~mask),
          updatedStored[index],
        ).named('${name}_metadata_${index}_after_write_$lane'),
    ];
  }

  invalidWrites <=
      [
        for (var lane = 0; lane < writeLaneCount; lane++)
          localWrites[lane] &
              ~inRange(localWriteData[lane].tag, 'write_$lane'),
      ].rconcat();
  for (var index = 0; index < capacity; index++) {
    entryData[index] <= updatedStored[index];
    registeredEntryData[index] <= stored[index];
  }

  final invalidLookupBits = <Logic>[];
  for (var port = 0; port < lookupCount; port++) {
    Logic selected = Const(0, width: dataWidth);
    Logic registeredSelected = Const(0, width: dataWidth);
    for (var index = capacity - 1; index >= 0; index--) {
      selected = mux(
        localLookupTags[port].eq(index),
        updatedStored[index],
        selected,
      ).named('${name}_lookup_${port}_through_$index');
      registeredSelected = mux(
        localLookupTags[port].eq(index),
        stored[index],
        registeredSelected,
      ).named('${name}_registered_lookup_${port}_through_$index');
    }
    final range = inRange(localLookupTags[port], 'lookup_$port');
    lookupData[port] <= selected;
    registeredLookupData[port] <= registeredSelected;
    invalidLookupBits.add(~range);
  }
  invalidLookups <= invalidLookupBits.rconcat();

  if (storageImplementation == null) {
    Sequential(localClk, reset: localReset, resetValues: {
      for (final entry in stored) entry: this.resetValue,
    }, [
      for (var index = 0; index < capacity; index++)
        stored[index] < updatedStored[index],
    ]);
  } else {
    connectRegisterFileBank(
      clk: localClk,
      reset: localReset,
      current: stored,
      next: updatedStored,
      resetValues: List.filled(capacity, this.resetValue),
      implementation: storageImplementation,
      name: '${name}_storage',
    );
  }
}