Multiple Return Values in VHDL

VHDL has built-in support for multiple return values through the use of records. This feature is used often in idiomatic VHDL, for example to return both result and status values from a function.

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

entity multiple_return_values is
end multiple_return_values;

architecture Behavioral of multiple_return_values is
    -- Define a record type to hold multiple return values
    type vals_type is record
        a : integer;
        b : integer;
    end record;

    -- Function declaration with a record return type
    function vals return vals_type;

    -- Function implementation
    function vals return vals_type is
        variable result : vals_type;
    begin
        result.a := 3;
        result.b := 7;
        return result;
    end function;

    -- Procedure to simulate print functionality
    procedure print(x : in integer) is
    begin
        report integer'image(x);
    end procedure;

begin
    process
        variable result : vals_type;
        variable c : integer;
    begin
        -- Here we use the record return value from the function call
        result := vals;
        print(result.a);
        print(result.b);

        -- If you only want a subset of the returned values,
        -- you can just access the desired field
        c := vals.b;
        print(c);

        wait;
    end process;
end Behavioral;

In this VHDL code:

  1. We define a record type vals_type to hold multiple return values.

  2. The vals function returns a vals_type record containing two integers.

  3. In the main process, we call the vals function and assign its result to a variable of type vals_type.

  4. We can then access individual fields of the record using dot notation.

  5. To demonstrate using only a subset of the returned values, we directly access the b field of the record returned by vals.

  6. The print procedure is used to simulate the printing functionality, as VHDL doesn’t have built-in print functions like other languages.

When simulated, this VHDL code will produce output similar to:

3
7
7

This example demonstrates how VHDL can handle multiple return values using records, which is a common pattern in VHDL design.