3
\$\begingroup\$

For example,

always @(posedge clk)
begin
 repeat (20)
 @(posedge clk) ;
end

In this statement, when the clk is triggered at first time, repeat statement will be executed. However, if the clk is triggered next time, does it mean that there are two running repeat statements?

ocrdu
9,34123 gold badges33 silver badges43 bronze badges
asked Apr 29, 2018 at 16:07
\$\endgroup\$
0

2 Answers 2

5
\$\begingroup\$

A 'posedge' (or other) condition only becomes effective (can be triggered again) if the events triggered by it have finished.

Thus the first posedge will start the repeat condition. After 20 "posedges" the one at the top can be triggered again.


A question popped up:
Yes, it is complete legal Verilog and can be used in test benches.

This is probably a more useful example:

always @(posedge signal)
begin 
 <do something>
 // ignore any posedges of signal for the next 20 clock cycles
 repeat (20)
 @(posedge clk);
end
answered Apr 29, 2018 at 16:15
\$\endgroup\$
4
  • \$\begingroup\$ Thks! But, I have one more. Let's assume that the always block is triggered by posedge clk as well as other signals, if always block is triggered by posedge clk, then even though other signals come in while it is executing, then always block is not triggered until repeat (20) is finished? \$\endgroup\$ Commented Apr 29, 2018 at 16:20
  • \$\begingroup\$ All the conditions in the always @(...) section are ineffective until the code triggered by one or more of them it has finished. \$\endgroup\$ Commented Apr 29, 2018 at 16:25
  • \$\begingroup\$ @sungjuncho correct. For simulation constructs like this, everything in the always block executes sequentially. Until it reaches the end, it will never start again - even if that causes it to miss events. \$\endgroup\$ Commented Apr 29, 2018 at 16:26
  • \$\begingroup\$ this is bit late but what if I have always@(*) repeat (5) #a = 5 ; b = 4; # a= 4 ; b = 5; and in Testbench a = 4 and b = 5 initially \$\endgroup\$ Commented May 29, 2023 at 10:32
0
\$\begingroup\$

Here is an example I just found from this Chinese Blog: Runoob verilog: loop

In the code provided, an 8-bit continuous loading circuit is implemented.

always @(posedge clk or negedge rstn) begin
 j = 0 ;
 if (!rstn) begin
 repeat (8) begin
 buffer[j] <= 'b0 ; //assign all 8 regs to 0, without delay
 j = j + 1 ;
 end
 end
 else if (enable) begin
 repeat (8) begin
 @(posedge clk) buffer[j] <= counter3 ; //assign @ nxt posedge clk
 j = j + 1 ;
 end
 end
end

In this example, with rstn set, buffer[j] <= counter3 is executed 8 times in 9 consecutive clk pos edges, and j loops from 0 to 8.

Based on the simulation, we find the top @(pose clk or negedge) would not be executed until its sub loop repeat(8) has finished.

enter image description here

ocrdu
9,34123 gold badges33 silver badges43 bronze badges
answered Apr 7, 2024 at 5:17
\$\endgroup\$

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.