I've built a 8*2bits array to represent a piece of memory in Verilog
reg [1:0] m [0:7]
There is a reset signal for this memory and if reset is 1, all the bits in this memory should be reset to 0. But I don't know how to set all the bits of m in a concise way, because if there are hundreds thousands of bits in the memory, the following way is obviously unfeasible.
always@(posedge clk or posedge reset)
begin
if (reset)
begin
m[0]<=2'b00;
m[1]<=2'b00;
m[2]<=2'b00;
m[3]<=2'b00;
m[4]<=2'b00;
m[5]<=2'b00;
m[6]<=2'b00;
m[7]<=2'b00;
end
else
....
end
To initialize a 2D array with zeroes, you can make use of the explicit initialization property of the arrays, which states that the uninitialized part of an array is initialized with static storage duration.
Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces. Ex: int a[2][3]={0,0,0,1,1,1}; initializes the elements of the first row to zero and the second row to one. The initialization is done row by row.
Use a for
loop:
integer i;
always@(posedge clk or posedge reset)
begin
if (reset)
begin
for (i=0; i<8; i=i+1) m[i] <= 2'b00;
end
else
....
end
This is described in the IEEE Std 1800-2012 (Section 12.7.1 The for-loop, for example).
If you can use the current system verilog syntax, then this should work:
always_ff @(posedge clk or posedge reset)
begin
if(reset) begin
m <= '{default:2'b00};
end
else
...
end
See section 5.11 (Array Literals) of the 1800-2012 IEEE standard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With