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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or1200/] [rtl/] [verilog/] [or1200_alu.v] - Blame information for rev 642

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

Line No. Rev Author Line
1 10 unneback
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  OR1200's ALU                                                ////
4
////                                                              ////
5
////  This file is part of the OpenRISC 1200 project              ////
6 185 julius
////  http://www.opencores.org/project,or1k                       ////
7 10 unneback
////                                                              ////
8
////  Description                                                 ////
9
////  ALU                                                         ////
10
////                                                              ////
11
////  To Do:                                                      ////
12
////   - make it smaller and faster                               ////
13
////                                                              ////
14
////  Author(s):                                                  ////
15
////      - Damjan Lampret, lampret@opencores.org                 ////
16
////                                                              ////
17
//////////////////////////////////////////////////////////////////////
18
////                                                              ////
19
//// Copyright (C) 2000 Authors and OPENCORES.ORG                 ////
20
////                                                              ////
21
//// This source file may be used and distributed without         ////
22
//// restriction provided that this copyright statement is not    ////
23
//// removed from the file and that any derivative work contains  ////
24
//// the original copyright notice and the associated disclaimer. ////
25
////                                                              ////
26
//// This source file is free software; you can redistribute it   ////
27
//// and/or modify it under the terms of the GNU Lesser General   ////
28
//// Public License as published by the Free Software Foundation; ////
29
//// either version 2.1 of the License, or (at your option) any   ////
30
//// later version.                                               ////
31
////                                                              ////
32
//// This source is distributed in the hope that it will be       ////
33
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
34
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
35
//// PURPOSE.  See the GNU Lesser General Public License for more ////
36
//// details.                                                     ////
37
////                                                              ////
38
//// You should have received a copy of the GNU Lesser General    ////
39
//// Public License along with this source; if not, download it   ////
40
//// from http://www.opencores.org/lgpl.shtml                     ////
41
////                                                              ////
42
//////////////////////////////////////////////////////////////////////
43
//
44 141 marcus.erl
// $Log: or1200_alu.v,v $
45
// Revision 2.0  2010/06/30 11:00:00  ORSoC
46
// Minor update: 
47
// Defines added, flags are corrected. 
48 10 unneback
 
49
// synopsys translate_off
50
`include "timescale.v"
51
// synopsys translate_on
52
`include "or1200_defines.v"
53
 
54
module or1200_alu(
55
        a, b, mult_mac_result, macrc_op,
56 640 julius
        alu_op, alu_op2, comp_op,
57 10 unneback
        cust5_op, cust5_limm,
58
        result, flagforw, flag_we,
59 642 julius
        ovforw, ov_we,
60 10 unneback
        cyforw, cy_we, carry, flag
61
);
62
 
63
parameter width = `OR1200_OPERAND_WIDTH;
64
 
65
//
66
// I/O
67
//
68
input   [width-1:0]              a;
69
input   [width-1:0]              b;
70
input   [width-1:0]              mult_mac_result;
71
input                           macrc_op;
72
input   [`OR1200_ALUOP_WIDTH-1:0]        alu_op;
73 401 julius
input   [`OR1200_ALUOP2_WIDTH-1:0]       alu_op2;
74 10 unneback
input   [`OR1200_COMPOP_WIDTH-1:0]       comp_op;
75
input   [4:0]                    cust5_op;
76
input   [5:0]                    cust5_limm;
77
output  [width-1:0]              result;
78
output                          flagforw;
79
output                          flag_we;
80
output                          cyforw;
81
output                          cy_we;
82 642 julius
output                          ovforw;
83
output                          ov_we;
84 10 unneback
input                           carry;
85
input         flag;
86
 
