Skip to content

Commit

Permalink
Add --disable-tracing and --prefix-trace options to HeMAiA (#90)
Browse files Browse the repository at this point in the history
* Switch to newer snitch version

* Add --disable-tracing and --prefix-trace option

* Fix issues

* Fix HeMAiA styling

* Copy verilator_lib.cc from snax_cluster

* Add tb_bin from snax_cluster

* Revert "Switch to newer snitch version"

This reverts commit 72381a1.

---------

Co-authored-by: Josse Van Delm <[email protected]>
  • Loading branch information
JosseVanDelm and Josse Van Delm authored Nov 28, 2024
1 parent b23d337 commit 82d199f
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 15 deletions.
59 changes: 58 additions & 1 deletion target/rtl/test/tb_bin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,26 @@
// SPDX-License-Identifier: SHL-0.51

#include <printf.h>
#include <cstring>
#include <iostream>
#include <vector>

#include "sim.hh"

// Declare these as global variables
bool WRAPPER_disable_tracing = false;
char *WRAPPER_trace_prefix = nullptr;

void print_usage(const char *prog_name) {
std::cout << "Usage: " << prog_name
<< " [HeMAiA WRAPPER Options] [HTIF Options]\n"
<< " Wrap Rocket Chip Emulator in HeMAiA RTL Simulator\n\n"
<< "HeMAiA WRAPPER Options:\n"
<< " --disable-tracing Disable Snitch tracing\n"
<< " --prefix-trace <prefix> Set trace prefix (cannot be used "
"with --disable-tracing)\n\n";
}

int main(int argc, char **argv, char **env) {
// Write binary path to logs/binary for the `make annotate` target
FILE *fd;
Expand All @@ -17,7 +34,47 @@ int main(int argc, char **argv, char **env) {
fprintf(stderr,
"Warning: Failed to write binary name to logs/.rtlbinary\n");
}
// Parse custom wrapper arguments
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "--disable-tracing") == 0) {
WRAPPER_disable_tracing = true;
} else if (strncmp(argv[i], "--prefix-trace ", 15) == 0) {
WRAPPER_trace_prefix =
argv[i] + 15; // Extract the value after `--prefix-trace=`
} else if (strcmp(argv[i], "-h") == 0 ||
strcmp(argv[i], "--help") == 0) {
print_usage(argv[0]);
// Also show help from underlying fesvr function
// Note:
// If a binary is supplied, it will show both the snax wrapper help,
// AND continue with the execution of the program...
auto sim = std::make_unique<sim::Sim>(argc, argv);
return sim->run();
return -1;
}
}
// Validate conflicting options
if (WRAPPER_disable_tracing && WRAPPER_trace_prefix != nullptr) {
std::cerr << "Error: --disable-tracing and --prefix-trace cannot be "
"used together.\n";
return 1;
}
// Prepare filtered arguments
std::vector<char *> filtered_argv;
for (int i = 0; i < argc; ++i) {
// Skip custom options
if (strcmp(argv[i], "--disable-tracing") == 0 ||
strncmp(argv[i], "--prefix-trace ", 15) == 0) {
continue;
}
filtered_argv.push_back(argv[i]);
}
filtered_argv.push_back(nullptr); // Null-terminate for compatibility

auto sim = std::make_unique<sim::Sim>(argc, argv);
// Pass the filtered arguments to fesvr argument handling
int filtered_argc =
static_cast<int>(filtered_argv.size()) - 1; // Exclude null terminator
char **filtered_argv_ptr = filtered_argv.data();
auto sim = std::make_unique<sim::Sim>(filtered_argc, filtered_argv_ptr);
return sim->run();
}
26 changes: 26 additions & 0 deletions target/rtl/test/verilator_lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
// SPDX-License-Identifier: SHL-0.51

#include <printf.h>
#include <filesystem>
#include <string>

#include "Vtestharness.h"
#include "Vtestharness__Dpi.h"
#include "sim.hh"
#include "tb_lib.hh"
#include "verilated.h"
#include "verilated_vcd_c.h"

// Declare these as globally declared (and parsed) in tb_bin.cc
extern bool WRAPPER_disable_tracing;
extern char *WRAPPER_trace_prefix;

namespace sim {

// Number of cycles between HTIF checks.
Expand Down Expand Up @@ -106,6 +113,25 @@ void tb_memory_write(long long addr, int len, const svOpenArrayHandle data,
(const uint8_t *)strb_ptr);
}

svBit disable_tracing() {
// function to enable/disable tracers
return WRAPPER_disable_tracing;
}

const char *get_trace_file_prefix() {
if (WRAPPER_trace_prefix == nullptr) {
// Use the standard prefix, and create a logs directory if necessary
std::string foldername = "logs/";
std::filesystem::create_directories(foldername);
static std::string log_file_name = "logs/";
return log_file_name.c_str();
}
// Return the one parsed from the command line otherwise
else {
return WRAPPER_trace_prefix;
}
}

