Search code examples
bit-manipulationverilogsystem-verilog

Verilog module outputs z's


I'm trying to make a parameter and module to AND two numbers together and return a result. However, I seem to be having troubles. My code is as follows:

module main;
   reg signed [3:0] x;
   reg signed [3:0] y;
   wire [3:0] out1;
   ander #(.L1(4)) andd(.in1(x), .in2(y), .out(out1));

   initial begin
       x = 4'b0111;
       y = 4'b1110;
       $display("%b", out1);
       $finish;
   end
endmodule
    
module ander        #(parameter L1 = 8)
                        
                     (input [L1-1:0] in1,
                        input [L1-1:0] in2,
                        output wire [L1-1:0] out);
                      
      assign out = in2&in1;
endmodule

When I try to run this, the output is "zzzz." Am I doing something wrong?

When I change it to

$display("%b", x&y);

it seems to display just fine.

Do you have any recommendations?


Solution

  • If you want a deterministic output, add some delay before your $display:

       initial begin
           x = 4'b0111;
           y = 4'b1110;
           #1;
           $display("%b", out1);
           $finish;
       end
    

    This prints 0110.