1

I am running this code

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

With this testbench:

`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

I get y4=X for each and every iteration. As far as I understand, this happens, because no initial value has been given to y4, and the default is X. However, I cannot change the type of y4 to reg in order to assign a value to it. That would result in an error message saying in the test bench, line 15, neuron n4:

tb_neuron_xor.v:15: error: reg y4; cannot be driven by primitives or continuous assignment. tb_neuron_xor.v:15: error: Output port expression must support continuous assignment. tb_neuron_xor.v:15: : Port y0 of neuron is connected to y4

How could I work my way around this?

In case a better context is needed, here is how neuron module is defined:

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 Answers1

1

Why not assign default value to y0 of neuron() ?

output reg signed [4:0] y0=0;

I would also suggest that you use a reset on your regs, and reset them at the beginning of the simulation.

Alper Kucukkomurler
  • 1,706
  • 2
  • 13
  • 19