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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [or1200/] [rtl/] [verilog/] [or1200_alu.v] - Blame information for rev 1774

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

Line No. Rev Author Line
1 504 lampret
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  OR1200's ALU                                                ////
4
////                                                              ////
5
////  This file is part of the OpenRISC 1200 project              ////
6
////  http://www.opencores.org/cores/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
// CVS Revision History
45
//
46
// $Log: not supported by cvs2svn $
47 1334 andreje
// Revision 1.14  2004/06/08 18:17:36  lampret
48
// Non-functional changes. Coding style fixes.
49
//
50 1293 lampret
// Revision 1.13  2004/05/09 19:49:03  lampret
51
// Added some l.cust5 custom instructions as example
52
//
53 1284 lampret
// Revision 1.12  2004/04/05 08:29:57  lampret
54
// Merged branch_qmem into main tree.
55
//
56 1267 lampret
// Revision 1.11  2003/04/24 00:16:07  lampret
57
// No functional changes. Added defines to disable implementation of multiplier/MAC
58
//
59 1159 lampret
// Revision 1.10  2002/09/08 05:52:16  lampret
60
// Added optional l.div/l.divu insns. By default they are disabled.
61
//
62 1035 lampret
// Revision 1.9  2002/09/07 19:16:10  lampret
63
// If SR[CY] implemented with OR1200_IMPL_ADDC enabled, l.add/l.addi also set SR[CY].
64
//
65 1033 lampret
// Revision 1.8  2002/09/07 05:42:02  lampret
66
// Added optional SR[CY]. Added define to enable additional (compare) flag modifiers. Defines are OR1200_IMPL_ADDC and OR1200_ADDITIONAL_FLAG_MODIFIERS.
67
//
68 1032 lampret
// Revision 1.7  2002/09/03 22:28:21  lampret
69
// As per Taylor Su suggestion all case blocks are full case by default and optionally (OR1200_CASE_DEFAULT) can be disabled to increase clock frequncy.
70
//
71 1022 lampret
// Revision 1.6  2002/03/29 16:40:10  lampret
72
// Added a directive to ignore signed division variables that are only used in simulation.
73
//
74 795 lampret
// Revision 1.5  2002/03/29 16:33:59  lampret
75
// Added again just recently removed full_case directive
76
//
77 794 lampret
// Revision 1.4  2002/03/29 15:16:53  lampret
78
// Some of the warnings fixed.
79
//
80 788 lampret
// Revision 1.3  2002/01/28 01:15:59  lampret
81
// Changed 'void' nop-ops instead of insn[0] to use insn[16]. Debug unit stalls the tick timer. Prepared new flag generation for add and and insns. Blocked DC/IC while they are turned off. Fixed I/D MMU SPRs layout except WAYs. TODO: smart IC invalidate, l.j 2 and TLB ways.
82
//
83 617 lampret
// Revision 1.2  2002/01/14 06:18:22  lampret
84
// Fixed mem2reg bug in FAST implementation. Updated debug unit to work with new genpc/if.
85
//
86 562 lampret
// Revision 1.1  2002/01/03 08:16:15  lampret
87
// New prefixes for RTL files, prefixed module names. Updated cache controllers and MMUs.
88
//
89 504 lampret
// Revision 1.10  2001/11/12 01:45:40  lampret
90
// Moved flag bit into SR. Changed RF enable from constant enable to dynamic enable for read ports.
91
//
92
// Revision 1.9  2001/10/21 17:57:16  lampret
93
// 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.
94
//
95
// Revision 1.8  2001/10/19 23:28:45  lampret
96
// Fixed some synthesis warnings. Configured with caches and MMUs.
97
//
98
// Revision 1.7  2001/10/14 13:12:09  lampret
99
// MP3 version.
100
//
101
// Revision 1.1.1.1  2001/10/06 10:18:35  igorm
102
// no message
103
//
104
// Revision 1.2  2001/08/09 13:39:33  lampret
105
// Major clean-up.
106
//
107
// Revision 1.1  2001/07/20 00:46:03  lampret
108
// Development version of RTL. Libraries are missing.
109
//
110
//
111
 
112
// synopsys translate_off
113
`include "timescale.v"
114
// synopsys translate_on
115
`include "or1200_defines.v"
116
 
