picorv32/firmware/stats.c

43 lines
1.1 KiB
C
Raw Normal View History

// This is free and unencumbered software released into the public domain.
2015-07-02 04:49:35 -04:00
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
2015-06-26 16:02:22 -04:00
#include "firmware.h"
2015-06-06 08:01:37 -04:00
static void stats_print_dec(unsigned int val, int digits, bool zero_pad)
2015-06-06 08:01:37 -04:00
{
char buffer[32];
char *p = buffer;
while (val || digits > 0) {
if (val)
*(p++) = '0' + val % 10;
else
*(p++) = zero_pad ? '0' : ' ';
val = val / 10;
digits--;
}
while (p != buffer) {
if (p[-1] == ' ' && p[-2] == ' ') p[-1] = '.';
2015-06-26 16:02:22 -04:00
print_chr(*(--p));
2015-06-06 08:01:37 -04:00
}
}
void stats(void)
2015-06-06 08:01:37 -04:00
{
unsigned int num_cycles, num_instr;
__asm__ volatile ("rdcycle %0; rdinstret %1;" : "=r"(num_cycles), "=r"(num_instr));
2015-06-06 08:01:37 -04:00
print_str("Cycle counter ........");
2015-06-26 16:02:22 -04:00
stats_print_dec(num_cycles, 8, false);
2015-06-06 08:01:37 -04:00
print_str("\nInstruction counter ..");
2015-06-26 16:02:22 -04:00
stats_print_dec(num_instr, 8, false);
2015-06-06 08:01:37 -04:00
print_str("\nCPI: ");
2015-06-26 16:02:22 -04:00
stats_print_dec((num_cycles / num_instr), 0, false);
2015-06-06 08:01:37 -04:00
print_str(".");
2015-06-26 16:02:22 -04:00
stats_print_dec(((100 * num_cycles) / num_instr) % 100, 2, true);
2015-06-06 08:01:37 -04:00
print_str("\n");
}