VHDL functions inside packages

Status
Not open for further replies.

mahmoudathab

Member level 1
Joined
Apr 28, 2012
Messages
34
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,495
hi i would like to include this functions in a package and use it in any other design can u please help?


Code VHDL - [expand]
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
function to_bcd ( bin : std_logic_vector(7 downto 0) ) return std_logic_vector is
variable i : integer:=0;
variable bcd : std_logic_vector(11 downto 0) := (others => '0');
variable bint : std_logic_vector(7 downto 0) := bin;
 
begin
for i in 0 to 7 loop  -- repeating 8 times.
bcd(11 downto 1) := bcd(10 downto 0);  --shifting the bits.
bcd(0) := bint(7);
bint(7 downto 1) := bint(6 downto 0);
bint(0) :='0';
 
 
if(i < 7 and bcd(3 downto 0) > "0100") then --add 3 if BCD digit is greater than 4.
bcd(3 downto 0) := bcd(3 downto 0) + "0011";
end if;
 
if(i < 7 and bcd(7 downto 4) > "0100") then --add 3 if BCD digit is greater than 4.
bcd(7 downto 4) := bcd(7 downto 4) + "0011";
end if;
 
if(i < 7 and bcd(11 downto 8) > "0100") then  --add 3 if BCD digit is greater than 4.
bcd(11 downto 8) := bcd(11 downto 8) + "0011";
end if;
 
 
end loop;
return bcd;
end to_bcd

 
Last edited by a moderator:

What's you particularly question? I see, that you have been able to copy the popular "double dabble" code from the internet. (Reference in https://en.wikipedia.org/wiki/Double_dabble)

As far as I see, it's a syntactically correct VHDL function. I assume, it will serve it's purpose.

P.S.:
In addition, you would want to add a standard package header (I guess, you'll find a template in your favourite VHDL text book).
Code:
LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE ieee.std_logic_arith.all;
USE ieee.std_logic_unsigned.all;

package my_bcd is
function to_bcd ( bin : std_logic_vector(7 downto 0) ) return std_logic_vector;
end package;

package body my_bcd is
-- your code
end package body my_bcd;
 
Last edited:

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…