87
//
88
// Internal wires and regs
89
//
90
reg     [width-1:0]              result;
91
reg     [width-1:0]              shifted_rotated;
92 640 julius
reg     [width-1:0]              extended;
93 10 unneback
reg     [width-1:0]              result_cust5;
94
reg                             flagforw;
95
reg                             flagcomp;
96
reg                             flag_we;
97 642 julius
reg                             cyforw;
98 10 unneback
reg                             cy_we;
99 642 julius
reg                             ovforw;
100
reg                             ov_we;
101 10 unneback
wire    [width-1:0]              comp_a;
102
wire    [width-1:0]              comp_b;
103
`ifdef OR1200_IMPL_ALU_COMP1
104
wire                            a_eq_b;
105
wire                            a_lt_b;
106
`endif
107
wire    [width-1:0]              result_sum;
108
wire    [width-1:0]              result_and;
109
wire                            cy_sum;
110 141 marcus.erl
`ifdef OR1200_IMPL_SUB
111
wire                            cy_sub;
112
`endif
113 642 julius
wire                            ov_sum;
114
wire    [width-1:0]              carry_in;
115 10 unneback
 
116 642 julius
wire    [width-1:0]              b_mux;
117
 
118
 
119
 
120 10 unneback
//
121
// Combinatorial logic
122
//
123
assign comp_a = {a[width-1] ^ comp_op[3] , a[width-2:0]};
124
assign comp_b = {b[width-1] ^ comp_op[3] , b[width-2:0]};
125
`ifdef OR1200_IMPL_ALU_COMP1
126
assign a_eq_b = (comp_a == comp_b);
127
assign a_lt_b = (comp_a < comp_b);
128
`endif
129 141 marcus.erl
`ifdef OR1200_IMPL_SUB
130 642 julius
assign cy_sub = (comp_a < comp_b);
131 141 marcus.erl
`endif
132 642 julius
`ifdef OR1200_IMPL_ADDC
133
assign carry_in = (alu_op==`OR1200_ALUOP_ADDC) ?
134
                  {{width-1{1'b0}},carry} : {width{1'b0}};
135
`else
136
assign carry_in = {width-1{1'b0}};
137 10 unneback
`endif
138 642 julius
`ifdef OR1200_IMPL_SUB
139
assign b_mux = (alu_op==`OR1200_ALUOP_SUB) ? (~b)+1 : b;
140
`else
141
assign b_mux = b;
142
`endif
143
assign {cy_sum, result_sum} = (a + b_mux) + carry_in;
144
// Numbers either both +ve and bit 31 of result set
145
assign ov_sum = ((!a[width-1] & !b_mux[width-1]) & result_sum[width-1]) |
146
// or both -ve and bit 31 of result clear
147
                ((a[width-1] & b_mux[width-1]) & !result_sum[width-1]);
148 10 unneback
assign result_and = a & b;
149
 
150
//
151
// Simulation check for bad ALU behavior
152
//
153
`ifdef OR1200_WARNINGS
154
// synopsys translate_off
155
always @(result) begin
156
        if (result === 32'bx)
157
                $display("%t: WARNING: 32'bx detected on ALU result bus. Please check !", $time);
158
end
159
// synopsys translate_on
160
`endif
161
 
162
//
163
// Central part of the ALU
164
//
165 401 julius
always @(alu_op or alu_op2 or a or b or result_sum or result_and or macrc_op
166
         or shifted_rotated or mult_mac_result or flag or result_cust5 or carry
167 640 julius
`ifdef OR1200_IMPL_ALU_EXT
168
         or extended
169
`endif
170 141 marcus.erl
) begin
171 10 unneback
`ifdef OR1200_CASE_DEFAULT
172 364 julius
        casez (alu_op)          // synopsys parallel_case
173 10 unneback
`else
174 364 julius
        casez (alu_op)          // synopsys full_case parallel_case
175 10 unneback
`endif
176 401 julius
`ifdef OR1200_IMPL_ALU_FFL1
177
                `OR1200_ALUOP_FFL1: begin
178
`ifdef OR1200_CASE_DEFAULT
179
                   casez (alu_op2) // synopsys parallel_case
180
`else
181
                   casez (alu_op2) // synopsys full_case parallel_case
182
`endif
183
                     0: begin // FF1
