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