117
module or1200_alu(
118
        a, b, mult_mac_result, macrc_op,
119
        alu_op, shrot_op, comp_op,
120 1284 lampret
        cust5_op, cust5_limm,
121 1032 lampret
        result, flagforw, flag_we,
122 1334 andreje
        cyforw, cy_we, carry, flag
123 504 lampret
);
124
 
125
parameter width = `OR1200_OPERAND_WIDTH;
126
 
127
//
128
// I/O
129
//
130
input   [width-1:0]              a;
131
input   [width-1:0]              b;
132
input   [width-1:0]              mult_mac_result;
133
input                           macrc_op;
134
input   [`OR1200_ALUOP_WIDTH-1:0]        alu_op;
135
input   [`OR1200_SHROTOP_WIDTH-1:0]      shrot_op;
136
input   [`OR1200_COMPOP_WIDTH-1:0]       comp_op;
137 1284 lampret
input   [4:0]                    cust5_op;
138
input   [5:0]                    cust5_limm;
139 504 lampret
output  [width-1:0]              result;
140
output                          flagforw;
141
output                          flag_we;
142 1032 lampret
output                          cyforw;
143
output                          cy_we;
144
input                           carry;
145 1334 andreje
input         flag;
146 504 lampret
 
147
//
148
// Internal wires and regs
149
//
150
reg     [width-1:0]              result;
151
reg     [width-1:0]              shifted_rotated;
152 1284 lampret
reg     [width-1:0]              result_cust5;
153 504 lampret
reg                             flagforw;
154 617 lampret
reg                             flagcomp;
155 504 lampret
reg                             flag_we;
156 1032 lampret
reg                             cy_we;
157 504 lampret
wire    [width-1:0]              comp_a;
158
wire    [width-1:0]              comp_b;
159
`ifdef OR1200_IMPL_ALU_COMP1
160
wire                            a_eq_b;
161
wire                            a_lt_b;
162
`endif
163 617 lampret
wire    [width-1:0]              result_sum;
164 1032 lampret
`ifdef OR1200_IMPL_ADDC
165
wire    [width-1:0]              result_csum;
166 1033 lampret
wire                            cy_csum;
167 1032 lampret
`endif
168 617 lampret
wire    [width-1:0]              result_and;
169 1033 lampret
wire                            cy_sum;
170
reg                             cyforw;
171 504 lampret
 
172
//
173
// Combinatorial logic
174
//
175
assign comp_a = {a[width-1] ^ comp_op[3] , a[width-2:0]};
176
assign comp_b = {b[width-1] ^ comp_op[3] , b[width-2:0]};
177
`ifdef OR1200_IMPL_ALU_COMP1
178
assign a_eq_b = (comp_a == comp_b);
179
assign a_lt_b = (comp_a < comp_b);
180
`endif
181 1033 lampret
assign {cy_sum, result_sum} = a + b;
182 1032 lampret
`ifdef OR1200_IMPL_ADDC
183 1293 lampret
assign {cy_csum, result_csum} = a + b + {32'd0, carry};
184 1032 lampret
`endif
185 617 lampret
assign result_and = a & b;
186 504 lampret
 
187
//
188
// Simulation check for bad ALU behavior
189
//
190
`ifdef OR1200_WARNINGS
191
// synopsys translate_off
192
always @(result) begin
193
        if (result === 32'bx)
194
                $display("%t: WARNING: 32'bx detected on ALU result bus. Please check !", $time);
195
end
196
// synopsys translate_on
197
`endif
198
 
199
//
200
// Central part of the ALU
201
//
202 617 lampret
always @(alu_op or a or b or result_sum or result_and or macrc_op or shifted_rotated or mult_mac_result) begin
203 1022 lampret
`ifdef OR1200_CASE_DEFAULT
204
        casex (alu_op)          // synopsys parallel_case
205
`else
206
        casex (alu_op)          // synopsys full_case parallel_case
207
`endif
208 1334 andreje
    `OR1200_ALUOP_FF1: begin
209
        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;
210
    end
211 1284 lampret
                `OR1200_ALUOP_CUST5 : begin
212
                                result = result_cust5;
213
                end
214 504 lampret
                `OR1200_ALUOP_SHROT : begin
215
                                result = shifted_rotated;
216
                end
