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

Subversion Repositories alu_with_selectable_inputs_and_outputs

[/] [alu_with_selectable_inputs_and_outputs/] [trunk/] [rtl/] [alu.v] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dragos_don
/////////////////////////////////////////////////////////////////////
2
////                                                             ////
3
////      This project has been provided to you on behalf of:    ////
4
////                                                             ////
5
////            S.C. ASICArt S.R.L.                              ////
6
////                            www.asicart.com                  ////
7
////                            eli_f@asicart.com                ////
8
////                                                             ////
9
////        Author: Dragos Constantin Doncean                    ////
10
////        Email: doncean@asicart.com                           ////
11
////        Mobile: +40-740-936997                               ////
12
////                                                             ////
13
////      Downloaded from: http://www.opencores.org/             ////
14
////                                                             ////
15
/////////////////////////////////////////////////////////////////////
16
////                                                             ////
17
//// Copyright (C) 2007 Dragos Constantin Doncean                ////
18
////                         www.asicart.com                     ////
19
////                         doncean@asicart.com                 ////
20
////                                                             ////
21
//// This source file may be used and distributed without        ////
22
//// restriction provided that this copyright statement is not   ////
23
//// removed from the file and that any derivative work contains ////
24
//// the original copyright notice and the associated disclaimer.////
25
////                                                             ////
26
////     THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY     ////
27
//// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   ////
28
//// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS   ////
29
//// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR      ////
30
//// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,         ////
31
//// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    ////
32
//// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE   ////
33
//// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR        ////
34
//// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  ////
35
//// LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT  ////
36
//// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  ////
37
//// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE         ////
38
//// POSSIBILITY OF SUCH DAMAGE.                                 ////
39
////                                                             ////
40
/////////////////////////////////////////////////////////////////////
41
 
42
 
43
//ALU
44
 
45
module ALU(clk, res, alu_stb_in,
46
        alu_data_in, alu_data_valid_in,
47
        alu_result, result_parity,
48
        output_channel,
49
        alu_stb_out);
50
 
51
input clk, res, alu_stb_in;
52
input [7:0] alu_data_in;
53
input alu_data_valid_in;
54
output [15:0] alu_result;
55
output result_parity;
56
output output_channel;
57
output alu_stb_out;
58
 
59
reg [15:0] alu_result;
60
reg result_parity;
61
reg [3:0] operator_type;
62
reg [2:0] operator_symbol;
63
reg output_channel;
64
reg [7:0] alu_memory [0:15];
65
reg alu_stb_out;
66
 
67
integer i;
68
integer j;
69
reg executed_case_once;
70
 
71
always @ (posedge clk or posedge res)
72
if(res)
73
begin
74
        alu_result = 16'b0;
75
        result_parity = 1'b0;
76
        operator_type = 4'b1111;
77
        operator_symbol = 3'b111;
78
        output_channel = 1'b0;
79
        for(i = 0; i < 15; i = i + 1)
80
                alu_memory[i] = 8'b0;
81
        i = 0;
82
        j = 0;
83
        executed_case_once = 1'b0;
84
end
85
else
86
begin
87
        if(alu_stb_in)
88
        begin
89
                operator_type = alu_data_in[7:4];
90
                operator_symbol = alu_data_in[3:1];
91
                output_channel = alu_data_in[0];
92
                executed_case_once = 1'b0;
93
        end
94
        if((alu_data_valid_in) && (! alu_stb_in))
95
        begin
96
                alu_memory[i] = alu_data_in;
97
                i = i + 1;
98
        end
99
        if((! alu_data_valid_in) && (! alu_stb_in) && (! executed_case_once))
100
        begin
101
                executed_case_once = 1'b1;
102
                if(i !== 0)
103
                        for(j=i;j<16; j=j+1)
104
                                alu_memory[j] = 8'b0;
105
                i = 0;
106
                case (operator_type)
107
                'd0: //Arithmetic
108
                begin
109
                        $display("Arithmetic operator");
110
                        case(operator_symbol)
111
                        'd0: //Multiply
112
                        begin
113
                                alu_result = (alu_memory[0] * alu_memory[1]);
114
                                $display("OPERATION * (Multiply): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
115
                                        alu_memory[0], alu_memory[1], alu_result);
116
                        end
117
                        'd1: //Divide
118
                        begin
119
                                alu_result = (alu_memory[0] / alu_memory[1]);
120
                                $display("OPERATION / (Divide): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
121
                                        alu_memory[0], alu_memory[1], alu_result);
122
                        end
123
                        'd2: //Add
124
                        begin
125
                                alu_result = (alu_memory[0] + alu_memory[1]);
126
                                $display("OPERATION + (Add): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
127
                                        alu_memory[0], alu_memory[1], alu_result);
128
                        end
129
                        'd3: //Substract
130
                        begin
131
                                alu_result = (alu_memory[0] - alu_memory[1]);
132
                                $display("OPERATION - (Substract): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
133
                                        alu_memory[0], alu_memory[1], alu_result);
134
                        end
135
                        'd4: //Modulus
136
                        begin
137
                                alu_result = (alu_memory[0] % alu_memory[1]);
