OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

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

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

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

powered by: WebSVN 2.1.0

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