Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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).

share|improve this answer
    
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. – Paebbels Jan 3 at 11:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.