2011-02-03 16 views

回答

2

你可以使用“不”向量。用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; 
+0

感谢那正是我正在寻找。 – JC2 2011-02-03 13:13:38

7

按位反转。

一般在VHDL(LRM 7.2.1)中:“对于在一维数组类型上定义的一元操作not,对操作数的每个元素执行操作,结果是具有相同索引的数组范围作为操作数“。

1

如果你真的想否定一个向量,你需要使用一个具有为它定义的某些属性的向量。具体做法是:

  • 数值的一些概念(所以不能使用bit_vectorstd_logic_vector,它们只是位的集合)
  • “签到”

的一些概念从ieee.numeric_std包,你应该使用signed这个类型:

use ieee.numeric_std.all; 
... 
variable a,b:signed(8 downto 0); 
... 
a := "000000001"; 
b := -a;