VHDL如何将std_logic_vector与std_logic信号相加?
我正在
douta : in std_logic_vector (3 downto 0);
doutb : in std_logic_vector (3 downto 0);
c0 : in std_logic;
f1 : in std_logic;
f0 : in std_logic;
res : out std_logic_vector (3 downto 0);
尝试构建一个简单的 ALU,该 ALU 提供的功能之一是 当
f1 and f0 both = 1
res = douta plus b plus c0
我这样写
f1 = '1' and f0 = '1' then res <= douta + doutb + c0;
时,但显然它不会工作,因为 douta
和 doutb
的数据类型是 std_logic_vector
其中 co
只是 std_logic
,我在编译时收到此错误,
' Error 603 line 34 : Incompatible types for arithmetic operator: LHS=std_logic_vector!18, RHS=std_logic
知道如何解决此问题吗?
编辑: 也尝试过
f1 = '1' and f0 = '1' then res <= douta + doutb + ("000" & c0);
但仍然没有运气,这次编译器说
LHS=std_logic_vector!7, RHS=array_1_of_std_logic_3_d_0
I've got
douta : in std_logic_vector (3 downto 0);
doutb : in std_logic_vector (3 downto 0);
c0 : in std_logic;
f1 : in std_logic;
f0 : in std_logic;
res : out std_logic_vector (3 downto 0);
I'm trying to build a simple ALU, and one of the functions this ALU provides is
when
f1 and f0 both = 1
res = douta plus b plus c0
so I wrote
f1 = '1' and f0 = '1' then res <= douta + doutb + c0;
but obviously its not gonna work because the datatype of douta
and doutb
is std_logic_vector
where as co
is just std_logic
and I got this error when compile
' Error 603 line 34 : Incompatible types for arithmetic operator: LHS=std_logic_vector!18, RHS=std_logic
any idea how I can fix this problem ?
edit:
also have tried
f1 = '1' and f0 = '1' then res <= douta + doutb + ("000" & c0);
but still no luck, this time the compiler says
LHS=std_logic_vector!7, RHS=array_1_of_std_logic_3_d_0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您要对它们进行算术运算,请不要使用
std_logic_vector
。使用ieee.numeric_std
,并使用signed
或unsigned
类型。然后您只需添加“0”或“1”即可。使用
std_logic_arith.all
的解决方法是使用非标准库的胡编乱造,当您更改工具链时,这可能会给您带来可移植性的麻烦。我对此写了一个更详细的页面: http://parallelpoints.com/node/3
有一些在 comp.lang.vhdl 上对此进行的讨论:http://groups.google.com/group/comp.lang.vhdl/browse_thread/thread/549e1bbffd35914d/83cc0f19350fc392?hl=en&q=group: comp.lang.vhdl+numeric_std#83cc0f19350fc392 以及 comp.lang.vhdl 常见问题解答中。
Please don't use
std_logic_vector
if you're going to do arithmetic on them. Useieee.numeric_std
, and use thesigned
orunsigned
types. Then you can just add your '0' or '1' to it.The workaround of using
std_logic_arith.all
is a fudge using a non-standard library, which can get you into portability troubles when you change toolchains.I wrote a more detailed page on this: http://parallelpoints.com/node/3
There was some discussion on this on comp.lang.vhdl here: http://groups.google.com/group/comp.lang.vhdl/browse_thread/thread/549e1bbffd35914d/83cc0f19350fc392?hl=en&q=group:comp.lang.vhdl+numeric_std#83cc0f19350fc392 and also in the comp.lang.vhdl FAQ.
您可以将 c0 转换为 std_logic_vector。我已经很久没有制作VHDL了...
这可能性能不那么好,也许silicium编译器合成它那么好,也许更好的想法是这样编写它,以便将c0修改为向量,然后将所有相加三个douta、doutb 和转换器c0。另一种选择是逐位进行计算,但是编译器有什么用呢?
这可能听起来很荒谬,但有时如果你给它一些像这样的提示,编译器会产生更好的结果(只需尝试一下并验证输出,通过这样一个小例子没什么大不了的):
另一个建议,如果你写 if 和结果有点奇怪,引入一个 else 来修复未决定的状态(一些编译器如果有太多的自由度,就会表现得很糟糕!)。但这是我2004年的经验!!!
You could transform c0 into a std_logic_vector. It has been a long time since I made VHDL...
This is probably not so performant, maybe the silicium compiler synthesizes it so something good, maybe it's a better idea to write it so that c0 is modified into a vector and then add all three douta, doutb and the converter c0. Another option would be to do the calculations bit for bit but then what do you have the compiler for?
It may sound ridiculous but sometimes the compiler produces a better result if you give it some hints like this (just try it out and verify the output, by such a small example not a big deal):
Another advice, if you write if's and the results are somewhat strange, introduce an else to fix the undecided states (some compilers behave badly if they have too much freedom!). But that's from my experience that is already getting back to year 2004!!!
哦,我想我找到了一个修复程序,需要添加以下库
,我们尝试过的将起作用:) 感谢@jdehaan
Oh I think I found a fix, need to add the following library
and what we tried will work :) Thanks to @jdehaan
您不需要将 std_logic 转换为 std_logic_vector 来进行加法。只需按原样进行加法,并确保“res”有足够的位来保存最大答案。在这种情况下,res 需要为 5 位宽才能溢出。
作为一个风格专家,不要将你的输入端口命名为“dout”。这是糟糕的形式。称他们为喧闹吧,因为他们就是这样。在实例化该架构中,您将另一个名为“dout”的组件的输出端口连接到该组件的“din”。
You don't need to convert the std_logic to a std_logic_vector to do the addition. Just do the addition as is, and make sure that "res" has enough bits to hold the max answer. In this case res needs to be 5 bits wide in order for it to now overflow.
And as a style nit, don't name your input ports "dout". That's bad form. Call them din here, because that's what they are. And in the architecture than instantiates this you connect another component's output port named "dout" to the "din" of this component.