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