Forum Discussion
Altera_Forum
Honored Contributor
15 years ago --- Quote Start --- I would never recommend recursion in VHDL to a beginner. A recent example (http://alteraforum.com/forum/showthread.php?t=27911&highlight=recursion) showed someone trying to use recursion like they were writing software, and they caused a stack overflow in Quartus during synthesis. I agree it can be used when you know what's coming, but uncontrolled and misunderstood uses of it can lead to problems. --- Quote End --- You should give beginners some credit too, shouldn't you? I agree there are some pitfalls, but it is often very useful and elegant. Plus having a function makes it very little work adding (or deleting) inputs, (re-)writing the binary tree by hand can be tedious if you add or delete a member. --- Quote Start --- ..., then a binary tree would be the most effective way to achieve it --- Quote End --- That is exactly what the recursive function achieves. --- Quote Start --- A reasonable viewpoint. The suggested recursion is actually ending up in a binary tree. For a limited number of inputs (e.g. <= 8) the code text will be shorter when writing it enrolled. For a higer number of inputs, you possibly want a pipelined action, which unfortunately can't be provided by the recursive construct. --- Quote End --- Yes for a small number of inputs initially it may quicker to write the code flat-out (unrolled), but if you later have to add an input or two you end up retyping the lot. A for-loop will be easier to write but ends up in a stair-case logic, which is a lot slower. The idea in VHDL should be to look for re-usability. Together with generics, functions help a lot. You are quite right that a recursive function cannot be pipelined (which is a real pity).