VHDL加法器,相同字长?
在VHDL中,我想添加5位和8位(无符号),输出有多少位 我希望我的代码能够回答我刚才提出的问题。我的代码目前看起来像这样 我的代码是:VHDL加法器,相同字长?,vhdl,bits,Vhdl,Bits,在VHDL中,我想添加5位和8位(无符号),输出有多少位 我希望我的代码能够回答我刚才提出的问题。我的代码目前看起来像这样 我的代码是: library ieee; use ieee-std_logic_1164.all; entity adder is port( a : in unsigned (7 downto 0); b : in unsigned (4 downto 0); - - Need to convert this to 8 bit right? But ho
library ieee;
use ieee-std_logic_1164.all;
entity adder is
port( a : in unsigned (7 downto 0);
b : in unsigned (4 downto 0); - - Need to convert this to 8 bit right? But how?
z : out unsigned(7 downto 0)); - - This one must be 8 bits right? Cuz a & b & z must have the same WL. Or am i wrong?
end adder;
archictecture add of adder is
begin
z <= a + b;
end archictecture;
ieee库;
使用ieee-std_逻辑_1164.all;
实体加法器是
端口(a:未签名(7到0);
b:在无符号(4到0)中;--需要将其转换为8位吗?但是如何转换呢?
z:输出无符号(从7到0));--这个必须是8位,对吗?因为a&b&z必须具有相同的WL。还是我错了?
末端加法器;
加法器的结构是加法
开始
z除此校正外:
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
你的代码很好
此外,添加8位数字和5位数字会产生9位数字,因为可能会溢出。例如,“11111111”+“11111”
溢出8位输出,但不会溢出9位输出。在函数“+”(L,R:UNSIGNED)返回UNSIGNED的包numeric_std中,最长参数的长度定义返回值长度:
function "+" (L, R: UNSIGNED) return UNSIGNED is
constant SIZE: NATURAL := MAX(L'LENGTH, R'LENGTH);
variable L01 : UNSIGNED(SIZE-1 downto 0);
variable R01 : UNSIGNED(SIZE-1 downto 0);
begin
if ((L'LENGTH < 1) or (R'LENGTH < 1)) then return NAU;
end if;
L01 := TO_01(RESIZE(L, SIZE), 'X');
if (L01(L01'LEFT)='X') then return L01;
end if;
R01 := TO_01(RESIZE(R, SIZE), 'X');
if (R01(R01'LEFT)='X') then return R01;
end if;
return ADD_UNSIGNED(L01, R01, '0');
end "+";
结果
的长度是L参数的长度,它与UNSIGNED_ADD
的两个参数相同。结果中没有隐含执行
与您的情况一样,分配给z
的结果可以是8位
修复端口声明中的注释分隔符,添加一个use子句来访问软件包数字_std,修复一个“-”应该是一个“.”,architecture
的拼写,并添加一个测试台,将a
和b
的值设置为所有“1”,您可以分析、详细说明并运行您的设计,而不会出错,告诉您没有执行数组长度错误
要将第九个“位”作为加法器的输出,您可以将一个参数的大小调整为9位,或者将一个参数与前导零连接,形成9位值:
z <= "0" & a + b;
za,
b=>b,
z=>z
);
监视器:
过程
开始
等待1ns;
报告“z=”&未签名的_图像(z);
等待
结束过程;
终端架构;
大卫_koontz@Macbook:ghdl-一个加法器。vhdl
大卫_koontz@Macbook:ghdl-e tb_加法器
大卫_koontz@Macbook:ghdl-r tb_加法器
adder.vhdl:54:9:@1ns:(报告注释):z=100011110
哦,我认为加法器中的a、b和z必须是相同的字长才能工作。但事实并非如此,规范明确允许参数具有不同的大小。这是VHDL如此困扰我的原因之一。函数unsigned_image相当复杂,它所做的只是打印出一个unsigned(从8到0)。这让我很沮丧,因为这比实际回答他原来的问题更复杂。@Russell,但数字\u std定义了过程to\u string(),当然可以用它代替?rick-to\u string仅在-2008年可用@Russell“…VHDL让我很烦恼”没有留下太多的推理空间。该函数有一个顺序语句和声明。具有图像和值属性,并且需要限定表达式。Stackoverflow是关于实际问题的详细答案。“这让我很沮丧”似乎有点不合时宜。测试台是需要第9位的前提的证明。VHDL也用于形式证明。数学运算符有其普通含义,本标准未涵盖。
z <= "0" & a + b;
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity adder is
port(
a: in unsigned (7 downto 0);
b: in unsigned (4 downto 0);
z: out unsigned (8 downto 0)
);
end adder;
architecture add of adder is
begin
z <= "0" & a + b;
end architecture;
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity tb_adder is
end entity;
architecture foo of tb_adder is
signal a: unsigned (7 downto 0) := (others => '1');
signal b: unsigned (4 downto 0) := (others => '1');
signal z: unsigned (8 downto 0);
function unsigned_image(inp: unsigned) return string is
variable image_str: string (1 to inp'length);
alias input_str: unsigned (1 to inp'length) is inp;
begin
for i in input_str'range loop
image_str(i) := character'VALUE(std_ulogic'IMAGE(input_str(i)));
end loop;
return image_str;
end;
begin
DUT:
entity work.adder
port map (
a => a,
b => b,
z => z
);
MONITOR:
process
begin
wait for 1 ns;
report "z = " & unsigned_image(z);
wait;
end process;
end architecture;