MemoryIndexAllocator constructor

MemoryIndexAllocator({
  1. required Logic clk,
  2. required Logic reset,
  3. required Logic allocate,
  4. required Logic release,
  5. required Logic releaseIndex,
  6. required int capacity,
  7. SynchronousMemoryFunctor? memoryFunctor,
  8. MemoryConfiguration? memoryConfiguration,
  9. SynchronousRamReadUnderWrite readUnderWrite = SynchronousRamReadUnderWrite.readFirst,
  10. int memoryReadLatency = 1,
  11. String name = 'memory_index_allocator',
})

Creates a synchronous-memory-backed index allocator core.

Implementation

MemoryIndexAllocator({
  required Logic clk,
  required Logic reset,
  required Logic allocate,
  required Logic release,
  required Logic releaseIndex,
  required this.capacity,
  SynchronousMemoryFunctor? memoryFunctor,
  MemoryConfiguration? memoryConfiguration,
  this.readUnderWrite = SynchronousRamReadUnderWrite.readFirst,
  this.memoryReadLatency = 1,
  super.name = 'memory_index_allocator',
})  : indexWidth = max(1, capacity.log2Ceil),
      super(
        definitionName: 'MemoryIndexAllocator_C${capacity}_'
            'L${memoryReadLatency}_U${readUnderWrite.name}_'
            'S${_memoryIndexAllocatorIdentity(
          memoryFunctor,
          memoryConfiguration,
          capacity,
          memoryReadLatency,
          readUnderWrite,
        )}',
      ) {
  if (capacity < 1 || memoryReadLatency < 1) {
    throw ArgumentError('capacity and memoryReadLatency must be positive.');
  }
  if (releaseIndex.width != indexWidth) {
    throw ArgumentError('Release indices must be $indexWidth bits wide.');
  }
  this.memoryFunctor = memoryFunctor ??
      _selectIndexAllocatorMemoryFunctor(
        memoryConfiguration,
        indexWidth: indexWidth,
        capacity: capacity,
        readLatency: memoryReadLatency,
        readUnderWrite: readUnderWrite,
      );

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  final localAllocate = addInput('allocate', allocate);
  final localRelease = addInput('releaseOffer', release);
  final localReleaseIndex =
      addInput('releaseIndex', releaseIndex, width: indexWidth);
  final countWidth = max(1, (capacity + 1).log2Ceil);
  final initialized = Logic(name: '${name}_initialized');
  final initIndex = Logic(name: '${name}_init_index', width: indexWidth);
  final head = Logic(name: '${name}_head', width: indexWidth);
  final tail = Logic(name: '${name}_tail', width: indexWidth);
  final headValid = Logic(name: '${name}_head_valid');
  final available = Logic(name: '${name}_available', width: countWidth);
  final write = DataPortInterface(indexWidth, indexWidth);
  final read = SynchronousReadDataPortInterface(indexWidth, indexWidth);
  final allocationOffered = initialized & headValid;
  final allocationAccepted = allocationOffered & localAllocate;
  final hasFree = available.neq(0);
  final allocationNeedsRefill = allocationAccepted & available.gt(1);
  final readLimiter = OutstandingRequestLimiter(
    clk: localClk,
    reset: localReset,
    requestRequested: allocationNeedsRefill,
    responseValid: read.valid,
    maximumOutstanding: 1,
    name: '${name}_read_limiter',
  );
  final releaseCanReplaceLast = allocationAccepted & available.eq(1);
  final releaseCanAppend = hasFree & ~releaseCanReplaceLast;
  final releaseReady =
      initialized & (available.lt(capacity) | allocationAccepted);
  final releaseAccepted = localRelease & releaseReady;
  addOutput('canAllocate') <= allocationOffered;
  addOutput('allocatedIndex', width: indexWidth) <= head;
  addOutput('allocateAccepted') <= allocationAccepted;
  addOutput('canRelease') <= releaseReady;
  addOutput('releaseAccepted') <= releaseAccepted;
  final initLast = initIndex.eq(capacity - 1);
  final initializingNow = ~initialized;
  write.en <= initializingNow | (releaseAccepted & releaseCanAppend);
  write.addr <= mux(initializingNow, initIndex, tail);
  write.data <=
      mux(
        initializingNow,
        mux(initLast, Const(0, width: indexWidth), initIndex + 1),
        localReleaseIndex,
      );
  read.en <= readLimiter.requestIssued;
  read.addr <= head;
  memoryBinding = this.memoryFunctor(
    clk: localClk,
    reset: localReset,
    writePorts: [write],
    readPorts: [read],
    entryCount: capacity,
    readLatency: memoryReadLatency,
    readUnderWrite: readUnderWrite,
    name: '${name}_free_list',
  );

  addOutput('availableCount', width: countWidth) <= available;
  addOutput('initializing') <= initializingNow;
  addOutput('exhausted') <= initialized & available.eq(0);
  final lastIndex = Const(capacity - 1, width: indexWidth);
  final nextInitIndex =
      mux(initLast, Const(0, width: indexWidth), initIndex + 1);
  Sequential(localClk, reset: localReset, resetValues: {
    initialized: 0,
    initIndex: 0,
    head: 0,
    tail: 0,
    headValid: 0,
    available: 0,
  }, [
    If(initializingNow, then: [
      initIndex < nextInitIndex,
      If(initLast, then: [
        initialized < 1,
        head < 0,
        tail < lastIndex,
        headValid < 1,
        available < capacity,
      ]),
    ], orElse: [
      If(allocationAccepted & ~releaseAccepted,
          then: [available < available - 1]),
      If(~allocationAccepted & releaseAccepted,
          then: [available < available + 1]),
      If(allocationAccepted, then: [headValid < 0]),
      If(read.valid, then: [
        head < read.data,
        headValid < 1,
      ]),
      If(releaseAccepted, then: [
        tail < localReleaseIndex,
        If(~hasFree | releaseCanReplaceLast, then: [
          head < localReleaseIndex,
          headValid < 1,
        ]),
      ]),
    ]),
  ]);
}