summaryrefslogtreecommitdiff
path: root/emulators/simulavr/patches/patch-src_systemclock_cpp
blob: 2bb7798ccb2a7d9ef1ac5809604a858f24aa7088 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
$NetBSD: patch-src_systemclock_cpp,v 1.1 2014/08/29 04:40:06 mef Exp $

clang flags as resize unresolved reference,
backport from git repository (as of 2013-09-15).

--- simulavr-1.0.0/src/systemclock.cpp	2012-02-13 00:26:38.000000000 +0900
+++ src/systemclock.cpp	2013-09-13 09:41:15.000000000 +0900
@@ -2,7 +2,7 @@
  ****************************************************************************
  *
  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
- * Copyright (C) 2001, 2002, 2003   Klaus Rudolph       
+ * Copyright (C) 2001, 2002, 2003 Klaus Rudolph
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -28,6 +28,7 @@
 #include "simulationmember.h"
 #include "helper.h"
 #include "application.h"
+#include "avrdevice.h"
 #include "avrerror.h"
 
 #include "signal.h"
@@ -35,83 +36,78 @@
 
 using namespace std;
 
-
 template<typename Key, typename Value>
 MinHeap<Key, Value>::MinHeap()
 {
-	this->reserve(10);  // vector would free&malloc when we keep inserting and removing only 1 element.
+    this->reserve(10);  // vector would free&malloc when we keep inserting and removing only 1 element.
 }
 
 template<typename Key, typename Value>
 void MinHeap<Key, Value>::RemoveMinimum()
 {
-	assert(!this->empty());
-	Key k = this->back().first;
-	Value v = this->back().second;
-	RemoveMinimumAndInsert(k, v);
-	this->pop_back();
+    assert(!this->empty());
+    Key k = this->back().first;
+    Value v = this->back().second;
+    RemoveMinimumAndInsert(k, v);
+    this->pop_back();
 }
 
 template<typename Key, typename Value>
 bool MinHeap<Key, Value>::ContainsValue(Value v) const
 {
-	for(unsigned i = 0; i < this->size(); i++)
-	{
-		std::pair<Key,Value> item = (*this)[i];
-		if(item.second == v)
-			return true;
-	}
-	return false;
+    for(unsigned i = 0; i < this->size(); i++)
+    {
+        std::pair<Key,Value> item = (*this)[i];
+        if(item.second == v)
+            return true;
+    }
+    return false;
 }
 
 template<typename Key, typename Value>
-void MinHeap<Key, Value>::Insert(Key k, Value v)
+void MinHeap<Key, Value>::InsertInternal(Key k, Value v, unsigned pos)
 {
-	resize(this->size()+1);
-	for(unsigned i = this->size();;) {
-		unsigned parent = i/2;
-		if(parent == 0 || (*this)[parent-1].first < k) {
-			(*this)[i-1].first = k;
-			(*this)[i-1].second = v;
-			return;
-		}
-		Key k_temp = (*this)[parent-1].first;
-		Value v_temp = (*this)[parent-1].second;
-		(*this)[i-1].first = k_temp;
-		(*this)[i-1].second = v_temp;
-		i = parent;
-	}
+    for(unsigned i = pos;;) {
+        unsigned parent = i/2;
+        if(parent == 0 || (*this)[parent-1].first <= k) {
+            (*this)[i-1].first = k;
+            (*this)[i-1].second = v;
+            return;
+        }
+        Key k_temp = (*this)[parent-1].first;
+        Value v_temp = (*this)[parent-1].second;
+        (*this)[i-1].first = k_temp;
+        (*this)[i-1].second = v_temp;
+        i = parent;
+    }
 }
 
 template<typename Key, typename Value>
