Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing arrays in Verilog

How do I initialize the array Save_state? This statement is giving X value at the output:

reg [9:0] count 
 reg [9:0] Save_state [0: 1024]; 

 always @ (posedge Clock )
 Count <=count+1 ;
 Save_state[count] <=count ;
like image 661
Misal313 Avatar asked Nov 01 '25 17:11

Misal313


1 Answers

You can use an initial block as well. This is allowed in simulation and is synthesizable on some architectures (Xilinx FPGA and CPLD support register initialization)

reg [9:0] count 
reg [9:0] Save_state [0: 1024]; 

integer i;
initial begin
  count = 0;
  for (i=0;i<=1024;i=i+1)
    Save_state[i] = 0;
end

always @ (posedge Clock ) begin
   count <= count + 1;
   Save_state[count] <= count;
end

Although for this particular example, in which the elements of the Save_state array will always have the same value, you can do like this (synthesizable on Xilinx and Altera, AFAIK):

reg [9:0] Save_state [0: 1024]; 

integer i;
initial begin
  for (i=0;i<=1024;i=i+1)
    Save_state[i] = i[9:0];
end

And at the beginning of you simulation, Save_state already have the values 0,1,2,...,1023 stored in it.

like image 126
mcleod_ideafix Avatar answered Nov 03 '25 08:11

mcleod_ideafix