138
                                $display("OPERATION  (Modulus): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
139
                                        alu_memory[0], alu_memory[1], alu_result);
140
                        end
141
                        endcase
142
                end
143
                'd1: //Logical
144
                begin
145
                        $display("Logical operator");
146
                        case(operator_symbol)
147
                        'd0: //Logical negation
148
                        begin
149
                                alu_result = (!alu_memory[0]);
150
                                $display("OPERATION ! (Logical negation): alu_memory[0]=%b, alu_result=%b",
151
                                        alu_memory[0], alu_result);
152
                        end
153
                        'd1: //Logical and
154
                        begin
155
                                alu_result = (alu_memory[0] && alu_memory[1]);
156
                                $display("OPERATION && (Logical and): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
157
                                        alu_memory[0], alu_memory[1], alu_result);
158
                        end
159
                        'd2: //Logical or
160
                        begin
161
                                alu_result = (alu_memory[0] || alu_memory[1]);
162
                                $display("OPERATION || (Logical or): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
163
                                        alu_memory[0], alu_memory[1], alu_result);
164
                        end
165
                        endcase
166
                end
167
                'd2: //Relational
168
                begin
169
                        $display("Relational operator");
170
                        case(operator_symbol)
171
                        'd0: //Greater than
172
                        begin
173
                                alu_result = (alu_memory[0] > alu_memory[1]);
174
                                $display("OPERATION > (Greater than): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
175
                                        alu_memory[0], alu_memory[1], alu_result);
176
                        end
177
                        'd1: //Less than
178
                        begin
179
                                alu_result = (alu_memory[0] < alu_memory[1]);
180
                                $display("OPERATION < (Less than): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
181
                                        alu_memory[0], alu_memory[1], alu_result);
182
                        end
183
                        'd2: //Greater than or equal
184
                        begin
185
                                alu_result = (alu_memory[0] >= alu_memory[1]);
186
                                $display("OPERATION >= (Greater than or equal): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
187
                                        alu_memory[0], alu_memory[1], alu_result);
188
                        end
189
                        'd3: //Less than or equal
190
                        begin
191
                                alu_result = (alu_memory[0] <= alu_memory[1]);
192
                                $display("OPERATION <= (Less than or equal): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
193
                                        alu_memory[0], alu_memory[1], alu_result);
194
                        end
195
                        endcase
196
                end
197
                'd3: //Equality
198
                begin
199
                        $display("Equality operator");
200
                        case(operator_symbol)
201
                        'd0: //Equality
202
                        begin
203
                                alu_result = (alu_memory[0] == alu_memory[1]);
204
                                $display("OPERATION == (Equality): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
205
                                        alu_memory[0], alu_memory[1], alu_result);
206
                        end
207
                        'd1: //Inequality
208
                        begin
209
                                alu_result = (alu_memory[0] != alu_memory[1]);
210
                                $display("OPERATION != (Inequality): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
211
                                        alu_memory[0], alu_memory[1], alu_result);
212
                        end
213
                        'd2: //Case equality
214
                        begin
215
                                alu_result = (alu_memory[0] === alu_memory[1]);
216
                                $display("OPERATION === (Case equality): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
217
                                        alu_memory[0], alu_memory[1], alu_result);
218
                        end
219
                        'd3: //Case inequality
220
                        begin
221
                                alu_result = (alu_memory[0] !== alu_memory[1]);
222
                                $display("OPERATION !== (Case inequality): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
223
                                        alu_memory[0], alu_memory[1], alu_result);
224
                        end
225
                        endcase
226
                end
227
                'd4: //Bitwise
228
                begin
229
                        $display("Bitwise operator");
230
                        case(operator_symbol)
231
                        'd0: //Bitwise negation
232
                        begin
233
                                alu_result = (~ alu_memory[0]);
234
                                $display("OPERATION ~ (Bitwise negation): alu_memory[0]=%b, alu_result=%b",
235
                                        alu_memory[0], alu_result);
236
                        end
237
                        'd1: //Bitwise and
238
                        begin
239
                                alu_result = (alu_memory[0] & alu_memory[1]);
240
                                $display("OPERATION & (Bitwise and): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
241
                                        alu_memory[0], alu_memory[1], alu_result);
242
                        end
243
                        'd2: //Bitwise or
244
                        begin
245
                                alu_result = (alu_memory[0] | alu_memory[1]);
246
                                $display("OPERATION | (Bitwise or): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
247
                                        alu_memory[0], alu_memory[1], alu_result);
248
                        end
249
                        'd3: //Bitwise xor
250
                        begin
251
                                alu_result = (alu_memory[0] ^ alu_memory[1]);
252
                                $display("OPERATION ^ (Bitwise xor): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
253
                                        alu_memory[0], alu_memory[1], alu_result);
254
                        end
255
                        'd4: //Bitwise xnor (1st operator symbol)
256
                        begin
257
                                alu_result = (alu_memory[0] ^~ alu_memory[1]);
258
                                $display("OPERATION ^~ (Bitwise xnor (1st operator symbol)): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
259
                                        alu_memory[0], alu_memory[1], alu_result);
