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

Subversion Repositories openrisc

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

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

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

powered by: WebSVN 2.1.0

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