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 39

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
// signed multiplication
44
module vl_mults (a,b,p);
45
parameter operand_a_width = 18;
46
parameter operand_b_width = 18;
47
parameter result_hi = 35;
48
parameter result_lo = 0;
49
input [operand_a_width-1:0] a;
50
input [operand_b_width-1:0] b;
51
output [result_hi:result_lo] p;
52
wire signed [operand_a_width-1:0] ai;
53
wire signed [operand_b_width-1:0] bi;
54
wire signed [operand_a_width+operand_b_width-1:0] result;
55
 
56
    assign ai = a;
57
    assign bi = b;
58
    assign result = ai * bi;
59
    assign p = result[result_hi:result_lo];
60
 
61
endmodule
62
 
63
module vl_mults18x18 (a,b,p);
64
input [17:0] a,b;
65
output [35:0] p;
66
vl_mult
67
    # (.operand_a_width(18), .operand_b_width(18))
68
    mult0 (.a(a), .b(b), .p(p));
69
endmodule
70
 
71
// unsigned multiplication
72
module vl_mult (a,b,p);
73
parameter operand_a_width = 18;
74
parameter operand_b_width = 18;
75
parameter result_hi = 35;
76
parameter result_lo = 0;
77
input [operand_a_width-1:0] a;
78
input [operand_b_width-1:0] b;
79
output [result_hi:result_hi] p;
80
 
81
wire [operand_a_width+operand_b_width-1:0] result;
82
 
83
    assign result = a * b;
84
    assign p = result[result_hi:result_lo];
85
 
86
endmodule
87
 
