【问题标题】:linking output from module 2 to if else statement of module 1 VERILOG将模块 2 的输出链接到模块 1 VERILOG 的 if else 语句
【发布时间】:2014-07-07 09:14:18
【问题描述】:

我的目标是当我的输入“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

【问题讨论】:

  • 是的,它用于合成。当它满足条件时,我正要放置一个累积奖金指示器,但我在 $finish 开始它以测试代码。它就像一个轮盘游戏。
  • "当输入“start=1”时,移位是无穷无尽的"移位发生在哪里?唯一的功能单元是递增计数count = count + 1;

标签: c verilog xilinx


【解决方案1】:

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

在模块random result[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 个,即 result 和 result2。

您的 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找到。

添加 cmets 中请求的示例以将输出捕获到不同的位置,这将迭代 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

【讨论】:

  • 我假设 random 模块中的 results[12] 链接到 smachine 模块中的 results[12] .. 我的测试台链接到模块 smachine 因为该模块正在执行移位(随机)过程..我可以用来将smachine模块中的结果[12]链接到随机模块中的结果[12]的任何建议?谢谢
  • @JohnMichael 你一直在说链接,但这对我来说不是很清楚。其中一个模块必须创造价值。如果子模块接收到它,则进行输入并连接端口。如果子模块创建它,则将值从端口中驱动出来并连接它。您在答案的最后一段中提到了未连接的端口。
  • @JohnMichael 没有自动链接,任何未实例化的模块都将成为顶级模块。您的 tb 应该真正实例化模块 smachine 并且您正确连接端口。
  • 有没有办法实例化这两个模块?
  • 我为我的无知道歉.. 我只是 Verilog 编码的新手,所以我试图一一理解它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-08
  • 1970-01-01
  • 2018-08-25
  • 1970-01-01
相关资源
最近更新 更多