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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [rtl/] [verilog/] [or1200/] [or1200_mult_mac.v] - Blame information for rev 522

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

Line No. Rev Author Line
1 350 julius
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  OR1200's Top level multiplier, divider and MAC              ////
4
////                                                              ////
5
////  This file is part of the OpenRISC 1200 project              ////
6
////  http://opencores.org/project,or1k                           ////
7
////                                                              ////
8
////  Description                                                 ////
9
////  Multiplier is 32x32 however multiply instructions only      ////
10
////  use lower 32 bits of the result. MAC is 32x32=64+64.        ////
11
////                                                              ////
12
////  To Do:                                                      ////
13
////   - make signed division better, w/o negating the operands   ////
14 435 julius
////   - implement non-serial divider that is synthesizable       ////
15 350 julius
////                                                              ////
16
////  Author(s):                                                  ////
17
////      - Damjan Lampret, lampret@opencores.org                 ////
18 435 julius
////      - Julius Baxter, julius@opencores.org                   ////
19 350 julius
////                                                              ////
20
//////////////////////////////////////////////////////////////////////
21
////                                                              ////
22 435 julius
//// Copyright (C) 2000, 2010 Authors and OPENCORES.ORG           ////
23 350 julius
////                                                              ////
24
//// This source file may be used and distributed without         ////
25
//// restriction provided that this copyright statement is not    ////
26
//// removed from the file and that any derivative work contains  ////
27
//// the original copyright notice and the associated disclaimer. ////
28
////                                                              ////
29
//// This source file is free software; you can redistribute it   ////
30
//// and/or modify it under the terms of the GNU Lesser General   ////
31
//// Public License as published by the Free Software Foundation; ////
32
//// either version 2.1 of the License, or (at your option) any   ////
33
//// later version.                                               ////
34
////                                                              ////
35
//// This source is distributed in the hope that it will be       ////
36
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
37
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
38
//// PURPOSE.  See the GNU Lesser General Public License for more ////
39
//// details.                                                     ////
40
////                                                              ////
41
//// You should have received a copy of the GNU Lesser General    ////
42
//// Public License along with this source; if not, download it   ////
43
//// from http://www.opencores.org/lgpl.shtml                     ////
44
////                                                              ////
45
//////////////////////////////////////////////////////////////////////
46
//
47
// CVS Revision History
48
//
49
// $Log: or1200_mult_mac.v,v $
50
// Revision 2.0  2010/06/30 11:00:00  ORSoC
51
// Minor update: 
52
// Bugs fixed. 
53
//
54
 
55
// synopsys translate_off
56
`include "timescale.v"
57
// synopsys translate_on
58
`include "or1200_defines.v"
59
 
60
module or1200_mult_mac(
61 435 julius
                       // Clock and reset
62
                       clk, rst,
63 350 julius
 
64 435 julius
                       // Multiplier/MAC interface
65
                       ex_freeze, id_macrc_op, macrc_op, a, b, mac_op, alu_op,
66
                       result, mult_mac_stall,
67 350 julius
 
68 502 julius
                       // Overflow
69
                       ovforw, ov_we,
70
 
71 435 julius
                       // SPR interface
72
                       spr_cs, spr_write, spr_addr, spr_dat_i, spr_dat_o
73
                       );
74 350 julius
 
75 435 julius
   parameter width = `OR1200_OPERAND_WIDTH;
76 350 julius
 
77 435 julius
   //
78
   // I/O
79
   //
80 350 julius
 
81 435 julius
   //
82
   // Clock and reset
83
   //
84
   input                                clk;
85
   input                                rst;
86 350 julius
 
87 435 julius
   //
88
   // Multiplier/MAC interface
89
   //
90
   input                                ex_freeze;
91
   input                                id_macrc_op;
92
   input                                macrc_op;
93
   input [width-1:0]                     a;
94
   input [width-1:0]                     b;
95
   input [`OR1200_MACOP_WIDTH-1:0]       mac_op;
