1 |
d93979b7
|
Arnaud Dieumegard
|
|
2 |
|
|
-- Copyright (C) 1996 Morgan Kaufmann Publishers, Inc
|
3 |
|
|
|
4 |
|
|
-- This file is part of VESTs (Vhdl tESTs).
|
5 |
|
|
|
6 |
|
|
-- VESTs is free software; you can redistribute it and/or modify it
|
7 |
|
|
-- under the terms of the GNU General Public License as published by the
|
8 |
|
|
-- Free Software Foundation; either version 2 of the License, or (at
|
9 |
|
|
-- your option) any later version.
|
10 |
|
|
|
11 |
|
|
-- VESTs is distributed in the hope that it will be useful, but WITHOUT
|
12 |
|
|
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
13 |
|
|
-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
14 |
|
|
-- for more details.
|
15 |
|
|
|
16 |
|
|
-- You should have received a copy of the GNU General Public License
|
17 |
|
|
-- along with VESTs; if not, write to the Free Software Foundation,
|
18 |
|
|
-- Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19 |
|
|
|
20 |
|
|
-- ---------------------------------------------------------------------
|
21 |
|
|
--
|
22 |
|
|
-- $Id: ch_06_mult-b.vhd,v 1.2 2001-10-26 16:29:34 paw Exp $
|
23 |
|
|
-- $Revision: 1.2 $
|
24 |
|
|
--
|
25 |
|
|
-- ---------------------------------------------------------------------
|
26 |
|
|
|
27 |
|
|
architecture behavioral of multiplier is
|
28 |
|
|
begin
|
29 |
|
|
|
30 |
|
|
behavior : process (a, b) is
|
31 |
|
|
|
32 |
|
|
constant Tpd_in_out : time := 40 ns;
|
33 |
|
|
variable negative_result : boolean;
|
34 |
|
|
variable op1 : std_ulogic_vector(15 downto 0);
|
35 |
|
|
variable op2 : std_ulogic_vector(15 downto 0);
|
36 |
|
|
variable result : std_ulogic_vector(31 downto 0);
|
37 |
|
|
variable carry_in, carry : std_ulogic;
|
38 |
|
|
|
39 |
|
|
begin
|
40 |
|
|
op1 := to_X01(a);
|
41 |
|
|
op2 := to_X01(b);
|
42 |
|
|
-- make both operands positive, remembering sign of result
|
43 |
|
|
negative_result := (op1(15) = '1') xor (op2(15) = '1');
|
44 |
|
|
if (op1(15) = '1') then
|
45 |
|
|
carry := '1';
|
46 |
|
|
for index in 0 to 15 loop
|
47 |
|
|
carry_in := carry;
|
48 |
|
|
carry := carry_in and not op1(index);
|
49 |
|
|
op1(index) := not op1(index) xor carry_in;
|
50 |
|
|
end loop;
|
51 |
|
|
end if;
|
52 |
|
|
if (op2(15) = '1') then
|
53 |
|
|
carry := '1';
|
54 |
|
|
for index in 0 to 15 loop
|
55 |
|
|
carry_in := carry;
|
56 |
|
|
carry := carry_in and not op2(index);
|
57 |
|
|
op2(index) := not op2(index) xor carry_in;
|
58 |
|
|
end loop;
|
59 |
|
|
end if;
|
60 |
|
|
-- do long multiplication
|
61 |
|
|
result := (others => '0');
|
62 |
|
|
for count in 0 to 15 loop
|
63 |
|
|
carry := '0';
|
64 |
|
|
if (op2(count) = '1') then
|
65 |
|
|
for index in 0 to 15 loop
|
66 |
|
|
carry_in := carry;
|
67 |
|
|
carry := (result(index+count) and op1(index))
|
68 |
|
|
or (carry_in and (result(index+count) xor op1(index)));
|
69 |
|
|
result(index+count) := result(index+count) xor op1(index) xor carry_in;
|
70 |
|
|
end loop;
|
71 |
|
|
result(count+16) := carry;
|
72 |
|
|
end if;
|
73 |
|
|
end loop;
|
74 |
|
|
-- result now contains unsigned product, with binary point
|
75 |
|
|
-- between bits 30 and 29. assign output with sign adjusted.
|
76 |
|
|
if negative_result then
|
77 |
|
|
carry := '1';
|
78 |
|
|
for index in 0 to 31 loop
|
79 |
|
|
carry_in := carry;
|
80 |
|
|
carry := carry_in and not result(index);
|
81 |
|
|
result(index) := not result(index) xor carry_in;
|
82 |
|
|
end loop;
|
83 |
|
|
end if;
|
84 |
|
|
p <= result after Tpd_in_out;
|
85 |
|
|
end process behavior;
|
86 |
|
|
|
87 |
|
|
end architecture behavioral;
|