Verilog:原语或连续分配

时间:2017-11-16 05:48:53

标签: verilog iverilog

我正在运行此代码

module neuron_xor(x0, x1, y4);
    input signed [4:0] x0, x1;
    reg signed [4:0] w02, w03, w12, w13, w24, w34;
    reg signed [4:0] th2, th3, th4;
    wire signed [4:0] y2, y3;
    output signed [4:0] y4;

    neuron n2(x0, x1, w02, w12, th2, y2);
    neuron n3(x0, x1, w03, w13, th3, y3);

    neuron n4(y2, y3, w24, w34, th4, y4);

    initial begin
    w02 = 2; w03 = -2;
    w12 = 2; w13 = -2;
    w24 = 2; w34 = 2;

    th2 = 1; th3 = -3; th4 = 3;
    end
endmodule

使用此测试平台:

`timescale 1ns/100ps

module tb_neuron_xor();

    reg signed [4:0] x0, x1;
    reg signed [4:0] w02, w03, w12, w13, w24, w34;
    reg signed [4:0] th2, th3, th4;
    wire signed [4:0] y2, y3;
    wire signed [4:0] y4;
    reg signed [4:0] ctrl;
    integer i, j, flag;

    neuron n2(x0, x1, w02, w12, th2, y2);
    neuron n3(x0, x1, w03, w13, th3, y3);
    neuron n4(y2, y3, w24, w34, th4, y4);

    initial begin

        $dumpfile("tb_neu_xor.vcd");
        $dumpvars;

        flag = 0;

        #10;
        for (i = -16; i < 15; i = i + 1) begin
            for (j = -16; j < 15; j = j + 1) begin

                x0 = i; x1 = j;
                ctrl = x0 ^ x1;

                if (ctrl != y4) begin
                    flag = 1;
                    $display("Error: x0=%b, x1=%b, y4=%b, ctrl=%b", x0,x1,y4,ctrl);
                end
                $display("y4=%d, ctrl=%d", y4, ctrl);
            end
        end

        if (flag == 0) begin
            $display("No Error!");
        end

        #10;
        $finish;

    end
endmodule

每次迭代都得到y4 = X.据我所知,这种情况发生了,因为没有给y4初始值,默认值是X.但是,我不能将y4的类型更改为reg以便为其赋值。这将导致在测试台,第15行,神经元n4:

中出现错误消息
  

tb_neuron_xor.v:15:错误:reg y4;不能由原语或连续分配驱动。   tb_neuron_xor.v:15:错误:输出端口表达式必须支持连续分配。   tb_neuron_xor.v:15::神经元的端口y0连接到y4

我怎样才能解决这个问题呢?

如果需要更好的上下文,下面是神经元模块的定义方式:

module neuron(x0, x1, w0, w1, th0, y0);
    input signed [4:0] x0, x1; 
    input signed [4:0] w0, w1;
    input signed [4:0] th0;
    output reg signed [4:0] y0;

    reg signed [11:0] a0, a1, a2;

    always @(x0 or x1 or w0 or w1 or th0)
    begin
        a0 = x0*w0;
        a1 = x1*w1;
        a2 = a0 + a1;

        if (a2 >= th0)
            y0 = 1;
        else
            y0 = 0;
    end

endmodule

1 个答案:

答案 0 :(得分:1)

为什么不将默认值分配给y0的{​​{1}}?

neuron()

我还建议您在注册表中使用重置,并在模拟开始时重置它们。