Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode an unsigned integer into BCD use VHDL

Tags:

integer

vhdl

bcd

As we can see that,in VHDL ,MOD and REM only can be simulated but can't be synthesized.So how can we get the BCD from an unsigned integer? For example,the integer is 23,how can we get the BCD:0b0010 and 0b0011? Thanks.

like image 714
user1673133 Avatar asked Dec 07 '25 02:12

user1673133


2 Answers

This has been covered elsewhere:

https://electronics.stackexchange.com/questions/22611/binary-to-bcd-converison

  • A lookup table is one option
  • A big case statement is another (Which will get turned into a lookup table)
  • Or an iterative method where you keep subtracting 10 until the remainder is < 10 - the count of subtractions is your tens digit, the remainder is your units digit.
like image 122
Martin Thompson Avatar answered Dec 08 '25 17:12

Martin Thompson


I am providing two VHDL functions below. This is to convert from binary to packed BCD and vice-versa. I have verified these on Xilinx Spartan 3AN Family and they can be synthesized. Use ieee.numeric_std.all; and ieee.std_logic_1164.all; libraries

Function 1: Binary to BCD --source:http://vhdlguru.blogspot.com.es/2010/04/8-bit-binary-to-bcd-converter-double.html (SO user Peque found the original url)

    function to_bcd ( bin : unsigned(7 downto 0) ) return unsigned is
        variable i : integer:=0;
        variable bcd : unsigned(11 downto 0) := (others => '0');
        variable bint : unsigned(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;

Function 2: BCD to Binary

    --(c)2012 Enthusiasticgeek for Stack Overflow. 
    --Use at your own risk (includes commercial usage). 
    --These functions are released in the public domain and 
    --free to use as long as this copyright notice is retained.

    --multiplication by 10 is achieved using shift operator   X<<3 + X<<1
    --input should be packed BCD.
    function to_binary ( bcd : unsigned(11 downto 0) ) return unsigned is
        variable i : integer:=0;
        variable binary : unsigned(7 downto 0) := (others => '0');  
        variable temp : unsigned(6 downto 0) := (others => '0');
        variable bcdt : unsigned(11 downto 0) := bcd;   
        variable tens : unsigned(7 downto 0) := (others => '0');
        variable hundreds_stepI : unsigned(7 downto 0) := (others => '0');
        variable hundreds_stepII : unsigned(7 downto 0) := (others => '0');

       begin

         for i in 0 to 11 loop  -- repeating 12 times.

         if(i >=0 and i<4) then
            binary := ((temp&bcdt(i) ) sll i ) + binary;
         end if;         

         if(i >=4 and i<8) then         
            tens := (((temp&bcdt(i) ) sll (i-4) ) sll 3) + (((temp&bcdt(i) ) sll (i-4) ) sll 1); --multiply by 10           
            binary := tens + binary;
         end if;         

         if(i >=8 and i<12) then         
            hundreds_stepI := (((temp&bcdt(i) ) sll (i-8) ) sll 3) + (((temp&bcdt(i) ) sll (i-8) ) sll 1); --multiply by 10
            hundreds_stepII := (hundreds_stepI sll 3) + (hundreds_StepI sll 1); -- multiply by 10 again so the effect is now multiply by 100                
            binary := hundreds_stepII + binary;
         end if;

         end loop;     

       return binary;
    end to_binary;

Note: You can convert your integer to unsigned using the info on the following link convert integer to std_logic

like image 45
enthusiasticgeek Avatar answered Dec 08 '25 15:12

enthusiasticgeek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!