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

Subversion Repositories openrisc

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

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

Line No. Rev Author Line
1 10 unneback
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  OR1200's Top level multiplier and MAC                       ////
4
////                                                              ////
5
////  This file is part of the OpenRISC 1200 project              ////
6
////  http://www.opencores.org/cores/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
////                                                              ////
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
// $Log: not supported by cvs2svn $
48
// Revision 1.4  2004/06/08 18:17:36  lampret
49
// Non-functional changes. Coding style fixes.
50
//
51
// Revision 1.3  2003/04/24 00:16:07  lampret
52
// No functional changes. Added defines to disable implementation of multiplier/MAC
53
//
54
// Revision 1.2  2002/09/08 05:52:16  lampret
55
// Added optional l.div/l.divu insns. By default they are disabled.
56
//
57
// Revision 1.1  2002/01/03 08:16:15  lampret
58
// New prefixes for RTL files, prefixed module names. Updated cache controllers and MMUs.
59
//
60
// Revision 1.3  2001/10/21 17:57:16  lampret
61
// Removed params from generic_XX.v. Added translate_off/on in sprs.v and id.v. Removed spr_addr from dc.v and ic.v. Fixed CR+LF.
62
//
63
// Revision 1.2  2001/10/14 13:12:09  lampret
64
// MP3 version.
65
//
66
// Revision 1.1.1.1  2001/10/06 10:18:38  igorm
67
// no message
68
//
69
//
70
 
71
// synopsys translate_off
72
`include "timescale.v"
73
// synopsys translate_on
74
`include "or1200_defines.v"
75
 
76
module or1200_mult_mac(
77
        // Clock and reset
78
        clk, rst,
79
 
80
        // Multiplier/MAC interface
81
        ex_freeze, id_macrc_op, macrc_op, a, b, mac_op, alu_op, result, mac_stall_r,
82
 
83
        // SPR interface
84
        spr_cs, spr_write, spr_addr, spr_dat_i, spr_dat_o
85
);
86
 
