Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
12 years ago

Can I use a function in a concurrent code?

Is it possible to use a function in a concurrent code or I only can use it in a PROCESS scope?

4 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Why didn't you just try using it and let the compiler decide?

    You can use a function pretty much anywhere, eg., I use them for setting (calculating) generics like address widths based on the number of registers, etc.,and that code occurs right at the beginning of the architecture.

    If you're having trouble using functions somewhere in your VHDL, post example code. If you're violating some feature of VHDL, I'll look in the standard and see what it says about your case.

    Cheers,

    Dave
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    Why didn't you just try using it and let the compiler decide?

    You can use a function pretty much anywhere, eg., I use them for setting (calculating) generics like address widths based on the number of registers, etc.,and that code occurs right at the beginning of the architecture.

    If you're having trouble using functions somewhere in your VHDL, post example code. If you're violating some feature of VHDL, I'll look in the standard and see what it says about your case.

    Cheers,

    Dave

    --- Quote End ---

    It was only a conceptual doubt.

    I did read in a book the following concept:

    "function and procedure (called subprograms) are very similar to process, in the sense that these three are the only sections of vhdl code that are interpreted sequentially (like regular computer programs)".

    And I understood that it was only allowed to use functions inside sequential code.

    I already did use functions in concurrent code but I was worried about using a "bad technique". Finite State Machines, for instance, can be written in VHDL, compiled correctly, but they can be developed without a good sequential/combinational segmentation of the code, resulting in a bad technique.

    Now it is clear.

    Thank you
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    To see an example of "function use on steriods" take a look at the code associated with this tutorial

    http://www.ovro.caltech.edu/~dwh/correlator/pdf/lfsr_tutorial.pdf

    http://www.ovro.caltech.edu/~dwh/correlator/pdf/lfsr_tutorial_src.zip

    and look at

    lfsr_tutorial_src\prbs\src\prbs.vhd

    The code uses functions to statically initialize what is effectively a matrix operation. The "static" initialization means that the code changes based on generics, but does not change once those generics have defined values.

    Cheers,

    Dave
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The logic generated depends on the function, and where it is called. A function cannot have pipelining of itself, but can be called inside a clocked process to place a register on the return value of the function.

    So, as with any code, too many operations between registers leads to poorer timing performance. A function is usually just a way of wrapping up code so that can be easily reused, or tidy up some messy type conversions that would actually be free in logic.

    The third case is as Dave mentions - creating compile time constants/generics. So for fairly straight forward operations, like generating a sin table for a ROM, you can use a function instead of manually creating a large array. Or you could use generics in some complicated formula to create other generics. Remember that constants are created at compile time and hence require 0 logic.