-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathALU.V
243 lines (202 loc) · 4.25 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/****************************************************************************************
MODULE: Sub Level Arithmatic Logic Unit Block
FILE NAME: alu.v
VERSION: 1.0
DATE: September 28th, 2001
AUTHOR: Hossein Amidi
COMPANY: California Unique Electrical Co.
CODE TYPE: Register Transfer Level
Instantiations:
DESCRIPTION:
Sub Level RTL Arithmatic Logic Unit block
Hossein Amidi
(C) September 2001
California Unique Electric
***************************************************************************************/
`timescale 1ns / 1ps
module ALU (// Input
ALUSrcA,
ALUSrcB,
OpCode,
CurrentState,
// Output
ALUDataOut
);
// Parameter
parameter DataWidth = 32;
parameter OpcodeSize = 8;
parameter StateSize = 2;
parameter FunctionSize = 8;
// Instructions options
parameter LDA = 8'h0;
parameter STO = 8'h1;
parameter ADD = 8'h2;
parameter SUB = 8'h3;
parameter JMP = 8'h4;
parameter JGE = 8'h5;
parameter JNE = 8'h6;
parameter STP = 8'h7;
parameter SHR = 8'h8;
parameter SHL = 8'h9;
parameter AND = 8'ha;
parameter OR = 8'hb;
parameter XOR = 8'hc;
parameter COM = 8'hd;
parameter SWP = 8'he;
parameter NOP = 8'hf;
// Instruction for Memory Map devices
parameter MAP = 9'h64;
// Current State options
parameter Init = 2'b00;
parameter InstrFetch = 2'b01;
parameter InstrExec = 2'b10;
// Function Select options
parameter FnAdd = 8'b0000_0000;
parameter FnSub = 8'b0000_0001;
parameter FnPassB = 8'b0000_0010;
parameter FnIncB = 8'b0000_0011;
parameter FnShtR = 8'b0000_0100;
parameter FnShtL = 8'b0000_0101;
parameter FnAnd = 8'b0000_0110;
parameter FnOr = 8'b0000_0111;
parameter FnXor = 8'b0000_1000;
parameter FnCom = 8'b0000_1001;
parameter FnSwp = 8'b0000_1010;
parameter FnNop = 8'b0000_1011;
// Input
input [DataWidth - 1 : 0] ALUSrcA;
input [DataWidth - 1 : 0] ALUSrcB;
input [OpcodeSize - 1 : 0] OpCode;
input [StateSize - 1 : 0] CurrentState;
// Output
output [DataWidth - 1 : 0] ALUDataOut;
// Signal Assignments
reg [DataWidth - 1 : 0] ALUDataOut;
reg [FunctionSize - 1 : 0] FunctSel;
reg CIn;
wire [DataWidth - 1 : 0] AIn, BIn;
// Assignment
assign AIn = ALUSrcA;
assign BIn = ALUSrcB;
always @(OpCode or CurrentState)
begin
if (CurrentState == InstrFetch)
begin
if (OpCode != STP) // In the Fetch cycle increment PC
begin
FunctSel <= FnIncB;
CIn <= 1;
end
else
begin
FunctSel <= FnPassB;
CIn <= 0;
end
end
else
if(CurrentState == InstrExec)
begin
case (OpCode)
LDA :
begin
FunctSel <= FnPassB;
CIn <= 0;
end
STO :
begin
FunctSel <= FnAdd;
CIn <= 0;
end
ADD :
begin
FunctSel <= FnAdd;
CIn <= 0;
end
SUB :
begin
FunctSel <= FnSub;
CIn <= 0;
end
JMP :
begin
FunctSel <= FnPassB;
CIn <= 0;
end
JGE :
begin
FunctSel <= FnPassB;
CIn <= 0;
end
JNE :
begin
FunctSel <= FnPassB;
CIn <= 0;
end
STP :
begin
FunctSel <= FnPassB;
CIn <= 0;
end
SHR :
begin
FunctSel <= FnShtR;
CIn <= 0;
end
SHL :
begin
FunctSel <= FnShtL;
CIn <= 0;
end
AND :
begin
FunctSel <= FnAnd;
CIn <= 0;
end
OR :
begin
FunctSel <= FnOr;
CIn <= 0;
end
XOR :
begin
FunctSel <= FnXor;
CIn <= 0;
end
COM :
begin
FunctSel <= FnCom;
CIn <= 0;
end
SWP :
begin
FunctSel <= FnSwp;
CIn <= 0;
end
NOP :
begin
FunctSel <= FnNop;
CIn <= 0;
end
default : ;
endcase
end
end
always @(AIn or BIn or CIn or FunctSel)
begin
case (FunctSel)
FnAdd : ALUDataOut <= AIn + BIn;
FnSub : ALUDataOut <= AIn - BIn;
FnPassB : ALUDataOut <= BIn;
FnIncB : ALUDataOut <= BIn + CIn;
FnShtR : ALUDataOut <= AIn >> 1;
FnShtL : ALUDataOut <= AIn << 1;
FnAnd : ALUDataOut <= AIn & BIn;
FnOr : ALUDataOut <= AIn | BIn;
FnXor : ALUDataOut <= AIn ^ BIn;
FnCom : ALUDataOut <= ~BIn;
FnSwp : ALUDataOut <= {BIn[15:0],BIn[31:16]};
FnNop : ALUDataOut <= BIn;
default : ALUDataOut <= AIn + BIn;
endcase
end
endmodule