4
\$\begingroup\$

I have a memory like this:

reg [7:0] memory [1023:0]

How should I use a for-loop to reset the memory in verilog? Please give me the code. For example if reset==0 then how can I reset it?

Paebbels
3,9872 gold badges23 silver badges43 bronze badges
asked Jan 3, 2016 at 10:50
\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

Usually large memories like what you are showing are implemented as block RAM type resources - dedicated RAM dotted about in the FPGA. These resources do not usually support reset - you can reset the output register, but not the array itself.

If you try and implement a reset signal, the synthesizer will realise that it cannot use a dedicated memory and try to implement the whole array in dedicated registers - in your case that would be 8192 registers, an 8bit 1024:1 multiplexer, and large quantities of address decoding logic. It would take a huge amount of resources!

If you still want to try the reset and see if it fits, you could simply add a for loop in your always block:

integer j;
always @ (posedge clock or negedge reset) begin
 if (~reset) begin //negative reset clause
 for (j=0; j < 1024; j=j+1) begin
 mem[j] <= 8'b0; //reset array
 end
 end else if (write) begin //write enable
 mem[waddr] <= data; //standard write logic
 end
end

However I would really seriously advise against this. Perhaps ask yourself why you need to do it at all and consider your other options.

One solution, albeit taking as many clock cycles as there are memory addresses is that on reset you have a counter which counts through all addresses and writes a zero to each one in turn (clears one address each clock cycle).

answered Jan 3, 2016 at 11:18
\$\endgroup\$
1
  • 2
    \$\begingroup\$ In addition to Tom's answer: You could implement a second memory with one valid bit per memory address. This reduces the amount of bits to resets to 1024. For example caches use this technique. \$\endgroup\$ Commented Jan 3, 2016 at 11:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.