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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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