FPGA verilog for循环

for语句在Verilog 中一般不在可综合代码中使用,因为for循环会被综合器展开为所有变量情况的执行语句 ,每个变量独立占用寄存器资源,每条执行语并不能有效地复用硬件逻辑资源,造成巨大的资源浪费。 简单的说就是:for语句循环几次,就是将相同的电路复制几次,因此循环次数越多,占用面积越大, 综合就越慢。for语句的一般 使用 情况: 在testbench中 使用 ,往往用于激励信号的生成。看如下图所示例子,该例子是野火rs232接口教程中采用for语句来产生测试激励的数据。

 先定义一个常量i  integer i 

采用for语句产生一帧数据,结构清晰;

for循环的主要功能用于赋值和延迟两个功能

赋值


module count(
input i_clk,
input i_rst,
output reg[9:0]o_count1,
output reg[9:0]o_count2,
output reg[9:0]o_count3
);
 
reg[7:0]tmps;
integer i;
reg[7:0]men_delay[16:1]; //定义二维数组men_delay
always @(posedge i_clk or posedge i_rst)
begin
     if(i_rst)
     begin
         tmps<=8'd0;
         for(i=1;i<=16;i=i+1)begin
            men_delay[i]<=8'd0;
         end
     end
else begin
         tmps<=tmps+8'd1;
         for(i=1;i<=16;i=i+1) begin
            men_delay[i]<=tmps;
         end
     end
end    
 
 
 
 
endmodule

写好仿真后 

 

通过integer i;定义一个for循环的计数器变量,

而对于变量,则通过reg[7:0]men_delay[16:1];这种方式来定义,即定义16个数组,其位宽为8。

通过仿真可以看到,每一次时钟周期,通过for循环,可以实现16个变量的赋值

延迟

module count(
input i_clk,
input i_rst,
 
input [7:0]din,
output [7:0]dout
);
 
 
integer i;
reg[7:0]men_delay[256:1]; 
always @(posedge i_clk or posedge i_rst)
begin
     if(i_rst)
     begin
         for(i=1;i<=256;i=i+1)
         begin
         men_delay[i]<=8'd0;
         end
     end
else begin
         men_delay[1]<=din;
         for(i=2;i<=256;i=i+1)
         begin
         men_delay[i]<=men_delay[i-1];
         end
     end
end    
 
assign dout= men_delay[200];
 
 
endmodule

输入的数据din为八位位宽,

reg[7:0]men_delay[256:1];定义256个数组,位宽为8

在每个时钟上升沿 men_delay[i]<=men_delay[i-1] 相当于把din延迟一拍,最后 assign out= men_delay[200],相当于延时200个周期;

编写测试文件仿真如下

当din发生改变时,一个周期10ns 发生了200个周期的延迟,因此2000ns之后dout才发生改变;

  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Verilog,for循环是一种可以被综合成电路的循环结构。它的一般形式是for(variable = start_value; continue_condition; circle_express) begin operations... end。其,variable是一个变量名,start_value是变量的初始值,continue_condition是循环的继续条件,circle_express是每个循环的步进操作,operations是每次循环的操作。\[1\] 然而,需要注意的是,在Verilog,for循环一般不在可综合代码使用,因为它会被综合器展开为所有变量情况的执行语句,每个变量独立占用寄存器资源,造成巨大的资源浪费。因此,for循环的使用会导致占用面积增大,综合速度变慢。\[3\] 在Verilog,for循环的一般使用情况是在testbench使用,用于生成激励信号。例如,在测试RS232接口时,可以使用for循环来产生测试激励的数据。\[3\] 总结起来,Verilog的for循环是一种可以被综合成电路的循环结构,但由于资源浪费的原因,一般不在可综合代码使用,而更多地用于testbench生成激励信号。 #### 引用[.reference_title] - *1* *2* [17,verilog之for循环](https://blog.csdn.net/fpga_start/article/details/122588375)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [FPGA verilog for循环](https://blog.csdn.net/weixin_49054039/article/details/126437027)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旭旭宝宝和车友车行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值