📄 instdecode.v
字号:
//*************************************************************////// instdecode module // author: lin zheng yi ////*************************************************************//`timescale 1ns/100ps module instdecode(npc_in2,inst_in2,clock2,reg_add_in,reg_data_in,reset2, reg_write_en,irout2,aout2,bout2,imout2,npcout2); input [31:0] npc_in2,inst_in2,reg_data_in; input [4:0] reg_add_in; input reg_write_en; input clock2; input reset2; output [31:0] aout2,bout2,irout2,imout2,npcout2; reg [31:0] aout2,bout2,irout2,imout2,npcout2; reg [31:0] regs[31:1]; // R1~R31,32bit //wire [31:0] imm16_32_cpl,imm16_32_sg,imm16_32_ug; wire [25:0] offset; wire [14:0] imm16_15_cpl,imm16_15; wire [15:0] imm16_16_cpl,imm16; wire [5:0] opcode,func; wire [4:0] rs1,rd_rs2,imm16_5; //---------------------------------operation code------------------------------------- parameter LB =6'b000001, LBU =6'b000010, LH =6'b000011, LHU =6'b000100, LW =6'b000101, SB =6'b001000, SH =6'b001001, SW =6'b001010, ADDI =6'b010000, ADDUI =6'b010001, SUBI =6'b010010, SUBUI =6'b010011, ANDI =6'b010100, ORI =6'b010101, XORI =6'b010110, LHI =6'b000110, SLLI =6'b010111, SRLI =6'b011000, SRAI =6'b011001, SLTI =6'b011010, SGTI =6'b011011, SGEI =6'b011100, SEQI =6'b011101, SLEI =6'b011110, SNEI =6'b011111, BEQZ =6'b100000, BNEZ =6'b100001, JR =6'b100010, JALR =6'b100011, J =6'b100100, JAL =6'b100101, TRAP =6'b100110, RFE =6'b100111, NOP =6'b000000, R_TYPE =6'b110000;//---------------------------------function code------------------------------------- parameter ADD =6'b000001, ADDU =6'b000010, SUB =6'b000011, SUBU =6'b000100, AND_ =6'b000101, OR_ =6'b000110, XOR_ =6'b000111, SLL =6'b001000, SRL =6'b001001, SRA =6'b001010, SLT =6'b001011, SGT =6'b001100, SLE =6'b001101, SGE =6'b001110, SEQ =6'b001111, SNE =6'b010000; assign opcode[5:0] = inst_in2[31:26]; // opcode assign imm16[15:0] = inst_in2[15:0]; // immediate assign rs1[4:0] = inst_in2[25:21]; // if I and R type ,it's source reg assign rd_rs2[4:0] = inst_in2[20:16]; // if I type ,it's rd; if R type ,it's rs2 assign func[5:0] = inst_in2[5:0]; // if R type, it's func code, func code =11'b00000_xxxxxxx , here only get its last 6 bits assign offset[25:0] = inst_in2[25:0]; // if J type ,it's offset assign imm16_15_cpl[14:0] = (~imm16[14:0]+1'b1); assign imm16_16_cpl[15:0] = (~imm16[15:0]+1'b1); assign imm16_5[4:0] = imm16[4:0]; assign imm16_15[14:0] = imm16[14:0]; always@(posedge clock2 or negedge reset2) begin if(~reset2) begin aout2<= 0; bout2<= 0; irout2<= 0; imout2<= 0; npcout2<= 0; // regs[0]<= 0; regs[1]<= 0; regs[2]<= 0; regs[3]<= 0; regs[4]<= 0; regs[5]<= 0; regs[6]<= 0; regs[7]<= 0; regs[8]<= 0; regs[9]<= 0; regs[10]<= 0; regs[11]<= 0; regs[12]<= 0; regs[13]<= 0; regs[14]<= 0; regs[15]<= 0; regs[16]<= 0; regs[17]<= 0; regs[18]<= 0; regs[19]<= 0; regs[20]<= 0; regs[21]<= 0; regs[22]<= 0; regs[23]<= 0; regs[24]<= 0; regs[25]<= 0; regs[26]<= 0; regs[27]<= 0; regs[28]<= 0; regs[29]<= 0; regs[30]<= 0; regs[31]<= 0; end else begin irout2 <= inst_in2; // IR2 -> IR3 npcout2 <= npc_in2; // NPC2 -> NPC3 if((|reg_add_in)&(reg_write_en)) // reg_add_in!=0 regs[reg_add_in] <= reg_data_in; // write back the data //-----------------------------------------LB,LH,LW,ADDI,SLTI,SGEI,SEQI,SLEI,SNEI----------------------------------- if((opcode==LB)|(opcode==LH)|(opcode==LW)|(opcode==ADDI)|((opcode>=SLTI)&(opcode<=SNEI))) begin if(~imm16[15]) /// imm16>0 begin imout2 <= {16'b0000_0000_0000_0000,imm16}; // 16 bit immediate extend to 32 bits if(~regs[rs1][31]) // regs[rs1]>0 aout2 <= regs[rs1]; // regs[rs1]->A else // regs[rs1]<0 aout2 <= {1'b1,~regs[rs1][30:0]+1}; // complemental code end else begin // imm16<0 imout2 <= {1'b1,(~{16'b0000_0000_0000_0000,imm16_15}+1)}; // complemental code,and extend to 32 bits if(~regs[rs1][31]) aout2 <= regs[rs1]; // imm16>0,regs[rs1]>0,regs[rs1]->A else // regs[rs1]<0 aout2 <= ~regs[rs1]+1; // complemental code end end //------------------------------------------LBU,LHU----------------------------------------------------------------- else if((opcode==LBU)|(opcode==LHU)) begin aout2 <= regs[rs1]; // they are unsigned,regs[rs1]->A imout2 <= {16'b0000_0000_0000_0000,imm16}; // 16 bit immediate extend to 32 bits end//-------------------------------------------------SUBI------------------------------------------------------------- else if(opcode==SUBI) begin
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -