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

Subversion Repositories altor32

[/] [altor32/] [trunk/] [rtl/] [cpu_lite/] [altor32_alu.v] - Blame information for rev 36

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 34 ultra_embe
//-----------------------------------------------------------------
2
//                           AltOR32 
3
//                Alternative Lightweight OpenRisc 
4 36 ultra_embe
//                            V2.1
5 34 ultra_embe
//                     Ultra-Embedded.com
6 36 ultra_embe
//                   Copyright 2011 - 2014
7 34 ultra_embe
//
8
//               Email: admin@ultra-embedded.com
9
//
10
//                       License: LGPL
11
//-----------------------------------------------------------------
12
//
13
// Copyright (C) 2011 - 2013 Ultra-Embedded.com
14
//
15
// This source file may be used and distributed without         
16
// restriction provided that this copyright statement is not    
17
// removed from the file and that any derivative work contains  
18
// the original copyright notice and the associated disclaimer. 
19
//
20
// This source file is free software; you can redistribute it   
21
// and/or modify it under the terms of the GNU Lesser General   
22
// Public License as published by the Free Software Foundation; 
23
// either version 2.1 of the License, or (at your option) any   
24
// later version.
25
//
26
// This source is distributed in the hope that it will be       
27
// useful, but WITHOUT ANY WARRANTY; without even the implied   
28
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      
29
// PURPOSE.  See the GNU Lesser General Public License for more 
30
// details.
31
//
32
// You should have received a copy of the GNU Lesser General    
33
// Public License along with this source; if not, write to the 
34
// Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
35
// Boston, MA  02111-1307  USA
36
//-----------------------------------------------------------------
37
 
38
//-----------------------------------------------------------------
39
// Includes
40
//-----------------------------------------------------------------
41
`include "altor32_defs.v"
42 36 ultra_embe
`include "altor32_funcs.v"
43 34 ultra_embe
 
44
//-----------------------------------------------------------------
45
// Module - ALU
46
//-----------------------------------------------------------------
47
module altor32_alu
48
(
49
    // ALU operation select
50
    input [3:0]     op_i        /*verilator public*/,
51
 
52
    // Operands
53
    input [31:0]    a_i         /*verilator public*/,
54
    input [31:0]    b_i         /*verilator public*/,
55
    input           c_i         /*verilator public*/,
56
 
57
    // Result
58
    output [31:0]   p_o         /*verilator public*/,
59
 
60
    // Carry
61
    output reg      c_o         /*verilator public*/,
62 36 ultra_embe
    output reg      c_update_o  /*verilator public*/,
63
 
64
    // Comparison    
65
    output reg      equal_o                /*verilator public*/,
66
    output reg      greater_than_signed_o  /*verilator public*/,
67
    output reg      greater_than_o         /*verilator public*/,
68
    output reg      less_than_signed_o     /*verilator public*/,
69
    output reg      less_than_o            /*verilator public*/,
70
    output          flag_update_o          /*verilator public*/
71 34 ultra_embe
);
72
 
73
//-----------------------------------------------------------------
74
// Registers
75
//-----------------------------------------------------------------
76
reg [31:0]      result;
77
 
78
reg [31:16]     shift_right_fill;
79
reg [31:0]      shift_right_1;
80
reg [31:0]      shift_right_2;
81
reg [31:0]      shift_right_4;
82
reg [31:0]      shift_right_8;
83
 
84
reg [31:0]      shift_left_1;
85
reg [31:0]      shift_left_2;
86
reg [31:0]      shift_left_4;
87
reg [31:0]      shift_left_8;
88
 
89
//-----------------------------------------------------------------
90
// ALU
91
//-----------------------------------------------------------------
92
always @ (op_i or a_i or b_i or c_i)
93
begin
94
   case (op_i)
95
       //----------------------------------------------
96
       // Shift Left
97
       //----------------------------------------------   
