如何将无符号整数解码为BCD使用VHDL

时间:2012-10-18 09:47:27

标签: integer vhdl bcd

我们可以看到,在VHDL中,只能模拟MOD和REM,但无法合成。那么我们如何才能从无符号整数中得到BCD?例如,整数是23,我们怎么能得到BCD:0b0010和0b0011?    感谢。

3 个答案:

答案 0 :(得分:1)

这在其他地方有所涉及:

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

  • 查找表是一个选项
  • 一个大案例陈述是另一个(将变成一个查找表)
  • 或者一种迭代方法,其中你保持减去10,直到余数为< 10 - 减法计数是你的十位数,余数是你的单位数。

答案 1 :(得分:1)

我在下面提供两个VHDL函数。这是从二进制转换为打包BCD,反之亦然。我已经在Xilinx Spartan 3AN系列上验证了这些,它们可以合成。使用 ieee.numeric_std.all; ieee.std_logic_1164.all;

功能1:二进制到BCD --source:http://vhdlguru.blogspot.com.es/2010/04/8-bit-binary-to-bcd-converter-double.html(SO用户Peque找到原始网址)

    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;

功能2:BCD到二进制

    --(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;

注意:您可以使用以下链接convert integer to std_logic

上的信息将整数转换为无符号

答案 2 :(得分:0)

你可能有兴趣看一下&#34; double dabble&#34;算法:

http://en.wikipedia.org/wiki/Double_dabble

基本上,你要做的是为你放置的BCD表示创建一个寄存器&#34;在左边&#34;整数表示。下面是一个示例,我们希望将数字23转换为其BCD表示形式:

BCD_1  BCD_0      Original
0000   0000       10111

现在创建一个for循环,在其中将Original位移到左侧(将这些位移入BCD寄存器)。在此循环中,您必须检查每个BCD_X数字是否大于4;在这种情况下,你将3添加到该数字:

shift_iteration   BCD_1  BCD_0      Original
0                 0000   0000       10111
1                 0000   0001       01110       (no digit greater than 4)
2                 0000   0010       11100       (no digit greater than 4)
3                 0000   0101       11000       (5 in BCD_0! we add 3...)
still 3...        0000   1000       11000       (after addition, shift again)
4                 0001   0001       10000       (no digit greater than 4)
5                 0010   0011       00000       (no digit greater than 4)

将所有原始位推送到BCD寄存器后(当任何数字大于4时使用+3规则),BCD代表0010 0011(23)。

有关详细信息,请参阅Wikipedia文章。您甚至可以找到VHDL实现的示例。