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 48

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
reg [width/32+3:0] ff1, fl1;
248
 
249
always @(din) begin
250
    ff1 = 0; i = 0;
251
    while (din[i] == 0 && i < width) begin // complex condition
252
        ff1 = ff1 + 1;
253
        i = i + 1;
254
    end
255
end
256
 
257
always @(din) begin
258
    fl1 = width; i = width-1;
259
    while (din[i] == 0 && i >= width) begin // complex condition
260
        fl1 = fl1 - 1;
261
        i = i - 1;
262
    end
263
end
264
 
265
generate
266
if (width==32) begin
267
    assign dout = (!opcode) ? {{58{1'b0}}, ff1} : {{58{1'b0}}, fl1};
268
end
269
endgenerate
270
generate
271
if (width==64) begin
272
    assign dout = (!opcode) ? {{27{1'b0}}, ff1} : {{27{1'b0}}, fl1};
273
end
274
endgenerate
275
 
276
endmodule
277
`endif
278
 
279
`ifdef EXT_UNIT
280
`define MODULE ext_unit
281
module `BASE`MODULE ( a, b, F, result, opcode);
282
`undef MODULE
283
parameter width = 32;
284
input [width-1:0] a, b;
285
input F;
286
output reg [width-1:0] result;
287
input [2:0] opcode;
288
 
289
generate
290
if (width==32) begin
291
always @ (a or b or F or opcode)
292
begin
293
    case (opcode)
294
    3'b000: result = {{24{1'b0}},a[7:0]};
295
    3'b001: result = {{24{a[7]}},a[7:0]};
296
    3'b010: result = {{16{1'b0}},a[7:0]};
297
    3'b011: result = {{16{a[15]}},a[15:0]};
298
    3'b110: result = (F) ? a : b;
299
    default: result = {b[15:0],16'h0000};
300
    endcase
301
end
302
end
303
endgenerate
304
 
305
generate
306
if (width==64) begin
307
always @ (a or b or F or opcode)
308
begin
309
    case (opcode)
310
    3'b000: result = {{56{1'b0}},a[7:0]};
311
    3'b001: result = {{56{a[7]}},a[7:0]};
312
    3'b010: result = {{48{1'b0}},a[7:0]};
313
    3'b011: result = {{48{a[15]}},a[15:0]};
314
    3'b110: result = (SR.F) ? a : b;
315
    default: result = {32'h00000000,b[15:0],16'h0000};
316
    endcase
317
end
318
end
319
endgenerate
320
endmodule
321
`endif

powered by: WebSVN 2.1.0

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