-
Notifications
You must be signed in to change notification settings - Fork 28
/
ARM_CPU.v
552 lines (447 loc) · 12 KB
/
ARM_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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
`timescale 1ns / 1ps
/*
Group Members: Nikita Eisenhauer and Warren Seto
Lab Name: ARM LEGv8 CPU Design (Single-Cycle)
*/
module ARM_CPU
(
input CLOCK,
input [31:0] INSTRUCTION,
input [63:0] REG_DATA1,
input [63:0] REG_DATA2,
input [63:0] data_memory_out,
output reg CONTROL_REG2LOC,
output reg CONTROL_REGWRITE,
output reg CONTROL_MEMREAD,
output reg CONTROL_MEMWRITE,
output reg CONTROL_BRANCH,
output reg [4:0] READ_REG_1,
output [4:0] READ_REG_2,
output reg [4:0] WRITE_REG,
output [63:0] ALU_Result_Out,
output [63:0] WRITE_REG_DATA,
output reg [63:0] PC
);
reg [4:0] tempRegNum1;
reg [4:0] tempRegNum2;
reg [10:0] tempInstruction;
reg CONTROL_MEM2REG;
reg CONTROL_ALUSRC;
reg CONTROL_UNCON_BRANCH;
reg [1:0] CONTROL_ALU_OP;
wire tempALUZero;
wire[3:0] tempALUControl;
wire[63:0] tempALUInput2;
wire[63:0] tempImmediate;
wire [63:0] tempShiftImmediate;
wire [63:0] nextnextPC;
reg CONTROL_JUMP;
wire [63:0] nextPC;
wire nextPCZero;
wire [63:0] shiftPC;
wire shiftPCZero;
reg tempBranchZero;
/* Multiplexer for the Program Counter */
PCMux mux1(nextPC, shiftPC, CONTROL_JUMP, nextnextPC);
/* Multiplexer before the Register */
RegisterMux mux2(tempRegNum1, tempRegNum2, CONTROL_REG2LOC, READ_REG_2);
/* Multiplexer before the ALU */
ALUMux mux3(REG_DATA2, tempImmediate, CONTROL_ALUSRC, tempALUInput2);
/* Multiplexer after the Data memory */
DataMemMux mux4(data_memory_out, ALU_Result_Out, CONTROL_MEM2REG, WRITE_REG_DATA);
/* Sign Extention Module */
SignExtend mod1(INSTRUCTION, tempImmediate);
/* Shift left by two module */
ShiftLeft2 mod2(tempImmediate, tempShiftImmediate);
/* ALU Control for the ALU */
ALU_Control mod3(CONTROL_ALU_OP, tempInstruction, tempALUControl);
/* ALU Result between the Registers and the Data Memory */
ALU aluResult(REG_DATA1, tempALUInput2, tempALUControl, ALU_Result_Out, tempALUZero);
/* An ALU module to calulcate the next sequential PC */
ALU adderNextPC(PC, 64'h00000004, 4'b0010, nextPC, nextPCZero);
/* An ALU module to calulcate a shifted PC */
ALU adderShiftPC(PC, tempShiftImmediate, 4'b0010, shiftPC, shiftPCZero);
/* Initialize when the CPU is first run */
initial begin
PC = 0;
CONTROL_REG2LOC = 1'bz;
CONTROL_MEM2REG = 1'bz;
CONTROL_REGWRITE = 1'bz;
CONTROL_MEMREAD = 1'bz;
CONTROL_MEMWRITE = 1'bz;
CONTROL_ALUSRC = 1'bz;
CONTROL_BRANCH = 1'b0;
CONTROL_UNCON_BRANCH = 1'b0;
tempBranchZero = tempALUZero & CONTROL_BRANCH;
CONTROL_JUMP = CONTROL_UNCON_BRANCH | tempBranchZero;
end
/* Parse and set the CPU's Control bits */
always @(posedge CLOCK or INSTRUCTION) begin
// Set the PC to the jumped value
if (CONTROL_JUMP == 1'b1) begin
PC = #1 nextnextPC - 4;
end
// Parse the incoming instruction for a given PC
tempInstruction = INSTRUCTION[31:21];
tempRegNum1 = INSTRUCTION[20:16];
tempRegNum2 = INSTRUCTION[4:0];
READ_REG_1 = INSTRUCTION[9:5];
WRITE_REG = INSTRUCTION[4:0];
if (INSTRUCTION[31:26] == 6'b000101) begin // Control bits for B
CONTROL_REG2LOC = 1'b0;
CONTROL_MEM2REG = 1'b0;
CONTROL_REGWRITE = 1'b0;
CONTROL_MEMREAD = 1'b0;
CONTROL_MEMWRITE = 1'b0;
CONTROL_ALUSRC = 1'b0;
CONTROL_ALU_OP = 2'b01;
CONTROL_BRANCH = 1'b0;
CONTROL_UNCON_BRANCH = 1'b1;
end else if (INSTRUCTION[31:24] == 8'b10110100) begin // Control bits for CBZ
CONTROL_REG2LOC = 1'b0;
CONTROL_MEM2REG = 1'b0;
CONTROL_REGWRITE = 1'b0;
CONTROL_MEMREAD = 1'b0;
CONTROL_MEMWRITE = 1'b0;
CONTROL_ALUSRC = 1'b0;
CONTROL_ALU_OP = 2'b01;
CONTROL_BRANCH = 1'b1;
CONTROL_UNCON_BRANCH = 1'b0;
end else begin // R-Type Control Bits
CONTROL_BRANCH = 1'b0;
CONTROL_UNCON_BRANCH = 1'b0;
case (tempInstruction)
11'b11111000010 : begin // Control bits for LDUR
CONTROL_REG2LOC = 1'bx;
CONTROL_MEM2REG = 1'b1;
CONTROL_REGWRITE = 1'b1;
CONTROL_MEMREAD = 1'b1;
CONTROL_MEMWRITE = 1'b0;
CONTROL_ALUSRC = 1'b1;
CONTROL_ALU_OP = 2'b00;
end
11'b11111000000 : begin //Control bits for STUR
// Control Bits
CONTROL_REG2LOC = 1'b1;
CONTROL_MEM2REG = 1'bx;
CONTROL_REGWRITE = 1'b0;
CONTROL_MEMREAD = 1'b0;
CONTROL_MEMWRITE = 1'b1;
CONTROL_ALUSRC = 1'b1;
CONTROL_ALU_OP = 2'b00;
end
11'b10001011000 : begin //Control bits for ADD
CONTROL_REG2LOC = 1'b0;
CONTROL_MEM2REG = 1'b0;
CONTROL_REGWRITE = 1'b1;
CONTROL_MEMREAD = 1'b0;
CONTROL_MEMWRITE = 1'b0;
CONTROL_ALUSRC = 1'b0;
CONTROL_ALU_OP = 2'b10;
end
11'b11001011000 : begin //Control bits for SUB
CONTROL_REG2LOC = 1'b0;
CONTROL_MEM2REG = 1'b0;
CONTROL_REGWRITE = 1'b1;
CONTROL_MEMREAD = 1'b0;
CONTROL_MEMWRITE = 1'b0;
CONTROL_ALUSRC = 1'b0;
CONTROL_ALU_OP = 2'b10;
end
11'b10001010000 : begin //Control bits for AND
CONTROL_REG2LOC = 1'b0;
CONTROL_MEM2REG = 1'b0;
CONTROL_REGWRITE = 1'b1;
CONTROL_MEMREAD = 1'b0;
CONTROL_MEMWRITE = 1'b0;
CONTROL_ALUSRC = 1'b0;
CONTROL_ALU_OP = 2'b10;
end
11'b10101010000 : begin //Control bits for ORR
CONTROL_REG2LOC = 1'b0;
CONTROL_MEM2REG = 1'b0;
CONTROL_REGWRITE = 1'b1;
CONTROL_MEMREAD = 1'b0;
CONTROL_MEMWRITE = 1'b0;
CONTROL_ALUSRC = 1'b0;
CONTROL_ALU_OP = 2'b10;
end
endcase
end
//Determine whether to branch
tempBranchZero = tempALUZero & CONTROL_BRANCH;
CONTROL_JUMP = CONTROL_UNCON_BRANCH | tempBranchZero;
// For non-branch code, set the next sequential PC value
if (CONTROL_JUMP == 1'b0) begin
PC <= #1 nextnextPC;
end
end
endmodule
module PCMux
(
input [63:0] pcInput,
input [63:0] shiftInput,
input CONTROL_JUMP,
output reg [63:0] pcOut
);
always @(pcInput, shiftInput, CONTROL_JUMP, pcOut) begin
if (CONTROL_JUMP == 0) begin
pcOut = pcInput;
end
else begin
pcOut = shiftInput;
end
end
endmodule
module RegisterMux
(
input [4:0] input1,
input [4:0] input2,
input CONTROL_REG2LOC,
output reg [4:0] muxOutput
);
always @(input1, input2, CONTROL_REG2LOC) begin
if (CONTROL_REG2LOC == 0) begin
muxOutput = input1;
end
else begin
muxOutput = input2;
end
end
endmodule
module ALUMux
(
input [63:0] input1,
input [63:0] input2,
input CONTROL_ALUSRC,
output reg [63:0] out
);
always @(input1, input2, CONTROL_ALUSRC, out) begin
if (CONTROL_ALUSRC == 0) begin
out = input1;
end
else begin
out = input2;
end
end
endmodule
module DataMemMux
(
input [63:0] readData,
input [63:0] aluOutput,
input CONTROL_MEM2REG,
output reg [63:0] out
);
always @(readData, aluOutput, CONTROL_MEM2REG, out) begin
if (CONTROL_MEM2REG == 0) begin
out = aluOutput;
end
else begin
out = readData;
end
end
endmodule
module SignExtend
(
input [31:0] inputInstruction,
output reg [63:0] outImmediate
);
always @(inputInstruction) begin
if (inputInstruction[31:26] == 6'b000101) begin // B
outImmediate[25:0] = inputInstruction[25:0];
outImmediate[63:26] = {64{outImmediate[25]}};
end else if (inputInstruction[31:24] == 8'b10110100) begin // CBZ
outImmediate[19:0] = inputInstruction[23:5];
outImmediate[63:20] = {64{outImmediate[19]}};
end else begin // D Type
outImmediate[9:0] = inputInstruction[20:12];
outImmediate[63:10] = {64{outImmediate[9]}};
end
end
endmodule
module ShiftLeft2
(
input [63:0] inputData,
output reg [63:0] outputData
);
always @(inputData) begin
outputData = inputData << 2;
end
endmodule
module ALU_Control
(
input [1:0] ALU_Op,
input [10:0] ALU_INSTRUCTION,
output reg [3:0] ALU_Out
);
always @(ALU_Op or ALU_INSTRUCTION) begin
case (ALU_Op)
2'b00 : ALU_Out = 4'b0010;
2'b01 : ALU_Out = 4'b0111;
2'b10 : begin
case (ALU_INSTRUCTION)
11'b10001011000 : ALU_Out = 4'b0010; // ADD
11'b11001011000 : ALU_Out = 4'b0110; // SUB
11'b10001010000 : ALU_Out = 4'b0000; // AND
11'b10101010000 : ALU_Out = 4'b0001; // ORR
endcase
end
endcase
end
endmodule
module ALU
(
input [63:0] A,
input [63:0] B,
input [3:0] CONTROL,
output reg [63:0] RESULT,
output reg ZEROFLAG
);
always @(A or B or CONTROL) begin
case (CONTROL)
4'b0000 : RESULT = A & B;
4'b0001 : RESULT = A | B;
4'b0010 : RESULT = A + B;
4'b0110 : RESULT = A - B;
4'b0111 : RESULT = B;
4'b1100 : RESULT = ~(A | B);
endcase
if (RESULT == 0) begin
ZEROFLAG = 1'b1;
end else begin
ZEROFLAG = 1'b0;
end
end
endmodule
module Instruction_Memory
(
input [63:0] PC,
output reg [31:0] CPU_Instruction
);
reg [8:0] Data[63:0];
initial begin
// LDUR x2, [x10]
Data[0] = 8'hF8;
Data[1] = 8'h40;
Data[2] = 8'h01;
Data[3] = 8'h42;
// LDUR x3, [x10, #1]
Data[4] = 8'hF8;
Data[5] = 8'h40;
Data[6] = 8'h11;
Data[7] = 8'h43;
// SUB x4, x3, x2
Data[8] = 8'hCB;
Data[9] = 8'h02;
Data[10] = 8'h00;
Data[11] = 8'h64;
// ADD x5, x3, x2
Data[12] = 8'h8B;
Data[13] = 8'h02;
Data[14] = 8'h00;
Data[15] = 8'h65;
// CBZ x1, #2
Data[16] = 8'hB4;
Data[17] = 8'h00;
Data[18] = 8'h00;
Data[19] = 8'h41;
// CBZ x0, #2
Data[20] = 8'hB4;
Data[21] = 8'h00;
Data[22] = 8'h00;
Data[23] = 8'h40;
// LDUR x2 [x10]
Data[24] = 8'hF8;
Data[25] = 8'h40;
Data[26] = 8'h01;
Data[27] = 8'h42;
// ORR x6, x2, x3
Data[28] = 8'hAA;
Data[29] = 8'h03;
Data[30] = 8'h00;
Data[31] = 8'h46;
// AND x7, x2, x3
Data[32] = 8'h8A;
Data[33] = 8'h03;
Data[34] = 8'h00;
Data[35] = 8'h47;
// STUR x4, [x7, #1]
Data[36] = 8'hF8;
Data[37] = 8'h00;
Data[38] = 8'h10;
Data[39] = 8'hE4;
// B #2
Data[40] = 8'h14;
Data[41] = 8'h00;
Data[42] = 8'h00;
Data[43] = 8'h03;
// LDUR x3, [x10, #1]
Data[44] = 8'hF8;
Data[45] = 8'h40;
Data[46] = 8'h11;
Data[47] = 8'h43;
// ADD x8, x0, x1
Data[48] = 8'h8B;
Data[49] = 8'h01;
Data[50] = 8'h00;
Data[51] = 8'h08;
end
always @(PC) begin
CPU_Instruction[8:0] = Data[PC+3];
CPU_Instruction[16:8] = Data[PC+2];
CPU_Instruction[24:16] = Data[PC+1];
CPU_Instruction[31:24] = Data[PC];
end
endmodule
module Registers
(
input [4:0] read1,
input [4:0] read2,
input [4:0] writeReg,
input [63:0] writeData,
input CONTROL_REGWRITE,
output reg [63:0] data1,
output reg [63:0] data2
);
reg [63:0] Data[31:0];
integer initCount;
initial begin
for (initCount = 0; initCount < 31; initCount = initCount + 1) begin
Data[initCount] = initCount;
end
Data[31] = 64’h00000000;
end
always @(read1, read2, writeReg, writeData, CONTROL_REGWRITE) begin
data1 = Data[read1];
data2 = Data[read2];
if (CONTROL_REGWRITE == 1) begin
Data[writeReg] = writeData;
end
end
endmodule
module Data_Memory
(
input [63:0] inputAddress,
input [63:0] inputData,
input CONTROL_MemRead,
input CONTROL_MemWrite,
output reg [63:0] outputData
);
reg [63:0] Data[31:0];
integer initCount;
initial begin
for (initCount = 0; initCount < 32; initCount = initCount + 1) begin
Data[initCount] = initCount * 100;
end
Data[10] = 1540;
Data[11] = 2117;
end
always @(inputAddress, inputData, CONTROL_MemRead, CONTROL_MemWrite) begin
if (CONTROL_MemWrite == 1) begin
Data[inputAddress] = inputData;
end
if (CONTROL_MemRead == 1) begin
outputData = Data[inputAddress];
end
end
endmodule