184 141 marcus.erl
                        result = a[0] ? 1 : a[1] ? 2 : a[2] ? 3 : a[3] ? 4 : a[4] ? 5 : a[5] ? 6 : a[6] ? 7 : a[7] ? 8 : a[8] ? 9 : a[9] ? 10 : a[10] ? 11 : a[11] ? 12 : a[12] ? 13 : a[13] ? 14 : a[14] ? 15 : a[15] ? 16 : a[16] ? 17 : a[17] ? 18 : a[18] ? 19 : a[19] ? 20 : a[20] ? 21 : a[21] ? 22 : a[22] ? 23 : a[23] ? 24 : a[24] ? 25 : a[25] ? 26 : a[26] ? 27 : a[27] ? 28 : a[28] ? 29 : a[29] ? 30 : a[30] ? 31 : a[31] ? 32 : 0;
185 401 julius
                     end
186
                     default: begin // FL1
187
                        result = a[31] ? 32 : a[30] ? 31 : a[29] ? 30 : a[28] ? 29 : a[27] ? 28 : a[26] ? 27 : a[25] ? 26 : a[24] ? 25 : a[23] ? 24 : a[22] ? 23 : a[21] ? 22 : a[20] ? 21 : a[19] ? 20 : a[18] ? 19 : a[17] ? 18 : a[16] ? 17 : a[15] ? 16 : a[14] ? 15 : a[13] ? 14 : a[12] ? 13 : a[11] ? 12 : a[10] ? 11 : a[9] ? 10 : a[8] ? 9 : a[7] ? 8 : a[6] ? 7 : a[5] ? 6 : a[4] ? 5 : a[3] ? 4 : a[2] ? 3 : a[1] ? 2 : a[0] ? 1 : 0 ;
188
                     end
189
                   endcase // casez (alu_op2)
190
                end // case: `OR1200_ALUOP_FFL1
191 640 julius
`endif //  `ifdef OR1200_IMPL_ALU_FFL1
192
`ifdef OR1200_IMPL_ALU_CUST5
193
 
194 10 unneback
                `OR1200_ALUOP_CUST5 : begin
195
                                result = result_cust5;
196
                end
197 640 julius
`endif
198 10 unneback
                `OR1200_ALUOP_SHROT : begin
199
                                result = shifted_rotated;
200
                end
201
`ifdef OR1200_IMPL_ADDC
202 642 julius
                `OR1200_ALUOP_ADDC,
203 10 unneback
`endif
204 141 marcus.erl
`ifdef OR1200_IMPL_SUB
205 642 julius
                `OR1200_ALUOP_SUB,
206
`endif
207
                `OR1200_ALUOP_ADD : begin
208
                                result = result_sum;
209 10 unneback
                end
210
                `OR1200_ALUOP_XOR : begin
211
                                result = a ^ b;
212
                end
213
                `OR1200_ALUOP_OR  : begin
214
                                result = a | b;
215
                end
216 640 julius
`ifdef OR1200_IMPL_ALU_EXT
217
                `OR1200_ALUOP_EXTHB  : begin
218
                                result = extended;
219
                end
220
                `OR1200_ALUOP_EXTW  : begin
221
                                result = extended;
222
                end
223
`endif
224 10 unneback
                `OR1200_ALUOP_MOVHI : begin
225
                                if (macrc_op) begin
226
                                        result = mult_mac_result;
227
                                end
228
                                else begin
229
                                        result = b << 16;
230
                                end
231
                end
232
`ifdef OR1200_MULT_IMPLEMENTED
233 258 julius
`ifdef OR1200_DIV_IMPLEMENTED
234 10 unneback
                `OR1200_ALUOP_DIV,
235
                `OR1200_ALUOP_DIVU,
236
`endif
237 481 julius
                `OR1200_ALUOP_MUL,
238
                `OR1200_ALUOP_MULU : begin
239 10 unneback
                                result = mult_mac_result;
240
                end
241
`endif
242 141 marcus.erl
                `OR1200_ALUOP_CMOV: begin
243
                        result = flag ? a : b;
244
                end
245 10 unneback
 
246
`ifdef OR1200_CASE_DEFAULT
247 141 marcus.erl
                default: begin
248 10 unneback
`else
249 141 marcus.erl
                `OR1200_ALUOP_COMP, `OR1200_ALUOP_AND: begin
250 10 unneback
`endif
251 141 marcus.erl
                        result=result_and;
