模拟用Chisel编写的CPU设计

问题描述 投票:5回答:1

我在Chisel3中编写了一个单循环CPU,它实现了大部分RV32I指令(CSR,Fence,ECALL / BREAK,LB / SB除外,后面可能会包含这些指令)。这些指令当前在指令存储器中进行了硬编码,但我将对其进行更改,以便从文件中读取指令。我遇到了如何实际模拟我的设计的麻烦。这是我将所有组件“粘合”在一起的代码。

class Core extends Module {
  val io = IO(new Bundle {
    val dc = Input(Bool())
})
io := DontCare

val pc          = RegInit(0.U)
val pcSelect    = Module(new PcSelect())
val pcPlusFour  = Module(new Adder())
val alu         = Module(new ALU())
val aluControl  = Module(new AluControl())
val control     = Module(new Control())
val immGen      = Module(new ImmGen())
val branchLogic = Module(new BranchLogic())
val branchUnit  = Module(new Adder())
val jumpReg     = Module(new JumpReg())
val regFile     = Module(new RegFile())
val jumpAdder   = Module(new Adder())
val dataMem     = Module(new DataMemory())
val instrMem    = Module(new InstructionMemory())

// Mux from data memory
val dataMux    = Mux(control.io.memToReg, dataMem.io.readDataOutput, alu.io.result)

// Mux to register file
val regFileMux = Mux(control.io.writeSrc, pcPlusFour.io.result, dataMux)

// PC + 4
pcPlusFour.io.in1 := pc
pcPlusFour.io.in2 := 4.U 

// Instruction memory
instrMem.io.address := pc
val instruction = instrMem.io.instruction
val opcode      = instruction(6, 0)

// Control
control.io.opcode := opcode

// Register file
regFile.io.readReg1  := instruction(19, 15) // rs1
regFile.io.readReg2  := instruction(24, 20) // rs2
regFile.io.writeReg  := instruction(11, 7)  // rd
regFile.io.regWrite  := control.io.regWrite
regFile.io.writeData := regFileMux

// ALU
val aluMux1 = Mux(control.io.aluSrc1, immGen.io.extendedU, regFile.io.readData1)
alu.io.in1 := aluMux1
val src = control.io.aluSrc2
val aluMux2 = Mux(src === 1.U, immGen.io.extendedI, Mux(src === 2.U, immGen.io.extendedS, Mux(src === 3.U, pc, regFile.io.readData2)))
alu.io.in2 := aluMux2
alu.io.aluOp := aluControl.io.output

// ALU control
aluControl.io.aluOp  := control.io.aluOp
aluControl.io.funct7 := instruction(31, 25)
aluControl.io.funct3 := instruction(14, 12) 

// Data Memory
dataMem.io.readAddress := alu.io.result
dataMem.io.writeData   := regFile.io.readData2
dataMem.io.memWrite    := control.io.memWrite
dataMem.io.memRead     := control.io.memRead

// Immediate generator
immGen.io.instr := instruction

// Branch logic
branchLogic.io.reg1 := regFile.io.readData1
branchLogic.io.reg2 := regFile.io.readData2
branchLogic.io.branch := control.io.branch
branchLogic.io.funct3 := instruction(14, 12)

// Jump reg
jumpReg.io.reg1 := regFile.io.readData1
jumpReg.io.imm  := immGen.io.extendedI

// Jump
jumpAdder.io.in1 := pc
jumpAdder.io.in2 := immGen.io.extendedJ

// Branch
branchUnit.io.in1 := pc
branchUnit.io.in2 := immGen.io.extendedB

// PC-select
pcSelect.io.pcPlus4      := pcPlusFour.io.result
pcSelect.io.branch       := branchUnit.io.result
pcSelect.io.jump         := jumpAdder.io.result
pcSelect.io.jalr         := jumpReg.io.output
pcSelect.io.branchSignal := branchLogic.io.result
pcSelect.io.jumpSignal   := control.io.jump
pcSelect.io.jalrSignal   := control.io.jumpReg 

pc := pcSelect.io.output
}

所以我的问题是:

  1. 如何模拟此设计以确保它正确执行所有指令?
  2. 我想在它上面运行基准“dhrystone”来衡量性能。我该怎么做(可能吗?)?如果需要,我不知道如何处理系统调用。

提前致谢!

simulation hdl riscv chisel
1个回答
4
投票

很好的问题:有很多方法可以解决这个问题。

一种常见的方法是从Chisel中获取生成的Verilog并编写自己的测试工具来实例化您的设计。此测试工具可以用C ++,Verilog,SystemVerilog或您喜欢的其他测试工具/粘合剂语言编写。

这种方法由Sodor(https://github.com/ucb-bar/riscv-sodor)和Rocket-Chip(https://github.com/freechipsproject/rocket-chip)使用,最外层的测试线束代码用C ++编写,但能够与Verilog模拟器(如Verilator和VCS)连接。 C ++测试逻辑允许用户通过命令行传入测试二进制文件,然后通过某种“魔法”将二进制文件加载到测试内存中。这个魔法可能是一个外部调试接口,一个Tether串行接口,或者它可能是一个提供的外部RAM模型,可以由测试工具加载(你自己写的东西很简单,或者像dramsim2那样复杂的东西)。

那东西相当复杂,所以我建议开始简单;一个选项是在Chisel中创建一个黑盒内存,该内存由一个使用readmemh初始化自身的简单内存支持。这里很好的功能是您不需要重新编译代码来运行新的二进制文件,只需将要加载的文件换出到测试内存中即可。

Chisel还提供了自己独立的测试人员,所以也许你可以完全在Scala中完成你的测试工具,但我还没有看到这样做的东西像一个非常依赖外部刺激和需要与之沟通的核心外面的世界。

© www.soinside.com 2019 - 2024. All rights reserved.