OpenCores
URL https://opencores.org/ocsvn/s6soc/s6soc/trunk

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [rtl/] [cpu/] [cpuops.v] - Blame information for rev 46

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 46 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 2 dgisselq
//
3
// Filename:    cpuops.v
4
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU soft core
6
//
7
// Purpose:     This supports the instruction set reordering of operations
8
//              created by the second generation instruction set, as well as
9
//      the new operations of POPC (population count) and BREV (bit reversal).
10
//
11
//
12
// Creator:     Dan Gisselquist, Ph.D.
13
//              Gisselquist Technology, LLC
14
//
15 46 dgisselq
////////////////////////////////////////////////////////////////////////////////
16 2 dgisselq
//
17 46 dgisselq
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
18 2 dgisselq
//
19
// This program is free software (firmware): you can redistribute it and/or
20
// modify it under the terms of  the GNU General Public License as published
21
// by the Free Software Foundation, either version 3 of the License, or (at
22
// your option) any later version.
23
//
24
// This program is distributed in the hope that it will be useful, but WITHOUT
25
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
26
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
27
// for more details.
28
//
29 46 dgisselq
// You should have received a copy of the GNU General Public License along
30
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
31
// target there if the PDF file isn't present.)  If not, see
32
// <http://www.gnu.org/licenses/> for a copy.
33
//
34 2 dgisselq
// License:     GPL, v3, as defined and found on www.gnu.org,
35
//              http://www.gnu.org/licenses/gpl.html
36
//
37
//
38 46 dgisselq
////////////////////////////////////////////////////////////////////////////////
39 2 dgisselq
//
40 46 dgisselq
`include "cpudefs.v"
41
//
42
module  cpuops(i_clk,i_rst, i_ce, i_op, i_a, i_b, o_c, o_f, o_valid,
43
                        o_busy);
44
        parameter       IMPLEMENT_MPY = `OPT_MULTIPLY;
45 2 dgisselq
        input           i_clk, i_rst, i_ce;
46
        input           [3:0]    i_op;
47
        input           [31:0]   i_a, i_b;
48
        output  reg     [31:0]   o_c;
49
        output  wire    [3:0]    o_f;
50
        output  reg             o_valid;
51
        output  wire            o_busy;
52
 
53
        // Shift register pre-logic
54 46 dgisselq
        wire    [32:0]           w_lsr_result, w_asr_result, w_lsl_result;
55
        wire    signed  [32:0]   w_pre_asr_input, w_pre_asr_shifted;
