| module MCRP(input clk,reset,
|
| output reg [1:0] pc_src_out,
|
| output reg ext_op_out,
|
| output reg [2:0] alu_op_out,
|
| output reg [1:0] alu_src_out,
|
| output reg mem_write_out,
|
| output reg mem_read_out,
|
| output reg reg_write_out,
|
| output reg wb_src_out,
|
| output reg stack_write_out,
|
| output reg stack_read_out,
|
| output reg zero_signal_out,
|
| output reg [31:0] pc_out,
|
| output reg [31:0] ir_out,
|
| output reg [31:0] a_out,
|
| output reg [31:0] b_out,
|
| output reg [31:0] alu_result,
|
| output reg [31:0] mem_result,
|
| output reg [31:0] wb_result,
|
| output reg reg_src_out,
|
| );
|
|
|
|
|
|
|
| parameter AND = 5'b00000;
|
| parameter ADD = 5'b00001;
|
| parameter SUB = 5'b00010;
|
| parameter CMP = 5'b00011;
|
|
|
| parameter ANDI = 5'b00000;
|
| parameter ADDI = 5'b00001;
|
| parameter LW = 5'b00010;
|
| parameter SW = 5'b00011;
|
| parameter BEQ = 5'b00100;
|
|
|
| parameter J = 5'b00000;
|
| parameter JAL = 5'b00001;
|
|
|
| parameter SLL = 5'b00000;
|
| parameter SLR = 5'b00001;
|
| parameter SLLV = 5'b00010;
|
| parameter SLRV = 5'b00011;
|
|
|
|
|
| parameter ADD_OP = 3'b000;
|
| parameter SUB_OP = 3'b001;
|
| parameter AND_OP = 3'b010;
|
| parameter SLL_OP = 3'b011;
|
| parameter SLR_OP = 3'b100;
|
|
|
|
|
| parameter IF = 3'b000;
|
| parameter ID = 3'b001;
|
| parameter EX = 3'b010;
|
| parameter MEM = 3'b011;
|
| parameter WB = 3'b100;
|
|
|
|
|
| reg [2:0] state;
|
| reg [2:0] next_state;
|
|
|
| reg [31:0] sp;
|
| reg [31:0] next_pc;
|
| reg [31:0] pc;
|
| wire [31:0] ir;
|
| wire [31:0] a;
|
| wire [31:0] b;
|
| wire [31:0] alu_out;
|
| wire [31:0] mem_out;
|
| wire [31:0] RA;
|
| reg [31:0] BTA;
|
| reg [31:0] JA;
|
| wire [31:0] extended_immediate;
|
| wire [31:0] extended_offset;
|
| wire [31:0] SA_extended;
|
| reg [31:0] alu_second_operand;
|
| reg [31:0] wb_data;
|
| reg [31:0] memory_address;
|
|
|
| reg [4:0] opcode;
|
| reg [4:0] rs1;
|
| reg [4:0] rs2;
|
| reg [4:0] rd;
|
| reg [4:0] shamt;
|
| reg [13:0] immediate;
|
| reg [23:0] offset;
|
| reg [1:0] instruction_type;
|
| reg stop_bit;
|
|
|
|
|
| wire [1:0] pc_src;
|
| wire reg_b;
|
| wire reg_write;
|
| wire ext_op;
|
| wire [1:0] alu_src;
|
| wire [2:0] alu_op;
|
| wire mem_read;
|
| wire mem_write;
|
| wire wb_src;
|
| wire stack_write;
|
| wire stack_read;
|
| reg zero_signal;
|
| wire next_sp = 0;
|
|
|
|
|
| instruction_memory im(pc,ir);
|
| register_file rf(clk, rs1, rs2, rd, wb_data, reg_write, a, b);
|
| alu alu(a, alu_second_operand, alu_op, alu_out);
|
| memory mem(clk, memory_address, b, mem_write, mem_read, mem_out);
|
| stack_memory stack(sp, stack_read, pc, stack_write, RA);
|
| immediate_extender ext(immediate, ext_op, extended_immediate);
|
| shift_extender shift(shamt, SA_extended);
|
| offset_extender offset_ext(offset, extended_offset);
|
| pc_control pc_ctrl(instruction_type, opcode, zero_signal, stop_bit, pc_src);
|
| control_unit ctrl(instruction_type, opcode, reg_b, reg_write, ext_op, alu_src, alu_op, mem_read, mem_write, wb_src);
|
| stack_control stack_ctrl(instruction_type, opcode, stop_bit, sp, stack_read, stack_write, next_sp);
|
| assign sp = next_sp;
|
|
|
| always @(posedge clk or posedge reset) begin
|
| if(reset) begin
|
| next_state = IF;
|
|
|
|
|
| pc = 32'h00000000;
|
| next_pc = 32'h00000000;
|
| alu_second_operand = 32'h00000000;
|
| wb_data = 32'h00000000;
|
| memory_address = 32'h00000000;
|
| zero_signal = 1'b0;
|
|
|
|
|
| opcode = 5'h00;
|
| rs1 = 5'h00;
|
| rs2 = 5'h00;
|
| rd = 5'h00;
|
| shamt = 5'h00;
|
| immediate = 14'h0000;
|
| offset = 24'h000000;
|
| instruction_type = 2'b00;
|
| stop_bit = 1'b0;
|
| #10;
|
| end
|
| else begin
|
| state = next_state;
|
| case(state)
|
| IF: begin
|
| next_pc <= pc + 32'h00000001;
|
| if (pc_src == 2'b00) begin
|
| pc = next_pc;
|
| end
|
| else if (pc_src == 2'b01) begin
|
| pc = BTA;
|
| next_pc <= BTA + 32'b00000001;
|
| end
|
| else if (pc_src == 2'b10) begin
|
| pc = JA;
|
| next_pc <= JA + 32'b00000001;
|
| end
|
| else if (pc_src == 2'b11) begin
|
| pc = RA;
|
| next_pc <= RA + 32'b00000001;
|
| end
|
| #30;
|
| pc_out = pc;
|
| pc_src_out = pc_src;
|
|
|
| end
|
| ID: begin
|
| # 50;
|
|
|
| opcode = ir[31:27];
|
| instruction_type = ir[2:1];
|
| rs1 = ir[26:22];
|
| rd = ir[21:17];
|
| rs2 = ir[16:12];
|
| shamt = ir[11:7];
|
| immediate = ir[16:3];
|
| offset = ir[26:3];
|
| stop_bit = ir[0];
|
| #30;
|
|
|
| JA = pc + extended_offset;
|
| BTA = next_pc + extended_immediate;
|
| #30;
|
| ir_out = ir;
|
| a_out = a;
|
| b_out = b;
|
| ext_op_out = ext_op;
|
| reg_write_out = reg_write;
|
| reg_src_out = reg_b;
|
| alu_op_out = alu_op;
|
| alu_src_out = alu_src;
|
| mem_write_out = mem_write;
|
| mem_read_out = mem_read;
|
| wb_src_out = wb_src;
|
| stack_write_out = stack_write;
|
| stack_read_out = stack_read;
|
| #20;
|
| end
|
| EX: begin
|
| #30;
|
|
|
| if (alu_src == 2'b00) begin
|
| alu_second_operand = extended_immediate;
|
| end
|
| else if (alu_src == 2'b01) begin
|
| alu_second_operand = b;
|
| end
|
| else if (alu_src == 2'b10) begin
|
| alu_second_operand = SA_extended;
|
| end
|
| #30;
|
|
|
| if(instruction_type == 2'b00)begin
|
| if(opcode == CMP) begin
|
| if (alu_out < 0) begin
|
| zero_signal = 1'b1;
|
| end
|
| else begin
|
| zero_signal = 1'b0;
|
| end
|
| end
|
| end
|
| else if(instruction_type == 2'b01)begin
|
| if(opcode == BEQ) begin
|
| if (alu_out == 0) begin
|
| zero_signal = 1'b1;
|
| end
|
| else begin
|
| zero_signal = 1'b0;
|
| end
|
| end
|
| end
|
| alu_result = alu_out;
|
| zero_signal_out = zero_signal;
|
| end
|
| MEM: begin
|
| memory_address = alu_out;
|
| #150;
|
| mem_result = mem_out;
|
| end
|
| WB: begin
|
| if (wb_src == 1'b0) begin
|
| wb_data = alu_out;
|
| end
|
| else if (wb_src == 1'b1) begin
|
| wb_data = mem_out;
|
| end
|
| #20;
|
| wb_result = wb_data;
|
| end
|
| endcase
|
|
|
| case(state)
|
| IF: begin
|
| next_state = ID;
|
| end
|
| ID: begin
|
| if (instruction_type == 2'b10) begin
|
| next_state = IF;
|
| end
|
| else begin
|
| next_state = EX;
|
| end
|
| end
|
| EX: begin
|
| if (instruction_type == 2'b00) begin
|
| if (opcode == CMP) begin
|
| next_state = IF;
|
| end
|
| else begin
|
| next_state = WB;
|
| end
|
| end
|
| else if(instruction_type == 2'b01) begin
|
| if (opcode == LW || opcode == SW) begin
|
| next_state = MEM;
|
| end
|
| else begin
|
| next_state = WB;
|
| end
|
| end
|
| else if(instruction_type == 2'b11) begin
|
| next_state = WB;
|
| end
|
| end
|
| MEM: begin
|
| if (opcode == LW) begin
|
| next_state = WB;
|
| end
|
| else begin
|
| next_state = IF;
|
| end
|
| end
|
| WB: begin
|
| next_state = IF;
|
| end
|
| endcase
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| end
|
| end
|
| endmodule
|
|
|