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

Subversion Repositories versatile_library

[/] [versatile_library/] [trunk/] [rtl/] [verilog/] [arith.v] - Blame information for rev 153

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 18 unneback
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  Arithmetic functions                                        ////
4
////                                                              ////
5
////  Description                                                 ////
6
////  Arithmetic functions for ALU and DSP                        ////
7
////                                                              ////
8
////                                                              ////
9
////  To Do:                                                      ////
10
////   -                                                          ////
11
////                                                              ////
12
////  Author(s):                                                  ////
13
////      - Michael Unneback, unneback@opencores.org              ////
14
////        ORSoC AB                                              ////
15
////                                                              ////
16
//////////////////////////////////////////////////////////////////////
17
////                                                              ////
18
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
19
////                                                              ////
20
//// This source file may be used and distributed without         ////
21
//// restriction provided that this copyright statement is not    ////
22
//// removed from the file and that any derivative work contains  ////
23
//// the original copyright notice and the associated disclaimer. ////
24
////                                                              ////
25
//// This source file is free software; you can redistribute it   ////
26
//// and/or modify it under the terms of the GNU Lesser General   ////
27
//// Public License as published by the Free Software Foundation; ////
28
//// either version 2.1 of the License, or (at your option) any   ////
29
//// later version.                                               ////
30
////                                                              ////
31
//// This source is distributed in the hope that it will be       ////
32
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
33
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
34
//// PURPOSE.  See the GNU Lesser General Public License for more ////
35
//// details.                                                     ////
36
////                                                              ////
37
//// You should have received a copy of the GNU Lesser General    ////
38
//// Public License along with this source; if not, download it   ////
39
//// from http://www.opencores.org/lgpl.shtml                     ////
40
////                                                              ////
41
//////////////////////////////////////////////////////////////////////
42
 
43 40 unneback
`ifdef MULTS
44 18 unneback
// signed multiplication
45 40 unneback
`define MODULE mults
46
module `BASE`MODULE (a,b,p);
47
`undef MODULE
48 18 unneback
parameter operand_a_width = 18;
49
parameter operand_b_width = 18;
50
parameter result_hi = 35;
51
parameter result_lo = 0;
52
input [operand_a_width-1:0] a;
53
input [operand_b_width-1:0] b;
54
output [result_hi:result_lo] p;
55
wire signed [operand_a_width-1:0] ai;
56
wire signed [operand_b_width-1:0] bi;
57
wire signed [operand_a_width+operand_b_width-1:0] result;
58
 
59
    assign ai = a;
60
    assign bi = b;
61
    assign result = ai * bi;
62
    assign p = result[result_hi:result_lo];
63
 
64
endmodule
65 40 unneback
`endif
66
`ifdef MULTS18X18
67
`define MODULE mults18x18
68
module `BASE`MODULE (a,b,p);
69
`undef MODULE
70 18 unneback
input [17:0] a,b;
71
output [35:0] p;
72
vl_mult
73
    # (.operand_a_width(18), .operand_b_width(18))
74
    mult0 (.a(a), .b(b), .p(p));
75
endmodule
76 40 unneback
`endif
77 18 unneback
 
78 40 unneback
`ifdef MULT
79
`define MODULE mult
80 18 unneback
// unsigned multiplication
81 40 unneback
module `BASE`MODULE (a,b,p);
82
`undef MODULE
83 18 unneback
parameter operand_a_width = 18;
84
parameter operand_b_width = 18;
85
parameter result_hi = 35;
86
parameter result_lo = 0;
87
input [operand_a_width-1:0] a;
88
input [operand_b_width-1:0] b;
89
output [result_hi:result_hi] p;
90
 
91
wire [operand_a_width+operand_b_width-1:0] result;
92
 
93
    assign result = a * b;
94
    assign p = result[result_hi:result_lo];
95
 
96
endmodule
97 40 unneback
`endif
98 18 unneback
 
99 40 unneback
`ifdef SHIFT_UNIT_32
100
`define MODULE shift_unit_32
101 18 unneback
// shift unit
102
// supporting the following shift functions
103
//   SLL
104
//   SRL
105
//   SRA
106
`define SHIFT_UNIT_MULT # ( .operand_a_width(25), .operand_b_width(16), .result_hi(14), .result_lo(7))
107 40 unneback
module `BASE`MODULE( din, s, dout, opcode);
108
`undef MODULE
109 18 unneback
input [31:0] din; // data in operand
110
input [4:0] s; // shift operand
111
input [1:0] opcode;
112
output [31:0] dout;
113
 
