PDA

View Full Version : [VHDL] Moltiplicatore


matty.ma
29-09-2008, 09:41
Ciao a tutti, devo realizzare un moltiplicatore in vhdl, con il modelsim, ho buttato giù il codice, ma mi sono accorto che la moltiplicazione tra positivi funziona, ma se uno dei due è negativo, il risultato, invece di negarlo me lo da sempre positivo.

Vi posto il codice :

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use IEEE.NUMERIC_STD.ALL;

entity Multiplier is
Port ( a : in SIGNED (31 downto 0);
b : in SIGNED (31 downto 0);
p : out SIGNED (31 downto 0));
end Multiplier;

architecture aMultiplier of Multiplier is
begin

process (a, b) is

constant T_in_out : time := 20 ns;

variable negative_result : boolean;
variable op1 : signed(31 downto 0);
variable op2 : signed(31 downto 0);
variable result : signed(63 downto 0);
variable carry_in, carry: std_ulogic;
begin

op1 := a;
op2 := b;
-- controlla e ritorna il segno del risultato
negative_result := (op1(op1'left) = '1') xor (op2(op2'left) = '1');
-- porta positivi tutti e due gli operandi
if (op1(op1'left) = '1') then
carry := '1';
for index in op1'reverse_range loop
carry_in := carry;
carry := carry_in and not op1(index);
op1(index) := not op1(index) xor carry_in;
end loop;
end if;
if (op2(op2'left) = '1') then
carry := '1';
for index in op2'reverse_range loop
carry_in := carry;
carry := carry_in and not op2(index);
op2(index) := not op2(index) xor carry_in;
end loop;
end if;

-- esegue la moltiplicazione
result := (others => '0');

for count in op2'reverse_range loop
carry := '0';
if (op2(count) = '1') then

for index in op1'reverse_range loop
carry_in := carry;
carry := ((result(index + count) and op1(index)) or (carry_in and (result(index + count) xor op1(index))));
result(index + count) := result(index + count) xor op1(index) xor carry_in;
end loop;
result(count + 16) := carry;

nd if;
end loop;

-- in base al segno del risultato calcolato prima
-- si riporta il risultato negativo se necessario
if negative_result then

carry := '1';
for index in p'reverse_range loop
carry_in := carry;
carry := carry_in and not result(index);
result(index) := not result(index) xor carry_in;
end loop;

end if;

-- Riporta nel segnale di uscita solamente
-- i 32 bit significativi
p <= result(60 downto 29) after T_in_out;

end process;

end aMultiplier;

Qualcuno sa dirmi cosa c'è che non va ??