微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > FPGA,CPLD和ASIC > 菜鸟请教代码问题

菜鸟请教代码问题

时间:10-02 整理:3721RD 点击:
代码功能如下:
fq_chs:通过不同的confg0,confg1,confg2选择输出的fq_out为频率略有差别的时钟信号。在一段时间内对fq_out进行计数输出  
          cnt_numb。
问题:1.代码风格有没有问题,如果有,怎么改进?
       2.写testbench测试confg不同cnt_numb数值时,会出现一些未知的不定态,我问题出在哪?
fq_chs fq_chs(.ctrl(ctrl),.confg0(confg0),.confg1(confg1),
                     .confg2(confg2),.fq_out(fq_out));
always@(negedge cnt_rst or posedge clk_std)
   begin
     if(!cnt_rst)
       std_numb <= 8'h0;
     else
       std_numb <= std_numb+1;
   end
  assign compare_out=(std_numb==8'h77)?1:0;  
  always@(negedge cnt_rst or posedge compare_out)
   begin
     if(!cnt_rst)
       param<=0;
     else
       param<=1;
   end   
  always@(negedge cnt_rst or posedge fq_out )
     begin
       if(!cnt_rst)
          cnt_numb <= 8'h0;
       else
          if(param)
            cnt_numb<=cnt_numb;
          else
            cnt_numb<=cnt_numb+1;      
     end

你的param会是个latch。 最后的 if(param) cnt_numb<=cnt_numb; 没有必要。实际等效是 if(!param) cnt_numb <= cnt_numb + 1;


谢谢。latch会造成什么影响?怎么改为好

当 第一个posedge compare_out出现以后,param就不会再变了。
不太清楚你的第三态是什么意思,Z?



不确定状态,X。
我的目的是:在标准时钟clk_std作用一定(比方说500个)周期,在这段时间来对f_out计数,从而测f_out的频率。所以我要param保持不变。
我把latch改成如下之后还是仿真还是会出现部分X状态
  always@(negedge cnt_rst or posedge fq_out or posedge param )
     begin
       if(!cnt_rst)
          cnt_numb <= 8'h0;
       else
          if(param)
            cnt_numb<=cnt_numb;
          else
            cnt_numb<=cnt_numb+1;      
     end

always@(negedge cnt_rst or posedge clk_std)
   begin
     if(!cnt_rst)
       std_numb <= 8'h0;
     else
       std_numb <= std_numb+1;
   end
  always@(negedge cnt_rst or posedge fq_out )
       if(!cnt_rst)  
            cnt_numb <= 8'h0;
       else if(std_numb <= 8'h77)
            cnt_numb<=<=cnt_numb+1;
  
逻辑上应该实现你所想象的功能。除非你的fq_chs里面有其他的bug

问题:
       2.写testbench测试confg不同cnt_numb数值时,会出现一些未知的不定态,我问题出在哪?
你这个问题本身就有问题。根据你的code,cnt_numb怎么可以config呢?
cnt_numb是这个always block控制的,测定是多少就是多少。不知道你说的config是何意思?

你要测频率,也可以对待测时钟进行分频,让后用基准clock去测分频后的高电平或低电平长。这样不是思路很清晰吗?


我在问题中有提到:always block前面还有个模块
    fq_chs fq_chs(.ctrl(ctrl),.confg0(confg0),.confg1(confg1),
                     .confg2(confg2),.fq_out(fq_out));
fq_chs:通过不同的confg0,confg1,confg2选择输出的fq_out为频率略有差别的时钟信号。我指的confg就是信号confg0,confg1,confg2

周末闲着也是闲着,以下是我的思路写出的TB程序。既然是 TB就不必拘泥于正沿、负沿。
没测过,仅供参考。
reg [1:0] detector;
reg [7:0] cycle_cnt, std_numb;
wire raise_edge;
always @(negedge clk_std) detector <= {detector[0],fq_out};
assign raise_edge = (detector == 2'b01)? 1:0;
always @(posedge clk_std or negedge cnt_rst)
  begin
    if(!cnt_rst) begin
      cycle_cnt <= 'h0; std_numb = 'h0;
    end
    else begin
      if(cycle_cnt >1) begin
        if(cycle_cnt[0] == 0 && std_numb <= 8'h77) std_numb = std_numb + 1;
        else begin
          $display("time %4d std_numb = %h", $time, std_numb);
        end
      end
      if(raise_edge) cycle_cnt <= cycle_cnt + 1;
    end
  end

都不是时钟沿触发,不推荐



    求指点

观摩一下

还没解决,我不说了吗?你对 fout分频然后用clk_std去计数他的高电平不就可以了。这样就可以测频率了。



    已解决,谢谢

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top