Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > VHDL > coding style for arithmetic operations

Reply
Thread Tools

coding style for arithmetic operations

 
 
Frank Buss
Guest
Posts: n/a
 
      08-22-2009
I have a generic entity with a signals like this:

writeAddress: out unsigned(ADDRESS_WIDTH-1 downto 0);
dstStart: in unsigned(ADDRESS_WIDTH-1 downto 0);
dstPitch: in unsigned(15 downto 0);

Then I have a procedure:

procedure setPixel(x: unsigned(15 downto 0); y: unsigned(15 downto 0)) is
begin
writeAddress <= dstStart + dstPitch * y + x;
writeEnable <= '1';
data <= color;
end;

Xilinx ISE compiles it, but generates a warning:

Width mismatch. <writeAddress> has a width of 14 bits but assigned
expression is 32-bit wide

IIRC in Quartus this would be an error. So to avoid the warning, I have
written this function:

function adjustLength(value: unsigned; length: natural) return unsigned is
variable result: unsigned(length-1 downto 0);
begin
if value'length > length then
result := value(length-1 downto 0);
else
result := to_unsigned(0, length - value'length) & value;
end if;
return result;
end;

Now I can write it like this:

writeAddress <= adjustLength(
dstStart + dstPitch * y + x,
writeAddress'length);

and there is no warning anymore, but I wonder if this is good VHDL coding
style, or if there is already library function which does this. I want to
avoid using integer variables with ranges, because then I don't need too
many conversions for other operations for the block RAM interface and
parameter passing from external devices, which are all unsigned vectors.

I'm using this use-statements:

use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

--
Frank Buss,
http://www.frank-buss.de, http://www.it4-systems.de
 
Reply With Quote
 
 
 
 
Frank Buss
Guest
Posts: n/a
 
      08-22-2009
Frank Buss wrote:

> function adjustLength(value: unsigned; length: natural) return unsigned is
> variable result: unsigned(length-1 downto 0);
> begin
> if value'length > length then
> result := value(length-1 downto 0);
> else
> result := to_unsigned(0, length - value'length) & value;
> end if;
> return result;
> end;


There was a small bug in the function if the length was already at the
right size, this is the corrected version:

function adjustLength(value: unsigned; length: natural) return unsigned is
variable result: unsigned(length-1 downto 0);
begin
if value'length >= length then
result := value(length-1 downto 0);
else
result := to_unsigned(0, length - value'length) & value;
end if;
return result;
end;

This is very useful for other problems, too, like converting hex input from
RS232, entered in some terminal program, and sending it to an SPI port:

if rs232DataIn >= x"30" and rs232DataIn <= x"39" then
spiNibble <= adjustLength(rs232DataIn - x"30", 4);
startSpiTransfer;
elsif rs232DataIn >= x"61" and rs232DataIn <= x"66" then
spiNibble <= adjustLength(rs232DataIn - x"57", 4);
startSpiTransfer;
end if;

--
Frank Buss,
http://www.frank-buss.de, http://www.it4-systems.de
 
Reply With Quote
 
 
 
 
Andy
Guest
Posts: n/a
 
      08-23-2009
Just use numeric_std.resize; that's what it was designed for.

Andy
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VHDL Packages, Coding Styles for Arithmetic Operations and VHDL-200xAdditions Amal VHDL 1 03-04-2010 04:09 AM
Automatically generating arithmetic operations for a subclass Steven D'Aprano Python 12 04-18-2009 08:33 PM
Successive arithmetic operations within a process dennismccarty VHDL 1 03-15-2009 08:20 AM
general coding issues - coding style... calmar Python 11 02-21-2006 10:36 AM
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57