Skip to content

Commit

Permalink
[dv] Fix performance counter printing in simple system
Browse files Browse the repository at this point in the history
This will avoid printing out a load of spurious zeros if the Ibex
config doesn't enable the corresponding counter.
  • Loading branch information
rswarbrick committed Nov 20, 2023
1 parent f95b958 commit 2258690
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
24 changes: 22 additions & 2 deletions dv/verilator/pcount/cpp/ibex_pcounts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <svdpi.h>

extern "C" {
extern unsigned int mhpmcounter_num();
extern unsigned long long mhpmcounter_get(int index);
}

Expand All @@ -32,14 +33,31 @@ const std::vector<std::string> ibex_counter_names = {
"Multiply Wait",
"Divide Wait"};

static bool has_hpm_counter(int index)
{
// The "cycles" and "instructions retired" counters are special and always exist.
if (index == 0 || index == 2) return true;

// The "NONE" counter is fake. That never exists.
if (index == 1) return false;

// Otherwise, a counter exists if the index is strictly less than
// the MHPMCounterNum parameter that got passed to the
// ibex_cs_registers module.
return index < mhpmcounter_num();
}

std::string ibex_pcount_string(bool csv) {
char separator = csv ? ',' : ':';
std::string::size_type longest_name_length;

if (!csv) {
longest_name_length = 0;
for (const std::string &counter_name : ibex_counter_names) {
longest_name_length = std::max(longest_name_length, counter_name.length());
for (int i = 0; i < ibex_counter_names.size(); ++i) {
if (has_hpm_counter(i)) {
longest_name_length = std::max(longest_name_length,
ibex_counter_names[i].length());
}
}

// Add 1 to always get at least once space after the separator
Expand All @@ -49,6 +67,8 @@ std::string ibex_pcount_string(bool csv) {
std::stringstream pcount_ss;

for (int i = 0; i < ibex_counter_names.size(); ++i) {
if (!has_hpm_counter(i)) continue;

pcount_ss << ibex_counter_names[i] << separator;

if (!csv) {
Expand Down
6 changes: 6 additions & 0 deletions examples/simple_system/rtl/ibex_simple_system.sv
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ module ibex_simple_system (
.timer_intr_o (timer_irq)
);

export "DPI-C" function mhpmcounter_num;

function automatic int unsigned mhpmcounter_num();
return u_top.u_ibex_top.u_ibex_core.cs_registers_i.MHPMCounterNum;
endfunction

export "DPI-C" function mhpmcounter_get;

function automatic longint unsigned mhpmcounter_get(int index);
Expand Down

0 comments on commit 2258690

Please sign in to comment.