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 137

Go to most recent revision | 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
//parameter opcode_srl = 2'b01;
116
parameter opcode_sra = 2'b10;
117
//parameter opcode_ror = 2'b11;
118
 
119
wire sll, sra;
120
assign sll = opcode == opcode_sll;
121
assign sra = opcode == opcode_sra;
122
 
123
wire [15:1] s1;
124
wire [3:0] sign;
125
wire [7:0] tmp [0:3];
126
 
127
// first stage is multiplier based
128
// shift operand as fractional 8.7
129
assign s1[15] = sll & s[2:0]==3'd7;
130
assign s1[14] = sll & s[2:0]==3'd6;
131
assign s1[13] = sll & s[2:0]==3'd5;
132
assign s1[12] = sll & s[2:0]==3'd4;
133
assign s1[11] = sll & s[2:0]==3'd3;
134
assign s1[10] = sll & s[2:0]==3'd2;
135
assign s1[ 9] = sll & s[2:0]==3'd1;
136
assign s1[ 8] = s[2:0]==3'd0;
137
assign s1[ 7] = !sll & s[2:0]==3'd1;
138
assign s1[ 6] = !sll & s[2:0]==3'd2;
139
assign s1[ 5] = !sll & s[2:0]==3'd3;
140
assign s1[ 4] = !sll & s[2:0]==3'd4;
141
assign s1[ 3] = !sll & s[2:0]==3'd5;
142
assign s1[ 2] = !sll & s[2:0]==3'd6;
143
assign s1[ 1] = !sll & s[2:0]==3'd7;
144
 
145
assign sign[3] = din[31] & sra;
146
assign sign[2] = sign[3] & (&din[31:24]);
147
assign sign[1] = sign[2] & (&din[23:16]);
148
assign sign[0] = sign[1] & (&din[15:8]);
149 40 unneback
`define MODULE mults
150
`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]));
151
`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]));
152
`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]));
153
`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]));
154
`undef MODULE
155 18 unneback
// second stage is multiplexer based
156
// shift on byte level
157
 
