简体   繁体   English

VHDL - 将两个8位向量添加到9位向量中

[英]VHDL - Adding two 8-bit vectors into a 9-bit vector

This is a pretty simple question, but I haven't been able to make this work yet, nor has any searching on google or here turned up anything really useful. 这是一个非常简单的问题,但我还没有能够完成这项工作,也没有任何搜索谷歌或这里发现任何真正有用的东西。

All I'm trying to do is add two 8-bit vectors and store the result in a 9-bit vector. 我要做的就是添加两个8位向量并将结果存储在9位向量中。

signal operand1, operand2 : STD_LOGIC_VECTOR(7 downto 0);

signal sum : STD_LOGIC_VECTOR(8 downto 0);

sum <= operand1 + operand2;

However I get the warning: 但是我得到了警告:

Width mismatch. <sum> has a width of 9 bits but assigned expression is 8-bit wide.

Shouldn't VHDL have some sort of built in routine to know that an extra bit is necessary for addition overflow? 难道VHDL不应该有某种内置例程来知道额外的溢出需要额外的位吗?

I have these packages included: 我有这些包:

use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

Perhaps STD_LOGIC_VECTOR is always signed? 也许STD_LOGIC_VECTOR总是签名? If so, then I need to define them explicity as unsigned? 如果是这样,那么我需要将它们明确定义为无符号?

If your goal is to do arithmetic on your signals, get into the habit of declaring them with better-suited types for the job: unsigned or integer are good choices in your case. 如果您的目标是对信号进行算术运算,那么就养成用更适合的类型声明它们的习惯: unsignedinteger是您的理想选择。

Note that to prevent overflow you must concatenate a leading '0' to each operand, instead of doing it to the result: 请注意,为防止溢出,必须将前导'0'连接到每个操作数,而不是将其连接到结果:

sum <= ('0' & operand1) + ('0' & operand2);

Use the standard ieee.numeric_std library. 使用标准的ieee.numeric_std库。

Then make your numbers either of unsigned or signed type, and make use of the handy resize function: 然后将您的数字设置为unsigned或有signed类型,并使用方便的调整大小功能:

answer <= resize(operand1, answer'length) + resize(operand2, answer'length);

And bear in mind that many times it's much easier to just use integers, no conversions required, and arithmetic doesn't require you to jump through any resizing hoops! 请记住,很多时候使用整数更容易,不需要转换,算术也不需要你跳过任何调整大小的箍!

The suggestion above is correct: use the unsigned or signed type when implementing arithmetic circuits (remember to include the numeric_std package in your code). 上面的建议是正确的:在实现运算电路时使用unsignedsigned类型(记住在代码中包含numeric_std包)。

Since in your case sum has an extra bit (compared to the largest of the operands), the following can be done: 因为在你的情况下, sum有一个额外的位(与最大的操作数相比),可以完成以下操作:

1) If the system is unsigned: 1)如果系统未签名:

sum <= ('0' & operand1) + ('0' & operand2);

2) If the system is signed (requires sign extension): 2)如果系统已签名(需要签名延期):

sum <= (operand1(N-1) & operand1) + (operand2(N-1) & operand2);

Try this: Combine your result with a '0'-vector logically before writing it into the longer vector. 试试这个:在将结果写入较长的向量之前,将结果与'0'向量逻辑组合。 For me it worked. 对我来说它有效。

sum <= '0' & (operand1 + operand2);

Hope that helps :) 希望有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM