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

Subversion Repositories openrisc

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

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
wire                            a_eq_b;
104
wire                            a_lt_b;
105
wire    [width-1:0]              result_sum;
106
wire    [width-1:0]              result_and;
107
wire                            cy_sum;
108 141 marcus.erl
`ifdef OR1200_IMPL_SUB
109
wire                            cy_sub;
110
`endif
111 642 julius
wire                            ov_sum;
112
wire    [width-1:0]              carry_in;
113 10 unneback
 
114 642 julius
wire    [width-1:0]              b_mux;
115
 
116
 
117
 
118 10 unneback
//
119
// Combinatorial logic
120
//
121 643 julius
 
122 10 unneback
assign comp_a = {a[width-1] ^ comp_op[3] , a[width-2:0]};
123
assign comp_b = {b[width-1] ^ comp_op[3] , b[width-2:0]};
124
`ifdef OR1200_IMPL_ALU_COMP1
125
assign a_eq_b = (comp_a == comp_b);
126
assign a_lt_b = (comp_a < comp_b);
127
`endif
128 643 julius
`ifdef OR1200_IMPL_ALU_COMP3
129
assign a_eq_b = !(|result_sum);
130
// signed compare when comp_op[3] is set
131
assign a_lt_b = comp_op[3] ? ((a[width-1] & !b[width-1]) |
132
                              (!a[width-1] & !b[width-1] & result_sum[width-1])|
133
                              (a[width-1] & b[width-1] & result_sum[width-1])):
134
// a < b if (a - b) subtraction wrapped and a[width-1] wasn't set
135
                (result_sum[width-1] & !a[width-1]) |
136
// or if (a - b) wrapped and both a[width-1] and b[width-1] were set
137
                (result_sum[width-1] & a[width-1] & b[width-1] );
138
`endif
139
 
140 141 marcus.erl
`ifdef OR1200_IMPL_SUB
141 643 julius
 `ifdef OR1200_IMPL_ALU_COMP3
142
assign cy_sub = a_lt_b;
143
 `else
144 642 julius
assign cy_sub = (comp_a < comp_b);
145 643 julius
 `endif
