Binary to BCD code understanding

Joined
Dec 27, 2021
Messages
1
Reaction score
0
Hi,
I am a newbie in VHDL coding and I needed help in understanding a binary to bcd conversion code.

Regards,
Divya

Code:
library IEEE;
use IEEE.STD_LOGIC_1164.all;
--use IEEE.NUMERIC_STD.ALL;
use ieee.std_logic_unsigned.all;

package bin2bcd is


function to_bcd ( bin : std_logic_vector(39 downto 0) ) return std_logic_vector;
    
end bin2bcd;

-- package body declaration---

package body bin2bcd is


function to_bcd ( bin : std_logic_vector(39 downto 0) ) return std_logic_vector is
variable i : integer:=0;
variable bcd : std_logic_vector(51 downto 0) := (others => '0');
variable bint : std_logic_vector(39 downto 0) := bin;

begin
for i in 0 to 39 loop  -- repeating 40 times.
bcd(51 downto 1) := bcd(50 downto 0);  --shifting the bits.
bcd(0) := bint(39);
bint(39 downto 1) := bint(38 downto 0);
bint(0) :='0';


if(i < 39 and bcd(3 downto 0) > "0100") then --add 3 if BCD digit is greater than 4.        1
bcd(3 downto 0) := bcd(3 downto 0) + "0011";
end if;

if(i < 39 and bcd(7 downto 4) > "0100") then --add 3 if BCD digit is greater than 4.        2
bcd(7 downto 4) := bcd(7 downto 4) + "0011";
end if;

if(i < 39 and bcd(11 downto 8) > "0100") then  --add 3 if BCD digit is greater than 4.      3
bcd(11 downto 8) := bcd(11 downto 8) + "0011";
end if;

if(i < 39 and bcd(15 downto 12) > "0100") then  --add 3 if BCD digit is greater than 4.      4
bcd(15 downto 12) := bcd(15 downto 12) + "0011";
end if;

if(i < 39 and bcd(19 downto 16) > "0100") then  --add 3 if BCD digit is greater than 4.       5
bcd(19 downto 16) := bcd(19 downto 16) + "0011";
end if;

if(i < 39 and bcd(23 downto 20) > "0100") then  --add 3 if BCD digit is greater than 4.       6
bcd(23 downto 20) := bcd(23 downto 20) + "0011";
end if;

if(i < 39 and bcd(27 downto 24) > "0100") then  --add 3 if BCD digit is greater than 4.       7
bcd(27 downto 24) := bcd(27 downto 24) + "0011";
end if;

if(i < 39 and bcd(31 downto 28) > "0100") then  --add 3 if BCD digit is greater than 4.       8
bcd(31 downto 28) := bcd(31 downto 28) + "0011";
end if;

if(i < 39 and bcd(35 downto 32) > "0100") then  --add 3 if BCD digit is greater than 4.       9
bcd(35 downto 32) := bcd(35 downto 32) + "0011";
end if;

if(i < 39 and bcd(39 downto 36) > "0100") then  --add 3 if BCD digit is greater than 4.      10
bcd(39 downto 36) := bcd(39 downto 36) + "0011";
end if;

if(i < 39 and bcd(43 downto 40) > "0100") then  --add 3 if BCD digit is greater than 4.       11
bcd(43 downto 40) := bcd(43 downto 40) + "0011";
end if;

if(i < 39 and bcd(47 downto 44) > "0100") then  --add 3 if BCD digit is greater than 4.       12
bcd(47 downto 44) := bcd(47 downto 44) + "0011";
end if;

if(i < 39 and bcd(51 downto 48) > "0100") then  --add 3 if BCD digit is greater than 4.       13
bcd(51 downto 48) := bcd(51 downto 48) + "0011";
end if;


end loop;
return bcd;
end to_bcd;

 
end bin2bcd;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,056
Messages
2,570,443
Members
47,089
Latest member
Bobby2025b

Latest Threads

Top