添加 std_logic_vectors 时出错

发布于 2024-09-30 02:42:17 字数 588 浏览 6 评论 0原文

我想要一个添加两个 std_logic_vector 的简单模块。但是,当使用代码时 下面的 + 运算符不会合成。

library IEEE; 
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;

entity add_module is
        port(
  pr_in1   : in std_logic_vector(31 downto 0);
  pr_in2   : in std_logic_vector(31 downto 0);
  pr_out   : out std_logic_vector(31 downto 0)  
        );
end add_module;

architecture Behavior of add_module is

begin

    pr_out <= pr_in1 + pr_in2;

end architecture Behavior;

我从 XST 第 17 行得到的错误消息。

+ 在这种情况下不能有这样的操作数。

我想念图书馆吗?如果可能的话,我不想将输入转换为自然数。

非常感谢

I wanna have a simple module that adds two std_logic_vectors. However, when using the code
below with the + operator it does not synthesize.

library IEEE; 
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;

entity add_module is
        port(
  pr_in1   : in std_logic_vector(31 downto 0);
  pr_in2   : in std_logic_vector(31 downto 0);
  pr_out   : out std_logic_vector(31 downto 0)  
        );
end add_module;

architecture Behavior of add_module is

begin

    pr_out <= pr_in1 + pr_in2;

end architecture Behavior;

The error message I get from XST

Line 17. + can not have such operands in this context.

Do I miss a library? If possible, I do not wanna convert the inputs into natural numbers.

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

随波逐流 2024-10-07 02:42:17

您希望编译器如何知道您的 std_logic_vectors 是有符号的还是无符号的?在这两种情况下,加法器的实现并不相同,因此您需要明确告诉编译器您希望它做什么;-)

注意:StackOverflow 中的 VHDL 语法高亮显示很糟糕。将此代码复制/粘贴到您首选的 VHDL 编辑器中,以便更轻松地阅读。

library IEEE; 
use IEEE.std_logic_1164.all;
-- use IEEE.std_logic_arith.all; -- don't use this
use IEEE.numeric_std.all; -- use that, it's a better coding guideline

-- Also, never ever use IEEE.std_unsigned.all or IEEE.std_signed.all, these
-- are the worst libraries ever. They automatically cast all your vectors
-- to signed or unsigned. Talk about maintainability and strong typed language...

entity add_module is
  port(
    pr_in1   : in std_logic_vector(31 downto 0);
    pr_in2   : in std_logic_vector(31 downto 0);
    pr_out   : out std_logic_vector(31 downto 0)  
  );
end add_module;

architecture Behavior of add_module is
begin

  -- Here, you first need to cast your input vectors to signed or unsigned 
  -- (according to your needs). Then, you will be allowed to add them.
  -- The result will be a signed or unsigned vector, so you won't be able
  -- to assign it directly to your output vector. You first need to cast
  -- the result to std_logic_vector.

  -- This is the safest and best way to do a computation in VHDL.

  pr_out <= std_logic_vector(unsigned(pr_in1) + unsigned(pr_in2));

end architecture Behavior;

How do you want the compiler to know if your std_logic_vectors are signed or unsigned ? Adder implementation is not the same in these two cases, so you need to explicitly tell the compiler what you want it to do ;-)

Note: VHDL syntax highlighting in StackOverflow is crappy. Copy/paste this code in your preferred VHDL editor to read it more easily.

library IEEE; 
use IEEE.std_logic_1164.all;
-- use IEEE.std_logic_arith.all; -- don't use this
use IEEE.numeric_std.all; -- use that, it's a better coding guideline

-- Also, never ever use IEEE.std_unsigned.all or IEEE.std_signed.all, these
-- are the worst libraries ever. They automatically cast all your vectors
-- to signed or unsigned. Talk about maintainability and strong typed language...

entity add_module is
  port(
    pr_in1   : in std_logic_vector(31 downto 0);
    pr_in2   : in std_logic_vector(31 downto 0);
    pr_out   : out std_logic_vector(31 downto 0)  
  );
end add_module;

architecture Behavior of add_module is
begin

  -- Here, you first need to cast your input vectors to signed or unsigned 
  -- (according to your needs). Then, you will be allowed to add them.
  -- The result will be a signed or unsigned vector, so you won't be able
  -- to assign it directly to your output vector. You first need to cast
  -- the result to std_logic_vector.

  -- This is the safest and best way to do a computation in VHDL.

  pr_out <= std_logic_vector(unsigned(pr_in1) + unsigned(pr_in2));

end architecture Behavior;
生寂 2024-10-07 02:42:17

不要使用std_logic_arith - 我已经写过这个(有一定长度:)。

使用 numeric_std - 并且在实体端口上使用正确的类型。如果您正在进行算术运算,请使用数字类型(整数或(无)符号向量,视情况而定)。他们会完美地合成。

std_logic_vector 非常有用。

  • 当您不关心数值(一组控制位,一些随机数据位)
  • 时,当您不知道输入的类型(例如一个加法器)时, 可以基于控制标志对有符号和无符号数进行操作)。

Don't use std_logic_arith - I've written about this (at some length :).

Do use numeric_std - and do use the right type on your entity ports. If you are doing arithmetic, use numerical types (either integers, or (un)signed vectors, as appropriate). They'll synthesise perfectly well.

std_logic_vectors are good for

  • when you don't care about numerical values (a set of control bits, some random data bits)
  • when you don't know about the type of the input (say an adder which can operate on both signed and unsigned numbers based on a control flag).
述情 2024-10-07 02:42:17

@Aurelien 使用 numeric_std 的好建议。

请记住,将两个 32 位值相加可能会得到一个 33 位值,并决定您要如何处理溢出。

Good advice from @Aurelien to use numeric_std.

Bear in mind that adding two 32 bit values can result in a 33 bit value and decide how you want to handle the overflow.

倦话 2024-10-07 02:42:17

您不能仅使用 std_logic_vector 进行算术运算。您必须将 std_logic_vector 转换为有符号/无符号(取决于您的代码要求)(请参见下面的 1),或者将它们转换为整数(请参见下面的 2)

  1. <前><代码> pr_out = std_logic_vector(无符号(pr_in1) + "01")

  2. <前><代码> pr_out = std_logic_vector(整数(pr_in1) + 99)

这些只是示例。您可以根据您的要求更改它们。

You cannot do an arithmetic operation with just std_logic_vector. Either you have to convert the std_logic_vector to signed/unsigned (depending on your code requirements) (see 1 below) or else convert them to integers (see 2 below)

  1.      pr_out = std_logic_vector(unsigned(pr_in1) + "01")
    
  2.      pr_out = std_logic_vector(integer(pr_in1) + 99)
    

These are just examples. You can change them based on your requirements.

征棹 2024-10-07 02:42:17

解决此错误的简单方法是:
添加unsign库,
之后您的代码开始工作。

使用

ieee.std_logic_unsigned.all;
pr_out <= pr_in1 + pr_in2;

The easy way to solve this error is:
Add library of unsign,
After that your code starts to work.

Use

ieee.std_logic_unsigned.all;
pr_out <= pr_in1 + pr_in2;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文