56
        assign  w_pre_asr_input = { i_a, 1'b0 };
57
        assign  w_pre_asr_shifted = w_pre_asr_input >>> i_b[4:0];
58 2 dgisselq
        assign  w_asr_result = (|i_b[31:5])? {(33){i_a[31]}}
59 46 dgisselq
                                : w_pre_asr_shifted;// ASR
60
        assign  w_lsr_result = ((|i_b[31:6])||(i_b[5]&&(i_b[4:0]!=0)))? 33'h00
61
                                :((i_b[5])?{32'h0,i_a[31]}
62
 
63
                                : ( { i_a, 1'b0 } >> (i_b[4:0]) ));// LSR
64
        assign  w_lsl_result = ((|i_b[31:6])||(i_b[5]&&(i_b[4:0]!=0)))? 33'h00
65
                                :((i_b[5])?{i_a[0], 32'h0}
66
                                : ({1'b0, i_a } << i_b[4:0]));   // LSL
67 2 dgisselq
 
68
        // Bit reversal pre-logic
69
        wire    [31:0]   w_brev_result;
70
        genvar  k;
71
        generate
72
        for(k=0; k<32; k=k+1)
73
        begin : bit_reversal_cpuop
74
                assign w_brev_result[k] = i_b[31-k];
75
        end endgenerate
76
 
77
        // Prelogic for our flags registers
78
        wire    z, n, v;
79 46 dgisselq
        reg     c, pre_sign, set_ovfl, keep_sgn_on_ovfl;
80 2 dgisselq
        always @(posedge i_clk)
81
                if (i_ce) // 1 LUT
82 46 dgisselq
                        set_ovfl<=(((i_op==4'h0)&&(i_a[31] != i_b[31]))//SUB&CMP
83 2 dgisselq
                                ||((i_op==4'h2)&&(i_a[31] == i_b[31])) // ADD
84
                                ||(i_op == 4'h6) // LSL
85
                                ||(i_op == 4'h5)); // LSR
86 46 dgisselq
        always @(posedge i_clk)
87
                if (i_ce) // 1 LUT
88
                        keep_sgn_on_ovfl<=
89
                                (((i_op==4'h0)&&(i_a[31] != i_b[31]))//SUB&CMP
90
                                ||((i_op==4'h2)&&(i_a[31] == i_b[31]))); // ADD
91 2 dgisselq
 
92 46 dgisselq
        wire    [63:0]   mpy_result; // Where we dump the multiply result
93
        reg     mpyhi;          // Return the high half of the multiply
94
        wire    mpybusy;        // The multiply is busy if true
95
        wire    mpydone;        // True if we'll be valid on the next clock;
96 2 dgisselq
 
97
        // A 4-way multiplexer can be done in one 6-LUT.
98
        // A 16-way multiplexer can therefore be done in 4x 6-LUT's with
99
        //      the Xilinx multiplexer fabric that follows. 
100
        // Given that we wish to apply this multiplexer approach to 33-bits,
101
        // this will cost a minimum of 132 6-LUTs.
102 46 dgisselq
 
103
        wire    this_is_a_multiply_op;
104
        assign  this_is_a_multiply_op = (i_ce)&&((i_op[3:1]==3'h5)||(i_op[3:0]==4'hc));
105
 
106 2 dgisselq
        generate
107
        if (IMPLEMENT_MPY == 0)
108 46 dgisselq
        begin // No multiply support.
109
                assign  mpy_result = 63'h00;
110
        end else if (IMPLEMENT_MPY == 1)
111
        begin // Our single clock option (no extra clocks)
112
                wire    signed  [63:0]   w_mpy_a_input, w_mpy_b_input;
113
                assign  w_mpy_a_input = {{(32){(i_a[31])&(i_op[0])}},i_a[31:0]};
114
                assign  w_mpy_b_input = {{(32){(i_b[31])&(i_op[0])}},i_b[31:0]};
115
                assign  mpy_result = w_mpy_a_input * w_mpy_b_input;
116
                assign  mpybusy = 1'b0;
117
                assign  mpydone = 1'b0;
118
                always @(*) mpyhi = 1'b0; // Not needed
119
        end else if (IMPLEMENT_MPY == 2)
120
        begin // Our two clock option (ALU must pause for 1 clock)
121
                reg     signed  [63:0]   r_mpy_a_input, r_mpy_b_input;
122 2 dgisselq
                always @(posedge i_clk)
123
                begin
124 46 dgisselq
                        r_mpy_a_input <={{(32){(i_a[31])&(i_op[0])}},i_a[31:0]};
125
                        r_mpy_b_input <={{(32){(i_b[31])&(i_op[0])}},i_b[31:0]};
126 2 dgisselq
                end
127
 
128 46 dgisselq
                assign  mpy_result = r_mpy_a_input * r_mpy_b_input;
129
                assign  mpybusy = 1'b0;
130 2 dgisselq
 
131 46 dgisselq
                initial mpypipe = 1'b0;
132
                reg     mpypipe;
133 2 dgisselq
                always @(posedge i_clk)
134 46 dgisselq
                        if (i_rst)
135
                                mpypipe <= 1'b0;
136
                        else
137
                                mpypipe <= (this_is_a_multiply_op);
138
 
139
                assign  mpydone = mpypipe; // this_is_a_multiply_op;
140
                always @(posedge i_clk)
141
                        if (this_is_a_multiply_op)
142
                                mpyhi  = i_op[1];
143
        end else if (IMPLEMENT_MPY == 3)
144
        begin // Our three clock option (ALU pauses for 2 clocks)
145
                reg     signed  [63:0]   r_smpy_result;
146
                reg             [63:0]   r_umpy_result;
147
                reg     signed  [31:0]   r_mpy_a_input, r_mpy_b_input;
148
                reg             [1:0]    mpypipe;
149
                reg             [1:0]    r_sgn;
150
 
151
                initial mpypipe = 2'b0;
152
                always @(posedge i_clk)
153
                        if (i_rst)
154
                                mpypipe <= 2'b0;
155
                        else
156
                        mpypipe <= { mpypipe[0], this_is_a_multiply_op };
157
 
158
                // First clock
159
                always @(posedge i_clk)
160
                begin
161
                        r_mpy_a_input <= i_a[31:0];
162
                        r_mpy_b_input <= i_b[31:0];
163
                        r_sgn <= { r_sgn[0], i_op[0] };
164
                end
165
 
166
                // Second clock
167
`ifdef  VERILATOR
168
                wire    signed  [63:0]   s_mpy_a_input, s_mpy_b_input;
169
                wire            [63:0]   u_mpy_a_input, u_mpy_b_input;
170
 
171
                assign  s_mpy_a_input = {{(32){r_mpy_a_input[31]}},r_mpy_a_input};
172
                assign  s_mpy_b_input = {{(32){r_mpy_b_input[31]}},r_mpy_b_input};
173
                assign  u_mpy_a_input = {32'h00,r_mpy_a_input};
174
                assign  u_mpy_b_input = {32'h00,r_mpy_b_input};
175
                always @(posedge i_clk)
176
                        r_smpy_result = s_mpy_a_input * s_mpy_b_input;
177
                always @(posedge i_clk)
178
                        r_umpy_result = u_mpy_a_input * u_mpy_b_input;
179
`else
180
 
181
                wire            [31:0]   u_mpy_a_input, u_mpy_b_input;
182
 
183
                assign  u_mpy_a_input = r_mpy_a_input;
184
                assign  u_mpy_b_input = r_mpy_b_input;
185
 
186
                always @(posedge i_clk)
187
                        r_smpy_result = r_mpy_a_input * r_mpy_b_input;
188
                always @(posedge i_clk)
189
                        r_umpy_result = u_mpy_a_input * u_mpy_b_input;
190 7 dgisselq
`endif
191 46 dgisselq
 
192
                always @(posedge i_clk)
193
                        if (this_is_a_multiply_op)
194
                                mpyhi  = i_op[1];
195
                assign  mpybusy = mpypipe[0];
196
                assign  mpy_result = (r_sgn[1])?r_smpy_result:r_umpy_result;
197
                assign  mpydone = mpypipe[1];
198
 
199
                // Results are then set on the third clock
200
        end else // if (IMPLEMENT_MPY <= 4)
201
        begin // The three clock option
202 7 dgisselq
                reg     [63:0]   r_mpy_result;
203 46 dgisselq
                reg     [31:0]   r_mpy_a_input, r_mpy_b_input;
204
                reg             r_mpy_signed;
205
                reg     [2:0]    mpypipe;
206 7 dgisselq
 
207 46 dgisselq
                // First clock, latch in the inputs
208
                always @(posedge i_clk)
209
                begin
210
                        // mpypipe indicates we have a multiply in the
211
                        // pipeline.  In this case, the multiply
212
                        // pipeline is a two stage pipeline, so we need 
213
                        // two bits in the pipe.
214
                        if (i_rst)
215
                                mpypipe <= 3'h0;
216
                        else begin
217
                                mpypipe[0] <= this_is_a_multiply_op;
218 7 dgisselq
                                mpypipe[1] <= mpypipe[0];
219 46 dgisselq
                                mpypipe[2] <= mpypipe[1];
220 7 dgisselq
                        end
221
 
222 46 dgisselq
                        if (i_op[0]) // i.e. if signed multiply
223 7 dgisselq
                        begin
224 46 dgisselq
                                r_mpy_a_input <= {(~i_a[31]),i_a[30:0]};
225
                                r_mpy_b_input <= {(~i_b[31]),i_b[30:0]};
226
                        end else begin
227
                                r_mpy_a_input <= i_a[31:0];
228
                                r_mpy_b_input <= i_b[31:0];
229 7 dgisselq
                        end
230 46 dgisselq
                        // The signed bit really only matters in the
231
                        // case of 64 bit multiply.  We'll keep track
232
                        // of it, though, and pretend in all other
233
                        // cases.
234
                        r_mpy_signed  <= i_op[0];
235 7 dgisselq
 
236 46 dgisselq
                        if (this_is_a_multiply_op)
237
                                mpyhi  = i_op[1];
238
                end
239 2 dgisselq
 
240 46 dgisselq
                assign  mpybusy = |mpypipe[1:0];
241
                assign  mpydone = mpypipe[2];
242
 
243
                // Second clock, do the multiplies, get the "partial
244
                // products".  Here, we break our input up into two
245
                // halves, 
246 2 dgisselq
                //
247 46 dgisselq
                //   A  = (2^16 ah + al)
248
                //   B  = (2^16 bh + bl)
249 2 dgisselq
                //
250 46 dgisselq
                // and use these to compute partial products.
251
                //
252
                //   AB = (2^32 ah*bh + 2^16 (ah*bl + al*bh) + (al*bl)
253
                //
254
                // Since we're following the FOIL algorithm to get here,
255
                // we'll name these partial products according to FOIL.
256
                //
257
                // The trick is what happens if A or B is signed.  In
258
                // those cases, the real value of A will not be given by
259
                //      A = (2^16 ah + al)
260
                // but rather
261
                //      A = (2^16 ah[31^] + al) - 2^31
262
                //  (where we have flipped the sign bit of A)
263
                // and so ...
264
                //
265
                // AB= (2^16 ah + al - 2^31) * (2^16 bh + bl - 2^31)
266
                //      = 2^32(ah*bh)
267
                //              +2^16 (ah*bl+al*bh)
268
                //              +(al*bl)
269
                //              - 2^31 (2^16 bh+bl + 2^16 ah+al)
270
                //              - 2^62
271
                //      = 2^32(ah*bh)
272
                //              +2^16 (ah*bl+al*bh)
273
                //              +(al*bl)
274
                //              - 2^31 (2^16 bh+bl + 2^16 ah+al + 2^31)
275
                //
276
                reg     [31:0]   pp_f, pp_l; // F and L from FOIL
277
                reg     [32:0]   pp_oi; // The O and I from FOIL
278
                reg     [32:0]   pp_s;
279 2 dgisselq
                always @(posedge i_clk)
280
                begin
281 46 dgisselq
                        pp_f<=r_mpy_a_input[31:16]*r_mpy_b_input[31:16];
282
                        pp_oi<=r_mpy_a_input[31:16]*r_mpy_b_input[15: 0]
283
                                + r_mpy_a_input[15: 0]*r_mpy_b_input[31:16];
284
                        pp_l<=r_mpy_a_input[15: 0]*r_mpy_b_input[15: 0];
285
                        // And a special one for the sign
286
                        if (r_mpy_signed)
287
                                pp_s <= 32'h8000_0000-(
288
                                        r_mpy_a_input[31:0]
289
                                        + r_mpy_b_input[31:0]);
290
                        else
291
                                pp_s <= 33'h0;
292
                end
293 2 dgisselq
 
294 46 dgisselq
                // Third clock, add the results and produce a product
295 2 dgisselq
                always @(posedge i_clk)
296 46 dgisselq
                begin
297
                        r_mpy_result[15:0] <= pp_l[15:0];
298
                        r_mpy_result[63:16] <=
299
                                { 32'h00, pp_l[31:16] }
300
                                + { 15'h00, pp_oi }
301
                                + { pp_s, 15'h00 }
302
                                + { pp_f, 16'h00 };
303
                end
304 2 dgisselq
 
305 46 dgisselq
                assign  mpy_result = r_mpy_result;
306
                // Fourth clock -- results are clocked into writeback
307
        end
308
        endgenerate // All possible multiply results have been determined
309 2 dgisselq
 
310 46 dgisselq
        //
311
        // The master ALU case statement
312
        //
313
        always @(posedge i_clk)
314
        if (i_ce)
315
        begin
316
                pre_sign <= (i_a[31]);
317
                c <= 1'b0;
318
                casez(i_op)
319
                4'b0000:{c,o_c } <= {1'b0,i_a}-{1'b0,i_b};// CMP/SUB
320
                4'b0001:   o_c   <= i_a & i_b;          // BTST/And
321
                4'b0010:{c,o_c } <= i_a + i_b;          // Add
322
                4'b0011:   o_c   <= i_a | i_b;          // Or
323
                4'b0100:   o_c   <= i_a ^ i_b;          // Xor
324
                4'b0101:{o_c,c } <= w_lsr_result[32:0];  // LSR
325
                4'b0110:{c,o_c } <= w_lsl_result[32:0]; // LSL
326
                4'b0111:{o_c,c } <= w_asr_result[32:0];  // ASR
327
                4'b1000:   o_c   <= w_brev_result;      // BREV
328
                4'b1001:   o_c   <= { i_a[31:16], i_b[15:0] }; // LODILO
329
                4'b1010:   o_c   <= mpy_result[63:32];  // MPYHU
330
                4'b1011:   o_c   <= mpy_result[63:32];  // MPYHS
331
                4'b1100:   o_c   <= mpy_result[31:0];    // MPY
332
                default:   o_c   <= i_b;                // MOV, LDI
333
                endcase
334
        end else // if (mpydone)
335
                o_c <= (mpyhi)?mpy_result[63:32]:mpy_result[31:0];
336 2 dgisselq
 
337 46 dgisselq
        reg     r_busy;
338
        initial r_busy = 1'b0;
339
        always @(posedge i_clk)
340
                if (i_rst)
341
                        r_busy <= 1'b0;
342
                else
343
                        r_busy <= ((IMPLEMENT_MPY > 1)
344
                                        &&(this_is_a_multiply_op))||mpybusy;
345
        assign  o_busy = (r_busy); // ||((IMPLEMENT_MPY>1)&&(this_is_a_multiply_op));
346
 
347
 
348 2 dgisselq
        assign  z = (o_c == 32'h0000);
349
        assign  n = (o_c[31]);
350
        assign  v = (set_ovfl)&&(pre_sign != o_c[31]);
351 46 dgisselq
        wire    vx = (keep_sgn_on_ovfl)&&(pre_sign != o_c[31]);
352 2 dgisselq
 
353 46 dgisselq
        assign  o_f = { v, n^vx, c, z };
354 2 dgisselq
 
355
        initial o_valid = 1'b0;
356
        always @(posedge i_clk)
357
                if (i_rst)
358
                        o_valid <= 1'b0;
359 46 dgisselq
                else if (IMPLEMENT_MPY <= 1)
360
                        o_valid <= (i_ce);
361 2 dgisselq
                else
362 46 dgisselq
                        o_valid <=((i_ce)&&(!this_is_a_multiply_op))||(mpydone);
363
 
364 2 dgisselq
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.