-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData_Path.v
More file actions
231 lines (168 loc) · 4.39 KB
/
Copy pathData_Path.v
File metadata and controls
231 lines (168 loc) · 4.39 KB
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
module alu(op1,op2,ctrl,result);
input [31:0] op1,op2;
input [3:0] ctrl;
wire [31:0] op1,op2;
wire [3:0] ctrl;
output[31:0] result;
reg [31:0] result;
//
always@(op1 or op2 or ctrl)
begin
case(ctrl)
0: begin
$display("and");
result = op1 & op2; // case 0 is for 0000 upcode for AND
end
1: begin
$display("or");
result = op1 | op2; // case 1 is for 0001 upcode for OR
end
2: begin
$display("add");
result = op1 + op2; // case 2 is for 0010 upcode for add
end
6: begin
$display("subtract");
result = op1 - op2; // case 6 is for 0110 upcode for sub
end
7:begin
$display ("Set on Less Than");
result = op1 < op2; // case 7 is for 0111 upcode for slt
if (!(op1<op2))
result = op2 < op1;
end
12:begin
$display ("NOR");
result = ~(op1 | op2); // case 12 is for 1100 upcode for NOR
end
endcase
end
endmodule
module twotoonemux(input1,input2,sel,outputval);
input [31:0] input1;
wire [31:0] input1;
input [31:0] input2;
wire [31:0] input2;
input sel;
wire sel;
output [31:0] outputval;
wire [31:0] outputval;
assign outputval = (sel) ? input1 : input2 ; // if not selecting input 1 then select input 2
endmodule
module registerfile(readReg1, readReg2, writeReg, writeData, regWrite, readData1, readData2 );
input [4:0] readReg1;
wire [4:0] readReg1;
input [4:0] readReg2;
wire [4:0] readReg2;
input [4:0] writeReg;
wire [4:0] writeReg;
input [31:0] writeData;
wire [31:0] writeData;
input regWrite;
wire regWrite;
output[31:0] readData1;
reg [31:0] readData1;
output[31:0] readData2;
reg [31:0] readData2;
reg [31:0] registers [0:31]; // creating 32 registers of 32 bits
always @ (*)
begin
readData1 = registers[readReg1]; // connect read data to the 32 registers of 32 bits
readData2 = registers[readReg2]; // and make them aware of the value of read registers
end
always @(posedge regWrite)
registers[writeReg] <= writeData;
endmodule
module signextend(inputVal,outputVal);
input [15:0] inputVal;
wire [15:0] inputVal;
output[31:0] outputVal;
wire [31:0] outputVal;
assign outputVal = {{16{inputVal[15]}} , inputVal}; // 16 bit to 32 extension preserving the sign
endmodule
/*********************************************************************************************************************************************/
module test;
// initialize the ALU variables insude the testbench
reg [31:0] Op1, Op2;
reg [3:0] Ctrl;
wire [31:0] Result;
alu t_alu(Op1,Op2,Ctrl,Result);
// initialize the MUX in the testbench
reg [31:0] Input1,Input2;
reg Sel;
wire [31:0] Outputval;
twotoonemux t_mux( Input1, Input2, Sel, Outputval);
// initialize the RegisterFile in the testbench
reg [4:0] ReadReg1 , ReadReg2 , WriteReg;
reg [31:0] WriteData;
reg RegWrite;
wire [31:0] ReadData1,ReadData2;
registerfile t_regfile (ReadReg1, ReadReg2, WriteReg, WriteData, RegWrite, ReadData1, ReadData2 );
// initialize the signed extend in the testbench
reg [15:0] InputVal;
wire [31:0] OutputVal;
signextend t_signex (InputVal, OutputVal);
initial
begin
Ctrl = 0;
Op1 = 0;
Op2 = 0;
Sel = 0;
Input1 = 0;
Input2 = 0;
ReadReg1 = 0;
ReadReg2 = 0;
WriteReg = 0;
WriteData = 0;
RegWrite = 0;
InputVal = 0;
end
initial
begin
// test the modules to see if there is a connection
// testing ALU
#1 Ctrl = 0;
#1 Op1 = 12;
#1 Op2 = 11;
#1 if (Ctrl == 0)
$display(" result = %d ", Result);
// testing MUX
#1 Sel = 1;
if (Sel==1)
begin
Input1 = 1;
$display("input1 = %d", Input1);
end
else
begin
Input2 = 0;
$display("input2 = %d", Input2);
end
// testing registerfile
#1 RegWrite = 1;
if (RegWrite == 1)
begin
ReadReg1 = 15;
ReadReg2 = 12;
WriteReg = 10;
WriteData =10;
$display("1ReadData1 = %d , 1ReadData2 = %d ", ReadData1, ReadData2);
end
else
begin
ReadReg1 = 0;
ReadReg2 = 0;
WriteReg = 0;
WriteData = 0;
$display("0ReadData1 = %d , 0ReadData2 = %d ", ReadData1, ReadData2);
end
// testing sign extend
#1 InputVal = 16'b1000001111000000;
$display("outputVal = %b", OutputVal);
end
initial
begin
$dumpfile("Data_Path.vcd");
$dumpvars;
end
endmodule