summaryrefslogtreecommitdiffstats
path: root/riscv/picorv32/firmware/stats.c
diff options
context:
space:
mode:
authorYann Herklotz <git@yannherklotz.com>2022-11-24 22:27:15 +0000
committerYann Herklotz <git@yannherklotz.com>2022-11-24 22:27:15 +0000
commitcfe60025ab06c73c44ce6b962a258668b3a90431 (patch)
tree292d7b2b01a6e0f2ee31b9d133312723980235c1 /riscv/picorv32/firmware/stats.c
parentc0056cea555efe0d6775e3b28ffa5a4a91293097 (diff)
downloadbutterstick-cfe60025ab06c73c44ce6b962a258668b3a90431.tar.gz
butterstick-cfe60025ab06c73c44ce6b962a258668b3a90431.zip
Move all files
Diffstat (limited to 'riscv/picorv32/firmware/stats.c')
-rw-r--r--riscv/picorv32/firmware/stats.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/riscv/picorv32/firmware/stats.c b/riscv/picorv32/firmware/stats.c
new file mode 100644
index 0000000..80e22dd
--- /dev/null
+++ b/riscv/picorv32/firmware/stats.c
@@ -0,0 +1,42 @@
+// This is free and unencumbered software released into the public domain.
+//
+// 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.
+
+#include "firmware.h"
+
+static void stats_print_dec(unsigned int val, int digits, bool zero_pad)
+{
+ 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] = '.';
+ print_chr(*(--p));
+ }
+}
+
+void stats(void)
+{
+ unsigned int num_cycles, num_instr;
+ __asm__ volatile ("rdcycle %0; rdinstret %1;" : "=r"(num_cycles), "=r"(num_instr));
+ print_str("Cycle counter ........");
+ stats_print_dec(num_cycles, 8, false);
+ print_str("\nInstruction counter ..");
+ stats_print_dec(num_instr, 8, false);
+ print_str("\nCPI: ");
+ stats_print_dec((num_cycles / num_instr), 0, false);
+ print_str(".");
+ stats_print_dec(((100 * num_cycles) / num_instr) % 100, 2, true);
+ print_str("\n");
+}
+