1 ---------------------------------------------------------------------
2 -- TITLE: Multiplication and Division Unit
3 -- AUTHORS: Steve Rhoads (rhoadss@yahoo.com)
4 -- DATE CREATED: 1/31/01
6 -- PROJECT: Plasma CPU core
7 -- COPYRIGHT: Software placed into the public domain by the author.
8 -- Software 'as is' without warranty. Author liable for nothing.
10 -- Implements the multiplication and division unit in 32 clocks.
12 -- To reduce space, compile your code using the flag "-mno-mul" which
13 -- will use software base routines in math.c if USE_SW_MULT is defined.
14 -- Then remove references to the entity mult in mlite_cpu.vhd.
18 -- for(i = 0; i < 32; ++i)
20 -- answer = (answer >> 1) + (((b&1)?a:0) << 31);
25 -- long upper=a, lower=0;
27 -- for(i = 0; i < 32; ++i)
29 -- lower = lower << 1;
30 -- if(upper >= a && a && b < 2)
35 -- a = ((b&2) << 30) | (a >> 1);
38 ---------------------------------------------------------------------
40 use ieee.std_logic_1164.all;
41 use ieee.std_logic_unsigned.all;
42 use IEEE.std_logic_arith.all;
43 use work.mlite_pack.all;
46 generic(mult_type : string := "DEFAULT");
47 port(clk : in std_logic;
48 reset_in : in std_logic;
49 a, b : in std_logic_vector(31 downto 0);
50 mult_func : in mult_function_type;
51 c_mult : out std_logic_vector(31 downto 0);
52 pause_out : out std_logic);
55 architecture logic of mult is
57 constant MODE_MULT : std_logic := '1';
58 constant MODE_DIV : std_logic := '0';
60 signal mode_reg : std_logic;
61 signal negate_reg : std_logic;
62 signal sign_reg : std_logic;
63 signal sign2_reg : std_logic;
64 signal count_reg : std_logic_vector(5 downto 0);
65 signal aa_reg : std_logic_vector(31 downto 0);
66 signal bb_reg : std_logic_vector(31 downto 0);
67 signal upper_reg : std_logic_vector(31 downto 0);
68 signal lower_reg : std_logic_vector(31 downto 0);
70 signal a_neg : std_logic_vector(31 downto 0);
71 signal b_neg : std_logic_vector(31 downto 0);
72 signal sum : std_logic_vector(32 downto 0);
77 c_mult <= lower_reg when mult_func = MULT_READ_LO and negate_reg = '0' else
78 bv_negate(lower_reg) when mult_func = MULT_READ_LO
79 and negate_reg = '1' else
80 upper_reg when mult_func = MULT_READ_HI else
82 pause_out <= '1' when (count_reg /= "000000") and
83 (mult_func = MULT_READ_LO or mult_func = MULT_READ_HI) else '0';
85 -- ABS and remainder signals
86 a_neg <= bv_negate(a);
87 b_neg <= bv_negate(b);
88 sum <= bv_adder(upper_reg, aa_reg, mode_reg);
90 --multiplication/division unit
91 mult_proc: process(clk, reset_in, a, b, mult_func,
92 a_neg, b_neg, sum, sign_reg, mode_reg, negate_reg,
93 count_reg, aa_reg, bb_reg, upper_reg, lower_reg)
94 variable count : std_logic_vector(2 downto 0);
97 if reset_in = '1' then
102 count_reg <= "000000";
107 elsif rising_edge(clk) then
109 when MULT_WRITE_LO =>
112 when MULT_WRITE_HI =>
116 mode_reg <= MODE_MULT;
120 count_reg <= "100000";
124 when MULT_SIGNED_MULT =>
125 mode_reg <= MODE_MULT;
133 sign_reg <= a(31) xor b(31);
136 count_reg <= "100000";
139 mode_reg <= MODE_DIV;
140 aa_reg <= b(0) & ZERO(30 downto 0);
143 count_reg <= "100000";
145 when MULT_SIGNED_DIVIDE =>
146 mode_reg <= MODE_DIV;
151 aa_reg(31) <= b_neg(0);
159 aa_reg(30 downto 0) <= ZERO(30 downto 0);
160 count_reg <= "100000";
161 negate_reg <= a(31) xor b(31);
164 if count_reg /= "000000" then
165 if mode_reg = MODE_MULT then
167 if bb_reg(0) = '1' then
168 upper_reg <= (sign_reg xor sum(32)) & sum(31 downto 1);
169 lower_reg <= sum(0) & lower_reg(31 downto 1);
170 sign2_reg <= sign2_reg or sign_reg;
172 bb_reg <= '0' & bb_reg(31 downto 1);
173 -- The following six lines are optional for speedup
174 --elsif bb_reg(3 downto 0) = "0000" and sign2_reg = '0' and
175 -- count_reg(5 downto 2) /= "0000" then
176 -- upper_reg <= "0000" & upper_reg(31 downto 4);
177 -- lower_reg <= upper_reg(3 downto 0) & lower_reg(31 downto 4);
179 -- bb_reg <= "0000" & bb_reg(31 downto 4);
181 upper_reg <= sign2_reg & upper_reg(31 downto 1);
182 lower_reg <= upper_reg(0) & lower_reg(31 downto 1);
183 bb_reg <= '0' & bb_reg(31 downto 1);
187 if sum(32) = '0' and aa_reg /= ZERO and
188 bb_reg(31 downto 1) = ZERO(31 downto 1) then
189 upper_reg <= sum(31 downto 0);
194 aa_reg <= bb_reg(1) & aa_reg(31 downto 1);
195 lower_reg(31 downto 1) <= lower_reg(30 downto 0);
196 bb_reg <= '0' & bb_reg(31 downto 1);
198 count_reg <= count_reg - count;
207 end; --architecture logic