260
                        end
261
                        'd5: //Bitwise xnor (2nd operator symbol)
262
                        begin
263
                                alu_result = (alu_memory[0] ~^ alu_memory[1]);
264
                                $display("OPERATION ~^ (Bitwise xnor (2nd operator symbol)): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
265
                                        alu_memory[0], alu_memory[1], alu_result);
266
                        end
267
                        endcase
268
                end
269
                'd5: //Reduction
270
                begin
271
                        $display("Reduction operator");
272
                        case(operator_symbol)
273
                        'd0: //Reduction and
274
                        begin
275
                                alu_result = (& alu_memory[0]);
276
                                $display("OPERATION & (Reduction and): alu_memory[0]=%b, alu_result=%b",
277
                                        alu_memory[0], alu_result);
278
                        end
279
                        'd1: //Reduction nand
280
                        begin
281
                                alu_result = (~& alu_memory[0]);
282
                                $display("OPERATION ~& (Reduction nand): alu_memory[0]=%b, alu_result=%b",
283
                                        alu_memory[0], alu_result);
284
                        end
285
                        'd2: //Reduction or
286
                        begin
287
                                alu_result = (| alu_memory[0]);
288
                                $display("OPERATION | (Reduction or): alu_memory[0]=%b, alu_result=%b",
289
                                        alu_memory[0], alu_result);
290
                        end
291
                        'd3: //Reduction nor
292
                        begin
293
                                alu_result = (~| alu_memory[0]);
294
                                $display("OPERATION ~| (Reduction nor): alu_memory[0]=%b, alu_result=%b",
295
                                        alu_memory[0], alu_result);
296
                        end
297
                        'd4: //Reduction xor
298
                        begin
299
                                alu_result = (^ alu_memory[0]);
300
                                $display("OPERATION ^ (Reduction xor): alu_memory[0]=%b, alu_result=%b",
301
                                        alu_memory[0], alu_result);
302
                        end
303
                        'd5: //Reduction xnor (1st operator symbol)
304
                        begin
305
                                alu_result = (^~ alu_memory[0]);
306
                                $display("OPERATION ^~ (Reduction xnor (1st operator symbol)): alu_memory[0]=%b, alu_result=%b",
307
                                        alu_memory[0], alu_result);
308
                        end
309
                        'd6: //Reduction xnor (2nd operator symbol)
310
                        begin
311
                                alu_result = (~^ alu_memory[0]);
312
                                $display("OPERATION ~^ (Reduction xnor (2nd operator symbol)): alu_memory[0]=%b, alu_result=%b",
313
                                        alu_memory[0], alu_result);
314
                        end
315
                        endcase
316
                end
317
                'd6: //Shift
318
                begin
319
                        $display("Shift operator");
320
                        case(operator_symbol)
321
                        'd0: //Right shift
322
                        begin
323
                                alu_result = (alu_memory[0] >> alu_memory[1]);
324
                                $display("OPERATION >> (Right shift): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
325
                                        alu_memory[0], alu_memory[1], alu_result);
326
                        end
327
                        'd1: //Left shift
328
                        begin
329
                                alu_result = (alu_memory[0] << alu_memory[1]);
330
                                $display("OPERATION << (Left shift): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
331
                                        alu_memory[0], alu_memory[1], alu_result);
332
                        end
333
                        endcase
334
                end
335
                'd7: //Concatenation
336
                begin
337
                        //$display("Concatenation operator");
338
                        case(operator_symbol)
339
                        'd0: //Concatenation
340
                        begin
341
                                alu_result = {alu_memory[0], alu_memory[1]};
342
                                $display("OPERATION {} (Concatenation): alu_memory[0]=%b, alu_memory[1]=%b, alu_result=%b",
343
                                        alu_memory[0], alu_memory[1], alu_result);
344
                        end
345
                        endcase
346
                end
347
                'd8: //Replication
348
                begin
349
                        $display("Replication operator");
350
                        case(operator_symbol)
351
                        'd0: //Replication
352
                        begin
353
                                alu_result = { 2 {alu_memory[0]} };
354
                                $display("OPERATION { { } } (Replication): alu_memory[0]=%b - replicated twice: alu_result=%b",
355
                                        alu_memory[0], alu_result);
356
                        end
357
                        endcase
358
                end
359
                'd9: //Conditional
360
                begin
361
                        $display("Conditional operator");
362
                        case(operator_symbol)
363
                        'd0: //Conditional
364
                        begin
365
                                alu_result = (alu_memory[0] ? alu_memory[1] : alu_memory[2]);
366
                                $display("OPERATION ?: (Conditional): alu_memory[0]=%b, alu_memory[1]=%b, alu_memory[2]=%b, alu_result=%b",
367
                                        alu_memory[0], alu_memory[1], alu_memory[2], alu_result);
368
                        end
369
                        endcase
370
                end
371
                endcase
372
                result_parity = ^alu_result; //Parity = XOR of all result's bits
373
        end
374
end
375
 
376
always @ (posedge clk)
377
        alu_stb_out = alu_stb_in;
378
 
379
endmodule

powered by: WebSVN 2.1.0

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