Skip to content

Commit

Permalink
[t1rocket] handle simulation end in difftest
Browse files Browse the repository at this point in the history
  • Loading branch information
Clo91eaf committed Aug 14, 2024
1 parent baa0552 commit 0d8a006
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 40 deletions.
26 changes: 16 additions & 10 deletions rocketv/src/RocketCore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ import org.chipsalliance.rvdecoderdb.Instruction

class RocketProbe(param: RocketParameter) extends Bundle {
// reg file
val rfWen = Bool()
val rfWaddr = UInt(param.lgNXRegs.W)
val rfWdata = UInt(param.xLen.W)
val rfWen: Bool = Bool()
val rfWaddr: UInt = UInt(param.lgNXRegs.W)
val rfWdata: UInt = UInt(param.xLen.W)

// rocket is idle
val idle: Bool = Bool()
}

object RocketParameter {
Expand Down Expand Up @@ -412,6 +415,10 @@ class Rocket(val parameter: RocketParameter)
def minFLen: Int = parameter.minFLen.getOrElse(0)
def hasDataECC: Boolean = parameter.hasDataECC

// probe defination
val probeWire = Wire(new RocketProbe(parameter))
define(io.rocketProbe, ProbeValue(probeWire))

// Signal outside from internal clock domain.

val longLatencyStall = Reg(Bool())
Expand Down Expand Up @@ -1063,6 +1070,10 @@ class Rocket(val parameter: RocketParameter)
)
when(rfWen) { rf.write(rfWaddr, rfWdata) }

probeWire.rfWen := rfWen
probeWire.rfWaddr := rfWaddr
probeWire.rfWdata := rfWdata

// hook up control/status regfile
csr.io.ungatedClock := io.clock
csr.io.decode(0).inst := idInstruction
Expand Down Expand Up @@ -1392,9 +1403,10 @@ class Rocket(val parameter: RocketParameter)

// Maintain vector counter
// There may be 4 instructions in the pipe
val (_, vectorFull) = counterManagement(countWidth, 4)(t1IssueQueue.io.enq.valid, t1.issue.fire)
val (vectorEmpty, vectorFull) = counterManagement(countWidth, 4)(t1IssueQueue.io.enq.valid, t1.issue.fire)
vectorLSUEmpty.foreach(_ := lsuEmpty)
vectorQueueFull.foreach(_ := vectorFull)
probeWire.idle := vectorEmpty

t1XRDRetireQueue.io.enq.valid := t1.retire.rd.valid
t1XRDRetireQueue.io.enq.bits := t1.retire.rd.bits
Expand Down Expand Up @@ -1475,12 +1487,6 @@ class Rocket(val parameter: RocketParameter)
// todo: perfEvents here.
// csr.io.counters.foreach { c => c.inc := RegNext(perfEvents.evaluate(c.eventSel)) }

// probe xrf write
val probeWire = Wire(new RocketProbe(parameter))
define(io.rocketProbe, ProbeValue(probeWire))
probeWire.rfWen := rfWen
probeWire.rfWaddr := rfWaddr
probeWire.rfWdata := rfWdata
}

def checkExceptions(x: Seq[(Bool, UInt)]) =
Expand Down
2 changes: 2 additions & 0 deletions t1/src/T1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ class T1Probe(parameter: T1Parameter) extends Bundle {
val laneProbes: Vec[LaneProbe] = Vec(parameter.laneNumber, new LaneProbe(parameter.laneParam))
val issue: ValidIO[UInt] = Valid(UInt(parameter.instructionIndexBits.W))
val retire: ValidIO[UInt] = Valid(UInt(parameter.xLen.W))
val idle: Bool = Bool()
}

class T1Interface(parameter: T1Parameter) extends Record {
Expand Down Expand Up @@ -1737,6 +1738,7 @@ class T1(val parameter: T1Parameter)
probeWire.issue.bits := instructionCounter
probeWire.retire.valid := io.retire.rd.valid
probeWire.retire.bits := io.retire.rd.bits.rdData
probeWire.idle := slots.map(_.state.idle).reduce(_ && _)

// new V Request from core
// val requestValidProbe: Bool = IO(Output(Probe(Bool())))
Expand Down
7 changes: 4 additions & 3 deletions t1rocketemu/offline/src/difftest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ impl Difftest {
Ok(())
}
JsonEvents::SimulationStop { reason, cycle } => {
info!("simulation stopped at cycle {}, reason {}", cycle, reason);
self.runner.cycle = *cycle;
Ok(())
anyhow::bail!("error: simulation stopped at cycle {}, reason {}", cycle, reason)
}
JsonEvents::SimulationEnd { cycle } => {
anyhow::bail!("simulation quit successfullly cycle {}", cycle);
}
JsonEvents::RegWrite { idx, data, cycle } => {
self.runner.cycle = *cycle;
Expand Down
2 changes: 1 addition & 1 deletion t1rocketemu/offline/src/dut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Dut {
pub fn step(&mut self) -> anyhow::Result<&JsonEvents> {
let event = match self.events.get(self.idx as usize) {
Some(event) => event,
None => return Err(anyhow::anyhow!("no more events")),
None => anyhow::bail!("error: simulation stopped with no more events"),
};
self.idx += 1;

Expand Down
3 changes: 3 additions & 0 deletions t1rocketemu/offline/src/json_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ pub(crate) enum JsonEvents {
SimulationStart {
cycle: u64,
},
SimulationEnd {
cycle: u64,
},
SimulationStop {
reason: u8,
cycle: u64,
Expand Down
2 changes: 1 addition & 1 deletion t1rocketemu/offline/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn main() -> anyhow::Result<()> {
match diff.diff() {
Ok(_) => {}
Err(e) => {
info!("Simulation quit/error with {}", e);
info!("{}", e);
return Ok(());
}
}
Expand Down
22 changes: 10 additions & 12 deletions t1rocketemu/online_dpi/src/dpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,16 @@ unsafe extern "C" fn cosim_watchdog(reason: *mut c_char) {
}
}

/// evaluate at every cycle, return quit_flag = false to continue simulation,
#[no_mangle]
unsafe extern "C" fn cosim_quit(quit_flag: *mut bool) {
// watchdog dpi call would be called before initialization, guard on null target
let mut driver = DPI_TARGET.lock().unwrap();
if let Some(driver) = driver.as_mut() {
*quit_flag = driver.quit as bool
}
}

#[no_mangle]
unsafe extern "C" fn get_resetvector(resetvector: *mut c_longlong) {
let mut driver = DPI_TARGET.lock().unwrap();
Expand All @@ -313,9 +323,6 @@ mod dpi_export {
#[cfg(feature = "trace")]
/// `export "DPI-C" function dump_wave(input string file)`
pub fn dump_wave(path: *const c_char);

/// 'export "DPI-C" function quit()'
pub fn quit();
}
}

Expand All @@ -329,12 +336,3 @@ pub(crate) fn dump_wave(scope: crate::svdpi::SvScope, path: &str) {
dpi_export::dump_wave(path_cstring.as_ptr());
}
}

pub(crate) fn quit(scope: crate::svdpi::SvScope) {
use crate::svdpi;

svdpi::set_scope(scope);
unsafe {
dpi_export::quit();
}
}
12 changes: 6 additions & 6 deletions t1rocketemu/online_dpi/src/drive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ pub(crate) struct Driver {
last_commit_cycle: u64,

shadow_mem: ShadowMem,

pub(crate) quit: bool,
}

#[cfg(feature = "trace")]
Expand Down Expand Up @@ -185,6 +187,8 @@ impl Driver {
last_commit_cycle: 0,

shadow_mem,

quit: false
}
}

Expand Down Expand Up @@ -345,8 +349,8 @@ impl Driver {
if addr == EXIT_POS {
let exit_data_slice = data[..4].try_into().expect("slice with incorrect length");
if u32::from_le_bytes(exit_data_slice) == EXIT_CODE {
info!("exit successfully");
self.quit()
info!("driver is ready to quit");
self.quit = true;
}
}
}
Expand Down Expand Up @@ -399,8 +403,4 @@ impl Driver {
fn start_dump_wave(&mut self) {
dump_wave(self.scope, &self.wave_path);
}

fn quit(&mut self) {
quit(self.scope)
}
}
18 changes: 11 additions & 7 deletions t1rocketemu/src/TestBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package org.chipsalliance.t1.t1rocketemu
import chisel3._
import chisel3.experimental.{BaseModule, ExtModule, SerializableModuleGenerator}
import chisel3.experimental.dataview.DataViewable
import chisel3.util.circt.dpi.RawUnclockedNonVoidFunctionCall
import chisel3.util.circt.dpi.{RawClockedNonVoidFunctionCall, RawUnclockedNonVoidFunctionCall}
import chisel3.util.{HasExtModuleInline, PopCount, UIntToOH, Valid}
import org.chipsalliance.amba.axi4.bundle._
import org.chipsalliance.t1.t1rocketemu.dpi._
Expand Down Expand Up @@ -34,11 +34,6 @@ class TestBench(generator: SerializableModuleGenerator[T1RocketTile, T1RocketTil
|`endif
| endfunction;
|
| export "DPI-C" function quit;
| function quit();
| $$finish;
| endfunction;
|
| import "DPI-C" context function void t1rocket_cosim_init();
| initial begin
| t1rocket_cosim_init();
Expand Down Expand Up @@ -224,7 +219,9 @@ class TestBench(generator: SerializableModuleGenerator[T1RocketTile, T1RocketTil
)

// t1 lsu enq
when(t1Probe.lsuProbe.reqEnq.orR)(printf(cf"""{"event":"LsuEnq","enq":${t1Probe.lsuProbe.reqEnq},"cycle":${simulationTime}}\n"""))
when(t1Probe.lsuProbe.reqEnq.orR)(
printf(cf"""{"event":"LsuEnq","enq":${t1Probe.lsuProbe.reqEnq},"cycle":${simulationTime}}\n""")
)

// t1 vrf scoreboard
val vrfWriteScoreboard: Seq[Valid[UInt]] = Seq.tabulate(2 * generator.parameter.t1Parameter.chainingSize) { _ =>
Expand Down Expand Up @@ -264,4 +261,11 @@ class TestBench(generator: SerializableModuleGenerator[T1RocketTile, T1RocketTil
scoreboard.bits := 0.U
}
}

// t1 quit
val quitFlag: Bool = RegInit(false.B)
quitFlag := RawClockedNonVoidFunctionCall("cosim_quit", Bool())(clock, !quitFlag)
when(quitFlag && t1Probe.idle && rocketProbe.idle) {
stop(cf"""{"event":"SimulationEnd", "cycle":${simulationTime}}\n""")
}
}

0 comments on commit 0d8a006

Please sign in to comment.