Skip to content

Commit 9116829

Browse files
committed
Add chisel_cpu_diff demo
1 parent 31343c7 commit 9116829

File tree

10 files changed

+294
-0
lines changed

10 files changed

+294
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
!*.py
1212
!*.mk
1313
!*.bin
14+
!*.scala
1415
!Makefile
1516
!README
1617
!README.md
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import chisel3._
2+
import chisel3.util.experimental._
3+
import difftest._
4+
5+
class Core extends Module {
6+
val io = IO(new Bundle {
7+
val imem = new RomIO
8+
val dmem = new RamIO
9+
})
10+
11+
val fetch = Module(new InstFetch)
12+
fetch.io.imem <> io.imem
13+
14+
val decode = Module(new Decode)
15+
decode.io.inst := fetch.io.inst
16+
17+
val rf = Module(new RegFile)
18+
rf.io.rs1_addr := decode.io.rs1_addr
19+
rf.io.rs2_addr := decode.io.rs2_addr
20+
rf.io.rd_addr := decode.io.rd_addr
21+
rf.io.rd_en := decode.io.rd_en
22+
23+
val execution = Module(new Execution)
24+
execution.io.opcode := decode.io.opcode
25+
execution.io.in1 := Mux(decode.io.rs1_en, rf.io.rs1_data, 0.U)
26+
execution.io.in2 := Mux(decode.io.rs2_en, rf.io.rs2_data, decode.io.imm)
27+
execution.io.dmem <> io.dmem
28+
rf.io.rd_data := execution.io.out
29+
30+
/* ----- Difftest ------------------------------ */
31+
32+
val dt_ic = Module(new DifftestInstrCommit)
33+
dt_ic.io.clock := clock
34+
dt_ic.io.coreid := 0.U
35+
dt_ic.io.index := 0.U
36+
dt_ic.io.valid := true.B
37+
dt_ic.io.pc := RegNext(fetch.io.pc)
38+
dt_ic.io.instr := RegNext(fetch.io.inst)
39+
dt_ic.io.skip := false.B
40+
dt_ic.io.isRVC := false.B
41+
dt_ic.io.scFailed := false.B
42+
dt_ic.io.wen := RegNext(decode.io.rd_en)
43+
dt_ic.io.wdata := RegNext(execution.io.out)
44+
dt_ic.io.wdest := RegNext(decode.io.rd_addr)
45+
46+
val dt_ae = Module(new DifftestArchEvent)
47+
dt_ae.io.clock := clock
48+
dt_ae.io.coreid := 0.U
49+
dt_ae.io.intrNO := 0.U
50+
dt_ae.io.cause := 0.U
51+
dt_ae.io.exceptionPC := 0.U
52+
53+
val cycle_cnt = RegInit(0.U(64.W))
54+
val instr_cnt = RegInit(0.U(64.W))
55+
56+
cycle_cnt := cycle_cnt + 1.U
57+
instr_cnt := instr_cnt + 1.U
58+
59+
val rf_a0 = WireInit(0.U(64.W))
60+
BoringUtils.addSink(rf_a0, "rf_a0")
61+
62+
val dt_te = Module(new DifftestTrapEvent)
63+
dt_te.io.clock := clock
64+
dt_te.io.coreid := 0.U
65+
dt_te.io.valid := (fetch.io.inst === "h0000006b".U)
66+
dt_te.io.code := rf_a0(2, 0)
67+
dt_te.io.pc := fetch.io.pc
68+
dt_te.io.cycleCnt := cycle_cnt
69+
dt_te.io.instrCnt := instr_cnt
70+
71+
val dt_cs = Module(new DifftestCSRState)
72+
dt_cs.io.clock := clock
73+
dt_cs.io.coreid := 0.U
74+
dt_cs.io.priviledgeMode := 3.U // Machine mode
75+
dt_cs.io.mstatus := 0.U
76+
dt_cs.io.sstatus := 0.U
77+
dt_cs.io.mepc := 0.U
78+
dt_cs.io.sepc := 0.U
79+
dt_cs.io.mtval := 0.U
80+
dt_cs.io.stval := 0.U
81+
dt_cs.io.mtvec := 0.U
82+
dt_cs.io.stvec := 0.U
83+
dt_cs.io.mcause := 0.U
84+
dt_cs.io.scause := 0.U
85+
dt_cs.io.satp := 0.U
86+
dt_cs.io.mip := 0.U
87+
dt_cs.io.mie := 0.U
88+
dt_cs.io.mscratch := 0.U
89+
dt_cs.io.sscratch := 0.U
90+
dt_cs.io.mideleg := 0.U
91+
dt_cs.io.medeleg := 0.U
92+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import chisel3._
2+
import chisel3.util._
3+
import Instructions._
4+
5+
class Decode extends Module {
6+
val io = IO(new Bundle {
7+
val inst = Input(UInt(32.W))
8+
val rs1_addr = Output(UInt(5.W))
9+
val rs1_en = Output(Bool())
10+
val rs2_addr = Output(UInt(5.W))
11+
val rs2_en = Output(Bool())
12+
val rd_addr = Output(UInt(5.W))
13+
val rd_en = Output(Bool())
14+
val opcode = Output(UInt(8.W))
15+
val imm = Output(UInt(64.W))
16+
})
17+
18+
val inst = io.inst
19+
val opcode = WireInit(UInt(8.W), 0.U)
20+
val imm_i = Cat(Fill(53, inst(31)), inst(30, 20))
21+
22+
// Only example here, use your own control flow!
23+
when (inst === ADDI) {
24+
opcode := 1.U
25+
}
26+
27+
io.rs1_addr := inst(19, 15)
28+
io.rs2_addr := inst(24, 20)
29+
io.rd_addr := inst(11, 7)
30+
31+
io.rs1_en := false.B
32+
io.rs2_en := false.B
33+
io.rd_en := false.B
34+
35+
when (inst === ADDI) {
36+
io.rs1_en := true.B
37+
io.rs2_en := false.B
38+
io.rd_en := true.B
39+
}
40+
41+
io.opcode := opcode
42+
io.imm := imm_i
43+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import chisel3._
2+
import chisel3.util._
3+
4+
class Execution extends Module {
5+
val io = IO(new Bundle {
6+
val opcode = Input(UInt(8.W))
7+
val in1 = Input(UInt(64.W))
8+
val in2 = Input(UInt(64.W))
9+
val out = Output(UInt(64.W))
10+
val dmem = new RamIO
11+
})
12+
13+
io.out := 0.U
14+
15+
// ADDI
16+
when (io.opcode === 1.U) {
17+
io.out := io.in1 + io.in2
18+
}
19+
20+
io.dmem.en := false.B
21+
io.dmem.addr := 0.U
22+
io.dmem.wen := false.B
23+
io.dmem.wdata := 0.U
24+
io.dmem.wmask := 0.U
25+
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import chisel3._
2+
import chisel3.util._
3+
4+
class InstFetch extends Module {
5+
val io = IO(new Bundle {
6+
val imem = new RomIO
7+
val pc = Output(UInt(32.W))
8+
val inst = Output(UInt(32.W))
9+
})
10+
11+
val pc_en = RegInit(false.B)
12+
pc_en := true.B
13+
14+
val pc = RegInit("h80000000".U(32.W))
15+
pc := pc + 4.U
16+
17+
io.imem.en := true.B
18+
io.imem.addr := pc.asUInt()
19+
20+
io.pc := Mux(pc_en, pc, 0.U)
21+
io.inst := Mux(pc_en, io.imem.rdata(31, 0), 0.U)
22+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import chisel3._
2+
import chisel3.util._
3+
4+
object Instructions {
5+
def ADDI = BitPat("b?????????????????000?????0010011")
6+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import chisel3._
2+
import chisel3.util._
3+
4+
class RomIO extends Bundle {
5+
val en = Output(Bool())
6+
val addr = Output(UInt(64.W))
7+
val rdata = Input(UInt(64.W))
8+
}
9+
10+
class RamIO extends RomIO {
11+
val wdata = Output(UInt(64.W))
12+
val wmask = Output(UInt(64.W))
13+
val wen = Output(Bool())
14+
}
15+
16+
class ram_2r1w extends BlackBox with HasBlackBoxResource {
17+
val io = IO(new Bundle {
18+
val clk = Input(Clock())
19+
val imem_en = Input(Bool())
20+
val imem_addr = Input(UInt(64.W))
21+
val imem_data = Output(UInt(32.W))
22+
val dmem_en = Input(Bool())
23+
val dmem_addr = Input(UInt(64.W))
24+
val dmem_rdata = Output(UInt(64.W))
25+
val dmem_wdata = Input(UInt(64.W))
26+
val dmem_wmask = Input(UInt(64.W))
27+
val dmem_wen = Input(Bool())
28+
})
29+
addResource("/vsrc/ram_2r1w.v")
30+
}
31+
32+
class Ram2r1w extends Module {
33+
val io = IO(new Bundle {
34+
val imem = Flipped(new RomIO)
35+
val dmem = Flipped(new RamIO)
36+
})
37+
val mem = Module(new ram_2r1w)
38+
mem.io.clk := clock
39+
mem.io.imem_en := io.imem.en
40+
mem.io.imem_addr := io.imem.addr
41+
io.imem.rdata := mem.io.imem_data
42+
mem.io.dmem_en := io.dmem.en
43+
mem.io.dmem_addr := io.dmem.addr
44+
io.dmem.rdata := mem.io.dmem_rdata
45+
mem.io.dmem_wdata := io.dmem.wdata
46+
mem.io.dmem_wmask := io.dmem.wmask
47+
mem.io.dmem_wen := io.dmem.wen
48+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import chisel3._
2+
import chisel3.util.experimental._
3+
import difftest._
4+
5+
class RegFile extends Module {
6+
val io = IO(new Bundle {
7+
val rs1_addr = Input(UInt(5.W))
8+
val rs2_addr = Input(UInt(5.W))
9+
val rs1_data = Output(UInt(64.W))
10+
val rs2_data = Output(UInt(64.W))
11+
val rd_addr = Input(UInt(5.W))
12+
val rd_data = Input(UInt(64.W))
13+
val rd_en = Input(Bool())
14+
})
15+
16+
val rf = RegInit(VecInit(Seq.fill(32)(0.U(64.W))))
17+
18+
when (io.rd_en && (io.rd_addr =/= 0.U)) {
19+
rf(io.rd_addr) := io.rd_data;
20+
}
21+
22+
io.rs1_data := Mux((io.rs1_addr =/= 0.U), rf(io.rs1_addr), 0.U)
23+
io.rs2_data := Mux((io.rs2_addr =/= 0.U), rf(io.rs2_addr), 0.U)
24+
25+
val dt_ar = Module(new DifftestArchIntRegState)
26+
dt_ar.io.clock := clock
27+
dt_ar.io.coreid := 0.U
28+
dt_ar.io.gpr := rf
29+
30+
BoringUtils.addSource(rf(10), "rf_a0")
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import chisel3._
2+
import chisel3.util._
3+
import difftest._
4+
5+
class SimTop extends Module {
6+
val io = IO(new Bundle {
7+
val logCtrl = new LogCtrlIO
8+
val perfInfo = new PerfInfoIO
9+
val uart = new UARTIO
10+
})
11+
12+
val core = Module(new Core)
13+
14+
val mem = Module(new Ram2r1w)
15+
mem.io.imem <> core.io.imem
16+
mem.io.dmem <> core.io.dmem
17+
18+
io.uart.out.valid := false.B
19+
io.uart.out.ch := 0.U
20+
io.uart.in.valid := false.B
21+
22+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object TopMain extends App {
2+
(new chisel3.stage.ChiselStage).execute(args, Seq(chisel3.stage.ChiselGeneratorAnnotation(() => new SimTop())))
3+
}

0 commit comments

Comments
 (0)