87
parameter width = `OR1200_OPERAND_WIDTH;
88
 
89
//
90
// I/O
91
//
92
 
93
//
94
// Clock and reset
95
//
96
input                           clk;
97
input                           rst;
98
 
99
//
100
// Multiplier/MAC interface
101
//
102
input                           ex_freeze;
103
input                           id_macrc_op;
104
input                           macrc_op;
105
input   [width-1:0]              a;
106
input   [width-1:0]              b;
107
input   [`OR1200_MACOP_WIDTH-1:0]        mac_op;
108
input   [`OR1200_ALUOP_WIDTH-1:0]        alu_op;
109
output  [width-1:0]              result;
110
output                          mac_stall_r;
111
 
112
//
113
// SPR interface
114
//
115
input                           spr_cs;
116
input                           spr_write;
117
input   [31:0]                   spr_addr;
118
input   [31:0]                   spr_dat_i;
119
output  [31:0]                   spr_dat_o;
120
 
121
//
122
// Internal wires and regs
123
//
124
`ifdef OR1200_MULT_IMPLEMENTED
125
reg     [width-1:0]              result;
126
reg     [2*width-1:0]            mul_prod_r;
127
`else
128
wire    [width-1:0]              result;
129
wire    [2*width-1:0]            mul_prod_r;
130
`endif
131
wire    [2*width-1:0]            mul_prod;
132
wire    [`OR1200_MACOP_WIDTH-1:0]        mac_op;
133
`ifdef OR1200_MAC_IMPLEMENTED
134
reg     [`OR1200_MACOP_WIDTH-1:0]        mac_op_r1;
135
reg     [`OR1200_MACOP_WIDTH-1:0]        mac_op_r2;
136
reg     [`OR1200_MACOP_WIDTH-1:0]        mac_op_r3;
137
reg                             mac_stall_r;
138
reg     [2*width-1:0]            mac_r;
139
`else
140
wire    [`OR1200_MACOP_WIDTH-1:0]        mac_op_r1;
141
wire    [`OR1200_MACOP_WIDTH-1:0]        mac_op_r2;
142
wire    [`OR1200_MACOP_WIDTH-1:0]        mac_op_r3;
143
wire                            mac_stall_r;
144
wire    [2*width-1:0]            mac_r;
145
`endif
146
wire    [width-1:0]              x;
147
wire    [width-1:0]              y;
148
wire                            spr_maclo_we;
149
wire                            spr_machi_we;
150
wire                            alu_op_div_divu;
151
wire                            alu_op_div;
152
reg                             div_free;
153
`ifdef OR1200_IMPL_DIV
154
wire    [width-1:0]              div_tmp;
155
reg     [5:0]                    div_cntr;
156
`endif
157
 
158
//
159
// Combinatorial logic
160
//
161
`ifdef OR1200_MAC_IMPLEMENTED
162
assign spr_maclo_we = spr_cs & spr_write & spr_addr[`OR1200_MAC_ADDR];
163
assign spr_machi_we = spr_cs & spr_write & !spr_addr[`OR1200_MAC_ADDR];
164
assign spr_dat_o = spr_addr[`OR1200_MAC_ADDR] ? mac_r[31:0] : mac_r[63:32];
165
`else
166
assign spr_maclo_we = 1'b0;
167
assign spr_machi_we = 1'b0;
168
assign spr_dat_o = 32'h0000_0000;
169
`endif
170
`ifdef OR1200_LOWPWR_MULT
171
assign x = (alu_op_div & a[31]) ? ~a + 1'b1 : alu_op_div_divu | (alu_op == `OR1200_ALUOP_MUL) | (|mac_op) ? a : 32'h0000_0000;
172
assign y = (alu_op_div & b[31]) ? ~b + 1'b1 : alu_op_div_divu | (alu_op == `OR1200_ALUOP_MUL) | (|mac_op) ? b : 32'h0000_0000;
173
`else
174
assign x = alu_op_div & a[31] ? ~a + 32'b1 : a;
175
assign y = alu_op_div & b[31] ? ~b + 32'b1 : b;
176
`endif
177
`ifdef OR1200_IMPL_DIV
178
assign alu_op_div = (alu_op == `OR1200_ALUOP_DIV);
179
assign alu_op_div_divu = alu_op_div | (alu_op == `OR1200_ALUOP_DIVU);
180
assign div_tmp = mul_prod_r[63:32] - y;
181
`else
182
assign alu_op_div = 1'b0;
183
assign alu_op_div_divu = 1'b0;
184
`endif
185
 
186
`ifdef OR1200_MULT_IMPLEMENTED
187
 
188
//
189
// Select result of current ALU operation to be forwarded
190
// to next instruction and to WB stage
191
//
192
always @(alu_op or mul_prod_r or mac_r or a or b)
193
        casex(alu_op)   // synopsys parallel_case
194
`ifdef OR1200_IMPL_DIV
195
                `OR1200_ALUOP_DIV:
196
                        result = a[31] ^ b[31] ? ~mul_prod_r[31:0] + 1'b1 : mul_prod_r[31:0];
197
                `OR1200_ALUOP_DIVU,
198
`endif
199
                `OR1200_ALUOP_MUL: begin
200
                        result = mul_prod_r[31:0];
201
                end
202
                default:
203
`ifdef OR1200_MAC_SHIFTBY
204
                        result = mac_r[`OR1200_MAC_SHIFTBY+31:`OR1200_MAC_SHIFTBY];
205
`else
206
                        result = mac_r[31:0];
207
`endif
208
        endcase
209
 
210
//
211
// Instantiation of the multiplier
212
//
213
`ifdef OR1200_ASIC_MULTP2_32X32
214
or1200_amultp2_32x32 or1200_amultp2_32x32(
215
        .X(x),
216
        .Y(y),
217
        .RST(rst),
218
        .CLK(clk),
219
        .P(mul_prod)
220
);
221
`else // OR1200_ASIC_MULTP2_32X32
222
or1200_gmultp2_32x32 or1200_gmultp2_32x32(
223
        .X(x),
224
        .Y(y),
225
        .RST(rst),
226
        .CLK(clk),
227
        .P(mul_prod)
228
);
229
`endif // OR1200_ASIC_MULTP2_32X32
230
 
231
//
232
// Registered output from the multiplier and
233
// an optional divider
234
//
235
always @(posedge rst or posedge clk)
236
        if (rst) begin
237
                mul_prod_r <= #1 64'h0000_0000_0000_0000;
238
                div_free <= #1 1'b1;
239
`ifdef OR1200_IMPL_DIV
240
                div_cntr <= #1 6'b00_0000;
241
`endif
242
        end
243
`ifdef OR1200_IMPL_DIV
244
        else if (|div_cntr) begin
245
                if (div_tmp[31])
246
                        mul_prod_r <= #1 {mul_prod_r[62:0], 1'b0};
247
                else
248
                        mul_prod_r <= #1 {div_tmp[30:0], mul_prod_r[31:0], 1'b1};
249
                div_cntr <= #1 div_cntr - 1'b1;
250
        end
251
        else if (alu_op_div_divu && div_free) begin
252
                mul_prod_r <= #1 {31'b0, x[31:0], 1'b0};
253
                div_cntr <= #1 6'b10_0000;
254
                div_free <= #1 1'b0;
255
        end
256
`endif // OR1200_IMPL_DIV
257
        else if (div_free | !ex_freeze) begin
258
                mul_prod_r <= #1 mul_prod[63:0];
259
                div_free <= #1 1'b1;
260
        end
261
 
262
`else // OR1200_MULT_IMPLEMENTED
263
assign result = {width{1'b0}};
264
assign mul_prod = {2*width{1'b0}};
265
assign mul_prod_r = {2*width{1'b0}};
266
`endif // OR1200_MULT_IMPLEMENTED
267
 
268
`ifdef OR1200_MAC_IMPLEMENTED
269
 
270
//
271
// Propagation of l.mac opcode
272
//
273
always @(posedge clk or posedge rst)
274
        if (rst)
275
                mac_op_r1 <= #1 `OR1200_MACOP_WIDTH'b0;
276
        else
277
                mac_op_r1 <= #1 mac_op;
278
 
279
//
280
// Propagation of l.mac opcode
281
//
282
always @(posedge clk or posedge rst)
283
        if (rst)
284
                mac_op_r2 <= #1 `OR1200_MACOP_WIDTH'b0;
285
        else
286
                mac_op_r2 <= #1 mac_op_r1;
287
 
288
//
289
// Propagation of l.mac opcode
290
//
291
always @(posedge clk or posedge rst)
292
        if (rst)
293
                mac_op_r3 <= #1 `OR1200_MACOP_WIDTH'b0;
294
        else
295
                mac_op_r3 <= #1 mac_op_r2;
296
 
297
//
298
// Implementation of MAC
299
//
300
always @(posedge rst or posedge clk)
301
        if (rst)
302
                mac_r <= #1 64'h0000_0000_0000_0000;
303
`ifdef OR1200_MAC_SPR_WE
304
        else if (spr_maclo_we)
305
                mac_r[31:0] <= #1 spr_dat_i;
306
        else if (spr_machi_we)
307
                mac_r[63:32] <= #1 spr_dat_i;
308
`endif
309
        else if (mac_op_r3 == `OR1200_MACOP_MAC)
310
                mac_r <= #1 mac_r + mul_prod_r;
311
        else if (mac_op_r3 == `OR1200_MACOP_MSB)
312
                mac_r <= #1 mac_r - mul_prod_r;
313
        else if (macrc_op & !ex_freeze)
314
                mac_r <= #1 64'h0000_0000_0000_0000;
315
 
316
//
317
// Stall CPU if l.macrc is in ID and MAC still has to process l.mac instructions
318
// in EX stage (e.g. inside multiplier)
319
// This stall signal is also used by the divider.
320
//
321
always @(posedge rst or posedge clk)
322
        if (rst)
323
                mac_stall_r <= #1 1'b0;
324
        else
325
                mac_stall_r <= #1 (|mac_op | (|mac_op_r1) | (|mac_op_r2)) & id_macrc_op
326
`ifdef OR1200_IMPL_DIV
327
                                | (|div_cntr)
328
`endif
329
                                ;
330
`else // OR1200_MAC_IMPLEMENTED
331
assign mac_stall_r = 1'b0;
332
assign mac_r = {2*width{1'b0}};
333
assign mac_op_r1 = `OR1200_MACOP_WIDTH'b0;
334
assign mac_op_r2 = `OR1200_MACOP_WIDTH'b0;
335
assign mac_op_r3 = `OR1200_MACOP_WIDTH'b0;
336
`endif // OR1200_MAC_IMPLEMENTED
337
 
338
endmodule

powered by: WebSVN 2.1.0

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