From: Symon on
On 7/16/2010 8:07 PM, Tim Wescott wrote:
> How do I assign an integer value to 'signed' or 'unsigned' from the IEEE
> libraries?
>
> I'm having this difficulty with my test benches. Surely there's a set of
> library functions to do it, but I can't seem to figure out what they are!!
>

http://www.synthworks.com/papers/vhdl_math_tricks_mapld_2003.pdf
From: Andy on
The information on types and conversions in Jim's paper is very good.

The recommendations for coding style (based on limitations of
synthesis tools) are a bit dated. Synth tools have come a long way in
7 years.

I recommend using integer for arithmetic if your data paths are less
than 32 bits. Mod has been well supported for quite a while (it was
then if you used decent FPGA tools), and is very handy for making sure
you don't have overflows (which do not go silently in the night in
simulation with integers). For example:

signal count: natural range 0 to max_count - 1;
....
count <= (count + 1 ) mod max_count; -- assume max_count = 2**n

will roll over automatically for both increment and decrement.

Also since count - 1 is completely legal for count = 0 (so long as you
don't try to store it back into count). Therefore, you can extract the
carry bit like so:

if count - 1 < 0 then -- carry bit set
do_something;
count <= max_count - 1; -- safe (BTW, constant arithmetic is free)
else
count <= count - 1;
end if;

This also works with "if count + 1 >= max_count then".

Integer type arithmetic also automatically promotes natural - natural
=> integer (use mod to "resize" when necessary).

You might look into the new fixed point package, which also can be
used for arithmetic w/o fractional bits. It automatically promotes
result size to accomodate largest possible result. Unfortunately, it
does not promote ufixed - ufixed => sfixed.

His recomendations for resource sharing and separation of computation
from state logic are unnecessary now; code it so you know WHAT it is
doing, and let the tool figure out HOW to do it.

Andy
From: Andy Peters on
On Jul 19, 4:23 pm, Andy <jonesa...(a)comcast.net> wrote:

> I recommend using integer for arithmetic if your data paths are less
> than 32 bits. Mod has been well supported for quite a while (it was
> then if you used decent FPGA tools), and is very handy for making sure
> you don't have overflows (which do not go silently in the night in
> simulation with integers). For example:
>
> signal count: natural range 0 to max_count - 1;
> ...
> count <= (count + 1 ) mod max_count; -- assume max_count = 2**n
>
> will roll over automatically for both increment and decrement.

With modern (and by this I mean XST 10.1.3 supports!) the modulo need
not be a power-of-two.

-a