-
Notifications
You must be signed in to change notification settings - Fork 2
/
ALU.v
219 lines (159 loc) · 4.08 KB
/
ALU.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 15:22:19 09/03/2015
// Design Name:
// Module Name: alu
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
`include "parameters.vh"
module ALU (
input wire [15:0] a,
input wire [15:0] b,
input wire [15:0] opcode,
input wire carry_in,
output reg [15:0] c,
output reg [4:0] flags
);
always @(a, b, opcode, carry_in)
begin
// Always reset all the flags
flags[4:0] = 5'b00000;
case(opcode[15:12])
`RTYPE:
begin
case(opcode[7:4])
`EXT_ADD:
begin
// Perform the addition with carry_in and set the carry flag if necessary
{flags[`CARRY_FLAG], c} = a + b + carry_in;
// Set if equal to zero
if(c == 0) flags[`ZERO_FLAG] = 1'b1;
// Set the overflow
if( (~a[15] & ~b[15] & c[15]) | (a[15] & b[15] & ~c[15]) ) flags[`FLAG_FLAG] = 1'b1;
end
`EXT_OR:
begin
c = a | b;
// No flags for or
end
`EXT_XOR:
begin
c = a ^ b;
// No flags for xor
end
`EXT_AND:
begin
c = a & b;
// No flags for and
end
`EXT_SUB:
begin
// Save the result and generate the carry flag if necessary
{flags[`CARRY_FLAG], c} = a - b;
// Generate the overflow flag as well
if( (~a[15] & ~b[15] & c[15]) | (a[15] & b[15] & ~c[15]) ) flags[`FLAG_FLAG] = 1'b1;
end
`EXT_CMP:
begin
if( $signed(a) < $signed(b) ) flags[`NEG_FLAG] = 1'b1;
else flags[`NEG_FLAG] = 1'b0;
if( $unsigned(a) < $unsigned(b) ) flags[`LOW_FLAG] = 1'b1;
else flags[`LOW_FLAG] = 1'b0;
if( a == b ) flags[`ZERO_FLAG] = 1'b1;
else flags[`ZERO_FLAG] = 1'b0;
c = 16'b0000000000000000;
end
default:
begin
flags = 5'b000001; // Set invalid op flag
c = 16'b0000000000000000;
end
endcase
end
`ADDI:
begin
// Add the number and the immediate with the carry_in
// Sumultaniously set the carry flag if necessary
{flags[`CARRY_FLAG], c} = $signed(a) + opcode[7:0] + carry_in;
end
`SHIFTS:
begin
case(opcode[7:4])
`EXT_LSHI_LEFT:
begin
c = a << opcode[3:0];
end
`EXT_LSHI_RIGHT:
begin
c = a >> opcode[3:0];
// LSHI has no flags
end
`EXT_ASHUI_LEFT:
begin
c = $signed(a) <<< opcode[3:0];
// AUSHI has no flags
end
`EXT_ASHUI_RIGHT:
begin
c = $signed(a) >>> opcode[3:0];
// AUSHI has no flags
end
`EXT_LSH:
begin
c = a << b;
// LSH has no flags
end
`EXT_ASHU:
begin
if($signed(b) >= 0) c = $signed(a) <<< b;
else c = $signed(a) >>> -b;
end
default:
begin
flags = 5'b000001; // Set invalid op flag
c = 16'b0000000000000000;
end
endcase
end
`SUBI:
begin
c = a - $signed(opcode[7:0]);
// Determine if overflow occurred
if( (~a[15] & opcode[7] & c[15]) | (a[15] & ~opcode[7] & ~c[15]) ) flags[`FLAG_FLAG] = 1'b1;
end
`CMPI:
begin
if( $signed(a) < $signed(opcode[7:0]) ) flags[`NEG_FLAG] = 1'b1;
else flags[`NEG_FLAG] = 1'b0;
if( $unsigned(a) < $unsigned(opcode[7:0]) ) flags[`LOW_FLAG] = 1'b1;
else flags[`LOW_FLAG] = 1'b0;
if( a == opcode[7:0] ) flags[`ZERO_FLAG] = 1'b1;
else flags[`ZERO_FLAG] = 1'b0;
c = 16'b0000000000000000;
end
`SETI:
begin
c = opcode[7:0];
flags = 5'b00000;
end
default:
begin
flags = 5'b000001; // Set invalid op flag
c = 16'b0000000000000000;
end
endcase
end
endmodule