158
// mux byte 3
159
assign dout[31:24] = (s[4:3]==2'b00) ? tmp[3] :
160
                     (sll & s[4:3]==2'b01) ? tmp[2] :
161
                     (sll & s[4:3]==2'b10) ? tmp[1] :
162
                     (sll & s[4:3]==2'b11) ? tmp[0] :
163
                     {8{sign[3]}};
164
 
165
// mux byte 2
166
assign dout[23:16] = (s[4:3]==2'b00) ? tmp[2] :
167
                     (sll & s[4:3]==2'b01) ? tmp[1] :
168
                     (sll & s[4:3]==2'b10) ? tmp[0] :
169
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
170
                     (s[4:3]==2'b01) ? tmp[3] :
171
                     {8{sign[3]}};
172
 
173
// mux byte 1
174
assign dout[15:8]  = (s[4:3]==2'b00) ? tmp[1] :
175
                     (sll & s[4:3]==2'b01) ? tmp[0] :
176
                     (sll & s[4:3]==2'b10) ? {8{1'b0}} :
177
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
178
                     (s[4:3]==2'b01) ? tmp[2] :
179
                     (s[4:3]==2'b10) ? tmp[3] :
180
                     {8{sign[3]}};
181
 
182
// mux byte 0
183
assign dout[7:0]   = (s[4:3]==2'b00) ? tmp[0] :
184
                     (sll) ?  {8{1'b0}}:
185
                     (s[4:3]==2'b01) ? tmp[1] :
186
                     (s[4:3]==2'b10) ? tmp[2] :
187
                     tmp[3];
188
 
189
endmodule
190 40 unneback
`endif
191 18 unneback
 
192 40 unneback
`ifdef LOGIC_UNIT
193 18 unneback
// logic unit
194
// supporting the following logic functions
195
//    a and b
196
//    a or  b
197
//    a xor b
198
//    not b
199 40 unneback
`define MODULE logic_unit
200
module `BASE`MODULE( a, b, result, opcode);
201
`undef MODULE
202 18 unneback
parameter width = 32;
203
parameter opcode_and = 2'b00;
204
parameter opcode_or  = 2'b01;
205
parameter opcode_xor = 2'b10;
206
input [width-1:0] a,b;
207
output [width-1:0] result;
208
input [1:0] opcode;
209
 
210
assign result = (opcode==opcode_and) ? a & b :
211
                (opcode==opcode_or)  ? a | b :
212
                (opcode==opcode_xor) ? a ^ b :
213
                b;
214
 
215
endmodule
216 48 unneback
`endif
217 18 unneback
 
218 48 unneback
`ifdef ARITH_UNIT
219
`define MODULE arith_unit
220
module `BASE`MODULE ( a, b, c_in, add_sub, sign, result, c_out, z, ovfl);
221
`undef MODULE
222 18 unneback
parameter width = 32;
223
parameter opcode_add = 1'b0;
224
parameter opcode_sub = 1'b1;
225
input [width-1:0] a,b;
226
input c_in, add_sub, sign;
227
output [width-1:0] result;
228
output c_out, z, ovfl;
229
 
230
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))};
231
assign z = (result=={width{1'b0}});
232
assign ovfl = ( a[width-1] &  b[width-1] & ~result[width-1]) |
233
               (~a[width-1] & ~b[width-1] &  result[width-1]);
234
endmodule
235 40 unneback
`endif
236 48 unneback
 
237
`ifdef COUNT_UNIT
238
`define MODULE count_unit
239
module `BASE`MODULE (din, dout, opcode);
240
`undef MODULE
241
parameter width = 32;
242
input [width-1:0] din;
243
output [width-1:0] dout;
244
input opcode;
245
 
246
integer i;
247 58 unneback
wire [width/32+4:0] ff1, fl1;
248 48 unneback
 
249 57 unneback
/*
250 48 unneback
always @(din) begin
251
    ff1 = 0; i = 0;
252
    while (din[i] == 0 && i < width) begin // complex condition
253
        ff1 = ff1 + 1;
254
        i = i + 1;
255
    end
256
end
257
 
258
always @(din) begin
259
    fl1 = width; i = width-1;
260
    while (din[i] == 0 && i >= width) begin // complex condition
261
        fl1 = fl1 - 1;
262
        i = i - 1;
263
    end
264
end
265 57 unneback
*/
266 48 unneback
 
267
generate
268
if (width==32) begin
269 57 unneback
 
270
    assign ff1 = din[0] ? 6'd1 :
271
                 din[1] ? 6'd2 :
272
                 din[2] ? 6'd3 :
273
                 din[3] ? 6'd4 :
274
                 din[4] ? 6'd5 :
275
                 din[5] ? 6'd6 :
276
                 din[6] ? 6'd7 :
277
                 din[7] ? 6'd8 :
278
                 din[8] ? 6'd9 :
279
                 din[9] ? 6'd10 :
280
                 din[10] ? 6'd11 :
281
                 din[11] ? 6'd12 :
282
                 din[12] ? 6'd13 :
283
                 din[13] ? 6'd14 :
284
                 din[14] ? 6'd15 :
285
                 din[15] ? 6'd16 :
286
                 din[16] ? 6'd17 :
287
                 din[17] ? 6'd18 :
288
                 din[18] ? 6'd19 :
289
                 din[19] ? 6'd20 :
290
                 din[20] ? 6'd21 :
291
                 din[21] ? 6'd22 :
292
                 din[22] ? 6'd23 :
293
                 din[23] ? 6'd24 :
294
                 din[24] ? 6'd25 :
295
                 din[25] ? 6'd26 :
296
                 din[26] ? 6'd27 :
297
                 din[27] ? 6'd28 :
298
                 din[28] ? 6'd29 :
299
                 din[29] ? 6'd30 :
300
                 din[30] ? 6'd31 :
301
                 din[31] ? 6'd32 :
302
                 6'd0;
303
 
304
    assign fl1 = din[31] ? 6'd32 :
305
                 din[30] ? 6'd31 :
306
                 din[29] ? 6'd30 :
307
                 din[28] ? 6'd29 :
308
                 din[27] ? 6'd28 :
309
                 din[26] ? 6'd27 :
310
                 din[25] ? 6'd26 :
311
                 din[24] ? 6'd25 :
312
                 din[23] ? 6'd24 :
313
                 din[22] ? 6'd23 :
314
                 din[21] ? 6'd22 :
315
                 din[20] ? 6'd21 :
316
                 din[19] ? 6'd20 :
317
                 din[18] ? 6'd19 :
318
                 din[17] ? 6'd18 :
319
                 din[16] ? 6'd17 :
320
                 din[15] ? 6'd16 :
321
                 din[14] ? 6'd15 :
322
                 din[13] ? 6'd14 :
323
                 din[12] ? 6'd13 :
324
                 din[11] ? 6'd12 :
325
                 din[10] ? 6'd11 :
326
                 din[9] ? 6'd10 :
327
                 din[8] ? 6'd9 :
328
                 din[7] ? 6'd8 :
329
                 din[6] ? 6'd7 :
330
                 din[5] ? 6'd6 :
331
                 din[4] ? 6'd5 :
332
                 din[3] ? 6'd4 :
333
                 din[2] ? 6'd3 :
334
                 din[1] ? 6'd2 :
335
                 din[0] ? 6'd1 :
336
                 6'd0;
337
 
338
    assign dout = (!opcode) ? {{26{1'b0}}, ff1} : {{26{1'b0}}, fl1};
339 48 unneback
end
340
endgenerate
341 57 unneback
 
342 48 unneback
generate
343
if (width==64) begin
344 57 unneback
    assign ff1 = 7'd0;
345
    assign fl1 = 7'd0;
346
    assign dout = (!opcode) ? {{57{1'b0}}, ff1} : {{57{1'b0}}, fl1};
347 48 unneback
end
348
endgenerate
349
 
350
endmodule
351
`endif
352
 
353
`ifdef EXT_UNIT
354
`define MODULE ext_unit
355
module `BASE`MODULE ( a, b, F, result, opcode);
356
`undef MODULE
357
parameter width = 32;
358
input [width-1:0] a, b;
359
input F;
360
output reg [width-1:0] result;
361
input [2:0] opcode;
362
 
363
generate
364
if (width==32) begin
365
always @ (a or b or F or opcode)
366
begin
367
    case (opcode)
368
    3'b000: result = {{24{1'b0}},a[7:0]};
369
    3'b001: result = {{24{a[7]}},a[7:0]};
370
    3'b010: result = {{16{1'b0}},a[7:0]};
371
    3'b011: result = {{16{a[15]}},a[15:0]};
372
    3'b110: result = (F) ? a : b;
373
    default: result = {b[15:0],16'h0000};
374
    endcase
375
end
376
end
377
endgenerate
378
 
379
generate
380
if (width==64) begin
381
always @ (a or b or F or opcode)
382
begin
383
    case (opcode)
384
    3'b000: result = {{56{1'b0}},a[7:0]};
385
    3'b001: result = {{56{a[7]}},a[7:0]};
386
    3'b010: result = {{48{1'b0}},a[7:0]};
387
    3'b011: result = {{48{a[15]}},a[15:0]};
388 57 unneback
    3'b110: result = (F) ? a : b;
389 48 unneback
    default: result = {32'h00000000,b[15:0],16'h0000};
390
    endcase
391
end
392
end
393
endgenerate
394
endmodule
395
`endif

powered by: WebSVN 2.1.0

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