Multiple Return Values in Verilog

Verilog does not have built-in support for multiple return values like some high-level programming languages. However, we can simulate this behavior using output ports in a module. Here’s an example of how we might implement a similar concept in Verilog:

module multiple_return_values;

  // This function-like module returns 2 integers
  module vals(output [31:0] a, output [31:0] b);
    assign a = 32'd3;
    assign b = 32'd7;
  endmodule

  reg [31:0] a, b, c;

  initial begin
    // Here we use the 2 different return values from the
    // module instantiation
    vals val_instance(a, b);
    $display("%0d", a);
    $display("%0d", b);

    // If you only want a subset of the returned values,
    // you can leave one of the ports unconnected
    vals val_instance2(, c);
    $display("%0d", c);
  end

endmodule

In this Verilog code:

  1. We define a module vals that takes two 32-bit output ports a and b. This simulates a function that returns two values.

  2. Inside the vals module, we assign the values 3 and 7 to a and b respectively.

  3. In the main module multiple_return_values, we declare three 32-bit registers a, b, and c.

  4. We instantiate the vals module twice:

    • First, we connect both outputs to a and b.
    • Then, we connect only the second output to c, leaving the first output unconnected. This simulates ignoring one of the return values.
  5. We use $display to print the values, which is Verilog’s equivalent of printing to the console.

To run this Verilog code, you would typically use a Verilog simulator. The output would look something like this:

3
7
7

In Verilog, we don’t have the concept of “blank identifiers” like in some high-level languages. Instead, we simply leave a port unconnected if we don’t need its value.

This example demonstrates how to simulate multiple return values in Verilog using module output ports. While it’s not a direct translation of the original code, it captures the essence of the concept in a way that’s idiomatic to Verilog.