00001 /* 00002 **************************************************************************** 00003 * 00004 * simulavr - A simulator for the Atmel AVR family of microcontrollers. 00005 * Copyright (C) 2001, 2002, 2003 Klaus Rudolph 00006 * 00007 * This program is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 * 00021 **************************************************************************** 00022 * 00023 * $Id$ 00024 */ 00025 00026 #ifndef SYSTEMCLOCK 00027 #define SYSTEMCLOCK 00028 00029 #include <map> 00030 #include <vector> 00031 00032 #include "avrdevice.h" 00033 #include "systemclocktypes.h" 00034 00037 template<typename Key, typename Value> 00038 class MinHeap : public std::vector<std::pair<Key,Value> > 00039 { 00040 public: 00041 MinHeap(); 00042 bool IsEmpty() const { return this->empty(); } 00043 Value GetMinimumKey() const { return this->front().first; } 00044 Value GetMinimumValue() const { return this->front().second; }; 00045 void RemoveMinimum(); 00046 bool ContainsValue(Value v) const; 00047 void Insert(Key k, Value v); 00048 void RemoveMinimumAndInsert(Key k, Value v); 00049 }; 00050 00051 00053 00067 class SystemClock 00068 { 00069 private: 00070 SystemClock(); 00071 SystemClock(const SystemClock &); 00072 00073 protected: 00074 SystemClockOffset currentTime; 00075 MinHeap<SystemClockOffset, SimulationMember *> syncMembers; 00076 std::vector<SimulationMember*> asyncMembers; 00077 00078 public: 00080 SystemClockOffset GetCurrentTime() const { return currentTime; } 00082 00083 void SetCurrentTime(SystemClockOffset of) { currentTime = of; } 00085 00086 void IncrTime(SystemClockOffset of) { currentTime += of; } 00088 void Add(SimulationMember *dev); 00090 void AddAsyncMember(SimulationMember *dev); 00092 int Step(bool &untilCoreStepFinished); 00094 void Endless(); 00096 void Run(SystemClockOffset maxRunTime); 00098 int RunTimeRange(SystemClockOffset timeRange); 00100 00101 static SystemClock& Instance(); 00103 00107 void Rescedule(SimulationMember *sm, SystemClockOffset newTime); 00109 void SetTraceModeForAllMembers(int trace_on); 00111 void stop(); 00113 void ResetClock(void); 00114 }; 00115 00116 #endif