parameter MEM_SIZE = 1024
reg [7:0] mem [0:MEM_SIZE -1]
initial
begin
for (k = 0; k < MEM_SIZE - 1; k = k + 1)
begin
mem[k] = 0;
end
end
reg [ 31:0] mem [0:4095];
initial $readmemh("contents.txt", mem);
F3FA0B59
i want to iniatialize array of reg [7:0]arr[15:0] using for loop ? anybody help me please..................
input clk, reset;
// Declare states
parameter S0 = 0, S1 = 1, S2 = 2, S3 = 3;
// Declare max address (it could be less words)
parameter MAXADDRESS = 8'bFF;
parameter FILLINGVALUE = 16'b0000;
reg [7:0] arr [15:0];
reg [7:0] i;
reg [1:0] state;
always @ (posedge clk or posedge reset) begin
if (reset)
state <= S0;
else
case (state)
S0: begin
i <= 0;
state <= S1;
end
S1: begin
arr[i] <= FILLINGVALUE;
if (i == MAXADDRESS) begin
state <= S2;
end
else begin
i <= i + 1;
state <= S1;
end
end
S2: begin
// ...
end
endcase
end
end
Actually $readmemh and $readmemb are synthesizable. Synthesis tools like Synplify Pro allow initial statements only for those functions. This is an example how I initialize the 2-D array mem with the file contents.txt:
In this case, the TXT file should only have hex characters and does not need to have the contents for all locations. For instance, something like this will initialize only the first location:Code:reg [ 31:0] mem [0:4095]; initial $readmemh("contents.txt", mem);
Code:F3FA0B59
...
S1: begin
arr[i] <= FILLINGVALUE;
if (i >= MAXADDRESS) begin
state <= S2;
end
else begin
i <= i + 1;
state <= S1;
end
end
...
jhunjhun, for a synthesizable memory initializer based on a state machine, I would only recommend you to initialize the addressing variable i.
Code:input clk, reset; // Declare states parameter S0 = 0, S1 = 1, S2 = 2, S3 = 3; // Declare max address (it could be less words) parameter MAXADDRESS = 8'bFF; parameter FILLINGVALUE = 16'b0000; reg [7:0] arr [15:0]; reg [7:0] i; reg [1:0] state; always @ (posedge clk or posedge reset) begin if (reset) state <= S0; else case (state) S0: begin i <= 0; state <= S1; end S1: begin arr[i] <= FILLINGVALUE; if (i == MAXADDRESS) begin state <= S2; end else begin i <= i + 1; state <= S1; end end S2: begin // ... end endcase end end
If you want to fill it with the address, you will have to pay attention to the word length.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?