forked from rezass/HMMsim-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBus.cpp
More file actions
executable file
·64 lines (54 loc) · 1.55 KB
/
Bus.cpp
File metadata and controls
executable file
·64 lines (54 loc) · 1.55 KB
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
/*
* Copyright (c) 2015 Santiago Bock
*
* See the file LICENSE.txt for copying permission.
*/
#include "Bus.H"
Bus::Bus(const string& nameArg,
const string& descArg,
Engine *engineArg,
StatContainer *statCont,
uint64 debugStartArg,
uint64 latencyArg) :
name(nameArg),
desc(descArg),
engine(engineArg),
debugStart(debugStartArg),
latency(latencyArg) {
//debugStart = 121000000;
//debugStart = 0;
}
uint64 Bus::schedule(uint64 delay, IBusCallback *caller){
uint64 timestamp = engine->getTimestamp();
debug("(%lu, %s)", delay, caller->getName());
uint64 start = delay + timestamp;
Queue::iterator it = queue.lower_bound(start-latency);
uint64 actualDelay;
if (it == queue.end()){
queue.emplace(start, caller);
actualDelay = start - timestamp;
} else {
Queue::iterator itNext = it;
++itNext;
while (itNext != queue.end() && (start < it->first + latency || itNext->first < it->first + 2*latency)){
++it;
++itNext;
}
bool ins = queue.emplace(it->first + latency, caller).second;
myassert(ins);
actualDelay = it->first + latency - timestamp;
}
debug(": \tscheduled bus at : %lu (callback at %lu)", actualDelay+timestamp, actualDelay+latency+timestamp);
engine->addEvent(actualDelay+latency, this);
return actualDelay;
}
void Bus::process(const Event *event){
uint64 timestamp = engine->getTimestamp();
debug("()");
Queue::iterator it = queue.begin();
myassert(it != queue.end());
myassert(it->first == timestamp - latency);
IBusCallback *caller = it->second;
queue.erase(it);
caller->transferCompleted();
}