Syntax 如何在VHDL中对整数进行位与运算?

Syntax 如何在VHDL中对整数进行位与运算?,syntax,vhdl,Syntax,Vhdl,我正在学习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 SI

我正在学习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”没有函数声明


基本上,我的目标是制作一个只有4096字节的16位地址总线和参考内存。为什么我会犯这个奇怪的错误?我是不是错过了一个图书馆

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

不过,为什么不将您的地址保留为
std\u logic\u vector
?当涉及到地址时,您通常希望能够通过直接查看某些位来轻松解码,因此我认为这很有意义


只需将
addr
a
std\u logic\u vector(11到0)
,并将
地址的最低12位分配给它-这将忽略上面的4个字节,并为您提供4096字节的空间(对于8位数据总线)。

首先:不要使用
std\u logic\u arith
数字std
。及

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

addr := Address and to_unsigned(SIZE-1, Address'length);
但您可能希望保证大小是2的幂

我倾向于创建一个以位为单位的常数,然后从那里开始计算:

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


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

您可以使用下面的语法

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;
IEEE库;
使用IEEE.std_logic_1164.all;
使用IEEE.std_logic_arith.all;
实体testand是
通用(nBITS:整数=32);
港口(
i:整数;
a:标准逻辑向量(nBITS-1向下至0);
o:输出标准逻辑向量(nBITS-1向下至0);
终端实体;
泰斯特兰德的建筑风格
信号v:std_逻辑_向量(a'长度-1向下至0);
开始

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

是否可以根据大小指定“动态”范围?我希望将来能够更改大小,而不必进行任何其他修改。此外,请注意变量。它们可能看起来很好,很像C语言,但您不一定知道它们实际实现为什么。我通常坚持使用
过程之外的信号或组合逻辑,除非我真的必须使用变量。我想这是一个风格的问题。看看
通用
(或包定义的常量)来定义
addr
向量的大小。这应该有助于您对其进行配置。顺便说一句,我自己的一些代码中有一个例子:-虽然我可能应该直接将
integer
用于泛型类型…不要害怕变量-您*可以*告诉您在计时过程中从变量中得到了什么:如果您仅在写入之后才引用它们,你得到了组合逻辑。如果你之前提到过它们,你会得到带有组合逻辑的触发器。您也可以同时执行这两项操作,合成器将计算出您想要触发器的哪一侧,以匹配您描述的功能
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;