单周期Risc-V指令拆分与datapath绘制

功能性单周期Risc-V 处理器设计

目标:
对照risc-v手册,实现并调试自研单、多周期处理器

本次实验要实现下面指令:

单周期实验目标模块

注意

Risc-V的指令构成与MIPS指令并不一样:

I-Type 指令:

根据这个表来扩展立即数

查看文献总结37条指令执行公式:

  • 通过I-type格式的特殊化来实现常数位移操作(Shifts by a constant)。
    • rs1 是被操作数,imm 是位移量,shift mount 在 imm 的低 5 位中。
    • The right shift type is encoded in a high bit of the I-immediate.
    • 0 表示逻辑右移(逻辑右移是在高位补 0),1 表示算术右移(算术右移是在高位补符号位)。

I-type 指令执行公式:

  1. ADDI : rd = rs1 + imm (sign_extend);
  2. SLTI :rd = rs1 < imm (sign_extend)? 1 : 0;
  3. SLTIU: rd = rs1 < imm (unsign_extend)? 1 : 0;
  4. ANDI : rd = rs1 & imm (sign_extend);
  5. ORI : rd = rs1 | imm (sign_extend);
  6. XORI : rd = rs1 ^ imm (sign_extend);(rs1 ^ -1 会得到 ~rs1)
  7. SLLI : rd = rs1 << imm4:0 (sign_extend);
  8. SRLI : rd = rs1 >> imm4:0 (sign_extend);

right shift type is encoded in the high bit of the I-immediate.

  1. SRAI : rd = rs1 >>> imm4:0 (sign_extend);

the original sign bit is copied into the vacated upper bits

U-type 指令执行公式:

  1. LUI : rd = imm31:12 << 12 (sign_extend);

LUI places the U-immediate value in the top 20 bits of the destination register rd, filling in the lowest 12 bits with zeros.

  1. AUIPC : rd = imm + PC (sign_extend);

R-type : rd = rs1 op rs2 (sign_extend);

All operations read the rs1 and rs2 registers as source operands and write the result into register rd.The funct7 and funct3 fields select thetype of operation.

  1. ADD: rd = rs1 + rs2 (sign_extend);
  2. SUB: rd = rs1 - rs2 (sign_extend);
  3. SLT: rd = rs1 < rs2 (sign_extend)? 1 : 0;
  4. SLTU : rd = rs1 < rs2 (unsign_extend)? 1 : 0;
  5. AND: rd = rs1 & rs2 (sign_extend);
  6. OR: rd = rs1 | rs2 (sign_extend);
  7. XOR: rd = rs1 ^ rs2 (sign_extend);
  8. SLL: rd = rs1 << rs24:0 (sign_extend);
  9. SRL: rd = rs1 >> rs24:0 (sign_extend);
  10. SRA: rd = rs1 >>> rs24:0 (sign_extend);

SLL, SRL, and SRA perform logical left, logical right, and arithmetic right shifts on the value in register rs1 by the shift amount held in the lower 5 bits of register rs2.
J-type 指令执行公式:

  1. JAL : rd = PC + 4; Target PC = imm + PC (sign_extend);

通过 i m m imm imm 扩展到 32 位,然后左移 1 位,与当前 P C PC PC 相加得到下一个 P C PC PC,就是目标跳转地址。

然后当前 P C + 4 PC+4 PC+4 存储在 r d rd rd 寄存器中。

  1. JALR : rd = PC + 4; PC = (rs1 + imm)&~1 (sign_extend); I-type Instruction 间接跳转指令

B-type 指令执行公式(与 Zero 相关 ):

  1. BEQ : if (rs1 == rs2) then PC = PC + imm else PC = PC + 4 (sign_extend and unsigned);

  2. BNE : if (rs1 != rs2) then PC = PC + imm else PC = PC + 4 (sign and unsign_extend);

  3. BLT : if (rs1 < rs2) then PC = PC + imm else PC = PC + 4 (sign_extend);

  4. BGE : if (rs1 >= rs2) then PC = PC + imm else PC = PC + 4 (sign_extend;

  5. BLTU : if (rs1 < rs2) then PC = PC + imm else PC = PC + 4 (unsign_extend);

  6. BGEU : if (rs1 >= rs2) then PC = PC + imm else PC = PC + 4 (unsign_extend);

Note, BGT, BGTU, BLE, and BLEU can be synthesized by reversing the operands to BLT, BLTU, BGE, and BGEU, respectively.

Load and Store Instructions

Load and store instructions transfer a value between the registers and memory.

Loads are encoded in the I-type format and stores are S-type.

Loads copy a value from memory to register rd. Stores copy the value in register rs2 to memory.

Effective Address = r s 1 + SignExtend ( 12-bit offset ) \text{Effective Address} = rs1 + \text{SignExtend}(\text{12-bit offset}) Effective Address=rs1+SignExtend(12-bit offset)

  1. LW: rd = memrs1 + imm ; 数据无需扩展
  2. LH: rd = memrs1 + imm ; 数据符号扩展到32位
  3. LB: rd = memrs1 + imm ; 符号扩展到32位
  4. LHU: rd = memrs1 + imm ; 数据零扩展到32位
  5. LBU: rd = memrs1 + imm ; 数据零扩展到32位
  6. SW: memrs1 + imm = rs2 ; 数据无需扩展
  7. SH: memrs1 + imm = rs215:0;
  8. SB: memrs1 + imm = rs27:0 ;

simple-cpu 流程图

很早就绘制好了这个datapath,现在再复盘发现还是要花点时间的,所以按照指令绘制datapath会提高写verilog代码的效率,如果只看图会乱。研究过指令后再看图会好一点。

相关推荐
天青色等烟雨..6 小时前
智慧农林核心遥感技术99个案例实践
运维·人工智能·spring boot·后端·自动化
数智化精益手记局6 小时前
拆解复杂项目管理流程:用项目管理流程解决跨部门协作低效难题
大数据·运维·数据库·人工智能·产品运营
Solis程序员6 小时前
长会话状态治理(下):数据更新机制、并发保护与可复用设计原则
运维·服务器
IpdataCloud6 小时前
跨境支付如何识别高风险IP?用IP风险画像服务选型与集成指南
服务器·网络·数据库·tcp/ip·安全
是个西兰花7 小时前
linux:命名管道与共享内存
linux·运维·服务器·网络·c++
herinspace7 小时前
管家婆财工贸软件中关于价格常见问题小结
服务器·网络·数据库·电脑·管家婆软件
MXsoft6187 小时前
**智慧校园运维实践:多校区、老旧设备的统一监控方案**
运维·自动化
Sean‘7 小时前
在隔离内网机器上使用 Filebeat 全量采集日志并推送到 ELK 的实战
运维·服务器·elk
Promise微笑7 小时前
精准微阻测量:微欧计的分类、场景应用与高效选型决策指南
大数据·运维·网络·人工智能
MageGojo7 小时前
R-Shell开源项目实战解析:用Rust打造命令行SSH工具,支持连接管理、远程执行、SFTP与MCP
运维·rust·开源项目·命令行工具·ssh客户端·mcp