Skip to content

Instantly share code, notes, and snippets.

@rossy
Created July 3, 2013 07:24
Show Gist options
  • Save rossy/5916073 to your computer and use it in GitHub Desktop.
Save rossy/5916073 to your computer and use it in GitHub Desktop.
Query the interrupt timer in Windows without any API calls.
#include <inttypes.h>
#include <stdio.h>
static uint64_t get_interrupt_time()
{
const void* const shared_user_data = (void*)0x7ffe0000;
const volatile struct {
uint32_t low;
uint32_t high1;
uint32_t high2;
}* const interrupt_time = (void*)((uint32_t*)shared_user_data + 2);
union {
struct {
uint32_t low;
uint32_t high;
} t;
uint64_t i;
} icopy;
do {
icopy.t.high = interrupt_time->high1;
icopy.t.low = interrupt_time->low;
} while (interrupt_time->high2 != icopy.t.high);
return icopy.i;
}
int main()
{
uint64_t itime, itime_old, itime_offset, delta;
unsigned counter = 0;
itime_offset = get_interrupt_time();
itime_old = itime_offset;
for (;;) {
do itime = get_interrupt_time();
while (itime <= itime_old);
delta = itime - itime_old;
itime_old = itime;
itime -= itime_offset;
if (++counter == 32) {
printf("t=%" PRIu64 ".%07" PRIu64 "s "
"d=%" PRIu64 ".%04" PRIu64 "ms \r",
itime / UINT64_C(10000000), itime % UINT64_C(10000000),
delta / UINT64_C(10000), delta % UINT64_C(10000));
fflush(stdout);
counter = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment