#include "printf.h" typedef unsigned long long uint64_t; typedef unsigned long uint32_t; #define MTIME_BASE 0x0200BFF8 uint64_t read_mtime_atomic() { uint32_t upper1, lower, upper2; do { upper1 = *(uint32_t*)(MTIME_BASE + 4); lower = *(uint32_t*)(MTIME_BASE); upper2 = *(uint32_t*)(MTIME_BASE + 4); } while (upper1 != upper2); // Repeat if upper changed during the process return ((uint64_t)upper1 << 32) | lower; } uint32_t timediff_ms(uint64_t start, uint64_t end) { uint64_t total = end-start; // Clock rate is currently 1MHz return (uint32_t)total / 1000; } int fib(int n) { if (n == 0 || n == 1) return 1; return fib(n-1) + fib(n-2); } int main() { int n = 24; uint64_t start = read_mtime_atomic(); int res = fib(n); uint64_t end = read_mtime_atomic(); uint32_t total_msec = timediff_ms(start, end); printf("fib(%d) = %d\n", n, res); printf("time: %dms\n", total_msec); return 0; }