Hi,
i tried to check clock cycles on HiFive Unleashed FU540 i am running following code to read the cycles
#include <stdio.h>
#define read_csr(reg) ({ unsigned long __tmp; \
asm volatile ("csrr %0, " #reg : “=r”(__tmp)); \
__tmp; })
#define write_csr(reg, val) ({ \
asm volatile ("csrw " #reg “, %0” :: “rK”(val)); })
#define swap_csr(reg, val) ({ unsigned long __tmp; \
asm volatile ("csrrw %0, " #reg “, %1” : “=r”(__tmp) : “rK”(val)); \
__tmp; })
#define set_csr(reg, bit) ({ unsigned long __tmp; \
asm volatile ("csrrs %0, " #reg “, %1” : “=r”(__tmp) : “rK”(bit)); \
__tmp; })
#define clear_csr(reg, bit) ({ unsigned long __tmp; \
asm volatile ("csrrc %0, " #reg “, %1” : “=r”(__tmp) : “rK”(bit)); \
__tmp; })
#define rdtime() read_csr(time)
#define rdcycle() read_csr(cycle)
#define rdinstret() read_csr(instret)
int main()
{
for (int i=0; i<10; ++i) {
unsigned long t = rdcycle();
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
....
....
....
unsigned long t1 = rdcycle();
printf("cycles: %lu\n", t1 - t);
}
}
#./a.out
cycles: 215
cycles: 142
cycles: 142
cycles: 112
cycles: 112
cycles: 127
cycles: 142
cycles: 142
cycles: 142
cycles: 127
i have include 100 nops which should take 100 cycles and few more cycles for returning from the function(i would like to mention with 1nop() i am getting 14 cycles) but i am not able to understand why it is taking more than that…can someone help us to rectify ??
Thanks!