Update verilog.txt
Browse files- verilog.txt +178 -1
verilog.txt
CHANGED
|
@@ -535,4 +535,181 @@ Line 61: end - Ends the write operation block.
|
|
| 535 |
Line 62: end - Ends the else block (normal operation).
|
| 536 |
Line 63: end - Ends the synchronous always block.
|
| 537 |
Line 64: - Blank line for visual separation.
|
| 538 |
-
Line 65: endmodule - Ends the Register File module definition.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
Line 62: end - Ends the else block (normal operation).
|
| 536 |
Line 63: end - Ends the synchronous always block.
|
| 537 |
Line 64: - Blank line for visual separation.
|
| 538 |
+
Line 65: endmodule - Ends the Register File module definition.
|
| 539 |
+
|
| 540 |
+
[Processor Top Module]
|
| 541 |
+
Line 1: `timescale 1ns / 1ps - Sets simulation time unit to 1 nanosecond and precision to 1 picosecond.
|
| 542 |
+
Line 2: - Blank line for visual separation.
|
| 543 |
+
Line 3: module Processor( - Defines the start of the main Processor module.
|
| 544 |
+
Line 4: input clk, - Declares the global clock input for synchronous operations.
|
| 545 |
+
Line 5: input rst - Declares the global reset input to initialize the processor.
|
| 546 |
+
Line 6: ); - Closes the module port declaration list.
|
| 547 |
+
Line 7: reg [7:0] pc; - Declares an 8-bit register for the Program Counter.
|
| 548 |
+
Line 8: // The program counter register - Comment explaining the PC register.
|
| 549 |
+
Line 9: reg [3:0] status_reg; - Declares a 4-bit register to hold processor status flags.
|
| 550 |
+
Line 10: // Register holding all the flags - Comment explaining status register purpose.
|
| 551 |
+
Line 11: reg [7:0] next_pc; - Declares an 8-bit register for the next PC value.
|
| 552 |
+
Line 12: // Value of next address to go to - Comment explaining next PC calculation.
|
| 553 |
+
Line 13: reg [63:0] hi_reg , lo_reg; - Declares two 64-bit registers for multiplication results.
|
| 554 |
+
Line 14: // Our special purpose registers - Comment explaining HI/LO registers.
|
| 555 |
+
Line 15: wire [19:0] instruction_wire; - Declares a 20-bit wire for instruction transmission.
|
| 556 |
+
Line 16: //Wire carrying the actual 20 bit instruction - Comment explaining instruction wire.
|
| 557 |
+
Line 17: reg signed [63:0] alu_input_b; - Declares a signed 64-bit register for ALU's second input.
|
| 558 |
+
Line 18: reg [63:0] write_back_data; - Declares a 64-bit register for register write-back data.
|
| 559 |
+
Line 19: // Data going to be saved in register coming from either alu or memory ( selected through mux) - Comment explaining write-back data source.
|
| 560 |
+
Line 20: // Data path wires - Comment section header for data path wires.
|
| 561 |
+
Line 21: wire [63:0] read_data1_reg; - Declares a 64-bit wire for first register read data.
|
| 562 |
+
Line 22: // Holds input 'a' going to alu - Comment explaining ALU input A.
|
| 563 |
+
Line 23: wire [63:0] read_data2_reg; - Declares a 64-bit wire for second register read data.
|
| 564 |
+
Line 24: // Holds input 'b' goes to mux - Comment explaining ALU input B source.
|
| 565 |
+
Line 25: wire [63:0] alu_result; - Declares a 64-bit wire for ALU computation result.
|
| 566 |
+
Line 26: // Holds the result of the alu after calculation - Comment explaining ALU result.
|
| 567 |
+
Line 27: wire [63:0] mem_read_data; - Declares a 64-bit wire for memory read data.
|
| 568 |
+
Line 28: // Data from memory going to register or alu through mux ( load word or load immediate ) - Comment explaining memory data path.
|
| 569 |
+
Line 29: wire [63:0] alu_hi_out , alu_lo_out; - Declares two 64-bit wires for ALU's HI/LO outputs.
|
| 570 |
+
Line 30: // wires holding hi and lo - Comment explaining HI/LO wires.
|
| 571 |
+
Line 31: - Blank line for visual separation.
|
| 572 |
+
Line 32: // Control signal wires - Comment section header for control signals.
|
| 573 |
+
Line 33: wire partial_write_signal; - Declares wire for partial write control signal.
|
| 574 |
+
Line 34: // control signal for mlw - Comment explaining modified load word signal.
|
| 575 |
+
Line 35: wire jump , branch , zero_flag , neg_flag , carry_flag , overflow_flag; - Declares multiple control and flag wires.
|
| 576 |
+
Line 36: wire reg_write_en , mem_write_en; - Declares wires for register and memory write enables.
|
| 577 |
+
Line 37: // enabelers to write to memory(store word) and registers - Comment explaining write enables (typo: "enablers").
|
| 578 |
+
Line 38: wire vector_mode; - Declares wire for vector mode control signal.
|
| 579 |
+
Line 39: // control signal for our custom vector load instruction - Comment explaining vector mode.
|
| 580 |
+
Line 40: wire alu_mux; - Declares wire for ALU input multiplexer select.
|
| 581 |
+
Line 41: // select line for mux before alu - Comment explaining ALU mux purpose.
|
| 582 |
+
Line 42: wire mem_to_reg; - Declares wire for memory-to-register multiplexer select.
|
| 583 |
+
Line 43: // select line choosing btw alu result or memory data to be stored in register file - Comment explaining write-back mux.
|
| 584 |
+
Line 44: // Splitter splitiing the instruction bits into respective wires according to the instruction type - Comment about instruction decoding (typo: "splitting").
|
| 585 |
+
Line 45: wire [4:0] opcode = instruction_wire[4:0]; - Extracts 5-bit opcode from instruction.
|
| 586 |
+
Line 46: wire [2:0] rd = instruction_wire[7:5]; - Extracts 3-bit destination register field.
|
| 587 |
+
Line 47: wire [2:0] rs = instruction_wire[10:8]; - Extracts 3-bit source register 1 field.
|
| 588 |
+
Line 48: wire [2:0] rt = instruction_wire[13:11]; - Extracts 3-bit source register 2 field.
|
| 589 |
+
Line 49: wire [8:0] branch_offset = instruction_wire[19:11]; - Extracts 9-bit branch offset field.
|
| 590 |
+
Line 50: wire [13:0] jump_const = instruction_wire[17:5]; - Extracts 14-bit jump constant field.
|
| 591 |
+
Line 51: wire [1:0] i_mod_lane = instruction_wire[19:18]; - Extracts 2-bit lane selector field.
|
| 592 |
+
Line 52: //lane selector - Comment explaining lane selection field.
|
| 593 |
+
Line 53: wire [5:0] shift_amount = instruction_wire[19:14]; - Extracts 6-bit shift amount field.
|
| 594 |
+
Line 54: // sign extension - Comment section header for sign extension.
|
| 595 |
+
Line 55: wire signed [8:0] signed_const = instruction_wire[19:11]; - Sign-extends I-type constant to 9 bits.
|
| 596 |
+
Line 56: // signed constant for simple i type - Comment explaining I-type constant.
|
| 597 |
+
Line 57: wire signed [6:0] i_mod_const = instruction_wire[17:11]; - Sign-extends modified I-type constant to 7 bits.
|
| 598 |
+
Line 58: // signed constant for mod i type - Comment explaining modified I-type constant.
|
| 599 |
+
Line 59: reg [2:0]reg2_address_in; - Declares 3-bit register for second register address input.
|
| 600 |
+
Line 60: // sequential logic to control clock - Comment explaining clocked sequential logic.
|
| 601 |
+
Line 61: always @ (posedge clk) - Begins synchronous always block on positive clock edge.
|
| 602 |
+
Line 62: begin - Starts the synchronous always block.
|
| 603 |
+
Line 63: if (rst == 1) begin - Conditional check for reset signal.
|
| 604 |
+
Line 64: pc <= 8'd0; - Resets Program Counter to zero.
|
| 605 |
+
Line 65: status_reg <= 4'd0; - Resets status register flags to zero.
|
| 606 |
+
Line 66: end - Ends the reset block.
|
| 607 |
+
Line 67: else - Else clause for normal operation.
|
| 608 |
+
Line 68: begin - Starts normal operation block.
|
| 609 |
+
Line 69: pc <= next_pc; - Updates PC with next PC value.
|
| 610 |
+
Line 70: status_reg <= {carry_flag , overflow_flag , zero_flag , neg_flag}; - Updates status register with ALU flags.
|
| 611 |
+
Line 71: if (opcode == 5'b01101 || opcode == 5'b10000) begin - Conditional check for multiplication instructions.
|
| 612 |
+
Line 72: hi_reg <= alu_hi_out; - Updates HI register with ALU HI output.
|
| 613 |
+
Line 73: lo_reg <= alu_lo_out; - Updates LO register with ALU LO output.
|
| 614 |
+
Line 74: end - Ends multiplication update block.
|
| 615 |
+
Line 75: end - Ends normal operation block.
|
| 616 |
+
Line 76: end - Ends the synchronous always block.
|
| 617 |
+
Line 77: // PC logic - Comment section header for PC calculation.
|
| 618 |
+
Line 78: always @ (*) - Begins combinatorial always block for PC logic.
|
| 619 |
+
Line 79: begin - Starts the PC logic block.
|
| 620 |
+
Line 80: if (jump == 1) - Conditional check for jump instruction.
|
| 621 |
+
Line 81: next_pc = pc + jump_const; - Calculates next PC for jump (PC + jump constant).
|
| 622 |
+
Line 82: else if ((branch == 1) && (zero_flag == 1)) - Conditional check for taken branch.
|
| 623 |
+
Line 83: next_pc = pc + branch_offset; - Calculates next PC for branch (PC + branch offset).
|
| 624 |
+
Line 84: else - Else clause for normal sequential execution.
|
| 625 |
+
Line 85: next_pc = pc + 1; - Calculates next PC for normal flow (PC + 1).
|
| 626 |
+
Line 86: end - Ends the PC logic block.
|
| 627 |
+
Line 87: // Mux before alu to select btw data from reg or mem - Comment explaining ALU input multiplexer.
|
| 628 |
+
Line 88: always @ (*) - Begins combinatorial always block for ALU input selection.
|
| 629 |
+
Line 89: if (alu_mux == 0) - Conditional check for register source.
|
| 630 |
+
Line 90: if (opcode==5'b01010) - Nested conditional for BEQZ instruction.
|
| 631 |
+
Line 91: begin - Starts BEQZ handling block.
|
| 632 |
+
Line 92: alu_input_b = 64'd0; - Sets ALU input B to zero for BEQZ comparison.
|
| 633 |
+
Line 93: end - Ends BEQZ handling block.
|
| 634 |
+
Line 94: else - Else clause for other register-source instructions.
|
| 635 |
+
Line 95: begin - Starts normal register source block.
|
| 636 |
+
Line 96: alu_input_b = read_data2_reg ; - Sets ALU input B to register read data 2.
|
| 637 |
+
Line 97: end - Ends normal register source block.
|
| 638 |
+
Line 98: else - Else clause for immediate/constant source.
|
| 639 |
+
Line 99: begin - Starts immediate source block.
|
| 640 |
+
Line 100: // when i mod instruction or mlw - Comment about modified instructions.
|
| 641 |
+
Line 101: if (opcode == 5'b10011) - Conditional check for modified load word.
|
| 642 |
+
Line 102: alu_input_b = i_mod_const; - Sets ALU input B to modified I-type constant.
|
| 643 |
+
Line 103: // if normal i type instruction - Comment about normal I-type.
|
| 644 |
+
Line 104: else - Else clause for normal I-type.
|
| 645 |
+
Line 105: alu_input_b = signed_const; - Sets ALU input B to sign-extended I-type constant.
|
| 646 |
+
Line 106: end - Ends immediate source block.
|
| 647 |
+
Line 107: // Mux to decide where is data coming from to be stored in reg - Comment explaining write-back multiplexer.
|
| 648 |
+
Line 108: always @ (*) - Begins combinatorial always block for write-back selection.
|
| 649 |
+
Line 109: // coming from alu - Comment for ALU source.
|
| 650 |
+
Line 110: if (mem_to_reg == 0) - Conditional check for ALU source.
|
| 651 |
+
Line 111: begin - Starts ALU source block.
|
| 652 |
+
Line 112: write_back_data = alu_result ; - Selects ALU result for write-back.
|
| 653 |
+
Line 113: end - Ends ALU source block.
|
| 654 |
+
Line 114: // coming from memory ( zero extended because data coming from mem is 16 bit and reg file is 64 bit ) - Comment for memory source.
|
| 655 |
+
Line 115: else - Else clause for memory source.
|
| 656 |
+
Line 116: begin - Starts memory source block.
|
| 657 |
+
Line 117: write_back_data = mem_read_data ; - Selects memory read data for write-back.
|
| 658 |
+
Line 118: end - Ends memory source block.
|
| 659 |
+
Line 119: always @(*) begin - Begins combinatorial always block for register address handling.
|
| 660 |
+
Line 120: // If the instruction is store word (sw), use 'rd' - Comment explaining SW register addressing.
|
| 661 |
+
Line 121: if (opcode == 5'b01000) begin - Conditional check for Store Word instruction.
|
| 662 |
+
Line 122: reg2_address_in = rd; - Uses destination register as source for SW.
|
| 663 |
+
Line 123: end - Ends SW handling block.
|
| 664 |
+
Line 124: // Otherwise, use the standard 'rt' - Comment for standard register addressing.
|
| 665 |
+
Line 125: else begin - Else clause for other instructions.
|
| 666 |
+
Line 126: reg2_address_in = rt; - Uses standard RT field for register addressing.
|
| 667 |
+
Line 127: end - Ends standard addressing block.
|
| 668 |
+
Line 128: end - Ends the register address handling block.
|
| 669 |
+
Line 129: - Blank line for visual separation.
|
| 670 |
+
Line 130: instruction_memory inst_mem ( .PC(pc) , .instruction(instruction_wire) ); - Instantiates Instruction Memory module.
|
| 671 |
+
Line 131: control_unit cntrl_un ( .opcode_instruction(opcode) - Instantiates Control Unit module with opcode.
|
| 672 |
+
Line 132: , .mem_write_sel(mem_write_en) - Connects memory write select signal.
|
| 673 |
+
Line 133: , .reg_write_en(reg_write_en) - Connects register write enable signal.
|
| 674 |
+
Line 134: , .alu_mux(alu_mux) - Connects ALU multiplexer select signal.
|
| 675 |
+
Line 135: , .mem_to_reg(mem_to_reg) - Connects memory-to-register select signal.
|
| 676 |
+
Line 136: , .branch(branch) - Connects branch control signal.
|
| 677 |
+
Line 137: , .jump(jump) - Connects jump control signal.
|
| 678 |
+
Line 138: ,.vector_mode(vector_mode) - Connects vector mode signal.
|
| 679 |
+
Line 139: ,.partial_write(partial_write_signal) - Connects partial write signal.
|
| 680 |
+
Line 140: ); - Ends Control Unit instantiation.
|
| 681 |
+
Line 141: - Blank line for visual separation.
|
| 682 |
+
Line 142: register_file reg_file ( .clk(clk), - Instantiates Register File module with clock.
|
| 683 |
+
Line 143: .rst(rst), - Connects reset signal.
|
| 684 |
+
Line 144: .write_en(reg_write_en), - Connects register write enable.
|
| 685 |
+
Line 145: .write_data(write_back_data), - Connects write-back data.
|
| 686 |
+
Line 146: .read_reg1(rs), - Connects first read register address.
|
| 687 |
+
Line 147: .read_reg2(reg2_address_in), - Connects second read register address.
|
| 688 |
+
Line 148: .write_reg(rd), - Connects write register address.
|
| 689 |
+
Line 149: .read_data1(read_data1_reg), - Connects first read data output.
|
| 690 |
+
Line 150: .read_data2(read_data2_reg) - Connects second read data output.
|
| 691 |
+
Line 151: ,.lane_select(i_mod_lane) - Connects lane selection input.
|
| 692 |
+
Line 152: ,.partial_write_en(partial_write_signal) - Connects partial write enable.
|
| 693 |
+
Line 153: ); - Ends Register File instantiation.
|
| 694 |
+
Line 154: alu alu_uut ( .a(read_data1_reg), - Instantiates ALU module with input A.
|
| 695 |
+
Line 155: .b(alu_input_b), - Connects ALU input B.
|
| 696 |
+
Line 156: .alu_control(opcode), - Connects ALU control (opcode).
|
| 697 |
+
Line 157: .hi_in(hi_reg), - Connects HI register input.
|
| 698 |
+
Line 158: .lo_in(lo_reg), - Connects LO register input.
|
| 699 |
+
Line 159: .shamt(shift_amount[4:0]), - Connects shift amount.
|
| 700 |
+
Line 160: .result(alu_result), - Connects ALU result output.
|
| 701 |
+
Line 161: .hi_out(alu_hi_out), - Connects ALU HI output.
|
| 702 |
+
Line 162: .lo_out(alu_lo_out), - Connects ALU LO output.
|
| 703 |
+
Line 163: .zero(zero_flag), - Connects zero flag output.
|
| 704 |
+
Line 164: .neg(neg_flag), - Connects negative flag output.
|
| 705 |
+
Line 165: .carry(carry_flag), - Connects carry flag output.
|
| 706 |
+
Line 166: .overflow(overflow_flag) - Connects overflow flag output.
|
| 707 |
+
Line 167: ); - Ends ALU instantiation.
|
| 708 |
+
Line 168: data_memory data_mem ( .clk(clk), - Instantiates Data Memory module with clock.
|
| 709 |
+
Line 169: .mem_write_en(mem_write_en), - Connects memory write enable.
|
| 710 |
+
Line 170: .mem_address(alu_result[15:0]), - Connects memory address from ALU result.
|
| 711 |
+
Line 171: .write_data(read_data2_reg[15:0]), - Connects write data from register.
|
| 712 |
+
Line 172: .read_data(mem_read_data), - Connects memory read data output.
|
| 713 |
+
Line 173: .vector_mode(vector_mode) - Connects vector mode signal.
|
| 714 |
+
Line 174: ); - Ends Data Memory instantiation.
|
| 715 |
+
Line 175: endmodule - Ends the Processor module definition.
|