Showing results for 
Show  only  | Search instead for 
Did you mean: 
Registered: ‎08-07-2017

Functions in VHDL

Hi everybody!
I'm studying functions in VHDL. I've read from some VHDL and FPGA books that parameters in a function can be CONSTANTs or SIGNALs and default is CONSTANT. I've defined this simple maximum function which accepts two CONSTANTs and returns maximum of them:
  -- ================================================================
function max ( CONSTANT a , b : in std_logic_vector) return std_logic_vector is
    variable a_us : unsigned(a'high downto a'low);
    variable b_us : unsigned(b'high downto b'low);
        a_us := unsigned(a);
        b_us := unsigned(b);
        if (a_us>b_us) then
            return a;
            return b;
        end if;
end function max;
 -- ================================================================
But when I input two SIGNALs as arguments to max function, ISE 
does not issue any error or warning and the answer (returned value) is also true. 
  -- ================================================================
a , b         : in         std_logic_vector(7 downto 0);
max_val   : out       std_logic_vector(7 downto 0);
max_val <= max(a , b);
  -- ================================================================
So I want to know 
1) Isn't ISE supposed to issue an error because of this situation?
2) If any function can accept SIGNALs when It's parameters implicitly or explicitly defined as CONSTANTs, So what's the point of  defining parameters as SIGNALs or CONSTANTs?
3) When I defined inputs of function as SIGNALs, the funnction does not accept CONSTANTs.
So why we can input a SIGNAL as an argument of function which accept CONSTANT, But can not
input a CONSTANT parameter to a function which accepts SIGNAL?
Tags (4)
0 Kudos
1 Reply
Registered: ‎09-07-2011

1) It's not an error to pass signals (or variables)  to constant parameters -  the constants simply take on the values of the signals.   


2)  Using signals, you can access signal attributes, and not just the value.     Famous example:


FUNCTION rising_edge  (SIGNAL s : std_ulogic) RETURN BOOLEAN IS
        RETURN (s'EVENT AND (To_X01(s) = '1') AND 
                            (To_X01(s'LAST_VALUE) = '0'));


3)   Not sure of exact answer.. never tried it.   I suppose the compiler could temporarily make  a signal out of a constant, but then the attributes would be unknown.  Also, signals are not created/destroyed runtime , so the signal would magically appear when the function is called... and then go away  when function finishes?  



0 Kudos