-void MinHeap<Key, Value>::RemoveMinimumAndInsert(Key k, Value v)
+void MinHeap<Key, Value>::RemoveAtPositionAndInsertInternal(Key k, Value v, unsigned pos)
 {
-	assert(!this->empty());
-	unsigned i = 1;
-	for(;;) {
-		unsigned left = 2*i;
-		unsigned right = 2*i + 1;
-		unsigned smallest = i;
-		if(left-1 < this->size() && (*this)[left-1].first < k)
-			smallest = left;
-		if(right-1 < this->size() && (*this)[right-1].first < k)
-			smallest = right;
-		if(smallest == i) {
-			(*this)[smallest-1].first = k;
-			(*this)[smallest-1].second = v;
-			return;
-		}
-		Key k_temp = (*this)[smallest-1].first;
-		Value v_temp = (*this)[smallest-1].second;
-		(*this)[smallest-1].first = k;
-		(*this)[smallest-1].second = v;
-		k = k_temp;
-		v = v_temp;
-		i = smallest;
-	}
+    assert(pos < this->size());
+    unsigned i = pos + 1;
+    for(;;) {
+        unsigned left = 2*i;
+        unsigned right = 2*i + 1;
+        unsigned smallest = i;
+        if(left-1 < this->size() && (*this)[left-1].first < k)
+            smallest = left;
+        if(right-1 < this->size() && (*this)[right-1].first < k && (*this)[right-1].first < (*this)[left-1].first)
+            smallest = right;
+        if(smallest == i) {
+            (*this)[smallest-1].first = k;
+            (*this)[smallest-1].second = v;
+            return;
+        }
+        Key k_temp = (*this)[smallest-1].first;
+        Value v_temp = (*this)[smallest-1].second;
+        (*this)[i-1].first = k_temp;
+        (*this)[i-1].second = v_temp;
+        i = smallest;
+    }
 }
 
-
 SystemClock::SystemClock() { 
     static int no = 0;
     currentTime = 0; 
@@ -130,7 +126,6 @@
     }
 } 
 
-
 void SystemClock::Add(SimulationMember *dev) {
     syncMembers.Insert(currentTime, dev);
 }
@@ -153,6 +148,8 @@
         currentTime = syncMembers.begin()->first;
         SystemClockOffset nextStepIn_ns = -1;
         
+        syncMembers.RemoveMinimum();
+
         // do a step on simulation member
         res = core->Step(untilCoreStepFinished, &nextStepIn_ns);
         
@@ -164,9 +161,7 @@
         // be called anymore!
         
         if(nextStepIn_ns > 0)
-            syncMembers.RemoveMinimumAndInsert(nextStepIn_ns, core);
-        else
-            syncMembers.RemoveMinimum();
+            syncMembers.Insert(nextStepIn_ns, core);
 
         // handle async simulation members
         amiEnd = asyncMembers.end();
@@ -180,12 +175,11 @@
 }
 
 void SystemClock::Rescedule(SimulationMember *sm, SystemClockOffset newTime) {
-    MinHeap<SystemClockOffset, SimulationMember *>::iterator ii;
 
-    for(ii=syncMembers.begin(); ii != syncMembers.end(); ii++) {
-        if(ii->second == sm) {
-            syncMembers.erase(ii); 
-            break;
+    for(unsigned i = 0; i < syncMembers.size(); i++) {
+        if(syncMembers[i].second == sm) {
+            syncMembers.RemoveAtPositionAndInsert(newTime+currentTime+1, sm, i);
+            return;
         }
     }
 
@@ -211,6 +205,7 @@
 }
 
 void SystemClock::Endless() {
+    breakMessage = false;        // if we run a second loop, clear break before entering loop
     int steps = 0;
     
     signal(SIGINT, OnBreak);
@@ -227,7 +222,6 @@
     Application::GetInstance()->PrintResults();
 }
 
-
 void SystemClock::Run(SystemClockOffset maxRunTime) {
     int steps = 0;
     
@@ -269,4 +263,3 @@
     static SystemClock obj;
     return obj;
 }
-