OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

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

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

Line No. Rev Author Line
1 10 unneback
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3 258 julius
////  OR1200's Top level multiplier, divider and MAC              ////
4 10 unneback
////                                                              ////
5
////  This file is part of the OpenRISC 1200 project              ////
6 258 julius
////  http://opencores.org/project,or1k                           ////
7 10 unneback
////                                                              ////
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
////                                                              ////
15
////  Author(s):                                                  ////
16
////      - Damjan Lampret, lampret@opencores.org                 ////
17
////                                                              ////
18
//////////////////////////////////////////////////////////////////////
19
////                                                              ////
20
//// Copyright (C) 2000 Authors and OPENCORES.ORG                 ////
21
////                                                              ////
22
//// This source file may be used and distributed without         ////
23
//// restriction provided that this copyright statement is not    ////
24
//// removed from the file and that any derivative work contains  ////
25
//// the original copyright notice and the associated disclaimer. ////
26
////                                                              ////
27
//// This source file is free software; you can redistribute it   ////
28
//// and/or modify it under the terms of the GNU Lesser General   ////
29
//// Public License as published by the Free Software Foundation; ////
30
//// either version 2.1 of the License, or (at your option) any   ////
31
//// later version.                                               ////
32
////                                                              ////
33
//// This source is distributed in the hope that it will be       ////
34
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
35
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
36
//// PURPOSE.  See the GNU Lesser General Public License for more ////
37
//// details.                                                     ////
38
////                                                              ////
39
//// You should have received a copy of the GNU Lesser General    ////
40
//// Public License along with this source; if not, download it   ////
41
//// from http://www.opencores.org/lgpl.shtml                     ////
42
////                                                              ////
43
//////////////////////////////////////////////////////////////////////
44
//
45
// CVS Revision History
46
//
47 141 marcus.erl
// $Log: or1200_mult_mac.v,v $
48
// Revision 2.0  2010/06/30 11:00:00  ORSoC
49
// Minor update: 
50
// Bugs fixed. 
51
//
52 10 unneback
 
53
// synopsys translate_off
54
`include "timescale.v"
55
// synopsys translate_on
56
`include "or1200_defines.v"
57
 
58
module or1200_mult_mac(
59
        // Clock and reset
60
        clk, rst,
61
 
62
        // Multiplier/MAC interface
63
        ex_freeze, id_macrc_op, macrc_op, a, b, mac_op, alu_op, result, mac_stall_r,
64
 
65
        // SPR interface
66
        spr_cs, spr_write, spr_addr, spr_dat_i, spr_dat_o
67
);
68
 