114
parameter opcode_sll = 2'b00;
115 149 unneback
parameter opcode_srl = 2'b01;
116 18 unneback
parameter opcode_sra = 2'b10;
117 149 unneback
parameter opcode_ror = 2'b11;
118 18 unneback
 
119 149 unneback
parameter mult=0; // if set to 1 implemented based on multipliers which saves LUT
120
 
121
generate
122
if (mult==1) begin : impl_mult
123 18 unneback
wire sll, sra;
124
assign sll = opcode == opcode_sll;
125
assign sra = opcode == opcode_sra;
126
 
127
wire [15:1] s1;
128
wire [3:0] sign;
129
wire [7:0] tmp [0:3];
130
 
131
// first stage is multiplier based
132
// shift operand as fractional 8.7
133
assign s1[15] = sll & s[2:0]==3'd7;
134
assign s1[14] = sll & s[2:0]==3'd6;
135
assign s1[13] = sll & s[2:0]==3'd5;
136
assign s1[12] = sll & s[2:0]==3'd4;
137
assign s1[11] = sll & s[2:0]==3'd3;
138
assign s1[10] = sll & s[2:0]==3'd2;
139
assign s1[ 9] = sll & s[2:0]==3'd1;
140
assign s1[ 8] = s[2:0]==3'd0;
141
assign s1[ 7] = !sll & s[2:0]==3'd1;
142
assign s1[ 6] = !sll & s[2:0]==3'd2;
143
assign s1[ 5] = !sll & s[2:0]==3'd3;
144
assign s1[ 4] = !sll & s[2:0]==3'd4;
145
assign s1[ 3] = !sll & s[2:0]==3'd5;
146
assign s1[ 2] = !sll & s[2:0]==3'd6;
147
assign s1[ 1] = !sll & s[2:0]==3'd7;
148
 
