verilog:在生成块中实例化和分配时出现问题

问题描述 投票:0回答:1

我无法理解生成块内的模块是如何实例化的。

我是 Verilog 新手,正在尝试实现一个检测 1010 的序列检测器。为此,我尝试使用 2 D 触发器。求解序列的状态表后,我得到

D1 = (Q2&~X | Q1&~Q2&X), D2 = X, Z = Q1&Q2&~X,其中 X 是我的输入,Z 是所需的输出。

用Verilog实现,目前我写的代码是:

module d_flipflop(d, clk, q);
  input d, clk;
  output reg q;
    
  always @ (posedge clk)
    q <= d;
  
endmodule


module seq_detector (clk, inp, outp);
  input [9:0] inp;
  input clk;
  output reg [9:0] outp;
  
  wire d1 = 0;
  wire q1 = inp[9], q2 = inp[8];
  
  generate
    genvar i;
    for (i=9; i>=0; i=i-1) begin
      
      d_flipflop ff2 (.d(inp[i]), .clk(clk), .q(q2) ); // X <=> inp[i]
      assign d1 = (q2 & ~inp[i]) | (q1 & ~q2 & inp[i] );
      d_flipflop ff1 (.d(d1), .clk(clk), .q(q1) );
      
      assign outp[i] = q1 & q2 & ~inp[i]; 
      
    end
  endgenerate
  
endmodule


module TB_seq_detector();
  reg [9:0] inp, clk;
  wire [9:0] outp;
  
  seq_detector DUT (.clk(clk), .inp(inp), .outp(outp) );
  
  // Generating clock:
  always #10 clk = ~clk;
  // Initializing clk:
  initial clk=0;
      
  // Test case:
  initial begin
    inp = 10'b1000101011;
  end
  
  initial begin
    $display("input: %b", inp[9:0]);
    $display("output: %b", outp[9:0]);
  end
  
  initial #100 $finish;
  
endmodule

我知道q1和q2实际上并没有被分配给inp[9]和inp[8](它们在时间=0时是x)。我该如何解决这个问题?我应该使用非阻塞赋值吗?另外,据我所知,生成块在模拟时间之前运行。所以我的clk也没有影响任何东西。我该如何解决这个问题?

verilog system-verilog
1个回答
0
投票

您的代码存在问题,需要修复。

如果您的模拟器没有向您显示类似于以下内容的警告,那么您应该在 EDAPlayground 上的模拟器上运行您的代码:

Warning-[PCWM-W] Port connection width mismatch
"seq_detector DUT( .clk (clk),  .inp (inp),  .outp (outp));"
  The following 10-bit expression is connected to 1-bit port "clk" of module 
  "seq_detector", instance "DUT".
  Expression: clk

以下行将

clk
声明为 10 位信号:

  reg [9:0] inp, clk;

您可能希望

clk
成为 1 位信号:

reg clk;

您有多个驱动程序用于

q1
信号:

  1. ff1
    实例的输出
  2. 连续作业:
    wire q1 = inp[9]

这会导致争用,导致 x(未知值)。您只能使用一个来源驱动

q1

q2
d1
也是如此。

© www.soinside.com 2019 - 2024. All rights reserved.