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

Subversion Repositories or1k_soc_on_altera_embedded_dev_kit

[/] [or1k_soc_on_altera_embedded_dev_kit/] [trunk/] [soc/] [rtl/] [or1200/] [rtl/] [verilog/] [or1200_mult_mac.v] - Blame information for rev 12

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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