【问题标题】:left shifting of a two's complement vector VHDL二进制补码向量 VHDL 的左移
【发布时间】:2018-05-29 09:28:46
【问题描述】:

我正在尝试解决一些练习,我必须将一个名为 A 的 8 位向量转换为 2A (A+A)。

我的解决方案是:(A(7) and '1') & A(6 downto 0) & '0';

在此之后,我以这种方式对 A 进行了补码:

entity complementare is
    port(a: in std_logic_vector(7 downto 0);
         b: out std_logic_vector(7 downto 0));
end complementare;

architecture C of complementare is
    signal mask, temp: std_logic_vector(7 downto 0);
    component ripplecarry8bit is
        port(a,b: std_logic_vector(7 downto 0);
             cin: in std_logic;
             cout: out std_logic;
             s: out std_logic_vector(7 downto 0));
    end component;
begin
    mask<="11111111";
    temp<=a nand mask;
    rc: ripplecarry8bit port map(temp, "00000001", '0', cout, b);
end C; 
--if you need I post ripplecarry code but consider it as a generic adder

为了得到-2A (-A-A),我想这样做:

signal compA: std_logic_vector(7 downto 0);
compA: complementar port map(A, compA);

--shifting
(compA(7) and '1') & compA(6 downto 0) & '0'; -- -A

现在,我的主要疑问是-A,在使用补码并获得 compA 后,我必须将 8 位向量扩展为 9 位向量(因为我的输出必须是 9 位向量),我是想这样做,但我有疑问:

'1' & compA; --or should I just append compA to a '0' value?

【问题讨论】:

  • A(7) and '1'等于A(7),你可以写2A &lt;= A &amp; '0'
  • compA(7) and '1' 与compA(7) 相同,所以为什么不简单地添加一个零并完成。左移在逻辑上和算术上是一样的。
  • -2A = -A&amp;'0'。 1 到 2 位示例:-1 = b'1' ==> -2 = b'10'.
  • 好的,谢谢大家,但我主要怀疑-A,正如我在帖子的最后几行所说的那样!当它是负数时,如何将 8 位向量扩展为 9 位向量。例如,我想将-A (8-bit) 扩展到-A (9-bit),我在想我不能这样做'0' &amp; -A 因为第一位必须是'1' 才能表示一个负数! @JHBonarius @grorel @Oldfart
  • 我认为 JHBonarius 不是在谈论 signed VHDL 信号类型,而是以数学方式阅读“有符号算术”,即如何使用 0 和1 ?

标签: vector vhdl bit-shift twos-complement


【解决方案1】:

为您的问题提供简单的有符号算术解决方案:


对于大小为C_SIZEOF_A 的std_logic_vector 信号A

signal A : std_logic_vector(C_SIZEOF_A-1 downto 0);

要获得等于 -A(相同大小)的信号:

对信号值进行补码并将结果加一:

signal minus_A : std_logic_vector(C_SIZEOF_A-1 downto 0);

minus_A <= (not A) + 1; -- Warning here !!!

警告:没有为 std_logic_vector 定义“+”运算符。您可以使用您喜欢的解决方案进行添加。我故意不想在这里给出解决方案,因为最简单的方法是使用signed 信号,但你说你不能。


将信号乘以 2(有符号或无符号)

添加一个空位作为 LSB:

signal 2A : std_logic_vector(C_SIZEOF_A downto 0);

2A <= A & '0';

将信号扩展 1 位(有符号):

MSB 是符号位。仅扩展这一点:

signal A_extended : std_logic_vector(C_SIZEOF_A downto 0);

A_extended <= A(C_SIZEOF_A-1) & A;

将信号扩展 1 位(无符号):

这里没有符号位,只需添加一个'0':

signal A_extended : std_logic_vector(C_SIZEOF_A downto 0);

A_extended <= '0' & A;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    相关资源
    最近更新 更多