#include "project.h"


static uint32_t high_tick;

#define QUARTER (1UL << 29)
#define HALF (1UL << 30)
#define THREE_QUARTERS (HALF+QUARTER)
#define ONE (~(uint32_t)0)

uint64_t abs_extend (uint32_t now)
{
  static int m;
  uint64_t ret;


  if (!m) {
    ret = high_tick;
    ret <<= 32;
    ret |= now;


    if ((now > THREE_QUARTERS) && (now <= ONE)) {
      high_tick++;
      m = 1;
    }


  } else {

    if (now < HALF) {
      ret = high_tick;
      ret <<= 32;
      ret |= now;
    } else {
      ret = high_tick - 1;
      ret <<= 32;
      ret |= now;
    }

    if ((now > QUARTER) && (now < HALF))
      m = 0;
  }

  return ret;
}


void abs_meh (void)
{
  printf ("HT %d\n", (int) high_tick);
}





uint64_t abs_get (void)
{
  uint32_t now = SCS_DWT_CYCCNT;
  return abs_extend (now);
}


void abs_slow_tick()
{
  abs_get();
}