146 141 marcus.erl
`endif
147 643 julius
 
148 642 julius
`ifdef OR1200_IMPL_ADDC
149
assign carry_in = (alu_op==`OR1200_ALUOP_ADDC) ?
150
                  {{width-1{1'b0}},carry} : {width{1'b0}};
151
`else
152
assign carry_in = {width-1{1'b0}};
153 10 unneback
`endif
154 643 julius
 
155
`ifdef OR1200_IMPL_ALU_COMP3
156 642 julius
`ifdef OR1200_IMPL_SUB
157 643 julius
assign b_mux = ((alu_op==`OR1200_ALUOP_SUB) | (alu_op==`OR1200_ALUOP_COMP)) ?
158
                (~b)+1 : b;
159
`else
160
assign b_mux = (alu_op==`OR1200_ALUOP_COMP) ? (~b)+1 : b;
161
`endif
162
`else // ! `ifdef OR1200_IMPL_ALU_COMP3
163
`ifdef OR1200_IMPL_SUB
164 642 julius
assign b_mux = (alu_op==`OR1200_ALUOP_SUB) ? (~b)+1 : b;
165
`else
166
assign b_mux = b;
167 643 julius
`endif
168
`endif
169 642 julius
assign {cy_sum, result_sum} = (a + b_mux) + carry_in;
170
// Numbers either both +ve and bit 31 of result set
171
assign ov_sum = ((!a[width-1] & !b_mux[width-1]) & result_sum[width-1]) |
172
// or both -ve and bit 31 of result clear
173
                ((a[width-1] & b_mux[width-1]) & !result_sum[width-1]);
174 10 unneback
assign result_and = a & b;
175
 
176
//
177
// Simulation check for bad ALU behavior
178
//
179
`ifdef OR1200_WARNINGS
180
// synopsys translate_off
181
always @(result) begin
182
        if (result === 32'bx)
183
                $display("%t: WARNING: 32'bx detected on ALU result bus. Please check !", $time);
184
end
185
// synopsys translate_on
186
`endif
187
 
188
//
189
// Central part of the ALU
190
//
191 401 julius
always @(alu_op or alu_op2 or a or b or result_sum or result_and or macrc_op
192
         or shifted_rotated or mult_mac_result or flag or result_cust5 or carry
193 640 julius
`ifdef OR1200_IMPL_ALU_EXT
194
         or extended
195
`endif
196 141 marcus.erl
) begin
197 10 unneback
`ifdef OR1200_CASE_DEFAULT
198 364 julius
        casez (alu_op)          // synopsys parallel_case
199 10 unneback
`else
200 364 julius
        casez (alu_op)          // synopsys full_case parallel_case
201 10 unneback
`endif
202 401 julius
`ifdef OR1200_IMPL_ALU_FFL1
203
                `OR1200_ALUOP_FFL1: begin
204
`ifdef OR1200_CASE_DEFAULT
205
                   casez (alu_op2) // synopsys parallel_case
206
`else
207
                   casez (alu_op2) // synopsys full_case parallel_case
208
`endif
209
                     0: begin // FF1
210 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;
211 401 julius
                     end
212
                     default: begin // FL1
213
                        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 ;
214
                     end
215
                   endcase // casez (alu_op2)
216
                end // case: `OR1200_ALUOP_FFL1
217 640 julius
`endif //  `ifdef OR1200_IMPL_ALU_FFL1
218
`ifdef OR1200_IMPL_ALU_CUST5
219
 
220 10 unneback
                `OR1200_ALUOP_CUST5 : begin
221
                                result = result_cust5;
222
                end
223 640 julius
`endif
224 10 unneback
                `OR1200_ALUOP_SHROT : begin
225
                                result = shifted_rotated;
226
                end
227
`ifdef OR1200_IMPL_ADDC
228 642 julius
                `OR1200_ALUOP_ADDC,
229 10 unneback
`endif
230 141 marcus.erl
`ifdef OR1200_IMPL_SUB
231 642 julius
                `OR1200_ALUOP_SUB,
232
`endif
233
                `OR1200_ALUOP_ADD : begin
234
                                result = result_sum;
235 10 unneback
                end
236
                `OR1200_ALUOP_XOR : begin
237
                                result = a ^ b;
238
                end
239
                `OR1200_ALUOP_OR  : begin
240
                                result = a | b;
241
                end
242 640 julius
`ifdef OR1200_IMPL_ALU_EXT
243
                `OR1200_ALUOP_EXTHB  : begin
244
                                result = extended;
245
                end
246
                `OR1200_ALUOP_EXTW  : begin
247
                                result = extended;
248
                end
249
`endif
250 10 unneback
                `OR1200_ALUOP_MOVHI : begin
251
                                if (macrc_op) begin
252
                                        result = mult_mac_result;
253
                                end
254
                                else begin
255
                                        result = b << 16;
256
                                end
257
                end
258
`ifdef OR1200_MULT_IMPLEMENTED
259 258 julius
`ifdef OR1200_DIV_IMPLEMENTED
260 10 unneback
                `OR1200_ALUOP_DIV,
261
                `OR1200_ALUOP_DIVU,
262
`endif
263 481 julius
                `OR1200_ALUOP_MUL,
264
                `OR1200_ALUOP_MULU : begin
265 10 unneback
                                result = mult_mac_result;
266
                end
267
`endif
268 141 marcus.erl
                `OR1200_ALUOP_CMOV: begin
269
                        result = flag ? a : b;
270
                end
271 10 unneback
 
272
`ifdef OR1200_CASE_DEFAULT
273 141 marcus.erl
                default: begin
274 10 unneback
`else
275 141 marcus.erl
                `OR1200_ALUOP_COMP, `OR1200_ALUOP_AND: begin
276 10 unneback
`endif
277 141 marcus.erl
                        result=result_and;
278
                end
279 10 unneback
        endcase
280
end
281
 
282
//
283
// Generate flag and flag write enable
284
//
285 141 marcus.erl
always @(alu_op or result_sum or result_and or flagcomp
286
) begin
287 364 julius
        casez (alu_op)          // synopsys parallel_case
288 10 unneback
`ifdef OR1200_ADDITIONAL_FLAG_MODIFIERS
289 642 julius
`ifdef OR1200_IMPL_ADDC
290
                `OR1200_ALUOP_ADDC,
291
`endif
292 10 unneback
                `OR1200_ALUOP_ADD : begin
293
                        flagforw = (result_sum == 32'h0000_0000);
294
                        flag_we = 1'b1;
295
                end
296
                `OR1200_ALUOP_AND: begin
297
                        flagforw = (result_and == 32'h0000_0000);
298
                        flag_we = 1'b1;
299
                end
300
`endif
301
                `OR1200_ALUOP_COMP: begin
302
                        flagforw = flagcomp;
303
                        flag_we = 1'b1;
304
                end
305
                default: begin
306 141 marcus.erl
                        flagforw = flagcomp;
307 10 unneback
                        flag_we = 1'b0;
308
                end
309
        endcase
310
end
311
 
312
//
313
// Generate SR[CY] write enable
314
//
315
always @(alu_op or cy_sum
316 141 marcus.erl
`ifdef OR1200_IMPL_CY
317
`ifdef OR1200_IMPL_SUB
318
        or cy_sub
319
`endif
320
`endif
321
) begin
322 364 julius
        casez (alu_op)          // synopsys parallel_case
323 10 unneback
`ifdef OR1200_IMPL_CY
324 642 julius
`ifdef OR1200_IMPL_ADDC
325
                `OR1200_ALUOP_ADDC,
326
`endif
327 10 unneback
                `OR1200_ALUOP_ADD : begin
328
                        cyforw = cy_sum;
329
                        cy_we = 1'b1;
330
                end
331 141 marcus.erl
`ifdef OR1200_IMPL_SUB
332
                `OR1200_ALUOP_SUB: begin
333
                        cyforw = cy_sub;
334
                        cy_we = 1'b1;
335
                end
336 10 unneback
`endif
337 141 marcus.erl
`endif
338 10 unneback
                default: begin
339
                        cyforw = 1'b0;
340
                        cy_we = 1'b0;
341
                end
342
        endcase
343
end
344
 
345 642 julius
 
346 10 unneback
//
347 642 julius
// Generate SR[OV] write enable
348
//
349
always @(alu_op or ov_sum) begin
350
        casez (alu_op)          // synopsys parallel_case
351
`ifdef OR1200_IMPL_OV
352
`ifdef OR1200_IMPL_ADDC
353
                `OR1200_ALUOP_ADDC,
354
`endif
355
`ifdef OR1200_IMPL_SUB
356
                `OR1200_ALUOP_SUB,
357
`endif
358
                `OR1200_ALUOP_ADD : begin
359
                        ovforw = ov_sum;
360
                        ov_we = 1'b1;
361
                end
362
`endif
363
                default: begin
364
                        ovforw = 1'b0;
365
                        ov_we = 1'b0;
366
                end
367
        endcase
368
end
369
 
370
//
371 10 unneback
// Shifts and rotation
372
//
373 640 julius
always @(alu_op2 or a or b) begin
374
        case (alu_op2)          // synopsys parallel_case
375
          `OR1200_SHROTOP_SLL :
376 10 unneback
                                shifted_rotated = (a << b[4:0]);
377 640 julius
          `OR1200_SHROTOP_SRL :
378 10 unneback
                                shifted_rotated = (a >> b[4:0]);
379
 
380
`ifdef OR1200_IMPL_ALU_ROTATE
381 640 julius
          `OR1200_SHROTOP_ROR :
382
                                shifted_rotated = (a << (6'd32-{1'b0,b[4:0]})) |
383
                                                  (a >> b[4:0]);
384 10 unneback
`endif
385 640 julius
          default:
386
                                shifted_rotated = ({32{a[31]}} <<
387
                                                   (6'd32-{1'b0, b[4:0]})) |
388
                                                  a >> b[4:0];
389 10 unneback
        endcase
390
end
391
 
392
//
393
// First type of compare implementation
394
//
395
`ifdef OR1200_IMPL_ALU_COMP1
396
always @(comp_op or a_eq_b or a_lt_b) begin
397
        case(comp_op[2:0])       // synopsys parallel_case
398
                `OR1200_COP_SFEQ:
399
                        flagcomp = a_eq_b;
400
                `OR1200_COP_SFNE:
401
                        flagcomp = ~a_eq_b;
402
                `OR1200_COP_SFGT:
403
                        flagcomp = ~(a_eq_b | a_lt_b);
404
                `OR1200_COP_SFGE:
405
                        flagcomp = ~a_lt_b;
406
                `OR1200_COP_SFLT:
407
                        flagcomp = a_lt_b;
408
                `OR1200_COP_SFLE:
409
                        flagcomp = a_eq_b | a_lt_b;
410
                default:
411
                        flagcomp = 1'b0;
412
        endcase
413
end
414
`endif
415
 
416
//
417
// Second type of compare implementation
418
//
419
`ifdef OR1200_IMPL_ALU_COMP2
420
always @(comp_op or comp_a or comp_b) begin
421
        case(comp_op[2:0])       // synopsys parallel_case
422
                `OR1200_COP_SFEQ:
423
                        flagcomp = (comp_a == comp_b);
424
                `OR1200_COP_SFNE:
425
                        flagcomp = (comp_a != comp_b);
426
                `OR1200_COP_SFGT:
427
                        flagcomp = (comp_a > comp_b);
428
                `OR1200_COP_SFGE:
429
                        flagcomp = (comp_a >= comp_b);
430
                `OR1200_COP_SFLT:
431
                        flagcomp = (comp_a < comp_b);
432
                `OR1200_COP_SFLE:
433
                        flagcomp = (comp_a <= comp_b);
434
                default:
435
                        flagcomp = 1'b0;
436
        endcase
437
end
438 643 julius
`endif //  `ifdef OR1200_IMPL_ALU_COMP2
439
 
440
`ifdef OR1200_IMPL_ALU_COMP3
441
always @(comp_op or a_eq_b or a_lt_b) begin
442
        case(comp_op[2:0])       // synopsys parallel_case
443
                `OR1200_COP_SFEQ:
444
                        flagcomp = a_eq_b;
445
                `OR1200_COP_SFNE:
446
                        flagcomp = ~a_eq_b;
447
                `OR1200_COP_SFGT:
448
                        flagcomp = ~(a_eq_b | a_lt_b);
449
                `OR1200_COP_SFGE:
450
                        flagcomp = ~a_lt_b;
451
                `OR1200_COP_SFLT:
452
                        flagcomp = a_lt_b;
453
                `OR1200_COP_SFLE:
454
                        flagcomp = a_eq_b | a_lt_b;
455
                default:
456
                        flagcomp = 1'b0;
457
        endcase
458
end
459 10 unneback
`endif
460 643 julius
 
461 10 unneback
 
462 640 julius
`ifdef OR1200_IMPL_ALU_EXT
463
   always @(alu_op or alu_op2 or a) begin
464
      casez (alu_op2)
465
        `OR1200_EXTHBOP_HS : extended = {{16{a[15]}},a[15:0]};
466
        `OR1200_EXTHBOP_BS : extended = {{24{a[7]}},a[7:0]};
467
        `OR1200_EXTHBOP_HZ : extended = {16'd0,a[15:0]};
468
        `OR1200_EXTHBOP_BZ : extended = {24'd0,a[7:0]};
469
        default: extended = a; // Used for l.extw instructions
470
      endcase // casez (alu_op2)
471
   end
472
`endif
473
 
474
 
475
//
476
// l.cust5 custom instructions
477
//
478
`ifdef OR1200_IMPL_ALU_CUST5
479
// Examples for move byte, set bit and clear bit
480
//
481
always @(cust5_op or cust5_limm or a or b) begin
482
        casez (cust5_op)                // synopsys parallel_case
483
                5'h1 : begin
484
                        casez (cust5_limm[1:0])
485
                          2'h0: result_cust5 = {a[31:8], b[7:0]};
486
                          2'h1: result_cust5 = {a[31:16], b[7:0], a[7:0]};
487
                          2'h2: result_cust5 = {a[31:24], b[7:0], a[15:0]};
488
                          2'h3: result_cust5 = {b[7:0], a[23:0]};
489
                        endcase
490
                end
491
                5'h2 :
492
                        result_cust5 = a | (1 << cust5_limm);
493
                5'h3 :
494
                        result_cust5 = a & (32'hffffffff ^ (1 << cust5_limm));
495
//
496
// *** Put here new l.cust5 custom instructions ***
497
//
498
                default: begin
499
                        result_cust5 = a;
500
                end
501
        endcase
502
end // always @ (cust5_op or cust5_limm or a or b)
503
`endif
504
 
505 10 unneback
endmodule

powered by: WebSVN 2.1.0

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