252
                end
253 10 unneback
        endcase
254
end
255
 
256
//
257
// Generate flag and flag write enable
258
//
259 141 marcus.erl
always @(alu_op or result_sum or result_and or flagcomp
260
) begin
261 364 julius
        casez (alu_op)          // synopsys parallel_case
262 10 unneback
`ifdef OR1200_ADDITIONAL_FLAG_MODIFIERS
263 642 julius
`ifdef OR1200_IMPL_ADDC
264
                `OR1200_ALUOP_ADDC,
265
`endif
266 10 unneback
                `OR1200_ALUOP_ADD : begin
267
                        flagforw = (result_sum == 32'h0000_0000);
268
                        flag_we = 1'b1;
269
                end
270
                `OR1200_ALUOP_AND: begin
271
                        flagforw = (result_and == 32'h0000_0000);
272
                        flag_we = 1'b1;
273
                end
274
`endif
275
                `OR1200_ALUOP_COMP: begin
276
                        flagforw = flagcomp;
277
                        flag_we = 1'b1;
278
                end
279
                default: begin
280 141 marcus.erl
                        flagforw = flagcomp;
281 10 unneback
                        flag_we = 1'b0;
282
                end
283
        endcase
284
end
285
 
286
//
287
// Generate SR[CY] write enable
288
//
289
always @(alu_op or cy_sum
290 141 marcus.erl
`ifdef OR1200_IMPL_CY
291
`ifdef OR1200_IMPL_SUB
292
        or cy_sub
293
`endif
294
`endif
295
) begin
296 364 julius
        casez (alu_op)          // synopsys parallel_case
297 10 unneback
`ifdef OR1200_IMPL_CY
298 642 julius
`ifdef OR1200_IMPL_ADDC
299
                `OR1200_ALUOP_ADDC,
300
`endif
301 10 unneback
                `OR1200_ALUOP_ADD : begin
302
                        cyforw = cy_sum;
303
                        cy_we = 1'b1;
304
                end
305 141 marcus.erl
`ifdef OR1200_IMPL_SUB
306
                `OR1200_ALUOP_SUB: begin
307
                        cyforw = cy_sub;
308
                        cy_we = 1'b1;
309
                end
310 10 unneback
`endif
311 141 marcus.erl
`endif
312 10 unneback
                default: begin
313
                        cyforw = 1'b0;
314
                        cy_we = 1'b0;
315
                end
316
        endcase
317
end
318
 
319 642 julius
 
320 10 unneback
//
321 642 julius
// Generate SR[OV] write enable
322
//
323
always @(alu_op or ov_sum) begin
324
        casez (alu_op)          // synopsys parallel_case
325
`ifdef OR1200_IMPL_OV
326
`ifdef OR1200_IMPL_ADDC
327
                `OR1200_ALUOP_ADDC,
328
`endif
329
`ifdef OR1200_IMPL_SUB
330
                `OR1200_ALUOP_SUB,
331
`endif
332
                `OR1200_ALUOP_ADD : begin
333
                        ovforw = ov_sum;
334
                        ov_we = 1'b1;
335
                end
336
`endif
337
                default: begin
338
                        ovforw = 1'b0;
339
                        ov_we = 1'b0;
340
                end
341
        endcase
342
end
343
 
344
//
345 10 unneback
// Shifts and rotation
346
//
347 640 julius
always @(alu_op2 or a or b) begin
348
        case (alu_op2)          // synopsys parallel_case
349
          `OR1200_SHROTOP_SLL :
350 10 unneback
                                shifted_rotated = (a << b[4:0]);
351 640 julius
          `OR1200_SHROTOP_SRL :
352 10 unneback
                                shifted_rotated = (a >> b[4:0]);
353
 
354
`ifdef OR1200_IMPL_ALU_ROTATE
355 640 julius
          `OR1200_SHROTOP_ROR :
356
                                shifted_rotated = (a << (6'd32-{1'b0,b[4:0]})) |
357
                                                  (a >> b[4:0]);
