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 |
|
|
|
217 |
|
|
module vl_arith_unit ( a, b, c_in, add_sub, sign, result, c_out, z, ovfl);
|
218 |
|
|
parameter width = 32;
|
219 |
|
|
parameter opcode_add = 1'b0;
|
220 |
|
|
parameter opcode_sub = 1'b1;
|
221 |
|
|
input [width-1:0] a,b;
|
222 |
|
|
input c_in, add_sub, sign;
|
223 |
|
|
output [width-1:0] result;
|
224 |
|
|
output c_out, z, ovfl;
|
225 |
|
|
|
226 |
|
|
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))};
|
227 |
|
|
assign z = (result=={width{1'b0}});
|
228 |
|
|
assign ovfl = ( a[width-1] & b[width-1] & ~result[width-1]) |
|
229 |
|
|
(~a[width-1] & ~b[width-1] & result[width-1]);
|
230 |
|
|
endmodule
|
231 |
40 |
unneback |
`endif
|