Skip to content

Commit

Permalink
[rocketemu] add quit functionality to terminate simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Clo91eaf committed Jul 24, 2024
1 parent d7217af commit 699f47e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
11 changes: 10 additions & 1 deletion rocketemu/dpi/dpi_pre_link.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,27 @@ class VTestBench;
VerilatedContext *contextp;
VTestBench *topp;

bool quit;

void quit_c() {
quit = true;
}

int verilator_main_c(int argc, char **argv) {
// Setup context, defaults, and parse command line
Verilated::debug(0);
contextp = new VerilatedContext();
contextp->fatalOnError(false);
contextp->commandArgs(argc, argv);

// Set quit flag, true means quit
quit = false;

// Construct the Verilated model, from Vtop.h generated from Verilating
topp = new VTestBench(contextp);

// Simulate until $finish
while (!contextp->gotFinish()) {
while (!contextp->gotFinish() && !quit) {
// Evaluate model
topp->eval();
// Advance time
Expand Down
2 changes: 2 additions & 0 deletions rocketemu/dpi/dpi_pre_link.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ extern "C" {

int verilator_main_c(int argc, char **argv);

void quit_c();

#ifdef VM_TRACE
void dump_wave_c(char *path);
#endif
Expand Down
8 changes: 8 additions & 0 deletions rocketemu/driver/src/dpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ unsafe extern "C" fn cosim_watchdog_rs(target: *mut (), reason: *mut c_char) {
extern "C" {
fn verilator_main_c(argc: c_int, argv: *mut *mut c_char) -> c_int;

fn quit_c();

#[cfg(feature = "trace")]
fn dump_wave_c(path: *const c_char);

Expand All @@ -190,6 +192,12 @@ pub(crate) fn get_t() -> u64 {
unsafe { get_t_c() / 20 }
}

pub(crate) fn quit() {
unsafe {
quit_c();
}
}

pub(crate) fn verilator_main() {
let mut c_args_ptr: Vec<*mut c_char> = std::env::args()
.collect::<Vec<String>>()
Expand Down
9 changes: 9 additions & 0 deletions rocketemu/driver/src/sim.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[cfg(feature = "trace")]
use crate::dpi::dump_wave;
use crate::dpi::get_t;
use crate::dpi::quit;

use clap::{arg, Parser};
use std::collections::HashMap;
Expand All @@ -23,6 +24,8 @@ pub(crate) struct AxiReadPayload {
pub(crate) data: Vec<u8>,
}

const EXIT_POS: u32 = 0x4000_0000;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct SimulationArgs {
Expand Down Expand Up @@ -265,6 +268,12 @@ impl Simulator {
get_t()
);

if addr == EXIT_POS {
info!("exit with code: {:x?}", data);
quit();
return;
}

self.write_mem(addr, self.dlen / 8, strobe, data);
}

Expand Down

0 comments on commit 699f47e

Please sign in to comment.