学习笔记--Verilog for 综合电路

背景:想学习for循环能不能综合出电路,这样提高可以写代码灵活性。网上说可以,于是简单做了实验。

实验:用for写可变长度的移位寄存器。默认为3级移位寄存器(depth=3),宽度为2bit(width=2)。

代码:

module shift_reg_M_N
#(  parameter width = 2,
    parameter depth = 3
)
(
input rstn,
input clk,
input [width -1 : 0] data_in,
output reg [width -1 : 0] data_out
);
reg [width -1 : 0] data_reg [0 :  depth - 1  ];
reg [$clog2(depth):0] i ;//more 1 bit width
always@(posedge clk or negedge rstn)begin
    if(!rstn) begin
        for( i = 0; i <  depth ; i = i + 1) begin
            data_reg[i] <= 0;
        end
        data_out <=  0;
    end
    else begin
        data_reg [0] <= data_in ; 
        for(i = 1; i < depth ; i = i + 1) begin :shift
            data_reg[i] <= data_reg[i - 1] ;
        end
        data_out <=  data_reg[depth - 1] ;
    end
    end
endmodule

 RTL 电路

综合后的电路

testbench仿真

在testbench中把移位寄存器设置4级(depth=4),位宽设置为5bit。

module tb_shift_M_N();
reg clk;
reg rstn;
initial begin
    clk <= 0;
    rstn <= 0;
end
localparam width_data = 5;
localparam depth_shft = 4;

always # 1 clk = ~ clk;
always begin
  #10 rstn <= 1;
  #1000 rstn <= 0;
end
reg [width_data-1 : 0] data_in;
wire [width_data-1 : 0] data_out;

always @(posedge clk or negedge rstn)begin
  if(!rstn) begin
    data_in <= 0;
  end 
  else begin
    data_in <= data_in + 1;
  end
end

shift_reg_M_N 
#(
  .width (width_data ),
  .depth (depth_shft )
)
u_shift_reg_M_N(
  .rstn     (rstn     ),
  .clk      (clk      ),
  .data_in  (data_in  ),
  .data_out (data_out )
);

endmodule

初步结论:

for循环可以综合电路,我理解就是把for符合条件表达式的各个项并行展开执行。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值