Anonymous Types on Interfaces

Proposal Editing Information

  • Who Updates: JimLewis, <Add YourName >, ...
  • Date Proposed: 2012-08-18
  • Date Last Updated: 2012-08-18
  • Priority:
  • Complexity:
  • Focus: Testbench

Requirement Summary

Currently interfaces (subprograms and entities) allow unconstrained arrays on interfaces. Anonymous types generalize this capability for interfaces where knowing the type is not important.

Related and/or Competing Issues: None

Current Status

With a formal generic type on the interface of a subprogram, we can do the following:

function Mux4Gen
generic (type AType) ;
parameter (
    Sel : std_logic_vector (1 downto 0) ; 
    A, B, C, D : AType 
) return AType is
    variable X : A'Subtype ; -- value = AType'left
begin
    case Sel is 
        when "00" =>     X := A ;
        when "01" =>     X := B ;
        when "10" =>     X := C ;
        when "11" =>     X := D ;
        when others =>  null ; 
    end case ; 
    return X ; 
end function Mux4Gen ; 

To use the subprogram, we first must create a subprogram instance, and then we can call it:

architecture a of e is
    function Mux4 is new Mux4Gen generic map (AType => std_logic_vector) ; 
    signal S : std_logic_vector(1 downto 0) ; 
    signal Y, A1, A2, A3, A4 : std_logic_vector(7 downto 0) ; 
 
begin
    Y <= Mux4(S, A1, A2, A3, A4) ; 
....

Use Model 1: Mux4

The idea behind anonymous types is to simplify this process.

function Mux4
parameter (
    Sel : std_logic_vector (1 downto 0) ; 
    A, B, C, D  : anonymous   -- restriction all must be same subtype
) return A'Subtype is
    variable X : A'Subtype ; 
begin
    case Sel is 
        when "00" =>     X := A ;
        when "01" =>     X := B ;
        when "10" =>     X := C ;
        when "11" =>     X := D ;
        when others =>  null ; -- result := A'SubType'initial ;
    end case ; 
    return X ; 
end function Mux4 ; 

The subtype is determined when the subprogram is called (elaborated):

architecture a of e is
    signal S : std_logic_vector(1 downto 0) ; 
    signal Y, A1, A2, A3, A4 : std_logic_vector(7 downto 0) ; 
 
begin
    Y <= Mux4(S, A1, A2, A3, A4) ; 
....

An alternative to declaring anonymous parameters together - A, B, C, D above - is to use 'Subtype in a interface list:

function Mux4
parameter (
    Sel : std_logic_vector (1 downto 0) ; 
    A : anonymous ;  
    B, C, D : A'Subtype  -- making B,C,D same subtype as A 
) return A'Subtype is
    variable X : A'Subtype ; 
begin

Use Model 2: Condition

A condition operation for all types whose first parameter is boolean could be defined as follows:

function condition ( 
    cond : boolean ; 
    A : anonymous ; 
    B : A'Subtype 
) return A'Subtype is 
begin 
    -- return A when Cond else B ; 
    if cond then 
        return A ; 
    else 
        return B ; 
    end if ; 
end function condition ;  

--Usage in a declaration: 
constant a : integer := condition (FAST_SIM, 100, 10000) ;

Questions

Does the type need to be visible to the package that defines the subprogram? For anonymous types to be useful, the type would not necessarily be known to the package, but instead is only known at the point of the call. Alternately stated, for this to be a library package, the package would not necessarily be able to see new types defined by the project.

- How does one perform type checking? Losing type checking on function calls would not seem to be the way that VHDL does things.

- If this proposal is accepted, then would there be any need to define data types for any of the parameters?

- Function overloading might be another casualty. "A, B, C, D : anonymous" just says that there are four parameters and each can be any type whatsoever, but one might want different behavior based on the parameters having different types. "A : anonymous ; B, C, D : A'Subtype" (currently not legal, but addressed by proposal OrderingAllInterfaceLists at least constrains the four parameters to be the same type. This might be safe enough to allow since it is probably not likely that current function overloads are performing anything any differently, just using a different data types.

-- KevinJennings - 2016-10-12

Proposal

General Comments

This is useful for RTL, not just for testbenches. -- JonnyDoin - 2013-01-03

This appears to the address same issue as Map Subprogram Generics on Call. -- ErnstChristen - 2015-01-27

Supporters

-- MartinThompson - 2012-09-14

-- Brent Hayhoe - 2012-10-09

-- JonnyDoin - 2013-01-03

-- KevinJennings - 2013-04-04

-- DanielKho - 2013-09-10

-- MortenZilmer - 2014-01-27

_Add your signature here to indicate your support for the proposal_</verbatim>

Topic revision: r13 - 2020-02-17 - 15:34:25 - JimLewis
 
Copyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback