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

Subversion Repositories or1k

[/] [or1k/] [tags/] [rel_29/] [or1200/] [rtl/] [verilog/] [or1200_mult_mac.v] - Blame information for rev 1780

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

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

powered by: WebSVN 2.1.0

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