69
parameter width = `OR1200_OPERAND_WIDTH;
70
 
71
//
72
// I/O
73
//
74
 
75
//
76
// Clock and reset
77
//
78
input                           clk;
79
input                           rst;
80
 
81
//
82
// Multiplier/MAC interface
83
//
84
input                           ex_freeze;
85
input                           id_macrc_op;
86
input                           macrc_op;
87
input   [width-1:0]              a;
88
input   [width-1:0]              b;
89
input   [`OR1200_MACOP_WIDTH-1:0]        mac_op;
90
input   [`OR1200_ALUOP_WIDTH-1:0]        alu_op;
91
output  [width-1:0]              result;
92
output                          mac_stall_r;
93
 
94
//
95
// SPR interface
96
//
97
input                           spr_cs;
98
input                           spr_write;
99
input   [31:0]                   spr_addr;
100
input   [31:0]                   spr_dat_i;
101
output  [31:0]                   spr_dat_o;
102
 
103
//
104
// Internal wires and regs
105
//
106
`ifdef OR1200_MULT_IMPLEMENTED
107
reg     [width-1:0]              result;
108
reg     [2*width-1:0]            mul_prod_r;
109
`else
110
wire    [width-1:0]              result;
111
wire    [2*width-1:0]            mul_prod_r;
112
`endif
113
wire    [2*width-1:0]            mul_prod;
114
wire    [`OR1200_MACOP_WIDTH-1:0]        mac_op;
115
`ifdef OR1200_MAC_IMPLEMENTED
116
reg     [`OR1200_MACOP_WIDTH-1:0]        mac_op_r1;
117
reg     [`OR1200_MACOP_WIDTH-1:0]        mac_op_r2;
118
reg     [`OR1200_MACOP_WIDTH-1:0]        mac_op_r3;
119
reg                             mac_stall_r;
120 356 julius
reg     [63:0]           mac_r;
121 10 unneback
`else
122
wire    [`OR1200_MACOP_WIDTH-1:0]        mac_op_r1;
123
wire    [`OR1200_MACOP_WIDTH-1:0]        mac_op_r2;
124
wire    [`OR1200_MACOP_WIDTH-1:0]        mac_op_r3;
125
wire                            mac_stall_r;
126 356 julius
wire    [63:0]           mac_r;
127 10 unneback
`endif
128
wire    [width-1:0]              x;
129
wire    [width-1:0]              y;
130
wire                            spr_maclo_we;
131
wire                            spr_machi_we;
132
wire                            alu_op_div_divu;
133
wire                            alu_op_div;
134
reg                             div_free;
135 258 julius
`ifdef OR1200_DIV_IMPLEMENTED
136 10 unneback
wire    [width-1:0]              div_tmp;
137
reg     [5:0]                    div_cntr;
138
`endif
139
 
140
//
141
// Combinatorial logic
142
//
143
`ifdef OR1200_MAC_IMPLEMENTED
144
assign spr_maclo_we = spr_cs & spr_write & spr_addr[`OR1200_MAC_ADDR];
145
assign spr_machi_we = spr_cs & spr_write & !spr_addr[`OR1200_MAC_ADDR];
146
assign spr_dat_o = spr_addr[`OR1200_MAC_ADDR] ? mac_r[31:0] : mac_r[63:32];
147
`else
148
assign spr_maclo_we = 1'b0;
149
assign spr_machi_we = 1'b0;
150
assign spr_dat_o = 32'h0000_0000;
151
`endif
152
`ifdef OR1200_LOWPWR_MULT
153 258 julius
assign x = (alu_op_div & a[31]) ? ~a + 1'b1 :
154
           alu_op_div_divu | (alu_op == `OR1200_ALUOP_MUL) | (|mac_op) ?
155
           a : 32'h0000_0000;
156
assign y = (alu_op_div & b[31]) ? ~b + 1'b1 :
157
           alu_op_div_divu | (alu_op == `OR1200_ALUOP_MUL) | (|mac_op) ?
158
           b : 32'h0000_0000;
159 10 unneback
`else
160
assign x = alu_op_div & a[31] ? ~a + 32'b1 : a;
161
assign y = alu_op_div & b[31] ? ~b + 32'b1 : b;
162
`endif
163 258 julius
`ifdef OR1200_DIV_IMPLEMENTED
164 10 unneback
assign alu_op_div = (alu_op == `OR1200_ALUOP_DIV);
165
assign alu_op_div_divu = alu_op_div | (alu_op == `OR1200_ALUOP_DIVU);
166
assign div_tmp = mul_prod_r[63:32] - y;
167
`else
168
assign alu_op_div = 1'b0;
169
assign alu_op_div_divu = 1'b0;
170
`endif
171
 
172
`ifdef OR1200_MULT_IMPLEMENTED
173
 
174
//
175
// Select result of current ALU operation to be forwarded
176
// to next instruction and to WB stage
177
//
178 356 julius
always @*
179 258 julius
  casex(alu_op) // synopsys parallel_case
180
 `ifdef OR1200_DIV_IMPLEMENTED
181
    `OR1200_ALUOP_DIV: begin
182
       result = a[31] ^ b[31] ? ~mul_prod_r[31:0] + 1'b1 : mul_prod_r[31:0];
183
    end
184
    `OR1200_ALUOP_DIVU,
185
 `endif
186
    `OR1200_ALUOP_MUL: begin
187
       result = mul_prod_r[31:0];
188
    end
189
    default:
190
 `ifdef OR1200_MAC_SHIFTBY
191
      result = mac_r[`OR1200_MAC_SHIFTBY+31:`OR1200_MAC_SHIFTBY];
192
 `else
193
      result = mac_r[31:0];
194
 `endif
195
  endcase
196
 
197
   //
198
   // Instantiation of the multiplier
199
   //
200
 `ifdef OR1200_ASIC_MULTP2_32X32
201 10 unneback
or1200_amultp2_32x32 or1200_amultp2_32x32(
202
        .X(x),
203
        .Y(y),
204
        .RST(rst),
205
        .CLK(clk),
206
        .P(mul_prod)
207
);
208
`else // OR1200_ASIC_MULTP2_32X32
209
or1200_gmultp2_32x32 or1200_gmultp2_32x32(
210
        .X(x),
211
        .Y(y),
212
        .RST(rst),
213
        .CLK(clk),
214
        .P(mul_prod)
215
);
216
`endif // OR1200_ASIC_MULTP2_32X32
217
 
218
//
219
// Registered output from the multiplier and
220
// an optional divider
221
//
222
always @(posedge rst or posedge clk)
223
        if (rst) begin
224 258 julius
                mul_prod_r <=  64'h0000_0000_0000_0000;
225
                div_free <=  1'b1;
226
`ifdef OR1200_DIV_IMPLEMENTED
227
                div_cntr <=  6'b00_0000;
228 10 unneback
`endif
229
        end
230 258 julius
`ifdef OR1200_DIV_IMPLEMENTED
231 10 unneback
        else if (|div_cntr) begin
232
                if (div_tmp[31])
233 258 julius
                        mul_prod_r <=  {mul_prod_r[62:0], 1'b0};
234 10 unneback
                else
235 258 julius
                        mul_prod_r <=  {div_tmp[30:0], mul_prod_r[31:0], 1'b1};
236
                div_cntr <=  div_cntr - 1'b1;
237 10 unneback
        end
238
        else if (alu_op_div_divu && div_free) begin
239 258 julius
                mul_prod_r <=  {31'b0, x[31:0], 1'b0};
240
                div_cntr <=  6'b10_0000;
241
                div_free <=  1'b0;
242 10 unneback
        end
243 258 julius
`endif // OR1200_DIV_IMPLEMENTED
244 10 unneback
        else if (div_free | !ex_freeze) begin
245 258 julius
                mul_prod_r <=  mul_prod[63:0];
246
                div_free <=  1'b1;
247 10 unneback
        end
248
 
249
`else // OR1200_MULT_IMPLEMENTED
250
assign result = {width{1'b0}};
251
assign mul_prod = {2*width{1'b0}};
252
assign mul_prod_r = {2*width{1'b0}};
253
`endif // OR1200_MULT_IMPLEMENTED
254
 
255
`ifdef OR1200_MAC_IMPLEMENTED
256 356 julius
// Signal to indicate when we should check for new MAC op
257
reg ex_freeze_r;
258
 
259
always @(posedge clk or posedge rst)
260
  if (rst)
261
    ex_freeze_r <= 1'b1;
262
  else
263
    ex_freeze_r <= ex_freeze;
264
 
265 10 unneback
//
266 356 julius
// Propagation of l.mac opcode, only register it for one cycle
267 10 unneback
//
268
always @(posedge clk or posedge rst)
269
        if (rst)
270 258 julius
                mac_op_r1 <=  `OR1200_MACOP_WIDTH'b0;
271 10 unneback
        else
272 356 julius
                mac_op_r1 <=  !ex_freeze_r ? mac_op : `OR1200_MACOP_WIDTH'b0;
273 10 unneback
 
274
//
275
// Propagation of l.mac opcode
276
//
277
always @(posedge clk or posedge rst)
278
        if (rst)
279 258 julius
                mac_op_r2 <=  `OR1200_MACOP_WIDTH'b0;
280 10 unneback
        else
281 258 julius
                mac_op_r2 <=  mac_op_r1;
282 10 unneback
 
283
//
284
// Propagation of l.mac opcode
285
//
286
always @(posedge clk or posedge rst)
287
        if (rst)
288 258 julius
                mac_op_r3 <=  `OR1200_MACOP_WIDTH'b0;
289 10 unneback
        else
290 258 julius
                mac_op_r3 <=  mac_op_r2;
291 10 unneback
 
292
//
293
// Implementation of MAC
294
//
295
always @(posedge rst or posedge clk)
296
        if (rst)
297 258 julius
                mac_r <=  64'h0000_0000_0000_0000;
298 10 unneback
`ifdef OR1200_MAC_SPR_WE
299
        else if (spr_maclo_we)
300 258 julius
                mac_r[31:0] <=  spr_dat_i;
301 10 unneback
        else if (spr_machi_we)
302 258 julius
                mac_r[63:32] <=  spr_dat_i;
303 10 unneback
`endif
304
        else if (mac_op_r3 == `OR1200_MACOP_MAC)
305 258 julius
                mac_r <=  mac_r + mul_prod_r;
306 10 unneback
        else if (mac_op_r3 == `OR1200_MACOP_MSB)
307 258 julius
                mac_r <=  mac_r - mul_prod_r;
308 141 marcus.erl
        else if (macrc_op && !ex_freeze)
309 258 julius
                mac_r <=  64'h0000_0000_0000_0000;
310 10 unneback
 
311
//
312
// Stall CPU if l.macrc is in ID and MAC still has to process l.mac instructions
313
// in EX stage (e.g. inside multiplier)
314
// This stall signal is also used by the divider.
315
//
316
always @(posedge rst or posedge clk)
317
        if (rst)
318 258 julius
                mac_stall_r <=  1'b0;
319 10 unneback
        else
320 258 julius
                mac_stall_r <=  (|mac_op | (|mac_op_r1) | (|mac_op_r2)) & (id_macrc_op | mac_stall_r)
321
`ifdef OR1200_DIV_IMPLEMENTED
322 10 unneback
                                | (|div_cntr)
323
`endif
324
                                ;
325
`else // OR1200_MAC_IMPLEMENTED
326
assign mac_stall_r = 1'b0;
327
assign mac_r = {2*width{1'b0}};
328
assign mac_op_r1 = `OR1200_MACOP_WIDTH'b0;
329
assign mac_op_r2 = `OR1200_MACOP_WIDTH'b0;
330
assign mac_op_r3 = `OR1200_MACOP_WIDTH'b0;
331
`endif // OR1200_MAC_IMPLEMENTED
332
 
333
endmodule

powered by: WebSVN 2.1.0

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