-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcpu_tb.sv
More file actions
688 lines (655 loc) · 22.3 KB
/
cpu_tb.sv
File metadata and controls
688 lines (655 loc) · 22.3 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
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
//-----------------------------------------------------------------------------
//
// Title : 8-bit microprocessor cpu
// Author : vinod sake <vinosake@pdx.edu>
// Company : Student
//
//-----------------------------------------------------------------------------
//
// File : cpu_tb.sv
// Generated : 29 Dec 2016
// Last Updated: 02 Jan 2017
//-----------------------------------------------------------------------------
//
// Description : Performing testing on all modules integrated
//
//
//-----------------------------------------------------------------------------
`include "constants.sv"
//-----------------------------------------------------------------------------------------------------------//
// Parameters for the module
parameter string_len = 200;
enum {A,B,C,D,E,F,G,H}gp_registers;
parameter data_width = 8;
parameter address_width = 16;
parameter rom_size = 1 << address_width;
parameter temp_rom_size = 1 << 6;
//-----------------------------------------------------------------------------------------------------------//
module cpu_tb();
//-----------------------------------------------------------------------------------------------------------//
// CPU inputs and outputs
logic clock;
logic reset;
logic force_rom_write = 1'b0; // Active high
logic force_rom_read = 1'b0; // Active high
logic force_rom_enable = 1'b0;
logic [address_width-1:0]force_rom_address;
wire [data_width-1:0]force_rom_data;
logic force_ram_write = 1'b0; // Active high
logic force_ram_read = 1'b0; // Active high
logic force_ram_enable = 1'b0;
logic [address_width-1:0]force_ram_address;
wire [data_width-1:0]force_ram_data;
//-----------------------------------------------------------------------------------------------------------//
//-----------------------------------------------------------------------------------------------------------//
// Internal strings, wires and regs
logic [15:0]instruction;
integer read_rom;
integer ram_read;
integer count = 0;
integer write_databus;
integer read_instruction;
integer string_space=0;
integer string_comma=0;
integer string_space_count=0;
integer string_comma_count=0;
string opcode;
string instruction_string;
string first_operand;
string second_operand;
string immediate_first_data_string;
string immediate_second_data_string;
logic [7:0]immediate_first_data;
logic [7:0]immediate_second_data;
logic [15:0]immediate_first_address;
logic [15:0]immediate_second_address;
logic [data_width-1:0]temp_data;
logic data_control = 1'b0;
logic [data_width-1:0]temp_ram_data;
logic data_ram_control = 1'b0;
//-----------------------------------------------------------------------------------------------------------//
// System Clock
initial clock = 1'b0;
always #50 clock = ~clock;
cpu cpu_dut(
.clock(clock),
.reset(reset),
.force_rom_write(force_rom_write),
.force_rom_read(force_rom_read),
.force_rom_enable(force_rom_enable),
.force_rom_address(force_rom_address),
.force_rom_data(force_rom_data),
.force_ram_write(force_ram_write),
.force_ram_read(force_ram_read),
.force_ram_enable(force_ram_enable),
.force_ram_address(force_ram_address),
.force_ram_data(force_ram_data)
);
// Waveform log file and monitoring
initial begin
$dumpfile("cpu.vcd");
$dumpvars();
end
initial begin
#50;
//-----------------------------------------------------------------------------------------------------------//
// Reading Instructions from file and converting it into Hex format and writing into ROM
read_instruction = $fopen("cpu_program.txt","r"); // Open instruction file in read mode
while(!$feof(read_instruction)) begin
instruction = $fscanf(read_instruction, "%s\n", instruction_string); // Read instructions from file to be loaded into ROM
instruction_decode(instruction_string); // Decodes instruction into Hex code
force_rom_write = 1;
force_rom_read = 0;
force_rom_enable = 1;
data_control = 1;
for (int j = 0; j < 2; j = j+1) begin
if(j == 0) begin // Storing high bits of instruction in ROM
force_rom_address = count;
temp_data = instruction[15:8];
count = count+1;
#50;
end
else if(j == 1) begin // Storing low bits of instruction in ROM
force_rom_address = count;
temp_data = instruction[7:0];
count = count+1;
#50;
end
end
// Store immediate data and address locations in ROM
// ALU operations
if(instruction[15] == 1'b0 && instruction[15:11] != 5'b01101 && instruction[15:11] != 5'b01110 && instruction[15:11] != `JUMP) begin
if(second_operand[0] == "#") begin // ALU operation with immediate data
force_rom_address = count;
temp_data = immediate_second_data;
count = count+1;
#50;
end
else begin
$display("%3d - Invalid second operand for ALU operations",`__LINE__);
end
end
// MOVE operation
else if(instruction[15:11] === `MOVE) begin
if(second_operand[0] == "#") begin
$display("%3d - Invalid second_operand for MOVE operation",`__LINE__);
end
else if((second_operand[0] != "#") && opcode == "MOVE") begin // MOVE operation with source memory address
for (int j = 0; j < 2; j = j+1) begin
if(j == 0) begin // Storing high bits of source memory address in ROM
force_rom_address = count;
temp_data = immediate_second_address[15:8];
count = count+1;
#50;
end
else if(j == 1) begin // Storing low bits of destination memory address in ROM
force_rom_address = count;
temp_data = immediate_second_address[7:0];
count = count+1;
#50;
end
end
end
if(first_operand[0] == "#") begin
$display("%3d - Invalid first_operand for MOVE operation",`__LINE__);
end
else if((first_operand[0] != "#") && opcode == "MOVE") begin // MOVE operation with destination memory address
for (int j = 0; j < 2; j = j+1) begin
if(j == 0) begin // Storing high bits of destination memory address in ROM
force_rom_address = count;
temp_data = immediate_first_address[15:8];
count = count+1;
#50;
end
else if(j == 1) begin // Storing low bits of destination memory address in ROM
force_rom_address = count;
temp_data = immediate_first_address[7:0];
count = count+1;
#50;
end
end
end
end
// LOAD operation
else if(instruction[15:11] === `LOAD) begin
if(second_operand[0] == "#") begin // LOAD operation with source immediate data
force_rom_address = count;
temp_data = immediate_second_data;
count = count+1;
#50;
end
else if(second_operand[0] != "#") begin // LOAD operation with source memory address
for (int j = 0; j < 2; j = j+1) begin
if(j == 0) begin // Storing high bits of source memory address in ROM
force_rom_address = count;
temp_data = immediate_second_address[15:8];
count = count+1;
#50;
end
else if(j == 1) begin // Storing low bits of source memory address in ROM
force_rom_address = count;
temp_data = immediate_second_address[7:0];
count = count+1;
#50;
end
end
end
end
// STORE operation
else if(instruction[15:11] === `STORE) begin
if(second_operand[0] == "#") begin // STORE operation with source immediate data
$display("%3d - Invalid second operand for STORE operation",`__LINE__);
end
else if(second_operand[0] != "#") begin // STORE operation with source memory address
for (int j = 0; j < 2; j = j+1) begin
if(j == 0) begin // Storing high bits of destination memory address in ROM
force_rom_address = count;
temp_data = immediate_second_address[15:8];
count = count+1;
#50;
end
else if(j == 1) begin // Storing low bits of destination memory address in ROM
force_rom_address = count;
temp_data = immediate_second_address[7:0];
count = count+1;
#50;
end
end
end
end
// JUMP operation
else if(instruction[15:11] === `JUMP) begin
$display("load");
if(first_operand[0] == "#") begin
$display("%3d - Invalid first operand for STORE operation",`__LINE__);
end
else if(first_operand[0] != "#") begin // STORE operation with source memory address
for (int j = 0; j < 2; j = j+1) begin
if(j == 0) begin // Storing high bits of jump memory address in ROM
force_rom_address = count;
temp_data = immediate_first_address[15:8];
count = count+1;
#50;
end
else if(j == 1) begin // Storing low bits of jump memory address in ROM
force_rom_address = count;
temp_data = immediate_first_address[7:0];
count = count+1;
#50;
end
end
end
end
end
//-----------------------------------------------------------------------------------------------------------//
//-----------------------------------------------------------------------------------------------------------//
// After writing instructions in ROM, print data into afile by reading memory from ROM
read_rom = $fopen("read_cpu_rom.txt","w"); // Open instruction file in write mode
for(int i=0; i<temp_rom_size; i=i+1) begin
@(posedge clock) begin
force_rom_read = 1;
force_rom_write = 0;
force_rom_enable = 1;
force_rom_address = i;
data_control = 0;
end
@(negedge clock) #5 begin
$fwrite(read_rom,"address:%h Data:%h\n", force_rom_address,force_rom_data); // Print address and corresponding data from ROM in a file
end
end
//-----------------------------------------------------------------------------------------------------------//
force_rom_write = 0;
force_rom_read = 0;
force_rom_enable = 0;
force_ram_enable = 0;
//Reset
@(negedge clock) begin
reset = 0; // System Reset
end
@(negedge clock) begin
reset = 1;
end
#5000; // Wait for all the instructions to execute in CPU
//-----------------------------------------------------------------------------------------------------------//
//Make sure the results are stored in RAM. Print address and corresponding data from RAM in a file
ram_read = $fopen("read_cpu_ram.txt","w"); // Open instruction file in write mode
for(int i=0; i<temp_rom_size; i=i+1) begin
@(posedge clock) begin
force_ram_read = 1;
force_ram_write = 0;
force_ram_enable = 1;
force_ram_address = i;
data_ram_control = 0;
end
@(negedge clock) #5 begin
$fwrite(ram_read,"address:%h Data:%h\n", force_ram_address,force_ram_data); // Print address and corresponding data from RAM in a file
end
end
//-----------------------------------------------------------------------------------------------------------//
$fclose(read_instruction);
$fclose(read_rom);
$fclose(ram_read);
end
assign force_ram_data = data_ram_control ? temp_ram_data : 8'hzz;
assign force_rom_data = data_control ? temp_data : 8'hzz;
//-----------------------------------------------------------------------------------------------------------//
// Task to decode instructions
task instruction_decode(input string instruction_string);
begin
string_space = 0;
string_comma = 0;
string_space_count = 0;
string_comma_count = 0;
for(int i=0; i<instruction_string.len(); i=i+1) begin
if(instruction_string[i] == "_") begin
string_space = i;
string_space_count = string_space_count+1;
end
else if(instruction_string[i] == ",") begin
string_comma = i;
string_comma_count = string_comma_count+1;
end
end
if(string_space_count == 0 && string_comma_count == 0) begin // No operands in instruction
opcode = instruction_string;
first_operand = "NA";
second_operand = "NA";
end
else if(string_space_count == 1 && string_comma_count == 0) begin // Single operand in instruction
opcode = instruction_string.substr(0,string_space-1);
first_operand = instruction_string.substr(string_space+1,instruction_string.len()-1);
second_operand = "NA";
if(first_operand[0] == "#") begin
immediate_first_data_string = first_operand.substr(1,first_operand.len()-1);
immediate_first_data = immediate_first_data_string.atohex;
end
else if(first_operand[0] != "#") begin
immediate_first_address = first_operand.atohex;
end
end
else if(string_space_count == 1 && string_comma_count == 1)begin // Two operands in instruction
opcode = instruction_string.substr(0,string_space-1);
first_operand = instruction_string.substr(string_space+1,string_comma-1);
second_operand = instruction_string.substr(string_comma+1,instruction_string.len()-1);
if(first_operand[0] == "#") begin
immediate_first_data_string = first_operand.substr(1,first_operand.len()-1);
immediate_first_data = immediate_first_data_string.atohex;
end
else if(first_operand[0] != "#") begin
immediate_first_address = first_operand.atohex;
end
if(second_operand[0] == "#") begin
immediate_second_data_string = second_operand.substr(1,second_operand.len()-1);
immediate_second_data = immediate_second_data_string.atohex;
end
else if(second_operand[0] != "#") begin
immediate_second_address = second_operand.atohex;
end
end
case(opcode)
"NOP": nop_halt();
"ADD": alu_operations();
"SUB": alu_operations();
"MUL": alu_operations();
"AND": alu_operations();
"OR": alu_operations();
"LSR": alu_operations();
"LSL": alu_operations();
"ASR": alu_operations();
"ASL": alu_operations();
"NOT": alu_operations();
"NEG": alu_operations();
"LOAD": load_operation();
"STORE":store_operation();
"MOVE": move_operation();
"JMP": jump_operation(); // Always Jump
"JC": jump_operation(); // Jump if carry
"JZ": jump_operation(); // Jump if Zero
"JS": jump_operation(); // Jump if Negitive
"HALT": nop_halt();
endcase
end
endtask
//-----------------------------------------------------------------------------------------------------------//
//-----------------------------------------------------------------------------------------------------------//
// Task to decode ALU operations instructions
task alu_operations();
begin
instruction[8] = 0;
instruction[1:0] = 2'b00;
case(opcode)
"ADD": instruction[15:11] = `ADD;
"SUB": instruction[15:11] = `SUBTRACT;
"MUL": instruction[15:11] = `MULTIPLY;
"AND": instruction[15:11] = `AND;
"OR": instruction[15:11] = `OR;
"LSR": instruction[15:11] = `LOGICAL_SHIFT_RIGHT;
"LSL": instruction[15:11] = `LOGICAL_SHIFT_LEFT;
"ASR": instruction[15:11] = `ARITH_SHIFT_RIGHT;
"ASL": instruction[15:11] = `ARITH_SHIFT_LEFT;
"NOT": instruction[15:11] = `COMPLEMENT;
"NEG": instruction[15:11] = `TWOS_COMPLEMENT;
endcase
// Single operand ALU operations
if(opcode == "LSR" || opcode == "LSL" || opcode == "ASR" || opcode == "ASL" || opcode == "NOT" || opcode == "NEG") begin
case(first_operand)
"A": instruction[4:2] = A;
"B": instruction[4:2] = B;
"C": instruction[4:2] = C;
"D": instruction[4:2] = D;
"E": instruction[4:2] = E;
"F": instruction[4:2] = F;
"G": instruction[4:2] = G;
"H": instruction[4:2] = H;
default : begin
$display("%3d - Invalid first operand for alu_operations, can be only source registers",`__LINE__);
end
endcase
instruction[7:5] = 0;
end
// Two operands ALU operations
else if(opcode == "ADD" || opcode == "SUB" || opcode == "MUL" || opcode == "AND" || opcode == "OR" ) begin
case(first_operand)
"A": instruction[7:5] = A;
"B": instruction[7:5] = B;
"C": instruction[7:5] = C;
"D": instruction[7:5] = D;
"E": instruction[7:5] = E;
"F": instruction[7:5] = F;
"G": instruction[7:5] = G;
"H": instruction[7:5] = H;
default : begin
$display("%3d - Invalid first operand for alu_operations, can only be source registers",`__LINE__);
end
endcase
case(second_operand)
"A": begin
instruction[4:2] = A;
instruction[10:9] = 2'b00; // Source Register
end
"B": begin
instruction[4:2] = B;
instruction[10:9] = 2'b00; // Source Register
end
"C": begin
instruction[4:2] = C;
instruction[10:9] = 2'b00; // Source Register
end
"D": begin
instruction[4:2] = D;
instruction[10:9] = 2'b00; // Source Register
end
"E": begin
instruction[4:2] = E;
instruction[10:9] = 2'b00; // Source Register
end
"F": begin
instruction[4:2] = F;
instruction[10:9] = 2'b00; // Source Register
end
"G": begin
instruction[4:2] = G;
instruction[10:9] = 2'b00; // Source Register
end
"H": begin
instruction[4:2] = H;
instruction[10:9] = 2'b00; // Source Register
end
default : if(second_operand[0] == "#") begin
instruction[10:9] = 2'b10; // Source Immediate
instruction[4:2] = 0;
end
else begin
$display("%3d - Invalid second operand for all alu_operations, can be source register or source immediate",`__LINE__);
end
endcase
end
end
endtask
//-----------------------------------------------------------------------------------------------------------//
//-----------------------------------------------------------------------------------------------------------//
// Task to decode NOP & HALT operations instructions
task nop_halt();
begin
instruction[8] = 0;
instruction[1:0] = 2'b00;
case(opcode)
"NOP": begin
instruction[15:11] = `NOP;
instruction[10:9] = 0;
instruction[7:5] = 0;
instruction[4:2] = 0;
end
"HALT": begin
instruction[15:11] = `HALT;
instruction[10:9] = 0;
instruction[7:5] = 0;
instruction[4:2] = 0;
end
endcase
end
endtask
//-----------------------------------------------------------------------------------------------------------//
//-----------------------------------------------------------------------------------------------------------//
// Task to decode LOAD operation instructions
task load_operation();
begin
instruction[8] = 0;
instruction[1:0] = 2'b00;
instruction[15:11] = `LOAD;
case(first_operand)
"A": instruction[4:2] = A;
"B": instruction[4:2] = B;
"C": instruction[4:2] = C;
"D": instruction[4:2] = D;
"E": instruction[4:2] = E;
"F": instruction[4:2] = F;
"G": instruction[4:2] = G;
"H": instruction[4:2] = H;
default : begin
$display("%3d - Invalid first operand for LOAD operation, can be only source registers",`__LINE__);
end
endcase
case(second_operand)
"A": begin
instruction[7:5] = A;
instruction[10:9] = 2'b00; // Source Register
end
"B": begin
instruction[7:5] = B;
instruction[10:9] = 2'b00; // Source Register
end
"C": begin
instruction[7:5] = C;
instruction[10:9] = 2'b00; // Source Register
end
"D": begin
instruction[7:5] = D;
instruction[10:9] = 2'b00; // Source Register
end
"E": begin
instruction[7:5] = E;
instruction[10:9] = 2'b00; // Source Register
end
"F": begin
instruction[7:5] = F;
instruction[10:9] = 2'b00; // Source Register
end
"G": begin
instruction[7:5] = G;
instruction[10:9] = 2'b00; // Source Register
end
"H": begin
instruction[7:5] = H;
instruction[10:9] = 2'b00; // Source Register
end
default : if(second_operand[0] == "#") begin
instruction[10:9] = 2'b10; // Source Immediate
instruction[7:5] = 0;
end
else if(second_operand != "#") begin
instruction[10:9] = 2'b01; // Source Memory
instruction[7:5] = 0;
end
endcase
end
endtask
//-----------------------------------------------------------------------------------------------------------//
//-----------------------------------------------------------------------------------------------------------//
// Task to decode Store operation instructions
task store_operation();
begin
instruction[8] = 0;
instruction[1:0] = 2'b00;
instruction[15:11] = `STORE;
case(first_operand)
"A": begin
instruction[7:5] = A;
instruction[10:9] = 2'b00; // Source Register
end
"B": begin
instruction[7:5] = B;
instruction[10:9] = 2'b00; // Source Register
end
"C": begin
instruction[7:5] = C;
instruction[10:9] = 2'b00; // Source Register
end
"D": begin
instruction[7:5] = D;
instruction[10:9] = 2'b00; // Source Register
end
"E": begin
instruction[7:5] = E;
instruction[10:9] = 2'b00; // Source Register
end
"F": begin
instruction[7:5] = F;
instruction[10:9] = 2'b00; // Source Register
end
"G": begin
instruction[7:5] = G;
instruction[10:9] = 2'b00; // Source Register
end
"H": begin
instruction[7:5] = H;
instruction[10:9] = 2'b00; // Source Register
end
default : begin
$display("%3d - Invalid first operand for STORE operation, can be only source registers",`__LINE__);
end
endcase
if(second_operand != "#") begin
instruction[10:9] = 2'b01; // Source Memory
instruction[4:2] = 0;
end
else begin
$display("%3d - Invalid second operand for STORE operation, can be only source memory",`__LINE__);
end
end
endtask
//-----------------------------------------------------------------------------------------------------------//
//-----------------------------------------------------------------------------------------------------------//
// Task to decode MOVE operation instructions
task move_operation();
begin
instruction[8] = 0;
instruction[1:0] = 2'b00;
instruction[15:11] = `MOVE;
instruction[7:5] = 0;
instruction[4:2] = 0;
if(first_operand != "#") begin
instruction[10:9] = 2'b01; // Source Memory
end
else begin
$display("%3d - Invalid first operand for MOVE operation, can be only source memory",`__LINE__);
end
if(second_operand != "#") begin
instruction[10:9] = 2'b01; // Source Memory
end
else begin
$display("Invalid second operand for MOVE operation, can be only source memory",`__LINE__);
end
end
endtask
//-----------------------------------------------------------------------------------------------------------//
//-----------------------------------------------------------------------------------------------------------//
// Task to decode JUMP operation instructions
task jump_operation();
begin
instruction[8] = 0;
instruction[15:11] = `JUMP;
instruction[7:5] = 0;
instruction[4:2] = 0;
instruction[10:9] = 0;
case(opcode)
"JMP": instruction[1:0] = 2'b00; // Always Jump
"JC": instruction[1:0] = 2'b01; // Jump if carry
"JZ": instruction[1:0] = 2'b10; // Jump if Zero
"JS": instruction[1:0] = 2'b11; // Jump if Negitive
default: $display("%3d - Invalid opcode for JUMP operation",`__LINE__);
endcase
end
endtask
//-----------------------------------------------------------------------------------------------------------//
endmodule