2024-12-14 22:56:28 +00:00
|
|
|
#include "printf.h"
|
2024-12-07 20:02:32 +00:00
|
|
|
|
2024-12-18 23:05:34 +00:00
|
|
|
typedef unsigned long long uint64_t;
|
|
|
|
typedef unsigned long uint32_t;
|
|
|
|
#define MTIME_BASE 0x0200BFF8
|
|
|
|
|
2024-12-19 02:30:53 +00:00
|
|
|
volatile uint32_t* mtime_upper = (uint32_t*)(MTIME_BASE+4);
|
|
|
|
volatile uint32_t* mtime_lower = (uint32_t*)MTIME_BASE;
|
|
|
|
|
|
|
|
#define FRAMEBUFFER_BASE 0x1D385000
|
|
|
|
|
2024-12-18 23:05:34 +00:00
|
|
|
uint64_t read_mtime_atomic() {
|
|
|
|
uint32_t upper1, lower, upper2;
|
|
|
|
|
|
|
|
do {
|
2024-12-19 02:30:53 +00:00
|
|
|
upper1 = *mtime_upper;
|
|
|
|
lower = *mtime_lower;
|
|
|
|
upper2 = *mtime_upper;
|
2024-12-18 23:05:34 +00:00
|
|
|
} while (upper1 != upper2); // Repeat if upper changed during the process
|
|
|
|
|
|
|
|
return ((uint64_t)upper1 << 32) | lower;
|
|
|
|
}
|
|
|
|
|
2024-12-19 00:50:10 +00:00
|
|
|
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)
|
2024-12-07 20:02:32 +00:00
|
|
|
return 1;
|
|
|
|
|
2024-12-19 00:50:10 +00:00
|
|
|
return fib(n-1) + fib(n-2);
|
2024-12-07 20:02:32 +00:00
|
|
|
}
|
2024-12-06 22:40:52 +00:00
|
|
|
|
2024-12-19 02:30:53 +00:00
|
|
|
void draw() {
|
2024-12-19 19:07:39 +00:00
|
|
|
for (int k = 0; k < 200; k++) {
|
|
|
|
for (int i = 0; i< 640; i++) {
|
|
|
|
for (int j = 0; j < 480; j++) {
|
|
|
|
uint32_t color = i*(j + k);
|
|
|
|
uint32_t* addr = (uint32_t*)(FRAMEBUFFER_BASE + ((j*640) + i) *4);
|
|
|
|
*addr = color;
|
|
|
|
}
|
2024-12-19 02:30:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-06 22:40:52 +00:00
|
|
|
int main() {
|
2024-12-19 00:50:10 +00:00
|
|
|
uint64_t start = read_mtime_atomic();
|
2024-12-19 02:30:53 +00:00
|
|
|
|
|
|
|
draw();
|
|
|
|
|
2024-12-19 00:50:10 +00:00
|
|
|
uint64_t end = read_mtime_atomic();
|
2024-12-19 02:30:53 +00:00
|
|
|
|
|
|
|
int n = 24;
|
|
|
|
int res = fib(n);
|
|
|
|
|
2024-12-19 00:50:10 +00:00
|
|
|
uint32_t total_msec = timediff_ms(start, end);
|
|
|
|
|
|
|
|
printf("fib(%d) = %d\n", n, res);
|
|
|
|
printf("time: %dms\n", total_msec);
|
2024-12-06 22:40:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|