-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMYTest.v
More file actions
443 lines (365 loc) · 10.7 KB
/
Copy pathMYTest.v
File metadata and controls
443 lines (365 loc) · 10.7 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Skip to content
// This repository
// Search
// Pull requests
// Issues
// Gist
// @kobija
// Watch 1
// Star 2
// Fork 0 tronophono/Gforce_Lab4 Private
// Code Issues 0 Pull requests 0 Projects 0 Wiki Pulse Graphs
// Branch: master Find file Copy pathGforce_Lab4/Gforce_cpu.v
// d9e6678 an hour ago
// @tronophono tronophono Updated the CPU
// 1 contributor
// RawBlameHistory
// 406 lines (331 sloc) 9.53 KB
/*
This files will contain all components that the CPU has inside of it
It will also be used to start putting things to gether.
*/
/* ALU Control - by Kevin Valdez
Takes ALUOp code from control and read function instruction and sets out
Alu control code to tell alu which operation to do. */
module ALUControl (ALUOp , Function , Output);
input wire [1:0] ALUOp;
input wire [5:0] Function;
output reg [3:0] Output;
always @(ALUOp or Function)
begin
case(ALUOp)
0: begin
$display("ALUOp = 00");
Output = 4'b0010;
end
1: begin
$display("AlUOp = 01");
Output = 4'b0110;
end
2: begin
$display("ALUOp = 10");
if (Function == 6'b100000)
begin
Output = 4'b0010; // add
$display("add");
end
else if(Function == 6'b100010)
begin
Output = 4'b0110; // subtract
$display("sub");
end
else if(Function == 6'b100100)
begin
Output = 4'b0000; // and
$display("and");
end
else if(Function == 6'b100101)
begin
Output = 4'b0001; // or
$display("or");
end
else if(Function == 6'b101010)
begin
Output = 4'b0111; // SLT (set less than)
$display("slt");
end
end
endcase
end
endmodule
/* Data Path Muxes - by Kevin Valdez
Multiplexers that are part of the cpu datapah that are controlled by
the control signals RegDst, ALUSrc, MemtoReg.
*/
module muxRegDestination(input1,input2, RegDestination ,outputval);
input wire [5:0] input1;
input wire [5:0] input2;
output wire[5:0] outputval;
input wire RegDestination;
assign outputval = (RegDestination)? input1 : input2;
endmodule
/******************************************************************************************************************************************/
module muxALUSrc (result, result2, ALUSrc , outputval2 );
input wire [31:0] result;
input wire [31:0] result2;
output wire[31:0] outputval2;
input wire ALUSrc;
assign outputval2 = (ALUSrc)? result: result2;
endmodule
/********************************************************************************************************************************************/
module muxMemtoReg (solution, solution2 , memtoReg , outputval3);
input wire [31:0] solution;
input wire [31:0] solution2;
input wire memtoReg;
output wire [31:0] outputval3;
assign outputval3 = (memtoReg)? solution: solution2;
endmodule
/*Instruction Memory - by Donato Kava
Slices up the instruction we give to cpu into 6 parts.*/
module instructmem(
input wire [31:0] inputVal,
output wire [31:26] instruct1,
output wire [25:21] instruct2,
output wire [20:16] instruct3,
output wire [15:11] instruct4,
output wire [15:0] instruct5,
output wire [5:0] instruct6);
//instruction mememory, takes register input(program instruction)
//and splits it up 6 parts
//registers to hold wire data
reg [5:0] inInstruct1;
reg [4:0] inInstruct2;
reg [4:0] inInstruct3;
reg [4:0] inInstruct4;
reg [15:0] inInstruct5;
reg [5:0] inInstruct6;
//always sets inputval bits to their internal regs
always
begin
#1
inInstruct1 = inputVal[31:26];
inInstruct2 = inputVal[25:21];
inInstruct3 = inputVal[20:16];
inInstruct4 = inputVal[15:11];
inInstruct5 = inputVal[15:0];
inInstruct6 = inputVal[5:0];
end
//set wires to internal registers
assign instruct1 = inInstruct1;
assign instruct2 = inInstruct2;
assign instruct3 = inInstruct3;
assign instruct4 = inInstruct4;
assign instruct5 = inInstruct5;
assign instruct6 = inInstruct6;
endmodule
/* Control - by Luis Santos
Takes the Opcode sent to it and sends
output signals dependant for the Opcode.
*/
module control(
input wire [5:0] Opcode,
output reg RegDst,
output reg Branch,
output reg MemtoRead,
output reg MemtoReg,
output reg [1:0] ALUOp,
output reg MemtoWrite,
output reg ALUSrc,
output reg RegWrite);
/*
This module sets the control signals for the control Path
The Signals are dependant on the opcode given
*/
always@(Opcode) begin
case (Opcode)
0 : begin RegDst = 1;
Branch = 0;
MemtoRead = 0;
MemtoReg = 0;
ALUOp = 2;
MemtoWrite = 0;
ALUSrc = 0;
RegWrite = 1;
end
35:begin
RegDst = 0;
ALUSrc = 1;
MemtoReg = 1;
RegWrite = 1;
MemtoRead = 1;
MemtoWrite = 0;
Branch = 0;
ALUOp = 0;
end
43:begin
ALUSrc = 1;
RegWrite = 0;
MemtoRead = 0;
MemtoWrite = 1;
Branch = 0;
ALUOp = 0;
end
endcase
end
endmodule
/* Data Memory - Eberado Sanchez*/
module Memory (wrctrl,rdctrl,addr,wrdata,rddata);
input wire wrctrl,rdctrl;
input wire [31:0] addr;
input wire [31:0] wrdata;
output reg [31:0] rddata;
reg [31:0] mem_file [0:127];
always @(posedge wrctrl or posedge rdctrl) begin
rddata = (rdctrl) ? mem_file[addr]:0;
mem_file[addr] = (wrctrl) ? wrdata:0;
end
endmodule
/* ALU component*/
module alu(
input wire [31:0] op1,
input wire [31:0] op2,
input wire [3:0] ctrl,
output reg [31:0] result
);
//Trigger when ctrl changes values
//Can change the blocking statements to nonblocking statements
//However changes in the testbench will be required(remove #'s , except for the op1)
always@(ctrl) begin
case(ctrl)
//Instructions as shown in table in pg 259
0 : result = op1 & op2;
1 : result = op1 | op2;
2 : result = op1 + op2;
6 : result = op1 - op2;
7 : result = op1 < op2;
12: result = ~(op1|op2);
default: result = 0; //Read that most ALUs have a 0 when
// no valid operation was chosen
endcase
end
endmodule
/*Register file component*/
module registerfile(
input wire [4:0] readReg1,
input wire [4:0] readReg2,
input wire [4:0] writeReg,
input wire [31:0] writeData,
input wire regWrite,
output reg [31:0] readData1,
output reg [31:0] readData2);
//used to make a array of 32 32-bit registers
reg [31:0] regfile[31:0];
//Registerfile will always read registers but will only write to them when
//regWrite is set.
always @ (readReg1,readReg2,regWrite) begin
readData1 = regfile[readReg1];
readData2 = regfile[readReg2];
if(regWrite == 1)
regfile[writeReg] = writeData;
end
endmodule
//Sign extend component
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
/*Main mipscpu*/
module mipscpu(
input wire reset,
input wire clock,
input wire [31:0] instrword,
input wire newinstr);
// Made wires to store the output signals
wire [31:26] opcodecpu;
wire [25:21] readReg1cpu;
wire [20:16] readReg2cpu;
wire [15:11] mux1rdcpu;
wire [15:0] signExtendercpu;
wire [5:0] alufunctioncpu;
// Made some output wires for the control signals
wire regdstcpu;
wire branchcpu;
wire memreadcpu;
wire memtoregcpu;
wire [1:0] aluopcpu;
wire memwritecpu;
wire alusrccpu;
wire regwritecpu;
wire [31:0] op2alu;
wire [31:0] outputtoregwrite;
wire [3:0] aluctrltoalu;
wire [31:0] aluresultcpu;
//Connecting instruction to instruction memory
instructmem insmemcpu(
instrword, //From testbench
opcodecpu, //Goes to control
readReg1cpu, //Goes to register file
readReg2cpu, //Goes to register file and mux
mux1rdcpu, //Rd address to mux
signExtendercpu, //
alufunctioncpu);
/*Inputs opcode made by instructmem and assign signals based on opcode
Signal names can be used on other devices to connect them together*/
control controlcpu(
opcodecpu,
regdstcpu,
branchcpu,
memreadcpu,
memtoregcpu,
aluopcpu,
memwritecpu,
alusrccpu,
regwritecpu);
/*Connect RegDest signal from control and the other parts of instruction word to mux
that later connects to register file*/
muxRegDestination muxRegDestcpu(
readReg2cpu,
mux1rdcpu,
regdstcpu,
towriteregistercpu);
//Decides if wether or not alu will use offset .
muxALUSrc muxAlusrccpu(
readdata2cpu,
signextresultcpu,
alusrccpu,
op2alu);
/*This mux will decide if either the value to be written back to register file
is either the result from alu, or from memory.*/
muxMemtoReg muxmemtoregcpu(
readdata,
aluresult,
memtoregcpu,
outputtoregwrite
);
ALUControl alucontrolcpu(
aluopcpu,
alufunctioncpu,
aluctrltoalu);
//In here replace readdata1cpu with register file read data 1 output name
alu alucpu(
readdata1cpu,
op2alu,
aluctrltoalu,
aluresultcpu);
registerfile registerfilecpu(
readReg1cpu,
readReg2cpu,
towriteregistercpu,
outputtoregwrite,
regwritecpu,
readdata1cpu,
readdata2cpu
);
signextend cpusignextende(
signextedcpu,
signextresultcpu
);
Memory memcpu(
memwritecpu,
memreadcpu,
aluresultcpu,
readdata2cpu,
readdata
);
endmodule
//Test Bench for CPU
//Runs clock, change register instructions, verify correct output
module testybench();
//input registers
//output reading register
reg Reset, Clock, Newinstr;
reg [31:0] Instrword;
mipscpu mycpu(
Reset,
Clock,
Instrword,
Newinstr);
//mipscpu.mycpu.mem_file[0] = 10; //a = 10
initial begin
mycpu.memcpu.mem_file[0] = 10;
end
endmodule