second version of multiplier (slower, i.e. more stages -> faster clock)
Stefan Schuermans

Stefan Schuermans commited on 2012-02-05 21:39:45
Showing 3 changed files, with 159 additions and 4 deletions.

... ...
@@ -0,0 +1,155 @@
1
+LIBRARY ieee;
2
+USE ieee.std_logic_1164.all;
3
+USE ieee.numeric_std.all;
4
+USE work.mips_types.all;
5
+
6
+ENTITY e_mips_mul IS
7
+    PORT (
8
+        rst:      IN  std_logic;
9
+        clk:      IN  std_logic;
10
+        i_a:      IN  std_logic_vector(31 DOWNTO 0);
11
+        i_b:      IN  std_logic_vector(31 DOWNTO 0);
12
+        i_signed: IN  std_logic;
13
+        i_start:  IN  std_logic;
14
+        o_busy:   OUT std_logic;
15
+        o_res:    OUT std_logic_vector(63 DOWNTO 0)
16
+    );
17
+END ENTITY e_mips_mul;
18
+
19
+ARCHITECTURE a_mips_mul OF e_mips_mul IS
20
+
21
+    TYPE t_state IS (idle, mul1, add1, mul2, add2, mul3, add3, mul4, add4,
22
+                     post);
23
+    SIGNAL n_state: t_state;
24
+    SIGNAL r_state: t_state;
25
+
26
+    SIGNAL n_a:   unsigned(31 DOWNTO 0);
27
+    SIGNAL n_b:   unsigned(31 DOWNTO 0);
28
+    SIGNAL n_neg: boolean;
29
+    SIGNAL n_r:   unsigned(31 DOWNTO 0);
30
+    SIGNAL n_res: unsigned(63 DOWNTO 0);
31
+    SIGNAL r_a:   unsigned(31 DOWNTO 0);
32
+    SIGNAL r_b:   unsigned(31 DOWNTO 0);
33
+    SIGNAL r_neg: boolean;
34
+    SIGNAL r_r:   unsigned(31 DOWNTO 0);
35
+    SIGNAL r_res: unsigned(63 DOWNTO 0);
36
+
37
+BEGIN
38
+
39
+    p_mul: PROCESS(r_state, r_a, r_b, r_neg, r_r, r_res,
40
+                   i_a, i_b, i_signed, i_start)
41
+        VARIABLE v_a:   unsigned(15 DOWNTO 0);
42
+        VARIABLE v_b:   unsigned(15 DOWNTO 0);
43
+        VARIABLE v_res: unsigned(31 DOWNTO 0);
44
+        VARIABLE v_add: unsigned(63 DOWNTO 0);
45
+        VARIABLE v_sum: unsigned(63 DOWNTO 0);
46
+    BEGIN
47
+        o_busy  <= '0';
48
+        n_state <= idle;
49
+        n_a     <= r_a;
50
+        n_b     <= r_b;
51
+        n_neg   <= r_neg;
52
+        n_res   <= r_res;
53
+        v_a     := (OTHERS => '0');
54
+        v_b     := (OTHERS => '0');
55
+        v_add   := (OTHERS => '0');
56
+        CASE r_state IS
57
+            WHEN idle =>
58
+                IF i_start = '1' THEN
59
+                    o_busy  <= '1';
60
+                    n_state <= mul1;
61
+                    IF i_signed = '1' THEN
62
+                        IF i_a(31) = '1' THEN
63
+                            n_a <= unsigned(-signed(i_a));
64
+                        ELSE
65
+                            n_a <= unsigned(i_a);
66
+                        END IF;
67
+                        IF i_b(31) = '1' THEN
68
+                            n_b <= unsigned(-signed(i_b));
69
+                        ELSE
70
+                            n_b <= unsigned(i_b);
71
+                        END IF;
72
+                        n_neg <= i_a(31) = '1' XOR i_b(31) = '1';
73
+                    ELSE
74
+                        n_a   <= unsigned(i_a);
75
+                        n_b   <= unsigned(i_b);
76
+                        n_neg <= false;
77
+                    END IF;
78
+                END IF;
79
+            WHEN mul1 =>
80
+                o_busy  <= '1';
81
+                n_state <= add1;
82
+                v_a     := r_a(15 DOWNTO  0);
83
+                v_b     := r_b(15 DOWNTO  0);
84
+            WHEN add1 =>
85
+                o_busy  <= '1';
86
+                n_state <= mul2;
87
+                v_add := X"00000000" & r_r;
88
+            WHEN mul2 =>
89
+                o_busy  <= '1';
90
+                n_state <= add2;
91
+                v_a     := r_a(31 DOWNTO 16);
92
+                v_b     := r_b(15 DOWNTO  0);
93
+            WHEN add2 =>
94
+                o_busy  <= '1';
95
+                n_state <= mul3;
96
+                v_add := X"0000" & r_r & X"0000";
97
+            WHEN mul3 =>
98
+                o_busy  <= '1';
99
+                n_state <= add3;
100
+                v_a     := r_a(15 DOWNTO  0);
101
+                v_b     := r_b(31 DOWNTO 16);
102
+            WHEN add3 =>
103
+                o_busy  <= '1';
104
+                n_state <= mul4;
105
+                v_add := X"0000" & r_r & X"0000";
106
+            WHEN mul4 =>
107
+                o_busy  <= '1';
108
+                n_state <= add4;
109
+                v_a     := r_a(31 DOWNTO 16);
110
+                v_b     := r_b(31 DOWNTO 16);
111
+            WHEN add4 =>
112
+                o_busy  <= '1';
113
+                n_state <= post;
114
+                v_add := r_r & X"00000000";
115
+            WHEN OTHERS => NULL;
116
+        END CASE;
117
+        n_r <= v_a * v_b;
118
+        v_sum := r_res + v_add;
119
+        CASE r_state IS
120
+            WHEN idle =>
121
+                n_res <= (OTHERS => '0');
122
+            WHEN add1 | add2 | add3 | add4 =>
123
+                n_res <= v_sum;
124
+            WHEN post =>
125
+                IF r_neg THEN
126
+                    n_res <= unsigned(-signed(r_res));
127
+                ELSE
128
+                    n_res <= r_res;
129
+                END IF;
130
+            WHEN OTHERS => NULL;
131
+        END CASE;
132
+    END PROCESS p_mul;
133
+
134
+    p_sync: PROCESS(rst, clk)
135
+    BEGIN
136
+        IF rst = '1' THEN
137
+            r_state <= idle;
138
+            r_a     <= (OTHERS => '0');
139
+            r_b     <= (OTHERS => '0');
140
+            r_neg   <= false;
141
+            r_r     <= (OTHERS => '0');
142
+            r_res   <= (OTHERS => '0');
143
+        ELSIF rising_edge(clk) THEN
144
+            r_state <= n_state;
145
+            r_a     <= n_a;
146
+            r_b     <= n_b;
147
+            r_neg   <= n_neg;
148
+            r_r     <= n_r;
149
+            r_res   <= n_res;
150
+        END IF;
151
+    END PROCESS p_sync;
152
+
153
+    o_res <= std_logic_vector(n_res);
154
+
155
+END ARCHITECTURE a_mips_mul;
... ...
@@ -49,14 +49,14 @@
49 49
       <association xil_pn:name="BehavioralSimulation" xil_pn:seqID="89"/>
50 50
       <association xil_pn:name="Implementation" xil_pn:seqID="7"/>
51 51
     </file>
52
-    <file xil_pn:name="mips/mul.vhd" xil_pn:type="FILE_VHDL">
53
-      <association xil_pn:name="BehavioralSimulation" xil_pn:seqID="93"/>
54
-      <association xil_pn:name="Implementation" xil_pn:seqID="4"/>
55
-    </file>
56 52
     <file xil_pn:name="mips/div.vhd" xil_pn:type="FILE_VHDL">
57 53
       <association xil_pn:name="BehavioralSimulation" xil_pn:seqID="97"/>
58 54
       <association xil_pn:name="Implementation" xil_pn:seqID="5"/>
59 55
     </file>
56
+    <file xil_pn:name="mips/mul_slow.vhd" xil_pn:type="FILE_VHDL">
57
+      <association xil_pn:name="BehavioralSimulation" xil_pn:seqID="98"/>
58
+      <association xil_pn:name="Implementation" xil_pn:seqID="98"/>
59
+    </file>
60 60
   </files>
61 61
 
62 62
   <properties>
63 63