#include #include #define HZ_TIMER_NUM 29 /* 508.4689655 kHz */ #define OFFSET_TIMER_NUM 15 /* 983.040 kHz */ #define TIMER_DEN 14745600 #define HZ 100 static unsigned long long base_timer; static unsigned long get_timer_hz(void) { return base_timer / HZ_TIMER_NUM; } static unsigned long get_timer_offset(void) { return base_timer / OFFSET_TIMER_NUM; } unsigned long next_jiffy_time; int accumulator; int ticks; static void timer_interrupt(void) { while ((signed long)(get_timer_offset() - next_jiffy_time) >= 0) { // printf("timer tick\n"); ticks++; next_jiffy_time += 983040 / HZ; accumulator += 983040 % HZ; if (accumulator >= HZ) { next_jiffy_time++; accumulator -= HZ; } } } int main() { unsigned long next_interrupt; int ints; ints = 0; next_interrupt = 0; for (base_timer = 0; base_timer < 50 * TIMER_DEN; base_timer += 4) { if (get_timer_hz() == next_interrupt) { // printf("timer interrupt\n"); timer_interrupt(); ints++; next_interrupt += 508469 / HZ; } } printf("%d\n", ints); printf("%d\n", ticks); return 0; }