【问题标题】:How to do a bitwise AND on integers in VHDL?如何对VHDL中的整数进行按位与?
【发布时间】:2012-04-30 18:35:24
【问题描述】:

我正在学习 VHDL,但我尝试编写一些代码来满足边界检查异常时遇到了问题。

这是我的基本总结代码:

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.std_logic_arith.all;
use IEEE.NUMERIC_STD.ALL;
use ieee.std_logic_unsigned.all; 
...
port(
Address: in std_logic_vector(15 downto 0);
...
constant SIZE : integer := 4096;
variable addr: integer range 0 to SIZE-1 := 0;
...
process ... 
addr := conv_integer(Address) and (SIZE-1); --error here

我得到的错误信息是

src/memory.vhd:37:35: 运算符“and”没有函数声明

基本上,我的目标是制作一个 16 位地址总线,引用内存只有 4096 字节。为什么我会收到这个奇怪的错误?我是否缺少库包含或其他内容?

【问题讨论】:

    标签: syntax vhdl


    【解决方案1】:

    第一:不要使用std_logic_arith numeric_std。还有you don't need std_logic_arith

    您不能对整数进行按位与运算,因此您需要执行以下操作:

    addr := Address and to_unsigned(SIZE-1, Address'length);
    

    但您可能希望保证 SIZE 是 2 的幂

    我倾向于在 bits 中创建一个常量并从那里开始:

    constant mem_bits : integer := 16;
    constant SIZE     : integer := 2**16;
    

    然后

    addr := Address(mem_bits-1 downto 0);
    

    【讨论】:

      【解决方案2】:

      我不认为and 是为整数定义的,尽管可能有一个包含该功能的标准库。

      为什么不将您的地址保留为std_logic_vector?在地址方面,您通常希望能够通过直接查看某些位来轻松解码,所以我认为这很有意义。

      只需将addr 设为std_logic_vector(11 downto 0),并将address 的最低12 位分配给它——这将忽略高4 字节,并为您提供4096 字节的空间(对于8 位数据总线)。

      【讨论】:

      • 是否可以根据 SIZE 指定“动态”范围?我希望将来能够更改 SIZE 而不必进行任何其他修改
      • 另外,请注意变量。它们可能看起来不错并且类似于 C,但您不一定知道它们实际实现的内容。我通常坚持process 之外的信号或组合逻辑,除非我真的必须使用变量。我想这是风格的问题。
      • 查看generic(或包定义的常量)来定义addr 向量的大小。这应该可以帮助您使其可配置。
      • 顺便说一句,我有一个来自我自己的代码的示例:opencores.org/… - 虽然我可能应该直接将integer 用于泛型类型...
      • 不要害怕变量——你*可以*告诉你在时钟过程中从变量中得到什么:如果你只在它们被写入之后引用它们,你就会得到组合逻辑。如果你之前提到过它们,你会得到带有组合逻辑的触发器。你也可以两者都做,合成器会找出你想要触发器的哪一面,以匹配你描述的功能
      【解决方案3】:

      而且对于整数没有意义。整数是一个范围内的数字,但它没有标准的实现方式,即它没有预定义的二进制表示。

      你可以使用类似下面的语法;

      library IEEE;
      use IEEE.std_logic_1164.all;
      use IEEE.std_logic_arith.all;
      
      
      
      
      
      entity testand is
          generic (nBITS:integer:=32);
          port (
              i:in integer;
              a:in std_logic_vector(nBITS-1 downto 0);
              o:out std_logic_vector(nBITS-1 downto 0));
      end entity;
      
      
      
      architecture beh of testand is
      
      signal v:std_logic_vector(a'length-1 downto 0);
      
      begin
      
          v<=std_logic_vector(conv_unsigned(i,o'length));
      
          o<=v and a;
      
      
      end architecture;
      

      【讨论】:

        【解决方案4】:

        在您的具体情况下,您也可以使用“mod SIZE”而不是“and (SIZE-1)”。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-13
          • 2019-11-25
          • 1970-01-01
          • 2013-10-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多