forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Timer.cpp
83 lines (75 loc) · 1.66 KB
/
Timer.cpp
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
// Copyright (c) 2011, Richard Osborne, All rights reserved
// This software is freely distributable under a derivative of the
// University of Illinois/NCSA Open Source License posted in
// LICENSE.txt and at <http://github.xcore.com/>
#include "Timer.h"
#include "Thread.h"
bool Timer::conditionMet(ticks_t time) const
{
if (!after)
return true;
return ((int32_t)(time / CYCLES_PER_TICK) - (int32_t)data) > 0;
}
bool Timer::
setCondition(Thread &thread, Condition c, ticks_t time)
{
updateOwner(thread);
switch (c) {
default: return false;
case COND_FULL:
after = false;
break;
case COND_AFTER:
after = true;
break;
}
return true;
}
bool Timer::
setData(Thread &thread, uint32_t d, ticks_t time)
{
updateOwner(thread);
data = d;
return true;
}
Resource::ResOpResult Timer::
in(Thread &thread, ticks_t time, uint32_t &val)
{
updateOwner(thread);
if (!conditionMet(time)) {
pausedIn = &thread;
scheduleUpdate(getEarliestReadyTime(time));
return DESCHEDULE;
}
val = (uint32_t)(time / CYCLES_PER_TICK);
return CONTINUE;
}
ticks_t Timer::getEarliestReadyTime(ticks_t time) const
{
if (conditionMet(time))
return time;
int32_t wait = (int32_t)(data + 1) - (int32_t)(time / CYCLES_PER_TICK);
return time + wait * CYCLES_PER_TICK;
}
void Timer::run(ticks_t time)
{
if (!conditionMet(time))
return;
if (eventsPermitted()) {
event(time);
}
if (pausedIn) {
pausedIn->time = time;
pausedIn->schedule();
pausedIn = 0;
}
}
bool Timer::seeEventEnable(ticks_t time)
{
if (conditionMet(time)) {
event(time);
return true;
}
scheduleUpdate(getEarliestReadyTime(time));
return false;
}