358 10 unneback
`endif
359 640 julius
          default:
360
                                shifted_rotated = ({32{a[31]}} <<
361
                                                   (6'd32-{1'b0, b[4:0]})) |
362
                                                  a >> b[4:0];
363 10 unneback
        endcase
364
end
365
 
366
//
367
// First type of compare implementation
368
//
369
`ifdef OR1200_IMPL_ALU_COMP1
370
always @(comp_op or a_eq_b or a_lt_b) begin
371
        case(comp_op[2:0])       // synopsys parallel_case
372
                `OR1200_COP_SFEQ:
373
                        flagcomp = a_eq_b;
374
                `OR1200_COP_SFNE:
375
                        flagcomp = ~a_eq_b;
376
                `OR1200_COP_SFGT:
377
                        flagcomp = ~(a_eq_b | a_lt_b);
378
                `OR1200_COP_SFGE:
379
                        flagcomp = ~a_lt_b;
380
                `OR1200_COP_SFLT:
381
                        flagcomp = a_lt_b;
382
                `OR1200_COP_SFLE:
383
                        flagcomp = a_eq_b | a_lt_b;
384
                default:
385
                        flagcomp = 1'b0;
386
        endcase
387
end
388
`endif
389
 
390
//
391
// Second type of compare implementation
392
//
393
`ifdef OR1200_IMPL_ALU_COMP2
394
always @(comp_op or comp_a or comp_b) begin
395
        case(comp_op[2:0])       // synopsys parallel_case
396
                `OR1200_COP_SFEQ:
397
                        flagcomp = (comp_a == comp_b);
398
                `OR1200_COP_SFNE:
399
                        flagcomp = (comp_a != comp_b);
400
                `OR1200_COP_SFGT:
401
                        flagcomp = (comp_a > comp_b);
402
                `OR1200_COP_SFGE:
403
                        flagcomp = (comp_a >= comp_b);
404
                `OR1200_COP_SFLT:
405
                        flagcomp = (comp_a < comp_b);
406
                `OR1200_COP_SFLE:
407
                        flagcomp = (comp_a <= comp_b);
408
                default:
409
                        flagcomp = 1'b0;
410
        endcase
411
end
412
`endif
413
 
414 640 julius
`ifdef OR1200_IMPL_ALU_EXT
415
   always @(alu_op or alu_op2 or a) begin
416
      casez (alu_op2)
417
        `OR1200_EXTHBOP_HS : extended = {{16{a[15]}},a[15:0]};
418
        `OR1200_EXTHBOP_BS : extended = {{24{a[7]}},a[7:0]};
419
        `OR1200_EXTHBOP_HZ : extended = {16'd0,a[15:0]};
420
        `OR1200_EXTHBOP_BZ : extended = {24'd0,a[7:0]};
421
        default: extended = a; // Used for l.extw instructions
422
      endcase // casez (alu_op2)
423
   end
424
`endif
425
 
426
 
427
//
428
// l.cust5 custom instructions
429
//
430
`ifdef OR1200_IMPL_ALU_CUST5
431
// Examples for move byte, set bit and clear bit
432
//
433
always @(cust5_op or cust5_limm or a or b) begin
434
        casez (cust5_op)                // synopsys parallel_case
435
                5'h1 : begin
436
                        casez (cust5_limm[1:0])
437
                          2'h0: result_cust5 = {a[31:8], b[7:0]};
438
                          2'h1: result_cust5 = {a[31:16], b[7:0], a[7:0]};
439
                          2'h2: result_cust5 = {a[31:24], b[7:0], a[15:0]};
440
                          2'h3: result_cust5 = {b[7:0], a[23:0]};
441
                        endcase
442
                end
443
                5'h2 :
444
                        result_cust5 = a | (1 << cust5_limm);
445
                5'h3 :
446
                        result_cust5 = a & (32'hffffffff ^ (1 << cust5_limm));
447
//
448
// *** Put here new l.cust5 custom instructions ***
449
//
450
                default: begin
451
                        result_cust5 = a;
452
                end
453
        endcase
454
end // always @ (cust5_op or cust5_limm or a or b)
455
`endif
456
 
457 10 unneback
endmodule

powered by: WebSVN 2.1.0

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