149
assign sign[3] = din[31] & sra;
150
assign sign[2] = sign[3] & (&din[31:24]);
151
assign sign[1] = sign[2] & (&din[23:16]);
152
assign sign[0] = sign[1] & (&din[15:8]);
153 40 unneback
`define MODULE mults
154
`BASE`MODULE `SHIFT_UNIT_MULT mult_byte3 ( .a({sign[3], {8{sign[3]}},din[31:24], din[23:16]}), .b({1'b0,s1}), .p(tmp[3]));
155
`BASE`MODULE `SHIFT_UNIT_MULT mult_byte2 ( .a({sign[2], din[31:24]  ,din[23:16],  din[15:8]}), .b({1'b0,s1}), .p(tmp[2]));
156
`BASE`MODULE `SHIFT_UNIT_MULT mult_byte1 ( .a({sign[1], din[23:16]  ,din[15:8],   din[7:0]}), .b({1'b0,s1}), .p(tmp[1]));
157
`BASE`MODULE `SHIFT_UNIT_MULT mult_byte0 ( .a({sign[0], din[15:8]   ,din[7:0],    8'h00}),      .b({1'b0,s1}), .p(tmp[0]));
158
`undef MODULE
159 18 unneback
// second stage is multiplexer based
160
// shift on byte level
161
 
162
// mux byte 3
163
assign dout[31:24] = (s[4:3]==2'b00) ? tmp[3] :
164
                     (sll & s[4:3]==2'b01) ? tmp[2] :
165
                     (sll & s[4:3]==2'b10) ? tmp[1] :
166
                     (sll & s[4:3]==2'b11) ? tmp[0] :
167
                     {8{sign[3]}};
168
 
169
// mux byte 2
170
assign dout[23:16] = (s[4:3]==2'b00) ? tmp[2] :
171
                     (sll & s[4:3]==2'b01) ? tmp[1] :
172
                     (sll & s[4:3]==2'b10) ? tmp[0] :
173
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
174
                     (s[4:3]==2'b01) ? tmp[3] :
175
                     {8{sign[3]}};
176
 
177
// mux byte 1
178
assign dout[15:8]  = (s[4:3]==2'b00) ? tmp[1] :
179
                     (sll & s[4:3]==2'b01) ? tmp[0] :
180
                     (sll & s[4:3]==2'b10) ? {8{1'b0}} :
181
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
182
                     (s[4:3]==2'b01) ? tmp[2] :
183
                     (s[4:3]==2'b10) ? tmp[3] :
184
                     {8{sign[3]}};
185
 
186
// mux byte 0
187
assign dout[7:0]   = (s[4:3]==2'b00) ? tmp[0] :
188
                     (sll) ?  {8{1'b0}}:
189
                     (s[4:3]==2'b01) ? tmp[1] :
190
                     (s[4:3]==2'b10) ? tmp[2] :
191
                     tmp[3];
192 149 unneback
end else begin : impl_classic
193 153 unneback
 
194
assign dout =
195
    (opcode==opcode_sll) ? din << s :
196
    (opcode==opcode_srl) ? din >> s :
197
    (opcode==opcode_sra) ? (din >> s) | ({32{din[31]}} << (6'd32-{1'b0,s})) :
198
    din << s;
199 149 unneback
 
200
end
201 152 unneback
endgenerate
202 18 unneback
 
203
endmodule
204 40 unneback
`endif
205 18 unneback
 
206 40 unneback
`ifdef LOGIC_UNIT
207 18 unneback
// logic unit
208
// supporting the following logic functions
209
//    a and b
210
//    a or  b
211
//    a xor b
212
//    not b
213 40 unneback
`define MODULE logic_unit
214
module `BASE`MODULE( a, b, result, opcode);
215
`undef MODULE
216 18 unneback
parameter width = 32;
217
parameter opcode_and = 2'b00;
218
parameter opcode_or  = 2'b01;
219
parameter opcode_xor = 2'b10;
220
input [width-1:0] a,b;
221
output [width-1:0] result;
222
input [1:0] opcode;
223
 
224
assign result = (opcode==opcode_and) ? a & b :
225
                (opcode==opcode_or)  ? a | b :
226
                (opcode==opcode_xor) ? a ^ b :
227
                b;
228
 
229
endmodule
230 48 unneback
`endif
231 18 unneback
 
232 48 unneback
`ifdef ARITH_UNIT
233
`define MODULE arith_unit
234
module `BASE`MODULE ( a, b, c_in, add_sub, sign, result, c_out, z, ovfl);
235
`undef MODULE
236 18 unneback
parameter width = 32;
237
parameter opcode_add = 1'b0;
238
parameter opcode_sub = 1'b1;
239
input [width-1:0] a,b;
240
input c_in, add_sub, sign;
241
output [width-1:0] result;
242
output c_out, z, ovfl;
243
 
244
assign {c_out,result} = {(a[width-1] & sign),a} + ({a[width-1] & sign,b} ^ {(width+1){(add_sub==opcode_sub)}}) + {{(width-1){1'b0}},(c_in | (add_sub==opcode_sub))};
245
assign z = (result=={width{1'b0}});
246
assign ovfl = ( a[width-1] &  b[width-1] & ~result[width-1]) |
247
               (~a[width-1] & ~b[width-1] &  result[width-1]);
248
endmodule
249 40 unneback
`endif
250 48 unneback
 
251
`ifdef COUNT_UNIT
252
`define MODULE count_unit
253
module `BASE`MODULE (din, dout, opcode);
254
`undef MODULE
255
parameter width = 32;
256
input [width-1:0] din;
257
output [width-1:0] dout;
258
input opcode;
259
 
260
integer i;
261 58 unneback
wire [width/32+4:0] ff1, fl1;
262 48 unneback
 
263 57 unneback
/*
264 48 unneback
always @(din) begin
265
    ff1 = 0; i = 0;
266
    while (din[i] == 0 && i < width) begin // complex condition
267
        ff1 = ff1 + 1;
268
        i = i + 1;
269
    end
270
end
271
 
272
always @(din) begin
273
    fl1 = width; i = width-1;
274
    while (din[i] == 0 && i >= width) begin // complex condition
275
        fl1 = fl1 - 1;
276
        i = i - 1;
277
    end
278
end
279 57 unneback
*/
280 48 unneback
 
281
generate
282
if (width==32) begin
283 57 unneback
 
284
    assign ff1 = din[0] ? 6'd1 :
285
                 din[1] ? 6'd2 :
286
                 din[2] ? 6'd3 :
287
                 din[3] ? 6'd4 :
288
                 din[4] ? 6'd5 :
289
                 din[5] ? 6'd6 :
290
                 din[6] ? 6'd7 :
291
                 din[7] ? 6'd8 :
292
                 din[8] ? 6'd9 :
293
                 din[9] ? 6'd10 :
294
                 din[10] ? 6'd11 :
295
                 din[11] ? 6'd12 :
296
                 din[12] ? 6'd13 :
297
                 din[13] ? 6'd14 :
298
                 din[14] ? 6'd15 :
299
                 din[15] ? 6'd16 :
300
                 din[16] ? 6'd17 :
301
                 din[17] ? 6'd18 :
302
                 din[18] ? 6'd19 :
303
                 din[19] ? 6'd20 :
304
                 din[20] ? 6'd21 :
305
                 din[21] ? 6'd22 :
306
                 din[22] ? 6'd23 :
307
                 din[23] ? 6'd24 :
308
                 din[24] ? 6'd25 :
309
                 din[25] ? 6'd26 :
310
                 din[26] ? 6'd27 :
311
                 din[27] ? 6'd28 :
312
                 din[28] ? 6'd29 :
313
                 din[29] ? 6'd30 :
314
                 din[30] ? 6'd31 :
315
                 din[31] ? 6'd32 :
316
                 6'd0;
317
 
318
    assign fl1 = din[31] ? 6'd32 :
319
                 din[30] ? 6'd31 :
320
                 din[29] ? 6'd30 :
321
                 din[28] ? 6'd29 :
322
                 din[27] ? 6'd28 :
323
                 din[26] ? 6'd27 :
324
                 din[25] ? 6'd26 :
325
                 din[24] ? 6'd25 :
326
                 din[23] ? 6'd24 :
327
                 din[22] ? 6'd23 :
328
                 din[21] ? 6'd22 :
329
                 din[20] ? 6'd21 :
330
                 din[19] ? 6'd20 :
331
                 din[18] ? 6'd19 :
332
                 din[17] ? 6'd18 :
333
                 din[16] ? 6'd17 :
334
                 din[15] ? 6'd16 :
335
                 din[14] ? 6'd15 :
336
                 din[13] ? 6'd14 :
337
                 din[12] ? 6'd13 :
338
                 din[11] ? 6'd12 :
339
                 din[10] ? 6'd11 :
340
                 din[9] ? 6'd10 :
341
                 din[8] ? 6'd9 :
342
                 din[7] ? 6'd8 :
343
                 din[6] ? 6'd7 :
344
                 din[5] ? 6'd6 :
345
                 din[4] ? 6'd5 :
346
                 din[3] ? 6'd4 :
347
                 din[2] ? 6'd3 :
348
                 din[1] ? 6'd2 :
349
                 din[0] ? 6'd1 :
350
                 6'd0;
351
 
352
    assign dout = (!opcode) ? {{26{1'b0}}, ff1} : {{26{1'b0}}, fl1};
353 48 unneback
end
354
endgenerate
355 57 unneback
 
356 48 unneback
generate
357
if (width==64) begin
358 57 unneback
    assign ff1 = 7'd0;
359
    assign fl1 = 7'd0;
360
    assign dout = (!opcode) ? {{57{1'b0}}, ff1} : {{57{1'b0}}, fl1};
361 48 unneback
end
362
endgenerate
363
 
364
endmodule
365
`endif
366
 
367
`ifdef EXT_UNIT
368
`define MODULE ext_unit
369
module `BASE`MODULE ( a, b, F, result, opcode);
370
`undef MODULE
371
parameter width = 32;
372
input [width-1:0] a, b;
373
input F;
374
output reg [width-1:0] result;
375
input [2:0] opcode;
376
 
377
generate
378
if (width==32) begin
379
always @ (a or b or F or opcode)
380
begin
381
    case (opcode)
382
    3'b000: result = {{24{1'b0}},a[7:0]};
383
    3'b001: result = {{24{a[7]}},a[7:0]};
384
    3'b010: result = {{16{1'b0}},a[7:0]};
385
    3'b011: result = {{16{a[15]}},a[15:0]};
386
    3'b110: result = (F) ? a : b;
387
    default: result = {b[15:0],16'h0000};
388
    endcase
389
end
390
end
391
endgenerate
392
 
393
generate
394
if (width==64) begin
395
always @ (a or b or F or opcode)
396
begin
397
    case (opcode)
398
    3'b000: result = {{56{1'b0}},a[7:0]};
399
    3'b001: result = {{56{a[7]}},a[7:0]};
400
    3'b010: result = {{48{1'b0}},a[7:0]};
401
    3'b011: result = {{48{a[15]}},a[15:0]};
402 57 unneback
    3'b110: result = (F) ? a : b;
403 48 unneback
    default: result = {32'h00000000,b[15:0],16'h0000};
404
    endcase
405
end
406
end
407
endgenerate
408
endmodule
409
`endif

powered by: WebSVN 2.1.0

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