用vhdl语言把八位二进制转换为十进制,怎么输出的是十六进制啊
LIBRARYIEEE;USEIEEE.STD_LOGIC_1164.ALL;USEIEEE.STD_LOGIC_ARITH.all;USEIEEE.STD_LOGIC_...
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_ARITH.all;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY CH17 IS
PORT(OP:IN STD_LOGIC_VECTOR(7 downto 0);
result:out integer range 0 to 255);
end ch17;
architecture maxpld of ch17 is
begin
process(op)
variable tmp:integer:=0;
begin
for i in 7 downto 0 loop
tmp:=tmp*2;
if(op(i)='1')then
tmp:=tmp+1;
end if;
end loop;
result<= tmp;
end process;
end maxpld; 展开
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_ARITH.all;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY CH17 IS
PORT(OP:IN STD_LOGIC_VECTOR(7 downto 0);
result:out integer range 0 to 255);
end ch17;
architecture maxpld of ch17 is
begin
process(op)
variable tmp:integer:=0;
begin
for i in 7 downto 0 loop
tmp:=tmp*2;
if(op(i)='1')then
tmp:=tmp+1;
end if;
end loop;
result<= tmp;
end process;
end maxpld; 展开
2个回答
展开全部
首先我们要明确我们要干什么。我猜你是想把二进制码转成十进制的BCD码。如果你是这么想的话,那你低估这个问题的复杂程度了。你的程序我仿真的时候有点问题(可能是我的ISE仿真程序对端口表中的interger仿真不支持的缘故),所以我改动了一下。在下面这个程序的仿真输出中,你可以看到自己的程序其实等同于把输入和输出短接,其实没有意义。
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_ARITH.all;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY CH17 IS
PORT(OP:IN STD_LOGIC_VECTOR(7 downto 0);
result:out STD_LOGIC_VECTOR(7 downto 0)
);
end ch17;
architecture maxpld of ch17 is
signal tb:integer range 0 to 255;
begin
process(op)
variable tmp:integer:=0;
begin
for i in 7 downto 0 loop
tmp:=tmp*2;
if(op(i)='1')then
tmp:=tmp+1;
end if;
end loop;
tb<= tmp;
end process;
result<=CONV_STD_LOGIC_VECTOR(tb,8);
end maxpld;
做这个有很多方法。下面我用查表法来做。总体是把源数据分成高四位和低四位。然后对高四位利用查表法进行转换(只做了一百以内的),然后和低四位进行加法,此时进行所谓的十六进制加法的十进制调整。
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_ARITH.all;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY CH17 IS
PORT(
OP:IN STD_LOGIC_VECTOR(7 downto 0);
result:OUT STD_LOGIC_VECTOR(7 downto 0)
);
end ch17;
architecture maxpld of ch17 is
signal tb:integer range 0 to 255;
begin
process(op)
variable tmp:integer:=0;
variable tmp1:integer:=0;--高四位
variable tmp2:integer:=0;--低四位
variable part:integer:=0;
variable carry:integer:=0;--进位
variable sum:integer:=0;
begin
tmp:=CONV_INTEGER(op);
tmp1:=tmp/16;
tmp2:=tmp rem 16;
if(tmp1=0)then
part:=0;
elsif(tmp1=1)then
part:=22;--16
elsif(tmp1=2)then
part:=50;--32
elsif(tmp1=3)then
part:=72;--48
elsif(tmp1=4)then
part:=100;--64
elsif(tmp1=5)then
part:=128;--80
else
part:=150;--96
end if;
sum:=(part rem 16)+tmp2;
carry:=0;
if(sum>19)then
sum:=sum+12;
carry:=2;
elsif(sum>9)then
sum:=sum+6;
carry:=1;
end if;
sum:=(sum rem 16)+(carry+part/16)*16;
tb<= sum;
end process;
result<=CONV_STD_LOGIC_VECTOR(tb,8);
end maxpld;
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_ARITH.all;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY CH17 IS
PORT(OP:IN STD_LOGIC_VECTOR(7 downto 0);
result:out STD_LOGIC_VECTOR(7 downto 0)
);
end ch17;
architecture maxpld of ch17 is
signal tb:integer range 0 to 255;
begin
process(op)
variable tmp:integer:=0;
begin
for i in 7 downto 0 loop
tmp:=tmp*2;
if(op(i)='1')then
tmp:=tmp+1;
end if;
end loop;
tb<= tmp;
end process;
result<=CONV_STD_LOGIC_VECTOR(tb,8);
end maxpld;
做这个有很多方法。下面我用查表法来做。总体是把源数据分成高四位和低四位。然后对高四位利用查表法进行转换(只做了一百以内的),然后和低四位进行加法,此时进行所谓的十六进制加法的十进制调整。
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_ARITH.all;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY CH17 IS
PORT(
OP:IN STD_LOGIC_VECTOR(7 downto 0);
result:OUT STD_LOGIC_VECTOR(7 downto 0)
);
end ch17;
architecture maxpld of ch17 is
signal tb:integer range 0 to 255;
begin
process(op)
variable tmp:integer:=0;
variable tmp1:integer:=0;--高四位
variable tmp2:integer:=0;--低四位
variable part:integer:=0;
variable carry:integer:=0;--进位
variable sum:integer:=0;
begin
tmp:=CONV_INTEGER(op);
tmp1:=tmp/16;
tmp2:=tmp rem 16;
if(tmp1=0)then
part:=0;
elsif(tmp1=1)then
part:=22;--16
elsif(tmp1=2)then
part:=50;--32
elsif(tmp1=3)then
part:=72;--48
elsif(tmp1=4)then
part:=100;--64
elsif(tmp1=5)then
part:=128;--80
else
part:=150;--96
end if;
sum:=(part rem 16)+tmp2;
carry:=0;
if(sum>19)then
sum:=sum+12;
carry:=2;
elsif(sum>9)then
sum:=sum+6;
carry:=1;
end if;
sum:=(sum rem 16)+(carry+part/16)*16;
tb<= sum;
end process;
result<=CONV_STD_LOGIC_VECTOR(tb,8);
end maxpld;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询