如何连接模块和传递值

How to wire up modules and pass value

module container(x1, x2, x3, NUMBER);

input x1, x2, x3;
output NUMBER;
wire w0, w1;

dec_counter U1 (x1, x2, x3, w0, w1);
doz_counter U2 (w1, w0, NUMBER);


endmodule


module dec_counter(clk, reset, clk_enable, counter, terminal);

    input clk;
    input reset;
    input clk_enable;
    output reg [3:0] counter;
    output reg terminal;



always @(posedge clk, posedge clk_enable, posedge reset)
        if(reset)
        begin
            terminal <= 1;
            counter <= 0;
        end
        else if(clk && clk_enable)
            if(counter < 9)
            begin
                terminal <= 1;
                counter <= counter + 1; 
            end           
            else
            begin
                terminal <= 1;
                counter <= 0;
            end  
endmodule



module doz_counter(dozens, unity, number);

input dozens;
input unity;
output reg [7:0] number;

initial begin
    number = 8'd0;
end    

always @(posedge dozens)
    if(dozens)
        number <= number + 1;

endmodule

您好!我是 verilog 的新手,我遇到了第一个问题。我有模块 dec_counter,它从 0 计数到 9。当它达到 9+1 时,它显示 0 并将 "output terminal" 设置为 1。现在我想将该值传递给我的下一个模块 doz_counter 为 "input dozens"。我试过接线,正如您在模块容器中看到的那样,但在我的模拟中,即使终端为 1,数十也始终为 X。

我觉得我犯了一些严重的设计错误。

您发布的代码工作正常端口连接规则参考下图。输出端口可以是 regwire 中的任何一个,但输入端口始终是 wire.

几个错误列举如下:

您已将 4 位端口dec_counter 模块中的 reg [3:0] counter; 连接到 单位端口 , w0container 模块中。这将导致端口连接宽度不匹配。

wire [3:0] w0;
wire w1;
// ...

类似地,单位端口 NUMBER in container 模块连接到8位端口 numberdoz_counter 模块中。这将导致端口连接宽度不匹配。

output [7:0] NUMBER;
//...

此外,重置后 terminal 的值可能为零if-else 条件驱动相同的值 terminalterminal 不同值 分别为 terminal <= 1terminal <= 0;

这是您的代码的测试平台,供参考:

module top();

bit x1, x2, x3;
bit [7:0] NUMBER;

container c(x1, x2, x3, NUMBER);

always #5 x1 = ~x1;

initial
begin
#6 x2 = 1;
#6 x2 = 0; x3 = 1;
#100 $finish;
end

endmodule