TreePseudoLruReplacement constructor

TreePseudoLruReplacement({
  1. required Logic clk,
  2. required Logic reset,
  3. required Logic validMask,
  4. required Logic eligibleMask,
  5. required List<ReplacementAccessInterface> accesses,
  6. required List<ReplacementAllocationInterface> allocations,
  7. required int entryCount,
  8. List<ReplacementCommitInterface> commits = const [],
  9. String name = 'tree_pseudo_lru_replacement',
})

Creates a tree pseudo-LRU policy.

Implementation

TreePseudoLruReplacement({
  required super.clk,
  required super.reset,
  required super.validMask,
  required super.eligibleMask,
  required super.accesses,
  required super.allocations,
  required super.entryCount,
  super.commits,
  super.name = 'tree_pseudo_lru_replacement',
}) : super(
          definitionName: 'TreePseudoLruReplacement_E${entryCount}_'
              'H${accesses.length}_A${allocations.length}'
              '${commits.isEmpty ? '' : '_C${commits.length}'}') {
  if ((entryCount & (entryCount - 1)) != 0) {
    throw ArgumentError.value(
        entryCount, 'entryCount', 'Must be a power of two.');
  }

  Logic update(Logic state, Logic index) {
    if (entryCount == 1) {
      return state;
    }
    final nodes = List<({int start, int end})?>.filled(entryCount - 1, null);
    void visit(int node, int start, int end) {
      if (end - start == 1) {
        return;
      }
      nodes[node] = (start: start, end: end);
      final middle = (start + end) ~/ 2;
      visit(node * 2 + 1, start, middle);
      visit(node * 2 + 2, middle, end);
    }

    visit(0, 0, entryCount);
    final extended = index.zeroExtend(indexWidth + 1);
    return [
      for (var node = 0; node < nodes.length; node++)
        mux<Logic>(
          extended.gte(Const(nodes[node]!.start, width: indexWidth + 1)) &
              extended.lt(Const(nodes[node]!.end, width: indexWidth + 1)),
          mux(
              extended.lt(Const((nodes[node]!.start + nodes[node]!.end) ~/ 2,
                  width: indexWidth + 1)),
              Const(1),
              Const(0)),
          state[node],
        )
    ].rconcat();
  }

  ReplacementSelection select(Logic state, Logic candidates) {
    ReplacementSelection visit(int node, int start, int end) {
      if (end - start == 1) {
        return (
          valid: candidates[start],
          index: Const(start, width: indexWidth)
        );
      }
      final middle = (start + end) ~/ 2;
      final left = visit(node * 2 + 1, start, middle);
      final right = visit(node * 2 + 2, middle, end);
      final chooseRight =
          mux(state[node], right.valid, ~left.valid & right.valid);
      return (
        valid: left.valid | right.valid,
        index: mux(chooseRight, right.index, left.index)
      );
    }

    return visit(0, 0, entryCount);
  }

  buildPolicy(
    stateWidth: entryCount == 1 ? 1 : entryCount - 1,
    onAccess: update,
    select: select,
    onAllocation: update,
  );
}