docs: 增加各模块功能说明和一些关键代码注释

main
Liphen 1 year ago
parent 67a04e71d4
commit 43209019e7

@ -31,4 +31,5 @@ class Core extends Module {
fetchUnit.decodeStage <> decodeStage.fetchUnit
// TODO: 完成Core模块的逻辑
// 在该模块中需要将各个模块连接起来形成一个完整的CPU核心
}

@ -22,7 +22,7 @@ class RdInfo extends Bundle {
}
class Info extends Bundle {
val valid = Bool()
val valid = Bool() // 用于标识当前流水级中的指令是否有效
val src1_raddr = UInt(REG_ADDR_WID.W)
val src2_raddr = UInt(REG_ADDR_WID.W)
val op = FuOpType()
@ -52,8 +52,8 @@ class DataSram extends Bundle {
}
class DEBUG extends Bundle {
val pc = Output(UInt(XLEN.W))
val commit = Output(Bool())
val rf_wnum = Output(UInt(REG_ADDR_WID.W))
val rf_wdata = Output(UInt(XLEN.W))
val commit = Output(Bool()) // 写回阶段的commit信号仅在每条指令提交时置为true
val pc = Output(UInt(XLEN.W)) // 写回阶段的pc
val rf_wnum = Output(UInt(REG_ADDR_WID.W)) // 写回阶段的寄存器写地址
val rf_wdata = Output(UInt(XLEN.W)) // 写回阶段的寄存器写数据
}

@ -17,6 +17,7 @@ object ZeroExtend {
if (aLen >= len) a(len - 1, 0) else Cat(0.U((len - aLen).W), a)
}
}
object LookupTree {
def apply[T <: Data](key: UInt, mapping: Iterable[(UInt, T)]): T =
Mux1H(mapping.map(p => (p._1 === key, p._2)))

@ -3,6 +3,7 @@ package cpu.defines
import chisel3._
import chisel3.util._
// 指令类型
trait HasInstrType {
def InstrN = "b000".U
def InstrI = "b100".U
@ -12,21 +13,24 @@ trait HasInstrType {
def InstrU = "b110".U
def InstrJ = "b111".U
// IRUJ类型的指令都需要写寄存器
def isRegWen(instrType: UInt): Bool = instrType(2)
}
// 功能单元类型 Function Unit Type
object FuType {
def num = 1
def alu = 0.U // arithmetic logic unit
def apply() = UInt(log2Up(num).W)
}
// 功能单元操作类型 Function Unit Operation Type
object FuOpType {
def apply() = UInt(5.W)
def apply() = UInt(5.W) // 宽度与最大的功能单元操作类型宽度一致
}
// ALU
// 算术逻辑单元操作类型 Arithmetic Logic Unit Operation Type
object ALUOpType {
def add = "b00000".U
def add = "b00000".U
// TODO: 定义更多的ALU操作类型
}

@ -31,8 +31,10 @@ object RV32I_ALUInstr extends HasInstrType with CoreParameter {
def AUIPC = BitPat("b????????????????????_?????_0010111")
def LUI = BitPat("b????????????????????_?????_0110111")
// 在Decoder模块中搭配ListLookup函数使用
val table = Array(
ADDI -> List(InstrI, FuType.alu, ALUOpType.add),
// ADD指令将被解析为InstrR类型的指令功能单元类型为alu功能单元操作类型为add
ADD -> List(InstrR, FuType.alu, ALUOpType.add)
// TODO: 完成其他指令的解析
)
}

@ -14,6 +14,8 @@ class DecodeUnit extends Module {
val executeStage = Output(new DecodeUnitExecuteUnit())
})
// 译码阶段完成指令的译码操作以及源操作数的准备
val decoder = Module(new Decoder()).io
decoder.in.inst := io.decodeStage.data.inst

@ -20,6 +20,7 @@ class Decoder extends Module with HasInstrType {
val inst = io.in.inst
// 根据输入的指令inst从Instructions.DecodeTable中查找对应的指令类型功能单元类型和功能单元操作类型
// 如果找不到匹配的指令则使用Instructions.DecodeDefault作为默认值
// instrTypefuType和fuOpType分别被赋值为Instructions.DecodeTable中的对应值
val instrType :: fuType :: fuOpType :: Nil =
ListLookup(inst, Instructions.DecodeDefault, Instructions.DecodeTable)

@ -14,6 +14,8 @@ class ExecuteUnit extends Module {
val dataSram = new DataSram()
})
// 执行阶段完成指令的执行操作
val fu = Module(new Fu()).io
fu.data.pc := io.executeStage.data.pc
fu.data.info := io.executeStage.data.info

@ -25,6 +25,8 @@ class FetchUnit extends Module {
is(receive) {}
}
// 取指阶段完成指令的取指操作
val pc = RegEnable(io.instSram.addr, (PC_INIT - 4.U), state =/= boot)
io.instSram.addr := pc + 4.U

@ -13,6 +13,8 @@ class MemoryUnit extends Module {
val writeBackStage = Output(new MemoryUnitWriteBackUnit())
})
// 访存阶段完成指令的访存操作
io.writeBackStage.data.pc := io.memoryStage.data.pc
io.writeBackStage.data.info := io.memoryStage.data.info
io.writeBackStage.data.rd_info.wdata := io.memoryStage.data.rd_info.wdata

@ -13,5 +13,7 @@ class WriteBackUnit extends Module {
val debug = new DEBUG()
})
// 写回阶段完成数据的写回操作
// 同时该阶段还负责差分测试的比对工作
// TODO: 完成WriteBackUnit模块的逻辑
}

Loading…
Cancel
Save