1

I want to build a simple module to compare two 2-bit numbers and get the maximum number in the output.

I've used the code posted here: How to find MAX or MIN in Verilog coding?

module max (input [1:0] a, 
            input [1:0] b, 
            output reg [1:0] out);
  always @* begin
    if (a>b)
      out = a;
    else
      out = b;
  end
endmodule

But the problem I have, is that the output "out" does not give me a 2-bit number, and also the code is not working well as you can see in the screenshot.

This is the testbench I'm using:

`timescale 1ns/10ps
module maxTB();
reg [1:0] a, b;

max dut (.a(a),.b(b),.out(out)); 

initial
begin
a = 2'b1; b= 2'b0;
#20 a = 2'b10;
#40 b = 2'b11;
#50 a = 2'b01;
end
endmodule

modelsim screenshot

1 Answer 1

4

You haven't declared out in your TB, so it has defaulted to a 1-bit net (a wire). This is a rather major failing in the language. To turn this behaviour off, add this outside any module:

`default_nettype none
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.