const long long clint_addr = sim::BOOTDATA.clint_base;
const long num_cores = sim::BOOTDATA.core_count;

Expand Down
106 changes: 92 additions & 14 deletions target/sim_chip/testharness/testharness.cc
Original file line number Diff line number Diff line change
@@ -1,35 +1,113 @@
#include <iostream> // For std::cout
#include "Vtestharness.h" // Verilated generated module
#include <getopt.h>
#include <filesystem>
#include <iostream> // For std::cout
#include "Vtestharness.h" // Verilated generated module
#include "Vtestharness__Dpi.h"
#include "verilated.h" // Verilator library
#include "verilated_vcd_c.h" // For VCD tracing (optional)

int main(int argc, char** argv, char** env) {
// Function to print help message
void print_help() {
std::cout
<< "HeMAiA Verilator Simulation Driver\n"
<< " -h :\t\t\t Show this help message\n"
<< " --vcd :\t\t\t Enable VCD trace dumping\n"
<< " --disable-tracing : \t Disable DASM tracing\n"
<< " --prefix-trace <prefix>: \t Prefix DASM files with prefix\n"
<< " \t(cannot be used together with "
"--disable-tracing)\n";
}

// Global variables
bool WRAPPER_disable_tracing = false;
std::string WRAPPER_trace_prefix;

// DPI calls
svBit disable_tracing() {
// function to enable/disable tracers
return WRAPPER_disable_tracing;
}

const char* get_trace_file_prefix() {
if (WRAPPER_trace_prefix.empty()) {
// Use the standard prefix, and create a logs directory if necessary
std::string foldername = "logs/";
std::filesystem::create_directories(foldername);
static std::string log_file_name = "logs/";
return log_file_name.c_str();
}
// Return the one parsed from the command line otherwise
else {
return WRAPPER_trace_prefix.c_str();
}
}

int main(int argc, char** argv) {
Verilated::commandArgs(argc,
argv); // Pass command-line arguments to Verilator
Vtestharness* top = new Vtestharness; // Instantiate the top module

// Variables for tracing
VerilatedVcdC* tfp = nullptr;
bool enable_vcd = false; // Flag to enable VCD tracing
// Variables for options
bool enable_vcd = false;

// Check if the --vcd flag was passed
for (int i = 1; i < argc; i++) {
if (std::string(argv[i]) == "--vcd") {
enable_vcd = true;
break;
// Define the long options
static struct option long_options[] = {
{"vcd", no_argument, nullptr, 0},
{"disable-tracing", no_argument, nullptr, 1},
{"prefix-trace", required_argument, nullptr, 2},
{"help", no_argument, nullptr, 'h'},
{nullptr, 0, nullptr, 0} // Terminate the option array
};

// Parse the command-line arguments
int option_index = 0;
int c;
while ((c = getopt_long(argc, argv, "h", long_options, &option_index)) !=
-1) {
switch (c) {
case 0: // --vcd
enable_vcd = true;
break;
case 1: // --disable-tracing
WRAPPER_disable_tracing = true;
break;
case 2: // --trace-prefix
WRAPPER_trace_prefix = optarg;
break;
case 'h': // -h or --help
print_help();
return 0;
default:
std::cerr << "Unknown option. Use -h for help.\n";
return 1;
}
}

// Enable tracing if --vcd was passed
// Validate conflicting options
if (WRAPPER_disable_tracing && !WRAPPER_trace_prefix.empty()) {
std::cerr << "Error: --trace-prefix cannot be used together with "
"--disable-tracing\n";
return 1;
}

// Handle VCD tracing
VerilatedVcdC* tfp = nullptr;
if (enable_vcd) {
Verilated::traceEverOn(true); // Enable trace logic
tfp = new VerilatedVcdC;
top->trace(tfp, 99); // Trace 99 levels of hierarchy
tfp->open("sim.vcd"); // Open the VCD dump file
std::cout << "VCD trace enabled. Dumping to sim.vcd..." << std::endl;
std::cout << "VCD trace enabled. Dumping to sim.vcd...\n";
}

// Additional logging based on other options
if (WRAPPER_disable_tracing) {
std::cout << "DASM tracing is disabled.\n";
} else if (!WRAPPER_trace_prefix.empty()) {
std::cout << "DASM tracing enabled with prefix: "
<< WRAPPER_trace_prefix << "\n";
}

// unsigned long main_time = 0; // Current simulation time
unsigned long main_time = 0; // Current simulation time
// Simulation loop
while (!Verilated::gotFinish()) {
Expand Down

0 comments on commit 82d199f

Please sign in to comment.