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
| module cpu( input clk,rst, input [31:0] io_din, input [7:0] m_rf_addr , output [7:0] io_addr , output [31:0] io_dout , output io_we , output [31:0] rf_data, output [31:0] m_data , output reg [31:0] pc );
wire Zero; wire Branch,MemRead,MemWrite,ALUSrc,RegWrite,JUMP,jalr_PC,auipc; wire MemWrite_true; wire [1:0] ALUOp ,MemtoReg; wire [31:0] ins, imm; wire [31:0] readData1,readData2,ALU_input2,ALU_input1; wire [31:0] Mem_ReadData, RF_writeData; wire [31:0] Mem_ReadData_waishe, Mem_ReadData_MEM; wire [31:0] nPC_4,PC_offset,PC_jalr,PC_mux; wire [31:0] nPC; wire which_PC; wire [31:0] ALU_result; reg [31:0] writeData; wire [2:0] sel; wire [31:0] io_addr1; wire [7:0] apc;
ins_memory IMem(.a(apc),.spo(ins)); data_memory DMem(.a(ALU_result[7:0]),.d(readData2),.dpra(m_rf_addr),.clk(clk),.we(MemWrite_true),.spo(Mem_ReadData_MEM),.dpo(m_data));
control control(.ins(ins), .ALUSrc(ALUSrc),.RegWrite(RegWrite),.MemRead(MemRead), .MemWrite(MemWrite),.Branch(Branch),.JUMP(JUMP),.jalr_PC(jalr_PC),.auipc(auipc), .ALUOp(ALUOp), .MemtoReg(MemtoReg) );
rf #(.m(5),.WIDTH(32)) rf (.clk(clk),.we(RegWrite), .wa(ins[11:7]),.ra0(ins[19:15]),.ra1(ins[24:20]), .wd(writeData),.rd0(readData1),.rd1(readData2), .rf_addr(m_rf_addr[4:0]), .rf_data(rf_data),.rst(rst)); immg immg(.ins(ins),.imm(imm));
alu_control alu_control(.aluop(ALUOp),.sel(sel)); alu # (32) alu(.a(ALU_input1),.b(ALU_input2),.ins(ins),.f(sel),.z(Zero),.y(ALU_result));
assign PC_jalr = ALU_result & (~32'b1); assign nPC_4 = pc + 4; assign PC_offset = pc + imm; assign which_PC = (Branch & Zero) | JUMP ; assign PC_mux = which_PC ? PC_offset : nPC_4; assign nPC = jalr_PC ? PC_jalr : PC_mux; assign ALU_input2 = ALUSrc ? imm : readData2; assign ALU_input1 = auipc ? pc : readData1; assign apc = pc[9:2];
always@(posedge clk or posedge rst) begin if(rst) pc <= 32'h0000_3000; else pc <= nPC; end
always@(*) begin if((JUMP==0)||(jalr_PC==0)) begin if(MemtoReg==0) writeData = ALU_result; else writeData = Mem_ReadData; end else writeData = nPC_4 ; end
assign io_addr1=ALU_result; assign io_addr = io_addr1[7:0]; assign MemWrite_true = (~io_addr1[10]) & MemWrite; assign Mem_ReadData_waishe = io_din; assign Mem_ReadData = io_addr1[10]? Mem_ReadData_waishe : Mem_ReadData_MEM; assign io_dout = readData2; assign io_we = io_addr1[10] & MemWrite;
endmodule
|