88
// shift unit
89
// supporting the following shift functions
90
//   SLL
91
//   SRL
92
//   SRA
93
`define SHIFT_UNIT_MULT # ( .operand_a_width(25), .operand_b_width(16), .result_hi(14), .result_lo(7))
94
module vl_shift_unit_32( din, s, dout, opcode);
95
input [31:0] din; // data in operand
96
input [4:0] s; // shift operand
97
input [1:0] opcode;
98
output [31:0] dout;
99
 
100
parameter opcode_sll = 2'b00;
101
//parameter opcode_srl = 2'b01;
102
parameter opcode_sra = 2'b10;
103
//parameter opcode_ror = 2'b11;
104
 
105
wire sll, sra;
106
assign sll = opcode == opcode_sll;
107
assign sra = opcode == opcode_sra;
108
 
109
wire [15:1] s1;
110
wire [3:0] sign;
111
wire [7:0] tmp [0:3];
112
 
113
// first stage is multiplier based
114
// shift operand as fractional 8.7
115
assign s1[15] = sll & s[2:0]==3'd7;
116
assign s1[14] = sll & s[2:0]==3'd6;
117
assign s1[13] = sll & s[2:0]==3'd5;
118
assign s1[12] = sll & s[2:0]==3'd4;
119
assign s1[11] = sll & s[2:0]==3'd3;
120
assign s1[10] = sll & s[2:0]==3'd2;
121
assign s1[ 9] = sll & s[2:0]==3'd1;
122
assign s1[ 8] = s[2:0]==3'd0;
123
assign s1[ 7] = !sll & s[2:0]==3'd1;
124
assign s1[ 6] = !sll & s[2:0]==3'd2;
125
assign s1[ 5] = !sll & s[2:0]==3'd3;
126
assign s1[ 4] = !sll & s[2:0]==3'd4;
127
assign s1[ 3] = !sll & s[2:0]==3'd5;
128
assign s1[ 2] = !sll & s[2:0]==3'd6;
129
assign s1[ 1] = !sll & s[2:0]==3'd7;
130
 
131
assign sign[3] = din[31] & sra;
132
assign sign[2] = sign[3] & (&din[31:24]);
133
assign sign[1] = sign[2] & (&din[23:16]);
134
assign sign[0] = sign[1] & (&din[15:8]);
135
vl_mults `SHIFT_UNIT_MULT mult_byte3 ( .a({sign[3], {8{sign[3]}},din[31:24], din[23:16]}), .b({1'b0,s1}), .p(tmp[3]));
136
vl_mults `SHIFT_UNIT_MULT mult_byte2 ( .a({sign[2], din[31:24]  ,din[23:16],  din[15:8]}), .b({1'b0,s1}), .p(tmp[2]));
137
vl_mults `SHIFT_UNIT_MULT mult_byte1 ( .a({sign[1], din[23:16]  ,din[15:8],   din[7:0]}), .b({1'b0,s1}), .p(tmp[1]));
138
vl_mults `SHIFT_UNIT_MULT mult_byte0 ( .a({sign[0], din[15:8]   ,din[7:0],    8'h00}),      .b({1'b0,s1}), .p(tmp[0]));
139
 
140
// second stage is multiplexer based
141
// shift on byte level
142
 
143
// mux byte 3
144
assign dout[31:24] = (s[4:3]==2'b00) ? tmp[3] :
145
                     (sll & s[4:3]==2'b01) ? tmp[2] :
146
                     (sll & s[4:3]==2'b10) ? tmp[1] :
147
                     (sll & s[4:3]==2'b11) ? tmp[0] :
148
                     {8{sign[3]}};
149
 
150
// mux byte 2
151
assign dout[23:16] = (s[4:3]==2'b00) ? tmp[2] :
152
                     (sll & s[4:3]==2'b01) ? tmp[1] :
153
                     (sll & s[4:3]==2'b10) ? tmp[0] :
154
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
155
                     (s[4:3]==2'b01) ? tmp[3] :
156
                     {8{sign[3]}};
157
 
158
// mux byte 1
159
assign dout[15:8]  = (s[4:3]==2'b00) ? tmp[1] :
160
                     (sll & s[4:3]==2'b01) ? tmp[0] :
161
                     (sll & s[4:3]==2'b10) ? {8{1'b0}} :
162
                     (sll & s[4:3]==2'b11) ? {8{1'b0}} :
163
                     (s[4:3]==2'b01) ? tmp[2] :
164
                     (s[4:3]==2'b10) ? tmp[3] :
165
                     {8{sign[3]}};
166
 
167
// mux byte 0
168
assign dout[7:0]   = (s[4:3]==2'b00) ? tmp[0] :
169
                     (sll) ?  {8{1'b0}}:
170
                     (s[4:3]==2'b01) ? tmp[1] :
171
                     (s[4:3]==2'b10) ? tmp[2] :
172
                     tmp[3];
173
 
174
endmodule
175
 
176
// logic unit
177
// supporting the following logic functions
178
//    a and b
179
//    a or  b
180
//    a xor b
181
//    not b
182
module vl_logic_unit( a, b, result, opcode);
183
parameter width = 32;
184
parameter opcode_and = 2'b00;
185
parameter opcode_or  = 2'b01;
186
parameter opcode_xor = 2'b10;
187
input [width-1:0] a,b;
188
output [width-1:0] result;
189
input [1:0] opcode;
190
 
191
assign result = (opcode==opcode_and) ? a & b :
192
                (opcode==opcode_or)  ? a | b :
193
                (opcode==opcode_xor) ? a ^ b :
194
                b;
195
 
196
endmodule
197
 
198
module vl_arith_unit ( a, b, c_in, add_sub, sign, result, c_out, z, ovfl);
199
parameter width = 32;
200
parameter opcode_add = 1'b0;
201
parameter opcode_sub = 1'b1;
202
input [width-1:0] a,b;
203
input c_in, add_sub, sign;
204
output [width-1:0] result;
205
output c_out, z, ovfl;
206
 
207
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))};
208
assign z = (result=={width{1'b0}});
209
assign ovfl = ( a[width-1] &  b[width-1] & ~result[width-1]) |
210
               (~a[width-1] & ~b[width-1] &  result[width-1]);
211
endmodule

powered by: WebSVN 2.1.0

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