我尝试实现了两个有符号数字的and相加。第一个是32位,第二个也是32位,但与前一个操作的相加相对应。VHLD代码如下:
Entity Sum_Position is
port
(
Clk: in std_logic;
Reset: in std_logic;
Raz_position: in std_logic;
Position_In: in std_logic_vector(31 downto 0);
Position_Out: out std_logic_vector(31 downto 0)
);
end Sum_Position;
Architecture Arch_position of sum_Position is
-- create locale signals
signal position_before: signed (31 downto 0):= (OTHERS => '0');
-- both signals have one more bit than the original
signal Position_s : SIGNED(Position_In'length downto 0):= (OTHERS => '0');
signal Position_Before_s : SIGNED(Position_In'length downto 0):= (OTHERS => '0');
signal Sum_Pos_s : SIGNED(Position_In'length downto 0):= (OTHERS => '0');
Begin -- begin of architecture
-- convert type and perform a sign-extension
Position_s <=SIGNED(Position_In(31) & Position_In);
Position_Before_s<=resize(signed(position_before), Position_Before_s'length);
Sum_of_position: process(Clk, Reset)
begin
IF (Reset='0') THEN -- when reset is selected
-- initialize all values
Sum_Pos_s<= (OTHERS => '0');
ELSIF (Clk'event and Clk = '1') then
-- addition of two 33 bit values
Sum_Pos_s <= Position_s + Position_Before_s;
END IF;
end process Sum_of_position;
-- resize to require size and type conversion
position_before <= (OTHERS => '0') WHEN Raz_position='1' else
signed(resize(Sum_Pos_s, position_before'length));
-- Resize and output the result
Position_Out <= (OTHERS => '0') WHEN Raz_position='1' else
std_logic_vector(resize(Sum_Pos_s, Position_Out'length));
end Arch_position;但是,我有溢出,因为结果非常奇怪。你能给我提个解决方案吗?
致以最良好的问候;
发布于 2016-08-26 14:32:18
首先,您的代码非常不清楚。
其次,position_before(_s)没有理由是异步的,它应该被计时,例如(汇总):
begin
IF (Reset='0') THEN -- when reset is selected
-- initialize all values
Sum_Pos_s<= (OTHERS => '0');
ELSIF (Clk'event and Clk = '1') then
Position_Before_s <= Sum_Pos_s
Sum_Pos_s <= Position_s + Position_Before_s;
END IF;
end process Sum_of_position;第三,回答你的问题。你把浮点数传递给你的VHDL引擎。将它们解释为签名并添加它们。你应该看看IEEE754 floats。有一个用于符号位的固定字段,一个用于指数,一个用于尾数。你不能把所有的东西都加起来。
第一步是在相同的指数基础上表达两者。然后添加调整后的尾数并保留指数。然后重新调整尾数以使最高有效位对应于0.5。
你要做的是: 0.4 + 40 = (0.1) *4+ (10) *4尾数都是4个指数都是-1和1。如果没有字段溢出,你的结果就会变成0的指数和8的尾数,所以是8。
发布于 2017-12-05 06:26:35
大多数现代VHDL语言工具都有Integer类型(signed和unsigned)。除非使用Range修饰符,否则它们通常为32位宽。
我建议你考虑使用整数而不是std_logic_vector。
可以在类型之间进行转换,就像C中的类型转换一样。
这是我最喜欢的关于转换VHDL类型的图表。我已经把它打印出来并贴在我的墙上http://www.bitweenie.com/listings/vhdl-type-conversion上了
VHDL http://vhdl.renerta.com/mobile/source/vhd00039.htm中整数形式的页面
https://stackoverflow.com/questions/39118952
复制相似问题