generateReplacementPolicyModelVerilog function

String generateReplacementPolicyModelVerilog(
  1. ReplacementPolicyModelDescription description,
  2. String moduleName
)

Generates a simulation-only implementation of a replacement policy model.

The module has the ports of description and reproduces the four-state behavior of ReplacementPolicyModel: allocation results are combinational; accesses, deferred commits, and committed allocations update state on a known 0 to 1 clock transition, in that order; state starts unknown and is cleared by reset, and an unknown reset merges the cleared and updated states.

Implementation

String generateReplacementPolicyModelVerilog(
    ReplacementPolicyModelDescription description, String moduleName) {
  if (!Sanitizer.isSanitary(moduleName)) {
    throw ArgumentError.value(moduleName, 'moduleName', 'Invalid identifier.');
  }
  final d = description;
  final tree = d.replacementKind == ReplacementPolicyModelKind.treePseudoLru;
  final e = d.entryCount;
  final i = d.indexWidth;
  final s = d.stateWidth;
  final r = e.bitLength;
  String declaration(String direction, MapEntry<String, int> port) =>
      '$direction logic [${port.value - 1}:0] ${port.key}';
  final ports = [
    for (final port in d.inputPorts.entries) declaration('input', port),
    for (final port in d.outputPorts.entries) declaration('output', port),
  ];
  // Mirrors `selectLogicValue`: an unknown select keeps only agreeing bits.
  String choose(String name, int width) => '''
  function automatic [${width - 1}:0] $name(input logic select,
      input logic [${width - 1}:0] a, input logic [${width - 1}:0] b);
    if (select === 1'b1) $name = a;
    else if (select === 1'b0) $name = b;
    else
      for (integer k=0;k<$width;k=k+1) $name[k] = a[k] === b[k] ? a[k] : 1'bx;
  endfunction''';
  final source = StringBuffer('''
module $moduleName (
  ${ports.join(',\n  ')}
);
  logic [${s - 1}:0] state = 'x;
  logic [${s - 1}:0] next_state;
  logic old_clock = 1'bx;
${choose('choose_bit', 1)}
${choose('choose_index', i)}
${choose('choose_state', s)}
${choose('choose_mask', e)}
${choose('choose_rank', r)}
''');
  if (tree) {
    source.writeln('''
  function automatic [${s - 1}:0] update_policy(input logic [${s - 1}:0] old,
      input logic [${i - 1}:0] index);
    logic [$i:0] wide;
    wide = {1'b0, index};
    update_policy = old;''');
    void update(int node, int start, int end) {
      if (end - start == 1) {
        return;
      }
      final middle = (start + end) ~/ 2;
      final range = "(wide >= ${i + 1}'d$start) & (wide < ${i + 1}'d$end)";
      final left = "choose_bit(wide < ${i + 1}'d$middle, 1'b1, 1'b0)";
      source.writeln(
          '    update_policy[$node] = choose_bit($range, $left, old[$node]);');
      update(node * 2 + 1, start, middle);
      update(node * 2 + 2, middle, end);
    }

    update(0, 0, e);
    source.writeln('''
  endfunction

  function automatic [${s - 1}:0] access_policy(input logic [${s - 1}:0] old,
      input logic [${i - 1}:0] index);
    access_policy = update_policy(old, index);
  endfunction

  function automatic [${s - 1}:0] allocate_policy(input logic [${s - 1}:0] old,
      input logic [${i - 1}:0] index);
    allocate_policy = update_policy(old, index);
  endfunction

  // Returns the selection's validity above its index.
  function automatic [$i:0] select_policy(input logic [${s - 1}:0] policy,
      input logic [${e - 1}:0] candidates);''');
    final body = StringBuffer();
    var next = 0;
    int visit(int node, int start, int end) {
      final id = next++;
      body.writeln('    logic valid_$id; logic [${i - 1}:0] index_$id;');
      if (end - start == 1) {
        return id;
      }
      final middle = (start + end) ~/ 2;
      visit(node * 2 + 1, start, middle);
      visit(node * 2 + 2, middle, end);
      return id;
    }

    // Declarations precede statements, so the tree is walked twice.
    visit(0, 0, e);
    source.write(body);
    next = 0;
    int assign(int node, int start, int end) {
      final id = next++;
      if (end - start == 1) {
        source.writeln(
            "    valid_$id = candidates[$start]; index_$id = $i'd$start;");
        return id;
      }
      final middle = (start + end) ~/ 2;
      final left = assign(node * 2 + 1, start, middle);
      final right = assign(node * 2 + 2, middle, end);
      source
        ..writeln('    choose_right = choose_bit(policy[$node], valid_$right, '
            '~valid_$left & valid_$right);')
        ..writeln('    valid_$id = valid_$left | valid_$right;')
        ..writeln('    index_$id = choose_index(choose_right, index_$right, '
            'index_$left);');
      return id;
    }

    source.writeln('    logic choose_right;');
    final root = assign(0, 0, e);
    source.writeln('''
    select_policy = {valid_$root, index_$root};
  endfunction''');
  } else {
    source.writeln('''
  function automatic [${s - 1}:0] access_policy(input logic [${s - 1}:0] old,
      input logic [${i - 1}:0] index);
    access_policy = old;
  endfunction

  function automatic [${s - 1}:0] allocate_policy(input logic [${s - 1}:0] old,
      input logic [${i - 1}:0] index);
    allocate_policy = choose_index(index == $i'd${e - 1}, $i'd0,
      $i'(index + $i'd1));
  endfunction

  // Returns the selection's validity above its index.
  function automatic [$i:0] select_policy(input logic [${s - 1}:0] policy,
      input logic [${e - 1}:0] candidates);
    logic [${r - 1}:0] pointer, current, rank, selected_rank;
    logic [${i - 1}:0] selected;
    logic found, take;
    pointer = ${r == i ? 'policy' : "{{${r - i}{1'b0}}, policy}"};
    found = 1'b0; selected = '0; selected_rank = '0;
    for (integer k=0;k<$e;k=k+1) begin
      current = $r'(k);
      rank = choose_rank(current >= pointer, $r'(current - pointer),
        $r'($r'd$e - pointer + current));
      take = candidates[k] & (~found | (rank < selected_rank));
      selected = choose_index(take, $i'(k), selected);
      selected_rank = choose_rank(take, rank, selected_rank);
      found = found | candidates[k];
    end
    select_policy = {found, selected};
  endfunction''');
  }
  source.writeln('''

  always @* begin : evaluate
    logic [${s - 1}:0] pending;
    logic [${e - 1}:0] reserved, available, unused, candidates, reservation;
    logic [$i:0] choice;
    logic in_range, granted, committed, selected_valid, entry_match;
    logic invalid_access, invalid_commit;
    pending = state;
    invalid_access = 1'b0;
    invalid_commit = 1'b0;''');
  for (final (port, invalid, update) in [
    for (var p = 0; p < d.accessCount; p++)
      ('access_$p', 'invalid_access', 'access_policy'),
    for (var p = 0; p < d.commitCount; p++)
      ('commit_$p', 'invalid_commit', 'allocate_policy'),
  ]) {
    source.writeln('''
    in_range = {1'b0, ${port}_index} < ${i + 1}'d$e;
    $invalid = $invalid | (${port}_en & ~in_range);
    pending = choose_state(${port}_en & in_range,
      $update(pending, ${port}_index), pending);''');
  }
  source.writeln("    reserved = '0;");
  for (var p = 0; p < d.allocationCount; p++) {
    final port = 'allocation_$p';
    source.writeln('''
    available = eligibleMask & ~reserved;
    unused = available & ~validMask;
    candidates = choose_mask(|unused, unused, available);
    choice = select_policy(pending, candidates);
    granted = ${port}_en & choice[$i];
    committed = granted & ${port}_commit;
    invalid_commit = invalid_commit | (${port}_commit & ~granted);
    selected_valid = 1'b0;
    for (integer k=0;k<$e;k=k+1) begin
      entry_match = choice[${i - 1}:0] == $i'(k);
      selected_valid = selected_valid | (validMask[k] & entry_match);
      reservation[k] = granted & entry_match;
    end
    ${port}_valid = granted;
    ${port}_index = choose_index(granted, choice[${i - 1}:0], '0);
    ${port}_evict = granted & selected_valid;
    reserved = reserved | reservation;
    pending = choose_state(committed,
      allocate_policy(pending, choice[${i - 1}:0]), pending);''');
  }
  source.writeln('''
    invalidAccess = invalid_access;
    invalidCommit = invalid_commit;
    next_state = pending;
  end

  // Only a known rising edge updates state, as with the model's posedge.
  always @(clk) begin
    if (old_clock === 1'b0 && clk === 1'b1)
      state <= choose_state(reset, '0, next_state);
    old_clock = clk;
  end
endmodule''');
  return source.toString();
}