repeat 循环语句执行指定循环数,如果循环计数表达式的值不确定,即为 x 或z 时,那么循环次数按 0 处理。repeat 循环语句的语法为:

repeat(循环次数表达式)
        begin
            语句块;
        end
        其中, “循环次数表达式”用于指定循环次数,可以是一个整数、变量或者数值表达式。如果是变量或者数值表达式,其数值只在第一次循环时得到计算,从而得以事先确定循环次数; “语句块”为重复执行的循环体。
        在可综合设计中, “循环次数表达式”必须在程序编译过程中保持不变。

Example1:
repeat (3) @(posedge clk)
		adder1<=adder1+1;

Example1中,每当到来一个时钟上升沿时,都会执行一次adder1<=adder1+1;

 Example2:
	repeat (3) @(posedge clk);
		adder1<=adder1+1;

Example2中,repeat (3) @(posedge clk);语句后有一个分号,也就是空语句,什么都不执行,当遇到3次时钟上升沿后,才会执行adder1<=adder1+1语句。

repeat用法示例1:语句块包含task语句

repeat(循环次数表达式)
        begin
            语句块;
        end
语句块可以包含task、function、for等语句,如下所示,是包含task的示例,将task任务执行10次:

`timescale 1ns / 1ps

module tb;

   reg  clk,rst,in;
   wire   out;
   
   initial 
    begin
        clk=0;
        rst=0;
        in =0;
        #100 rst=1;
    
    repeat (10)
		begin
			sim();
		end           
     end
   
   always #5 clk=~clk;

   task sim;
	begin
	
	  repeat(10)  @(posedge clk);
            in=0;
       repeat(10)  @(posedge clk);
            in=1;           
      repeat(10)  @(posedge clk);
            in=0; 
      repeat(10)  @(posedge clk);
            in=1;            
      repeat(10)  @(posedge clk);
            in=0;            
       repeat(10)  @(posedge clk);
            in=1;   	
	end
	
	endtask
 Top  inst(.clk(clk),.rst(rst),.in(in),.out(out)
    );

endmodule

repeat用法示例2:语句块包含for语句

`timescale 1ns / 1ps

module tb;

   reg  clk,rst,in;
   wire   out;
   
   integer i;
   initial 
    begin
        clk=0;
        rst=0;
        in =0;
        #100 rst=1;
       
    repeat (10)
		begin
			//sim();
		for (i=0;i<100;i=i+1)
			begin
				
				@(posedge clk)
					in<=~in;
				$display("the simulation time is %d\n",$time);
				
			end
		end           
    end
   
   always #5 clk=~clk;
   
   task sim;
	begin
	
	  repeat(10)  @(posedge clk);
            in=0;
       repeat(10)  @(posedge clk);
            in=1;           
      repeat(10)  @(posedge clk);
            in=0; 
      repeat(10)  @(posedge clk);
            in=1;            
      repeat(10)  @(posedge clk);
            in=0;            
       repeat(10)  @(posedge clk);
            in=1;   
	end
	
	endtask
 Top  inst(.clk(clk),.rst(rst),.in(in),.out(out)

    );

endmodule

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