simulavr  1.1.0
systemclock.h
Go to the documentation of this file.
1 /*
2  ****************************************************************************
3  *
4  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
5  * Copyright (C) 2001, 2002, 2003 Klaus Rudolph
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  ****************************************************************************
22  *
23  * $Id$
24  */
25 
26 #ifndef SYSTEMCLOCK
27 #define SYSTEMCLOCK
28 
29 #include <map>
30 #include <vector>
31 
32 #include "systemclocktypes.h"
33 
34 class SimulationMember;
35 
38 template<typename Key, typename Value>
39 class MinHeap : public std::vector<std::pair<Key,Value> >
40 {
41 public:
42  MinHeap();
43  bool IsEmpty() const { return this->empty(); }
44  Key GetMinimumKey() const { return this->front().first; }
45  Value GetMinimumValue() const { return this->front().second; };
46  void RemoveMinimum();
47  bool ContainsValue(Value v) const;
48  void Insert(Key k, Value v) {
49  this->resize(this->size()+1);
50  InsertInternal(k, v, this->size());
51  }
52  void RemoveMinimumAndInsert(Key k, Value v) {
54  }
55  void RemoveAtPositionAndInsert(Key k, Value v, unsigned pos) {
56  if(k < (*this)[pos-1].first)
57  InsertInternal(k, v, pos);
58  else
60  }
61 protected:
62  // These are internal because a bad value of `pos' could violate the binary heap invariant.
63  void InsertInternal(Key k, Value v, unsigned pos);
64  void RemoveAtPositionAndInsertInternal(Key k, Value v, unsigned pos);
65 };
66 
68 
83 {
84  private:
85  SystemClock();
86  SystemClock(const SystemClock &);
87 
88  protected:
91  std::vector<SimulationMember*> asyncMembers;
92 
93  public:
95  SystemClockOffset GetCurrentTime() const { return currentTime; }
97 
98  void SetCurrentTime(SystemClockOffset of) { currentTime = of; }
100 
101  void IncrTime(SystemClockOffset of) { currentTime += of; }
103  void Add(SimulationMember *dev);
105  void AddAsyncMember(SimulationMember *dev);
107  int Step(bool &untilCoreStepFinished);
109  long Endless();
111  long Run(SystemClockOffset maxRunTime);
113  long RunTimeRange(SystemClockOffset timeRange);
115 
116  static SystemClock& Instance();
118 
122  void Reschedule(SimulationMember *sm, SystemClockOffset newTime);
124  void SetTraceModeForAllMembers(int trace_on);
126  void Stop();
128  void ResetClock(void);
129 };
130 
131 #endif
SystemClockOffset GetCurrentTime() const
Returns the current simulation time.
Definition: systemclock.h:95
bool ContainsValue(Value v) const
Definition: systemclock.cpp:56
void IncrTime(SystemClockOffset of)
Increments the current simulation time with a offset.
Definition: systemclock.h:101
MinHeap< SystemClockOffset, SimulationMember * > syncMembers
earliest first
Definition: systemclock.h:90
long long SystemClockOffset
void InsertInternal(Key k, Value v, unsigned pos)
Definition: systemclock.cpp:68
Key GetMinimumKey() const
Definition: systemclock.h:44
void RemoveAtPositionAndInsert(Key k, Value v, unsigned pos)
Definition: systemclock.h:55
void SetCurrentTime(SystemClockOffset of)
Set the simulation time to a dedicated value.
Definition: systemclock.h:98
SystemClockOffset currentTime
time in [ns] since start of simulation
Definition: systemclock.h:89
void RemoveMinimumAndInsert(Key k, Value v)
Definition: systemclock.h:52
bool IsEmpty() const
Definition: systemclock.h:43
std::vector< SimulationMember * > asyncMembers
List of asynchron working simulation members, will be called every step!
Definition: systemclock.h:91
Class to store and manage the central simulation time.
Definition: systemclock.h:82
void Insert(Key k, Value v)
Definition: systemclock.h:48
void RemoveMinimum()
Definition: systemclock.cpp:46
Value GetMinimumValue() const
Definition: systemclock.h:45
void RemoveAtPositionAndInsertInternal(Key k, Value v, unsigned pos)
Definition: systemclock.cpp:86