Associative array

how can i restrict an associative array to store maximum of 1000 locations?

You write code that prevents it from happening.

1 Like

yes Dave, I tried writing code for it, but associative array locations are discrete. But I am unable to do that.

You have to restrict the numbers of WR.

Thank you chr_sue, I just want to know is there any other way to restrict an Associative Array.

You can check the size of your assiciative array by calling the size() or num() method before writing to this array.

1 Like

This is becoming an XY problem.

Why do you think you need to use an associative array? Why do you think you need to limit it to 1000 locations? Did you mean addresses 0-1000 or just the total number of elements? What do you want to have happen if someone tries to write more than 1000?

You can wrap your associative array in a class an provide whatever other functionality you want:

class pool #(type ELEMENT_t, INDEX_t, int MAX_SIZE);
  protected string m_name;
  function new(string name);
    m_name = name;
  endfunction
  protected ELEMENT_t aa[INDEX_t];
  function void write(INDEX_t index, ELEMENT_t value);
    if (aa.num() < MAX_SIZE)
      aa[index] = value;
    else
      $error("array %s has reached maximum size %d",m_name, MAX_SIZE);
  endfunction  
  function ELEMENT_t read(INDEX_t index);
    if (aa.exists(index))
      return aa[index];
    else
      $error("array %s has no element %0p", m_name, index);
  endfunction
endclass

module top;
  
  pool #(.ELEMENT_t(int), .INDEX_t(int),.MAX_SIZE(2)) my_array = new("my_array");

  initial begin
    my_array.write(1,2);
    $display(my_array.read(1));
    $display(my_array.read(2));
    my_array.write(2,4);
    my_array.write(0,6);
  end
endmodule
1 Like