模拟RISC v过程

如何模拟RISC v

    • 了解RV32I基础整数指令集
    • 了解不同类型指令解码过程
    • MIPS五级流水
    • 分支预测

了解RV32I基础整数指令集

模拟RISC v过程_第1张图片

了解不同类型指令解码过程

  1. Load和store指令
  2. 算术指令
  3. 逻辑指令
  4. 移位指令
  5. 比较指令
  6. 控制指令,包括无条件跳转指令和条件跳转指令
  7. 同步指令

解码过程如下

int opt = memory[reg_pc] & 127;
        switch (opt)
        {
     
        case 55: //LUI
            inst = LUI;
            rd = (sum >> 7) & 0x1f;
            immediate = sum >> 12 << 12;
            break;

        case 23: //AUIPC
            inst = AUIPC;
            rd = (sum >> 7) & 0x1f;
            immediate = sum >> 12 << 12;
            jump_flag = true;
            break;

        case 111: //JAL
            inst = JAL;
            rd = (sum >> 7) & 0x1f;
            immediate = J_offset(sum);
            jump_flag = true;
            break;

        case 103: //JALR
            inst = JALR;
            rd = (sum >> 7) & 0x1f;
            rs1 = (sum >> 15) & 0x1f;
            immediate = I_offset(sum);
            jump_flag = true;
            break;

        case 99: //BEQ……
            rs1 = (sum >> 15) & 0x1f;
            rs2 = (sum >> 20) & 0x1f;
            jump_flag = pred.get_predict(reg_pc);
            immediate = B_offset(sum);
            {
     
                int function = (sum >> 12) & 7;
                switch (function)
                {
     
                case 0:
                    inst = BEQ;
                    break;

                case 1:
                    inst = BNE;
                    break;

                case 4:
                    inst = BLT;
                    break;

                case 5:
                    inst = BGE;
                    break;

                case 6:
                    inst = BLTU;
                    break;

                case 7:
                    inst = BGEU;
                    break;

                default:
                    break;
                }
            }
            break;

        case 3: //LB……
            rd = (sum >> 7) & 0x1f;
            rs1 = (sum >> 15) & 0x1f;
            Three_cycle = true;
            immediate = I_offset(sum);
            {
     
                int function = (sum >> 12) & 7;
                switch (function)
                {
     
                case 0:
                    inst = LB;
                    break;

                case 1:
                    inst = LH;
                    break;

                case 2:
                    inst = LW;
                    break;

                case 4:
                    inst = LBU;
                    break;

                case 5:
                    inst = LHU;
                    break;
                }
            }
            break;

        case 35: //SB……
            rs1 = (sum >> 15) & 0x1f;
            rs2 = (sum >> 20) & 0x1f;
            Three_cycle = true;
            immediate = S_offset(sum);
            {
     
                int function = (sum >> 12) & 7;
                switch (function)
                {
     
                case 0:
                    inst = SB;
                    break;

                case 1:
                    inst = SH;
                    break;

                case 2:
                    inst = SW;
                    break;

                default:
                    break;
                }
            }
            break;

        case 19: //ADDI……
            rd = (sum >> 7) & 0x1f;
            rs1 = (sum >> 15) & 0x1f;
            immediate = I_offset(sum);
            {
     
                int function = (sum >> 12) & 7;
                switch (function)
                {
     
                case 0:
                    inst = ADDI;
                    break;

                case 2:
                    inst = SLTI;
                    break;

                case 3:
                    inst = SLTIU;
                    break;

                case 4:
                    inst = XORI;
                    break;

                case 6:
                    inst = ORI;
                    break;

                case 7:
                    inst = ANDI;
                    break;

                case 1:
                    inst = SLLI;
                    shamt = (sum >> 20) & 63;
                    break;

                case 5:
                    shamt = (sum >> 20) & 63;
                    if ((sum >> 30) & 1)
                    {
     
                        inst = SRAI;
                    }
                    else
                    {
     
                        inst = SRLI;
                    }
                    break;
                }
            }
            break;

        case 51: //ADD……
            rd = (sum >> 7) & 0x1f;
            rs1 = (sum >> 15) & 0x1f;
            rs2 = (sum >> 20) & 0x1f;
            {
     
                int function = (sum >> 12) & 7;
                switch (function)
                {
     
                case 0:
                    if ((sum >> 30) & 1)
                    {
     
                        inst = SUB;
                    }
                    else
                    {
     
                        inst = ADD;
                    }
                    break;

                case 1:
                    inst = SLL;
                    break;

                case 2:
                    inst = SLT;
                    break;

                case 3:
                    inst = SLTU;
                    break;

                case 4:
                    inst = XOR;
                    break;

                case 5:
                    if ((sum >> 30) & 1)
                    {
     
                        inst = SRA;
                    }
                    else
                    {
     
                        inst = SRL;
                    }
                    break;

                case 6:
                    inst = OR;
                    break;
                }
            }
            break;
        }

MIPS五级流水

模拟RISC v过程_第2张图片
设置四个缓存区,构建五级流水
IF_ID,ID_EXE,EXE_MEM,MEM_WB
一个cycle内顺序执行并将缓存传递到下一个
将执行倒过来即构成五级流水

分支预测

采用变种二位分支预测
模拟RISC v过程_第3张图片

到此模拟成功
还有其他功能暂未实现如:精确中断,乱序……

你可能感兴趣的:(模拟RISC v过程)