217
                `OR1200_ALUOP_ADD : begin
218 617 lampret
                                result = result_sum;
219 504 lampret
                end
220 1032 lampret
`ifdef OR1200_IMPL_ADDC
221
                `OR1200_ALUOP_ADDC : begin
222
                                result = result_csum;
223
                end
224
`endif
225 504 lampret
                `OR1200_ALUOP_SUB : begin
226
                                result = a - b;
227
                end
228
                `OR1200_ALUOP_XOR : begin
229
                                result = a ^ b;
230
                end
231
                `OR1200_ALUOP_OR  : begin
232
                                result = a | b;
233
                end
234
                `OR1200_ALUOP_IMM : begin
235
                                result = b;
236
                end
237
                `OR1200_ALUOP_MOVHI : begin
238
                                if (macrc_op) begin
239
                                        result = mult_mac_result;
240
                                end
241
                                else begin
242
                                        result = b << 16;
243
                                end
244
                end
245 1159 lampret
`ifdef OR1200_MULT_IMPLEMENTED
246 1035 lampret
`ifdef OR1200_IMPL_DIV
247
                `OR1200_ALUOP_DIV,
248
                `OR1200_ALUOP_DIVU,
249
`endif
250 504 lampret
                `OR1200_ALUOP_MUL : begin
251
                                result = mult_mac_result;
252
                end
253 1159 lampret
`endif
254 1334 andreje
    `OR1200_ALUOP_CMOV: begin
255
        result = flag ? a : b;
256
    end
257
 
258 1022 lampret
`ifdef OR1200_CASE_DEFAULT
259 1334 andreje
    default: begin
260 1022 lampret
`else
261 1334 andreje
    `OR1200_ALUOP_COMP, `OR1200_ALUOP_AND:
262
    begin
263 1022 lampret
`endif
264 1334 andreje
      result=result_and;
265
    end
266 617 lampret
        endcase
267
end
268
 
269
//
270 1284 lampret
// l.cust5 custom instructions
271
//
272
// Examples for move byte, set bit and clear bit
273
//
274
always @(cust5_op or cust5_limm or a or b) begin
275
        casex (cust5_op)                // synopsys parallel_case
276
                5'h1 : begin
277 1293 lampret
                        casex (cust5_limm[1:0])
278 1284 lampret
                                2'h0: result_cust5 = {a[31:8], b[7:0]};
279
                                2'h1: result_cust5 = {a[31:16], b[7:0], a[7:0]};
280
                                2'h2: result_cust5 = {a[31:24], b[7:0], a[15:0]};
281
                                2'h3: result_cust5 = {b[7:0], a[23:0]};
282
                        endcase
283
                end
284
                5'h2 :
285
                        result_cust5 = a | (1 << cust5_limm);
286
                5'h3 :
