1

I'm trying to access an element from an array using an input as index and I keep getting this error:

cache.v:27: error: array 'tagc' index must be a constant in this context.

Here's how I'm trying to do it:

assign tagc[index] = tag;

tagc is an array of 1024 regs; index is a 10 bits input; tag is a 20 bits input.

Is there a way to do that?

1
  • 1
    We would need to see some context and what you are trying to do. Assigning an incoming signal to a dynamically-changing index in an array doesn't seem like it would be valid at all. Commented Jun 30, 2016 at 23:35

2 Answers 2

1

Two possibilities:

  1. You're trying to assign an indexed location of tagc to mirror the value of tag, in which case you need index to be a constant (parameter, localparam, or `define).

  2. You're using tagc as a memory that stores the value of tag in a location indexed by a dynamic variable 'index'. In this case, you need to do the assignment in an always block, after deciding what event should trigger an update of tagc.

Sign up to request clarification or add additional context in comments.

Comments

0

Usually an array assignment using a dynamic index is done in a clocked procedural block.

always @(posedge clk) begin
  tagc[index] <= tag;
end

It can also be done with latching. Use an enable signal and ensure index does not change while enabled.

always @* begin
  if (enable) begin
    tagc[index] <= tag;
  end
end

Or another latch option:

integer i;
always @* begin
  for(i=0; i<PARAM_SIZE_OF_TAG; i=i+1)
    if (index==i) tagc[i] <= tag;
  end
end

FYI: tagc must be defined as a reg not a wire type

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.