How do I access an array element using a variable as index?

Ariel Jorge Rossi picture Ariel Jorge Rossi · Jul 1, 2016 · Viewed 11.9k times · Source

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?

Answer

teadotjay picture teadotjay · Jul 1, 2016

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.