1

我正在用 VHDL 编写代码,用于信号的算术运算。我声明信号如下:

    signal x : std_logic_vector (7 downto 0); 
    signal y: std_logic_vector (7 downto 0); 
    signal z: std_logic_vector (  7 downto 0);
    z<= x-y ;

详细地:

  library ieee;
  use ieee.std_logic_1164.all;
  use ieee.numeric_std.all;
  USE ieee.std_logic_arith.conv_std_logic_vector;   
  library ieee_proposed;
  use ieee_proposed.fixed_pkg.all;
  signal heat0,heat1,heat2 : std_logic_vector(31 downto 0); 
  signal Data_In, M_AXIS_TDATA Fixed_input_tdata: std_logic_vector (31 downto 0);
  shared variable float,  h0,h1,h2,fixed1,fixed2,fixed3,fixed_shift :ufixed (23 downto -8);
  shared variable  fixed_64: ufixed (31 downto -32); 
 float := to_ufixed(unsigned (Fixed_input_tdata), 23,-8); 
 h2 :=  to_ufixed(unsigned(Data_In),23,-8); 
 heat1 <= Data_in; 
 h1 :=  to_ufixed(unsigned(heat1),23,-8);  
 heat0<= heat1;
 h0 :=  to_ufixed(unsigned(heat0),23,-8);
 heat1_mult  := std_logic_vector(unsigned(heat1) sll 1);

 fixed_shift := to_ufixed(unsigned (heat1_mult), 23,-8); 
 fixed1 := fixed_shift+h2;   
 fixed2 := h0-fixed1;   
 fixed_64 := fixed2 *float; 
 fixed3 := h1+fixed_64;--(23 downto -8);
M_AXIS_TDATA <= std_logic_vector (fixed3); 

那么用定点对std_logiC-vector进行算术运算是正确的方法吗?

举个例子,z= 0x01-0x11。这将给出负输出(0xF0):但我不想要负输出。我想将此值视为正值。我什至尝试将这些信号类型更改为无符号,但仍然无法成功。实际上我在 vhdl 中有一些复杂的数学运算,所以我在这里只是举一个例子来说明我的观点。我不想使用带符号的值。如何发送正无符号输出?

再举一个例子:如果我的输出是 bf978000,它将显示为负数 -1.18。我希望它是积极的,而不是消极的。

让我再举一个例子:

z= 2+ [0.2 * (4-10) ] = 0.8 。0.8 定点格式(0x000000cd)(24 个整数,8 个小数格式):0.2 是 0x00000033 定点格式。(24 个整数,8 个小数格式)但我得到 [0x00000002 + (0x00000033 * (0x00000004-0x0000000A)] = FFFFFED0。(这是负数)。如何再次将输出表示为 0.8。

4

2 回答 2

1

如果您可以z <= x - y直接使用,听起来您使用的是 Synopsys 包之一ieee.std_logic_unsigned,但您可以考虑使用 VHDL 标准包ieee.numeric_std,它在下面的示例中使用。

默认情况下std_logic_vector没有任何数字表示;它只是一个数组std_logic。包中的unsigned类型ieee.numeric_std可以表示std_logic_vector做的数字表示unsigned(x). 所以上面使用无符号减法的表达式将是:

z <= std_logic_vector(unsigned(x) - unsigned(z));

任何欠载/溢出,如“z = 0x01-0x11”,将导致没有任何指示的回绕。

于 2015-05-26T08:38:40.430 回答
1

我认为你的问题需要一些改进。我不知道为什么它被标记为verilog,你显示的十六进制数字没有签名,而且std_logic_vector首先不是算术(添加这样的解释是用std_logic_arith或类似的,但最好使用无符号和有符号的数字标准)。因此,您的代码必须有更多内容才能使减法正常工作,并且显示的负数必须来自其他东西;一个模拟器,也许?模拟器和波形查看器往往有自己的设置来解释信号。

因此,扩展您的示例以显示您正在使用的环境,并说明您实际在寻找什么操作。你的意思是取差值的绝对值,还是使用饱和算术?

于 2015-05-26T08:39:27.363 回答