- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello, I came across a matter which I do not understand:
always@(posedge clk or negedge reset) begin
if(!reset) ....
When clk does not clock I change reset from high to low and ... always does not work. I would expect that when I change reset high to low (like Reset input of D-trigger) I have !reset condition effective in always construction. My assumption seem to be wrong. So my question is - what happens is a bug or a feature? If it is feature, please point me to the explanation of how this "always" with reset is implemented @ FPGA gate level. Thank you.
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
at FPGA gate level the reset is actually level-triggered, i.e. as long as reset is low, the register is held in reset. Depending on the FPGA you use, it might actually be an asynchronous active-high clear (as shown in the Cyclone IV handbook), or an asynchronous active-low clear (as shown in the Cyclone II handbook), but the synthesis tool will fix the logic for you. The only edge-sensitive element of a logic cell is the clock, which is always sensitive on the rising edge. However, simulation works different. There is no syntax (as far as I know) that describes level-sensitivity. Therefore you define the sensitivity as "@(posedge clk or negedge reset)", which means the always-block will be "executed" whenever the clock goes high, or when the reset becomes asserted (becomes low). It does not matter what happens while the reset is asserted, it only matters what happens when the reset becomes asserted. Example:- clock rising edge -> process becomes executed, the "else" path of your statement becomes executed since the reset is not asserted (i.e. not low)
- reset becomes low -> this is basically a reset falling edge -> process becomes executed, the "if" path of your statement becomes executed -> reset
- clock rising edge -> process becomes executed, the "if" path of your statement becomes executed, since the reset is still asserted (i.e. still low)
- reset becomes high -> nothing happens
- clock rising edge -> process becomes executed, the "else" path of your statement becomes executed since the reset is not asserted any more (i.e. not low again)
always @(posedge clk or negedge rst)
if(!rst)
q<=0;
else
q<=d;
If you do this instead, it won't work, as the second if will also execute: always @(posedge clk or negedge rst)
if(!rst)
q<=0;
if(clk) // WRONG!
q<=d;
Best regards, GooGooCluster
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you so much! Very informative and thorough answer. I use Cyclone-3, and use the "right" code. Tried moidelsim several times and it seems it was an intermittent problem of modelsim, or I did something wrong during simulation.
You raised one thing which was bothering me some time ago with your second, "wrong" example. When posedge of clk happens, what will happen if this "if (clk)" is used in the body of always? I thought to use it before, but decided that it may cause metastability of the circuit. Please advise.- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you use the "bad" example above:
always @(posedge clk or negedge rst)
if(!rst)
q<=0;
if(clk) // WRONG!
q<=d;
...this will happen during simulation: - clock rising edge -> process becomes executed, the "if(clk)" path of your statement becomes (so far so good)
- reset becomes low -> this is basically a reset falling edge -> process becomes executed, the "if(!rst)" path of your statement becomes executed -> reset (still good), but since the clock is still high, the "if(clk)" path is then also executed, virtually overriding the thing the "if(!rst)" path did!
- clock falling edge -> nothing happens (as expected)
- clock rising edge -> process becomes executed, again both paths become executed, as before - again, the reset has no effect!
- reset becomes high -> nothing happens (as expected)
- clock rising edge -> everything goes as usual

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page