将模块2的输出链接到模块1 VERILOG的if else语句

时间:2014-07-07 09:14:19

标签: c verilog xilinx

我的目标是当我的输入" start = 1"转变是无止境的,当我把它改为" start = 0"转移停止。

当我的输出(result1 = 1)和(result = 5)时,它应该在$ finish命令行结束。但它会在测试平台的$ stop结束。

我认为问题在于第二个模块中的两个输出(result1和result2)没有在第一个模块中链接。

如何将第二个模块中的输出链接到第一个模块,以便满足第一个模块中if-else语句中的条件,并继续$finish

我预设了我的测试平台代码,以便在start = 0时;它在result1 = 1和result2 = 5时停止。

这是我的代码 //第一个模块

module random(ps_in, ps_out, clk, start, result1, result2);

input      ps_in;
output reg ps_out;
input      clk;
input      start;

output [2:0] result1; //1st output based on the 2nd module
output [2:0] result2; //2nd output based on the 2nd module

reg count;

initial begin
  ps_out = 0;
  count  = 0;
end

always @ (posedge clk)
  if (start!=0) begin
    ps_out = ps_in;
  end
  else if (start!=1 && count!=1) begin
    ps_out = ps_in;
    count  = count + 1;
  end
  else if (start!=1 && result1==3'b001 && result2==3'b101) begin
    $finish; //IT MUST END IN THIS LINE
  end

endmodule



//2nd module


module smachine (start,clk,result1,result2);

input start;
input clk;

output [2:0] result1;
output [2:0] result2;

wire feedback1, feedback2, ffq1, ffq2, ffq3, ffq4;

random r1 (feedback1,  ffq1,          clk, start);
random r2 (ffq1,          result1[2], clk, start);
random r3 (result1[2], result1[1], clk, start);
random r4 (result1[1], result1[0], clk, start);
random r5 (result1[0], ffq2,       clk, start);

assign feedback1 = (result1[1] ^~ffq2);

random r6 (feedback2,  result2[2], clk, start);
random r7 (result2[2], result2[1], clk, start);
random r8 (result2[1], ffq3,       clk, start);
random r9 (ffq3,          result2[0], clk, start);
random r10(result2[0], ffq4,       clk, start);

assign feedback2 = (ffq3 ^~ffq4);

endmodule

这是我的测试平台

module qqqq;

    // Inputs
    reg start;
    reg clk;

    // Outputs
    wire [2:0] result1;
    wire [2:0] result2;

    // Instantiate the Unit Under Test (UUT)
    smachine uut (
        .start(start), 
        .clk(clk), 
        .result1(result1), 
        .result2(result2)
    );

always
#5 clk = ~clk;

    initial begin
        // Initialize Inputs
        #10 start = 1;
        clk = 0;
        #50 start = 0;
        clk = 0;
        #50 $stop;
    end

    endmodule

1 个答案:

答案 0 :(得分:1)

how-to-instantiate-a-module上阅读问题可能对您的理解有益。

在模块random中,结果[12]是输出:

output [2:0] result1; //1st output based on the 2nd module
output [2:0] result2; //2nd output based on the 2nd module

永远不会为它们分配值,因此条件result1==3'b001 && result2==3'b101永远不会成立。

if (start!=1 && result1==3'b001 && result2==3'b101) begin
  $finish; //IT MUST END IN THIS LINE
end

这就是为什么它不会退出$finish

可能他们应该投入?然后你需要从模块smachine驱动值。目前你只做了4个连接,忽略了最后2个结果和结果2。

你的Testbench module qqqq;没有实例化smachine,应该是:

module tb;
  logic clk;
  logic start;
  logic [2:0] result1;
  logic [2:0] result2;

smachine DUT (
  .start (start), //Port connection
  .clk   (clk),   //Driving Signals to smachine instance DUT

  .result1 (result1),
  .result2 (result2)
);  

initial 
  forever
    #5 clk = ~clk;

 initial begin
   // Initialize Inputs
   #10 start = 1;
   clk = 0;
   #50 start = 0;
   clk = 0;
   #50 $stop; // BUT IT ENDS HERE
  end

endmodule

可在EDAplayground上找到此示例。

添加注释中请求的示例以将输出捕获到不同的位置,这将迭代4个输出。

reg [1:0] state = 2'b0; //state is a basic sounter

//Nextstate assignment
always @(posedge clk) begin
  state <= nextstate ;
end

//Next state logic
always @* begin //Combinatorial section
  nextstate = state+1 ;
end

// State Output
always @( posedge clk) begin
  case(state)
    2'd0 : output1 <= ps_out;
    2'd1 : output2 <= ps_out;
    2'd2 : output3 <= ps_out;
    2'd3 : output4 <= ps_out;
end