96
   input [`OR1200_ALUOP_WIDTH-1:0]       alu_op;
97
   output [width-1:0]                    result;
98
   output                               mult_mac_stall;
99 502 julius
   output                               ovforw, ov_we;
100
 
101 435 julius
   //
102
   // SPR interface
103
   //
104
   input                                spr_cs;
105
   input                                spr_write;
106
   input [31:0]                  spr_addr;
107
   input [31:0]                  spr_dat_i;
108
   output [31:0]                         spr_dat_o;
109 350 julius
 
110 435 julius
   //
111
   // Internal wires and regs
112
   //
113
   reg [width-1:0]                       result;
114 499 julius
   reg                                  ex_freeze_r;
115 350 julius
`ifdef OR1200_MULT_IMPLEMENTED
116 435 julius
   reg [2*width-1:0]                     mul_prod_r;
117
   wire                                 alu_op_smul;
118
   wire                                 alu_op_umul;
119
   wire                                 alu_op_mul;
120
 `ifdef OR1200_MULT_SERIAL
121
   reg [5:0]                             serial_mul_cnt;
122
   reg                                  mul_free;
123
 `endif
124 350 julius
`else
125 435 julius
   wire [2*width-1:0]                    mul_prod_r;
126 350 julius
`endif
127 435 julius
   wire [2*width-1:0]                    mul_prod;
128
   wire                                 mul_stall;
129 499 julius
   reg [1:0]                             mul_stall_count;
130 435 julius
   wire [`OR1200_MACOP_WIDTH-1:0]        mac_op;
131 350 julius
`ifdef OR1200_MAC_IMPLEMENTED
132 435 julius
   reg [`OR1200_MACOP_WIDTH-1:0]         mac_op_r1;
133
   reg [`OR1200_MACOP_WIDTH-1:0]         mac_op_r2;
134
   reg [`OR1200_MACOP_WIDTH-1:0]         mac_op_r3;
135
   reg                                  mac_stall_r;
136
   reg [63:0]                            mac_r;
137 350 julius
`else
138 435 julius
   wire [`OR1200_MACOP_WIDTH-1:0]        mac_op_r1;
139
   wire [`OR1200_MACOP_WIDTH-1:0]        mac_op_r2;
140
   wire [`OR1200_MACOP_WIDTH-1:0]        mac_op_r3;
141
   wire                                 mac_stall_r;
142
   wire [63:0]                           mac_r;
143 350 julius
`endif
144 435 julius
   wire [width-1:0]                      x;
145
   wire [width-1:0]                      y;
146
   wire                                 spr_maclo_we;
147
   wire                                 spr_machi_we;
148
   wire                                 alu_op_div;
149
   wire                                 alu_op_udiv;
150
   wire                                 alu_op_sdiv;
151
   reg                                  div_free;
152
   wire                                 div_stall;
153 350 julius
`ifdef OR1200_DIV_IMPLEMENTED
154 435 julius
 `ifdef OR1200_DIV_SERIAL
155
   reg [2*width-1:0]                     div_quot_r;
156
   wire [width-1:0]                      div_tmp;
157
   reg [5:0]                             div_cntr;
158
 `else
159
   reg [width-1:0]                       div_quot_r;
160
   reg [width-1:0]                       div_quot_generic;
161 502 julius
 `endif
162
   wire                                 div_by_zero;
163 350 julius
`endif
164 502 julius
   reg                                  ovforw, ov_we;
165
 
166 435 julius
   //
167
   // Combinatorial logic
168
   //
169
`ifdef OR1200_MULT_IMPLEMENTED
170
   assign alu_op_smul = (alu_op == `OR1200_ALUOP_MUL);
171
   assign alu_op_umul = (alu_op == `OR1200_ALUOP_MULU);
172
   assign alu_op_mul = alu_op_smul | alu_op_umul;
173
`endif
174 350 julius
`ifdef OR1200_MAC_IMPLEMENTED
175 435 julius
   assign spr_maclo_we = spr_cs & spr_write & spr_addr[`OR1200_MAC_ADDR];
176
   assign spr_machi_we = spr_cs & spr_write & !spr_addr[`OR1200_MAC_ADDR];
177
   assign spr_dat_o = spr_addr[`OR1200_MAC_ADDR] ? mac_r[31:0] : mac_r[63:32];
178 350 julius
`else
179 435 julius
   assign spr_maclo_we = 1'b0;
180
   assign spr_machi_we = 1'b0;
181
   assign spr_dat_o = 32'h0000_0000;
182 350 julius
`endif
183
`ifdef OR1200_DIV_IMPLEMENTED
184 435 julius
   assign alu_op_sdiv = (alu_op == `OR1200_ALUOP_DIV);
185
   assign alu_op_udiv = (alu_op == `OR1200_ALUOP_DIVU);
186
   assign alu_op_div = alu_op_sdiv | alu_op_udiv;
187 350 julius
`else
188 435 julius
   assign alu_op_udiv = 1'b0;
189
   assign alu_op_sdiv = 1'b0;
190
   assign alu_op_div = 1'b0;
191 350 julius
`endif
192
 
193 435 julius
   assign x = (alu_op_sdiv | alu_op_smul) & a[31] ? ~a + 32'b1 :
194
              alu_op_div | alu_op_mul | (|mac_op) ? a : 32'd0;
195
   assign y = (alu_op_sdiv | alu_op_smul) & b[31] ? ~b + 32'b1 :
196
              alu_op_div | alu_op_mul | (|mac_op) ? b : 32'd0;
197 350 julius
 
198 502 julius
   assign div_by_zero = !(|b) & alu_op_div;
199
 
200
 
201 499 julius
   // Used to indicate when we should check for new multiply or MAC ops
202
   always @(posedge clk or `OR1200_RST_EVENT rst)
203
     if (rst == `OR1200_RST_VALUE)
204
       ex_freeze_r <= 1'b1;
205
     else
206
       ex_freeze_r <= ex_freeze;
207
 
208 435 julius
   //
209
   // Select result of current ALU operation to be forwarded
210
   // to next instruction and to WB stage
211
   //
212
   always @*
213
     casez(alu_op)      // synopsys parallel_case
214
`ifdef OR1200_DIV_IMPLEMENTED
215
       `OR1200_ALUOP_DIV: begin
216
          result = a[31] ^ b[31] ? ~div_quot_r[31:0] + 32'd1 : div_quot_r[31:0];
217
       end
218
       `OR1200_ALUOP_DIVU: begin
219
          result = div_quot_r[31:0];
220
       end
221
`endif
222
`ifdef OR1200_MULT_IMPLEMENTED
223
       `OR1200_ALUOP_MUL: begin
224
          result = a[31] ^ b[31] ? ~mul_prod_r[31:0] + 32'd1 : mul_prod_r[31:0];
225
       end
226
         `OR1200_ALUOP_MULU: begin
227
          result = mul_prod_r[31:0];
228
       end
229
`endif
230
       default:
231
`ifdef OR1200_MAC_IMPLEMENTED
232 350 julius
 `ifdef OR1200_MAC_SHIFTBY
233 435 julius
         result = mac_r[`OR1200_MAC_SHIFTBY+31:`OR1200_MAC_SHIFTBY];
234 350 julius
 `else
235 435 julius
       result = mac_r[31:0];
236 350 julius
 `endif
237 435 julius
`else
238
       result = {width{1'b0}};
239
`endif
240 502 julius
     endcase // casez (alu_op)
241 435 julius
 
242 502 julius
 
243
   //
244
   // Overflow generation
245
   //
246
   always @*
247
     casez(alu_op)      // synopsys parallel_case
248
`ifdef OR1200_IMPL_OV
249
 `ifdef OR1200_MULT_IMPLEMENTED
250
       `OR1200_ALUOP_MUL: begin
251
          // Actually doing unsigned multiply internally, and then negate on
252
          // output as appropriate, so if sign bit is set, then is overflow
253 505 julius
          // unless incoming signs differ and result is 2^(width-1)
254
          ovforw = (mul_prod_r[width-1] &&
255
                    !((a[width-1]^b[width-1]) && ~|mul_prod_r[width-2:0])) ||
256
                   |mul_prod_r[2*width-1:32];
257
 
258 502 julius
          ov_we = 1;
259
       end
260
       `OR1200_ALUOP_MULU : begin
261
          // Overflow on unsigned multiply is simpler.
262 505 julius
          ovforw = |mul_prod_r[2*width-1:32];
263 502 julius
          ov_we = 1;
264
       end
265
 `endif //  `ifdef OR1200_MULT_IMPLEMENTED
266
 `ifdef OR1200_DIV_IMPLEMENTED
267
       `OR1200_ALUOP_DIVU,
268
       `OR1200_ALUOP_DIV: begin
269 505 julius
          // Overflow on divide by zero or -2^(width-1)/-1
270
          ovforw = div_by_zero || (a==32'h8000_0000 && b==32'hffff_ffff);
271 502 julius
          ov_we = 1;
272
       end
273
 `endif
274
`endif //  `ifdef OR1200_IMPL_OV
275
       default: begin
276
          ovforw = 0;
277
          ov_we = 0;
278
       end
279
     endcase // casez (alu_op)
280
 
281
 
282 435 julius
`ifdef OR1200_MULT_IMPLEMENTED
283
 `ifdef OR1200_MULT_SERIAL
284
 
285
   always @(`OR1200_RST_EVENT rst or posedge clk)
286
     if (rst == `OR1200_RST_VALUE) begin
287
        mul_prod_r <=  64'h0000_0000_0000_0000;
288
        serial_mul_cnt <= 6'd0;
289
        mul_free <= 1'b1;
290
 
291
     end
292
     else if (|serial_mul_cnt) begin
293
        serial_mul_cnt <= serial_mul_cnt - 6'd1;
294
        if (mul_prod_r[0])
295
          mul_prod_r[(width*2)-1:width-1] <= mul_prod_r[(width*2)-1:width] + x;
296
        else
297
          mul_prod_r[(width*2)-1:width-1] <= {1'b0,mul_prod_r[(width*2)-1:
298
                                                              width]};
299
        mul_prod_r[width-2:0] <= mul_prod_r[width-1:1];
300
 
301
     end
302
     else if (alu_op_mul && mul_free) begin
303
        mul_prod_r <= {32'd0, y};
304
        mul_free <= 0;
305
        serial_mul_cnt <= 6'b10_0000;
306
     end
307
     else if (!ex_freeze | mul_free) begin
308
        mul_free <= 1'b1;
309
     end
310
 
311 501 julius
   assign mul_stall = (|serial_mul_cnt) | (alu_op_mul & !ex_freeze_r);
312 350 julius
 
313 435 julius
 `else
314
 
315 350 julius
   //
316
   // Instantiation of the multiplier
317
   //
318 435 julius
  `ifdef OR1200_ASIC_MULTP2_32X32
319
   or1200_amultp2_32x32 or1200_amultp2_32x32(
320
                                             .X(x),
321
                                             .Y(y),
322
                                             .RST(rst),
323
                                             .CLK(clk),
324
                                             .P(mul_prod)
325
                                             );
326
  `else // OR1200_ASIC_MULTP2_32X32
327
   or1200_gmultp2_32x32 or1200_gmultp2_32x32(
328
                                             .X(x),
329
                                             .Y(y),
330
                                             .RST(rst),
331
                                             .CLK(clk),
332
                                             .P(mul_prod)
333
                                             );
334
  `endif // OR1200_ASIC_MULTP2_32X32   
335
 
336
   //
337
   // Registered output from the multiplier
338
   //
339
   always @(`OR1200_RST_EVENT rst or posedge clk)
340
     if (rst == `OR1200_RST_VALUE) begin
341
        mul_prod_r <=  64'h0000_0000_0000_0000;
342
     end
343
     else begin
344
        mul_prod_r <=  mul_prod[63:0];
345
     end
346 350 julius
 
347 499 julius
   //
348
   // Generate stall signal during multiplication
349
   //
350
   always @(`OR1200_RST_EVENT rst or posedge clk)
351
     if (rst == `OR1200_RST_VALUE)
352
       mul_stall_count <= 0;
353
     else if (!(|mul_stall_count))
354
       mul_stall_count <= {mul_stall_count[0], alu_op_mul & !ex_freeze_r};
355
     else
356
       mul_stall_count <= {mul_stall_count[0],1'b0};
357
 
358
   assign mul_stall = (|mul_stall_count) |
359
                      (!(|mul_stall_count) & alu_op_mul & !ex_freeze_r);
360
 
361 435 julius
 `endif // !`ifdef OR1200_MULT_SERIAL   
362
 
363 350 julius
`else // OR1200_MULT_IMPLEMENTED
364 435 julius
   assign mul_prod = {2*width{1'b0}};
365
   assign mul_prod_r = {2*width{1'b0}};
366
   assign mul_stall = 0;
367 350 julius
`endif // OR1200_MULT_IMPLEMENTED
368
 
369
`ifdef OR1200_MAC_IMPLEMENTED
370 356 julius
 
371 435 julius
   //
372
   // Propagation of l.mac opcode, only register it for one cycle
373
   //
374
   always @(posedge clk or `OR1200_RST_EVENT rst)
375
     if (rst == `OR1200_RST_VALUE)
376
       mac_op_r1 <=  `OR1200_MACOP_WIDTH'b0;
377
     else
378
       mac_op_r1 <=  !ex_freeze_r ? mac_op : `OR1200_MACOP_WIDTH'b0;
379 350 julius
 
380 435 julius
   //
381
   // Propagation of l.mac opcode
382
   //
383
   always @(posedge clk or `OR1200_RST_EVENT rst)
384
     if (rst == `OR1200_RST_VALUE)
385
       mac_op_r2 <=  `OR1200_MACOP_WIDTH'b0;
386
     else
387
       mac_op_r2 <=  mac_op_r1;
388 350 julius
 
389 435 julius
   //
390
   // Propagation of l.mac opcode
391
   //
392
   always @(posedge clk or `OR1200_RST_EVENT rst)
393
     if (rst == `OR1200_RST_VALUE)
394
       mac_op_r3 <=  `OR1200_MACOP_WIDTH'b0;
395
     else
396
       mac_op_r3 <=  mac_op_r2;
397 350 julius
 
398 435 julius
   //
399
   // Implementation of MAC
400
   //
401
   always @(`OR1200_RST_EVENT rst or posedge clk)
402
     if (rst == `OR1200_RST_VALUE)
403
       mac_r <=  64'h0000_0000_0000_0000;
404
 `ifdef OR1200_MAC_SPR_WE
405
     else if (spr_maclo_we)
406
       mac_r[31:0] <=  spr_dat_i;
407
     else if (spr_machi_we)
408
       mac_r[63:32] <=  spr_dat_i;
409
 `endif
410
     else if (mac_op_r3 == `OR1200_MACOP_MAC)
411
       mac_r <=  mac_r + mul_prod_r;
412
     else if (mac_op_r3 == `OR1200_MACOP_MSB)
413
       mac_r <=  mac_r - mul_prod_r;
414
     else if (macrc_op && !ex_freeze)
415
       mac_r <=  64'h0000_0000_0000_0000;
416 350 julius
 
417 435 julius
   //
418
   // Stall CPU if l.macrc is in ID and MAC still has to process l.mac 
419
   // instructions in EX stage (e.g. inside multiplier)
420
   // This stall signal is also used by the divider.
421
   //
422
   always @(`OR1200_RST_EVENT rst or posedge clk)
423
     if (rst == `OR1200_RST_VALUE)
424
       mac_stall_r <=  1'b0;
425
     else
426
       mac_stall_r <=  (|mac_op | (|mac_op_r1) | (|mac_op_r2)) &
427
                       (id_macrc_op | mac_stall_r);
428 499 julius
 
429 350 julius
`else // OR1200_MAC_IMPLEMENTED
430 435 julius
   assign mac_stall_r = 1'b0;
431
   assign mac_r = {2*width{1'b0}};
432
   assign mac_op_r1 = `OR1200_MACOP_WIDTH'b0;
433
   assign mac_op_r2 = `OR1200_MACOP_WIDTH'b0;
434
   assign mac_op_r3 = `OR1200_MACOP_WIDTH'b0;
435 350 julius
`endif // OR1200_MAC_IMPLEMENTED
436
 
437 435 julius
`ifdef OR1200_DIV_IMPLEMENTED
438
 
439
   //
440
   // Serial division
441
   //
442
 `ifdef OR1200_DIV_SERIAL
443
   assign div_tmp = div_quot_r[63:32] - y;
444
   always @(`OR1200_RST_EVENT rst or posedge clk)
445
     if (rst == `OR1200_RST_VALUE) begin
446
        div_quot_r <=  64'h0000_0000_0000_0000;
447
        div_free <=  1'b1;
448
        div_cntr <=  6'b00_0000;
449
     end
450 502 julius
     else if (div_by_zero) begin
451
        div_quot_r <=  64'h0000_0000_0000_0000;
452
        div_free <=  1'b1;
453
        div_cntr <=  6'b00_0000;
454
     end
455 435 julius
     else if (|div_cntr) begin
456
        if (div_tmp[31])
457
          div_quot_r <=  {div_quot_r[62:0], 1'b0};
458
        else
459
          div_quot_r <=  {div_tmp[30:0], div_quot_r[31:0], 1'b1};
460
        div_cntr <=  div_cntr - 6'd1;
461
     end
462
     else if (alu_op_div && div_free) begin
463
        div_quot_r <=  {31'b0, x[31:0], 1'b0};
464
        div_cntr <=  6'b10_0000;
465
        div_free <=  1'b0;
466
     end
467
     else if (div_free | !ex_freeze) begin
468
        div_free <=  1'b1;
469
     end
470
 
471 501 julius
   assign div_stall = (|div_cntr) | (!ex_freeze_r & alu_op_div);
472 435 julius
 
473
 
474
 `else // !`ifdef OR1200_DIV_SERIAL
475
 
476
   // Full divider
477
   // TODO: Perhaps provide module that can be technology dependent.
478
   always @(`OR1200_RST_EVENT rst or posedge clk) begin
479
      if (rst == `OR1200_RST_VALUE) begin
480
         div_quot_r <=  32'd0;
481
         div_quot_generic <= 32'd0;
482
      end
483
      else begin
484
         if (alu_op_udiv & !(|y)) // unsigned divide by 0 - force to MAX
485
           div_quot_generic[31:0] <= 32'hffff_ffff;
486
         else if (alu_op_div)
487
           div_quot_generic[31:0] <= x / y;
488
      end
489
 
490
      // Add any additional statges of pipelining as required here. Ensure
491
      // ends with div_quot_r.
492
      // Then add logic to ensure div_stall stays high for as long as the
493
      // division should take.      
494
 
495
      div_quot_r[31:0] <= div_quot_generic;
496
 
497
   end
498
 
499
   assign div_stall = 0;
500
 
501
 `endif
502
 
503
`else // !`ifdef OR1200_DIV_IMPLEMENTED
504
 
505
   assign div_stall = 0;
506
 
507
`endif // !`ifdef OR1200_DIV_IMPLEMENTED
508
 
509
 
510
   //   
511
   // Stall output
512
   //
513
   assign mult_mac_stall = mac_stall_r | div_stall | mul_stall;
514
 
515 350 julius
endmodule

powered by: WebSVN 2.1.0

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