如何解码的无符号整数到BCD使用VHDL
正如我们所看到的是,在VHDL,MOD和REM唯一可以模拟,但不能synthesized.So我们怎样才能从一个无符号整数的BCD?例如,整数是23,我们如何得到BCD:0b0010和0b0011? 谢谢。如何解码的无符号整数到BCD使用VHDL
回答:
这已在别处盖:
https://electronics.stackexchange.com/questions/22611/binary-to-bcd-converison
- 查找表是一种选择
- 一个大case语句是另一种(这将让变成一个查找表)
- 或者在这里你一直减去10,直到其余的迭代方法是< 10 - 减法计数的十位数,其余的是你的单位数字。
回答:
我下面提供两个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发现原始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;
功能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
回答:
你可能有兴趣在具有看看“双玩水”的算法:
http://en.wikipedia.org/wiki/Double_dabble
基本上,你要做的就是创建为BCD表示一个寄存器,你把“左侧”的整数表示。下面是一个例子,我们在其中要数23
转换为其BCD表示:
BCD_1 BCD_0 Original 0000 0000 10111
现在你创建一个为你的初始位左移循环(你把那些位到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寄存器(使用+3
规则时,任何数字大于4)BCD表示0010 0011
(23) 。
欲了解更多详细信息,请参阅维基百科的文章。你甚至可以找到一个VHDL实现的例子。
以上是 如何解码的无符号整数到BCD使用VHDL 的全部内容, 来源链接: utcz.com/qa/264133.html