Verilog 10进制计数器及主要的分频器 今天看懂,任务

别小看这个程序,这个小小的程序能看出你的Verilog功底和你的硬件思想(有点夸张哈)

ps:synplify 真的很强,很能优化。。。。

代码1:

               module counter(clk, rst_n, out);
                                  input clk,rst_n;
                                  output [3:0] out;
                                  reg [3:0] count;
                              assign out = count;
                            always @(posedge clk)
                                      if(rst_n)
                                        count <= 0;
                                     else
                                       case(count)
                                         0,1,2,3,4,5,6,7,8: count <= count + 1;
                                       default: count <= 0;
                                  endcase
endmodule

综合的电路图:

                                

代码2:                  module counter(clk, rst_n, out);
                                       input clk,rst_n;
                                       output [3:0] out;
                                       reg    [3:0] out;
             
                                 always @(posedge clk)
                                        if(rst_n)
                                         out<= 0;
                                      else if(out<4'b1010)
                                         out<=out+1'b1;
                                      else out<=0;
endmodule

综合的电路图为:

                              

总结:上述的代码规范差不多,但是一看就知道第一个代码是比较有硬件思想的人,assign语句将结果寄存输出,从综合的电路图就可以知道,第一个代码用的器件少(少了一个比较器),第二个代码属于软件思维比较多一点。

ps:如果你在第一个else if中将out<4'b1001改成out[3]&&out[0]你就悲剧了。(这应该就是硬件与软件的区别吧)综合工具会将out的0到2位全接地,而out的out[3]接一个DFF。

 

任意整数通用分频器Verilog(占空比无要求)

module divx(clk,reset,count,out);
input clk,reset;
output out,count;
reg out;
integer count; //计数
parameter x=4; //参数设定
   always@(reset) // 初始化
    if(reset)
       begin
        out=0;
        count=0;
      end
   always@(posedge clk) //时钟上升延
      begin
        if(count==x)
           begin
              out<=~out;
              count<=0;
           end
        else
          count<=count+1;
     end
  always@(negedge clk)  //时钟下降延
      begin
        if(count==x)
           begin
             out<=~out;
             count<=0;
           end
        else
          count<=count+1;
     end
endmodule

你可能感兴趣的:(Verilog 10进制计数器及主要的分频器 今天看懂,任务)