287
                        result_cust5 = a & (32'hffffffff ^ (1 << cust5_limm));
288
//
289
// *** Put here new l.cust5 custom instructions ***
290
//
291
                default: begin
292
                        result_cust5 = a;
293
                end
294
        endcase
295
end
296
 
297
//
298 617 lampret
// Generate flag and flag write enable
299
//
300
always @(alu_op or result_sum or result_and or flagcomp) begin
301 788 lampret
        casex (alu_op)          // synopsys parallel_case
302 1032 lampret
`ifdef OR1200_ADDITIONAL_FLAG_MODIFIERS
303 617 lampret
                `OR1200_ALUOP_ADD : begin
304
                        flagforw = (result_sum == 32'h0000_0000);
305 1032 lampret
                        flag_we = 1'b1;
306 617 lampret
                end
307 1032 lampret
`ifdef OR1200_IMPL_ADDC
308
                `OR1200_ALUOP_ADDC : begin
309
                        flagforw = (result_csum == 32'h0000_0000);
310
                        flag_we = 1'b1;
311
                end
312
`endif
313 617 lampret
                `OR1200_ALUOP_AND: begin
314
                        flagforw = (result_and == 32'h0000_0000);
315 1032 lampret
                        flag_we = 1'b1;
316 617 lampret
                end
317 1032 lampret
`endif
318 504 lampret
                `OR1200_ALUOP_COMP: begin
319 617 lampret
                        flagforw = flagcomp;
320
                        flag_we = 1'b1;
321 504 lampret
                end
322 617 lampret
                default: begin
323
                        flagforw = 1'b0;
324
                        flag_we = 1'b0;
325 504 lampret
                end
326
        endcase
327
end
328
 
329
//
330 1032 lampret
// Generate SR[CY] write enable
331
//
332 1035 lampret
always @(alu_op or cy_sum
333
`ifdef OR1200_IMPL_ADDC
334
        or cy_csum
335
`endif
336
        ) begin
337 1032 lampret
        casex (alu_op)          // synopsys parallel_case
338 1267 lampret
`ifdef OR1200_IMPL_CY
339 1033 lampret
                `OR1200_ALUOP_ADD : begin
340
                        cyforw = cy_sum;
341 1032 lampret
                        cy_we = 1'b1;
342
                end
343 1267 lampret
`ifdef OR1200_IMPL_ADDC
344 1033 lampret
                `OR1200_ALUOP_ADDC: begin
345
                        cyforw = cy_csum;
346
                        cy_we = 1'b1;
347
                end
348 1032 lampret
`endif
349 1267 lampret
`endif
350 1032 lampret
                default: begin
351 1033 lampret
                        cyforw = 1'b0;
352 1032 lampret
                        cy_we = 1'b0;
353
                end
354
        endcase
355
end
356
 
357
//
358 504 lampret
// Shifts and rotation
359
//
360
always @(shrot_op or a or b) begin
361
        case (shrot_op)         // synopsys parallel_case
362 562 lampret
        `OR1200_SHROTOP_SLL :
363 504 lampret
                                shifted_rotated = (a << b[4:0]);
364
                `OR1200_SHROTOP_SRL :
365
                                shifted_rotated = (a >> b[4:0]);
366 562 lampret
 
367 504 lampret
`ifdef OR1200_IMPL_ALU_ROTATE
368
                `OR1200_SHROTOP_ROR :
369
                                shifted_rotated = (a << (6'd32-{1'b0, b[4:0]})) | (a >> b[4:0]);
370
`endif
371
                default:
372
                                shifted_rotated = ({32{a[31]}} << (6'd32-{1'b0, b[4:0]})) | a >> b[4:0];
373
        endcase
374
end
375
 
376
//
377
// First type of compare implementation
378
//
379
`ifdef OR1200_IMPL_ALU_COMP1
380
always @(comp_op or a_eq_b or a_lt_b) begin
381 788 lampret
        case(comp_op[2:0])       // synopsys parallel_case
382 504 lampret
                `OR1200_COP_SFEQ:
383 617 lampret
                        flagcomp = a_eq_b;
384 504 lampret
                `OR1200_COP_SFNE:
385 617 lampret
                        flagcomp = ~a_eq_b;
386 504 lampret
                `OR1200_COP_SFGT:
387 617 lampret
                        flagcomp = ~(a_eq_b | a_lt_b);
388 504 lampret
                `OR1200_COP_SFGE:
389 617 lampret
                        flagcomp = ~a_lt_b;
390 504 lampret
                `OR1200_COP_SFLT:
391 617 lampret
                        flagcomp = a_lt_b;
392 504 lampret
                `OR1200_COP_SFLE:
393 617 lampret
                        flagcomp = a_eq_b | a_lt_b;
394 504 lampret
                default:
395 617 lampret
                        flagcomp = 1'b0;
396 504 lampret
        endcase
397
end
398
`endif
399
 
400
//
401
// Second type of compare implementation
402
//
403
`ifdef OR1200_IMPL_ALU_COMP2
404
always @(comp_op or comp_a or comp_b) begin
405 788 lampret
        case(comp_op[2:0])       // synopsys parallel_case
406 504 lampret
                `OR1200_COP_SFEQ:
407 617 lampret
                        flagcomp = (comp_a == comp_b);
408 504 lampret
                `OR1200_COP_SFNE:
409 617 lampret
                        flagcomp = (comp_a != comp_b);
410 504 lampret
                `OR1200_COP_SFGT:
411 617 lampret
                        flagcomp = (comp_a > comp_b);
412 504 lampret
                `OR1200_COP_SFGE:
413 617 lampret
                        flagcomp = (comp_a >= comp_b);
414 504 lampret
                `OR1200_COP_SFLT:
415 617 lampret
                        flagcomp = (comp_a < comp_b);
416 504 lampret
                `OR1200_COP_SFLE:
417 617 lampret
                        flagcomp = (comp_a <= comp_b);
418 504 lampret
                default:
419 617 lampret
                        flagcomp = 1'b0;
420 504 lampret
        endcase
421
end
422
`endif
423
 
424
endmodule

powered by: WebSVN 2.1.0

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