summaryrefslogtreecommitdiffstats
path: root/app/abs.c
blob: 7a6b5d55defbe91d68dd76bb70c7001768561a6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "project.h"


static uint32_t high_tick;

#if HW_CLOCK_LEN == 32

#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;
}
#elif HW_CLOCK_LEN == 31

#define QUARTER (1UL << 28)
#define HALF (1UL << 29)
#define THREE_QUARTERS (HALF+QUARTER)
#define ONE (0x7fffffff)


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


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

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

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

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

  return ret;
}





#else
#error unknown hardware clock length
#endif








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





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


void abs_slow_tick()
{
  abs_get();
}