98
       `ALU_SHIFTL :
99
       begin
100
            if (b_i[0] == 1'b1)
101
                shift_left_1 = {a_i[30:0],1'b0};
102
            else
103
                shift_left_1 = a_i;
104
 
105
            if (b_i[1] == 1'b1)
106
                shift_left_2 = {shift_left_1[29:0],2'b00};
107
            else
108
                shift_left_2 = shift_left_1;
109
 
110
            if (b_i[2] == 1'b1)
111
                shift_left_4 = {shift_left_2[27:0],4'b0000};
112
            else
113
                shift_left_4 = shift_left_2;
114
 
115
            if (b_i[3] == 1'b1)
116
                shift_left_8 = {shift_left_4[23:0],8'b00000000};
117
            else
118
                shift_left_8 = shift_left_4;
119
 
120
            if (b_i[4] == 1'b1)
121
                result = {shift_left_8[15:0],16'b0000000000000000};
122
            else
123
                result = shift_left_8;
124
 
125
            c_o        = 1'b0;
126
            c_update_o = 1'b0;
127
       end
128
       //----------------------------------------------
129
       // Shift Right
130
       //----------------------------------------------
131
       `ALU_SHIFTR, `ALU_SHIRTR_ARITH:
132
       begin
133
            // Arithmetic shift? Fill with 1's if MSB set
134
            if (a_i[31] == 1'b1 && op_i == `ALU_SHIRTR_ARITH)
135
                shift_right_fill = 16'b1111111111111111;
136
            else
137
                shift_right_fill = 16'b0000000000000000;
138
 
139
            if (b_i[0] == 1'b1)
140
                shift_right_1 = {shift_right_fill[31], a_i[31:1]};
141
            else
142
                shift_right_1 = a_i;
143
 
144
            if (b_i[1] == 1'b1)
145
                shift_right_2 = {shift_right_fill[31:30], shift_right_1[31:2]};
146
            else
147
                shift_right_2 = shift_right_1;
148
 
149
            if (b_i[2] == 1'b1)
150
                shift_right_4 = {shift_right_fill[31:28], shift_right_2[31:4]};
151
            else
152
                shift_right_4 = shift_right_2;
153
 
154
            if (b_i[3] == 1'b1)
155
                shift_right_8 = {shift_right_fill[31:24], shift_right_4[31:8]};
156
            else
157
                shift_right_8 = shift_right_4;
158
 
159
            if (b_i[4] == 1'b1)
160
                result = {shift_right_fill[31:16], shift_right_8[31:16]};
161
            else
162
                result = shift_right_8;
163
 
164
            c_o        = 1'b0;
165
            c_update_o = 1'b0;
166
       end
167
       //----------------------------------------------
168
       // Arithmetic
169
       //----------------------------------------------
170
       `ALU_ADD :
171
       begin
172
            {c_o, result} = (a_i + b_i);
173
            c_update_o    = 1'b1;
174
       end
175
       `ALU_ADDC :
176
       begin
177
            {c_o, result} = (a_i + b_i) + {31'h00000000, c_i};
178
            c_update_o    = 1'b1;
179
       end
180
       `ALU_SUB :
181
       begin
182
            result        = (a_i - b_i);
183
            c_o           = 1'b0;
184
            c_update_o    = 1'b0;
185
       end
186
       //----------------------------------------------
187
       // Logical
188
       //----------------------------------------------       
189
       `ALU_AND :
190
       begin
191
            result        = (a_i & b_i);
192
            c_o           = 1'b0;
193
            c_update_o    = 1'b0;
194
       end
195
       `ALU_OR  :
196
       begin
197
            result        = (a_i | b_i);
198
            c_o           = 1'b0;
199
            c_update_o    = 1'b0;
200
       end
201
       `ALU_XOR :
202
       begin
203
            result        = (a_i ^ b_i);
204
            c_o           = 1'b0;
205
            c_update_o    = 1'b0;
206
       end
207
       default  :
208
       begin
209
            result        = a_i;
210
            c_o           = 1'b0;
211
            c_update_o    = 1'b0;
212
       end
213
   endcase
214
end
215
 
216
assign p_o    = result;
217
 
218 36 ultra_embe
//-----------------------------------------------------------------
219
// Comparisons
220
//-----------------------------------------------------------------
221
always @ *
222
begin
223
    if (a_i == b_i)
224
        equal_o = 1'b1;
225
    else
226
        equal_o = 1'b0;
227
 
228
    if (a_i < b_i)
229
        less_than_o = 1'b1;
230
    else
231
        less_than_o = 1'b0;
232
 
233
    if (a_i > b_i)
234
        greater_than_o = 1'b1;
235
    else
236
        greater_than_o = 1'b0;
237
 
238
    less_than_signed_o    = less_than_signed(a_i, b_i);
239
    greater_than_signed_o = ~(less_than_signed_o | equal_o);
240
end
241
 
242
assign flag_update_o = (op_i == `ALU_COMPARE);
243
 
244 34 ultra_embe
endmodule

powered by: WebSVN 2.1.0

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