【发布时间】:2011-02-03 03:22:46
【问题描述】:
在 VHDL 中对位向量求反是什么意思?例如,如果我有 10100111,它是一个名为 temp 的位向量,并且我执行类似 temp := not temp 之类的操作,我的输出将是什么?
【问题讨论】:
在 VHDL 中对位向量求反是什么意思?例如,如果我有 10100111,它是一个名为 temp 的位向量,并且我执行类似 temp := not temp 之类的操作,我的输出将是什么?
【问题讨论】:
按位反转。
一般在VHDL(LRM 7.2.1)中:“对于定义在一维数组类型上的一元运算not,对操作数的每个元素进行运算,结果是一个具有相同的数组索引范围作为操作数。”
【讨论】:
您可以在向量上使用“不”。只需使用 ModelSim 或 ISim 运行下面的程序,反转/取反的位向量就会在控制台中打印出来。
LIBRARY ieee;
USE ieee.numeric_bit.ALL;
entity test is
end entity test;
architecture beh of test is
function vec_image(arg : bit_vector) return string is
-- original author Mike Treseler (http://mysite.ncnetwork.net/reszotzl/)
-- recursive function call turns ('1','0','1') into "101"
-------------------------------------------------------------------------------
constant arg_norm : bit_vector(1 to arg'length) := arg;
constant center : natural := 2; -- 123
variable bit_image : string(1 to 3); -- '0'
variable just_the_number : character;
begin
if (arg'length > 0) then
bit_image := bit'image(arg_norm(1)); -- 3 chars: '0'
just_the_number := bit_image(center); -- 1 char 0
return just_the_number -- first digit
& vec_image(arg_norm(2 to arg_norm'length)); -- rest the same way
else
return ""; -- until "the rest" is nothing
end if;
end function vec_image;
begin
demo:process is
variable bitvec : bit_vector (7 downto 0) := "10100111";
begin
report vec_image(bitvec);
report vec_image(not bitvec); -- not bit vector
wait;
end process demo;
end architecture beh;
【讨论】:
如果你真的想否定一个向量,你需要使用一个为它定义了某些属性的向量。具体来说:
bit_vector 或std_logic_vector,它们只是位的集合)从ieee.numeric_std 包中,您应该为此使用signed 类型:
use ieee.numeric_std.all;
...
variable a,b:signed(8 downto 0);
...
a := "000000001";
b := -a;
【讨论】: