-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simple_Single_CPU.v
274 lines (239 loc) · 7.31 KB
/
Simple_Single_CPU.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Alden Rivera 0416329
// Jorge Pineda 0416330
//Subject: CO project 2 - Simple Single CPU
//--------------------------------------------------------------------------------
//Version: 1
//--------------------------------------------------------------------------------
//Writer:
//----------------------------------------------
//Date:
//----------------------------------------------
//Description:
//--------------------------------------------------------------------------------
module Simple_Single_CPU(
clk_i,
rst_i
);
//I/O port
input clk_i;
input rst_i;
//Internal Signles
wire [32-1:0] pc_out_o_wire;
wire [32-1:0] adder1_sum_o_wire;
wire [32-1:0] instr_o_wire;
reg [32-1:0] instr_o_wire2;
wire [32-1:0] RTdata_o_wire;
wire [32-1:0] RSdata_o_wire;
wire [32-1:0] Mux_ALUai_o_wire;
wire [32-1:0] adv_instr_o_wire;
wire [32-1:0] Sign_Extend_data_o_wire;
wire [32-1:0] Mux_ALUSrc_wire;
wire [32-1:0] ALU_result_o_wire;
wire [32-1:0] adder2_sum_o_wire;
wire [32-1:0] shifter_data_o;
wire [32-1:0] initial_feed;
wire [32-1:0] MEMdata_o;
wire [32-1:0] regdata_o;
wire [32-1:0] regdata2_o;
wire [5-1:0] Mux_Write_Reg_wire;
wire [5-1:0] Mux_Write_Reg_wire2;
wire [4-1:0] ALUCtrl_o_wire;
wire [3-1:0] ALU_op_o_wire;
wire RegWrite_o_wire;
wire RegWrite_o_wire2;
wire ALUSrc_o_wire;
wire RegDst_o_wire;
wire Branch_o_wire;
wire ai_select_wire;
wire branch_select_wire;
wire Mux_Branch_wire;
wire Mux_Branch_wire1;
wire ALU_zero_o_wire;
wire mainstream = 0;
wire [32-1:0] ShifterJump_o;
wire Jump_o;
wire [32-1:0] PCSource_mux;
wire [32-1:0] PCSource2_mux;
wire MemRead_o;
wire MemWrite_o;
wire MemtoReg_o;
wire Jumpal_o;
reg Jumpr_o;
wire slt_o;
wire Branchslt_o;
//Greate componentes
ProgramCounter PC(
.clk_i(clk_i),
.rst_i (rst_i),
.pc_in_i(initial_feed) ,
.pc_out_o(pc_out_o_wire)
);
Adder Adder1(
.src1_i(pc_out_o_wire),
.src2_i(32'd4),
.sum_o(adder1_sum_o_wire)
);
Instr_Memory IM(
.pc_addr_i(pc_out_o_wire),
.instr_o(instr_o_wire)
);
MUX_2to1 #(.size(5)) Mux_Write_Reg(
.data0_i(instr_o_wire[20:16]),
.data1_i(instr_o_wire[15:11]),
.select_i(RegDst_o_wire),
.data_o(Mux_Write_Reg_wire)
);
Reg_File RF(
.clk_i(clk_i),
.rst_i(rst_i) ,
.RSaddr_i(instr_o_wire[25:21]) ,
.RTaddr_i(instr_o_wire[20:16]) ,
.RDaddr_i(Mux_Write_Reg_wire2) ,
.RDdata_i(regdata2_o) ,
.RegWrite_i (RegWrite_o_wire2),
.RSdata_o(RSdata_o_wire) ,
.RTdata_o(RTdata_o_wire)
);
MUX_2to1 #(.size(1))RegWrite_mux(
.data0_i(RegWrite_o_wire),
.data1_i(1'b0),
.select_i(Jumpr_o),
.data_o(RegWrite_o_wire2)
);
Decoder Decoder(
.instr_op_i(instr_o_wire2[31:26]),
.RegWrite_o(RegWrite_o_wire),
.ALU_op_o(ALU_op_o_wire),
.ALUSrc_o(ALUSrc_o_wire),
.RegDst_o(RegDst_o_wire),
.Branch_o(Branch_o_wire),
.Jump_o(Jump_o),
.MemRead_o(MemRead_o),
.MemWrite_o(MemWrite_o),
.MemtoReg_o(MemtoReg_o),
.Jumpal_o(Jumpal_o),
.Branchslt_o(Branchslt_o)
);
ALU_Ctrl AC(
.funct_i(instr_o_wire[5:0]),
.ALUOp_i(ALU_op_o_wire),
.ALUCtrl_o(ALUCtrl_o_wire)
);
//AV modules
adv_instr_sel ai(.op_field_i(instr_o_wire[31:26]), .function_field_i(instr_o_wire[5:0]), .select(ai_select_wire));
MUX_2to1 #(.size(32))
Mux_ALUai(.data0_i(ALU_result_o_wire),
.data1_i(adv_instr_o_wire),
.select_i(ai_select_wire),
.data_o(Mux_ALUai_o_wire) );
MUX_2to1 #(.size(32))
Mux_ALUregdata(
.data0_i(Mux_ALUai_o_wire),
.data1_i(MEMdata_o),
.select_i(MemtoReg_o),
.data_o(regdata_o)
);
MUX_2to1 #(.size(32)) Mux_ALUregdata2(
.data0_i(regdata_o),
.data1_i(adder1_sum_o_wire),
.select_i(Jumpal_o),
.data_o(regdata2_o)
);
adv_instr a(.instr(instr_o_wire), .regs(RSdata_o_wire), .regt(RTdata_o_wire), .result(adv_instr_o_wire));
branch_sel b(.instr(instr_o_wire[31:26]), .select(branch_select_wire));
MUX_2to1 #(.size(1))
Mux_Branch(.data0_i(ALU_zero_o_wire && Branch_o_wire),
.data1_i((~ALU_zero_o_wire) && Branch_o_wire),
.select_i(branch_select_wire),
.data_o(Mux_Branch_wire1)
);
MUX_2to1 #(.size(1)) Mux_Branch2 (
.data0_i(Mux_Branch_wire1),
.data1_i(slt_o),
.select_i(Branchslt_o),
.data_o(Mux_Branch_wire)
);
//----------------------------------------------------------------------------------------------
Sign_Extend SE(
.data_i(instr_o_wire[15:0]),
.data_o(Sign_Extend_data_o_wire)
);
MUX_2to1 #(.size(32))
Mux_ALUSrc( .data0_i(RTdata_o_wire),
.data1_i(Sign_Extend_data_o_wire),
.select_i(ALUSrc_o_wire),
.data_o(Mux_ALUSrc_wire)
);
ALU ALU(
.src1_i(RSdata_o_wire),
.src2_i(Mux_ALUSrc_wire),
.ctrl_i(ALUCtrl_o_wire),
.result_o(ALU_result_o_wire),
.zero_o(ALU_zero_o_wire)
);
ALU ALU2(
.src1_i(RSdata_o_wire),
.src2_i(RTdata_o_wire),
.ctrl_i(4'b0111),
.result_o(slt_o)
);
Adder Adder2(
.src1_i(adder1_sum_o_wire),
.src2_i(shifter_data_o),
.sum_o(adder2_sum_o_wire)
);
Shift_Left_Two_32 Shifter(
.data_i(Sign_Extend_data_o_wire),
.data_o(shifter_data_o)
);
MUX_2to1 #(.size(32)) Mux_PC_Source(
.data0_i(adder1_sum_o_wire),
.data1_i(adder2_sum_o_wire),
.select_i(Mux_Branch_wire),
.data_o(PCSource_mux)
);
Data_Memory Data_Memory(
.clk_i(clk_i),
.addr_i(ALU_result_o_wire),
.data_i(RTdata_o_wire),
.MemRead_i(MemRead_o),
.MemWrite_i(MemWrite_o),
.data_o(MEMdata_o)
);
Shift_Left_Two_32 ShifterJump(
.data_i(instr_o_wire),
.data_o(ShifterJump_o)
);
MUX_2to1 #(.size(32)) Mux_PC_Source2(
.data0_i({adder1_sum_o_wire[31:28],ShifterJump_o[27:0]}),
.data1_i(PCSource_mux),
.select_i(Jump_o),
.data_o(PCSource2_mux)
);
MUX_2to1 #(.size(32)) Mux_PC_Source3(
.data0_i(PCSource2_mux),
.data1_i(RSdata_o_wire),
.select_i(Jumpr_o),
.data_o(initial_feed)
);
MUX_2to1 #(.size(5)) Mux_WriteReg2(
.data0_i(Mux_Write_Reg_wire),
.data1_i(5'b11111),
.select_i(Jumpal_o),
.data_o(Mux_Write_Reg_wire2)
);
always@(*)
begin
if(instr_o_wire==0)
begin
instr_o_wire2 = {6'b111111,26'b0};
end else instr_o_wire2 = instr_o_wire;
end
always@(*)
begin
if(instr_o_wire[31:26]==6'b000000 && instr_o_wire[5:0]==6'b001000)
begin
Jumpr_o = 1'b1;
end else Jumpr_o = 1'b0;
end
endmodule