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 HWSTACK 00027 #define HWSTACK 00028 00029 #include "rwmem.h" 00030 #include "funktor.h" 00031 #include "avrdevice.h" 00032 #include "traceval.h" 00033 00034 #include <map> 00035 00040 class Thread 00041 { 00042 public: 00044 int m_sp; 00045 int m_ip; 00046 bool m_alive; 00049 unsigned char registers[32]; 00050 int m_created_by_thread; 00051 }; 00052 00056 class ThreadList 00057 { 00059 std::vector<Thread*> m_threads; 00060 enum {eNormal, eReaded, eWritten, eWritten2} m_phase_of_switch; 00061 int m_last_SP_read; 00062 int m_last_SP_writen; 00063 int m_on_call_sp; 00064 int m_on_call_ip; 00065 00066 int m_cur_thread; 00067 AvrDevice & m_core; 00068 00069 ThreadList& operator=(const ThreadList&); // not assignable 00070 public: 00071 00072 ThreadList(AvrDevice & core); 00073 ~ThreadList(); 00074 void OnReset(); 00075 void OnCall(); 00076 void OnSPRead(int SP_value); 00077 void OnSPWrite(int new_SP); 00078 void OnPush(); 00079 void OnPop(); 00080 int GetThreadBySP(int SP) const; 00081 00082 int GetCurrentThreadForGDB() const; 00083 const Thread * GetThreadFromGDB(int thread_id) const; 00084 bool IsGDBThreadAlive(int thread_id) const; 00085 int GetCount() const; 00086 }; 00087 00089 00091 class HWStack { 00092 00093 protected: 00094 AvrDevice *core; 00095 uint32_t stackPointer; 00096 uint32_t lowestStackPointer; 00097 std::multimap<unsigned long, Funktor*> returnPointList; 00098 00100 void CheckReturnPoints(); 00101 00102 public: 00103 ThreadList m_ThreadList; 00104 00105 HWStack(AvrDevice *core); 00106 ~HWStack() {} 00107 00108 virtual void Push(unsigned char val)=0; 00109 virtual unsigned char Pop()=0; 00110 virtual void PushAddr(unsigned long addr)=0; 00111 virtual unsigned long PopAddr()=0; 00112 00113 virtual void Reset(); 00114 00116 unsigned long GetStackPointer() const { return stackPointer; } 00118 void SetStackPointer(unsigned long val) { stackPointer = val; } 00120 00122 void SetReturnPoint(unsigned long stackPointer, Funktor *listener); 00123 00125 void ResetLowestStackpointer(void) { lowestStackPointer = stackPointer; } 00127 unsigned long GetLowestStackpointer(void) { return lowestStackPointer; } 00128 }; 00129 00131 class HWStackSram: public HWStack, public TraceValueRegister { 00132 00133 protected: 00134 unsigned long stackCeil; 00135 bool initRAMEND; 00136 00137 void SetSpl(unsigned char); 00138 void SetSph(unsigned char); 00139 unsigned char GetSpl(); 00140 unsigned char GetSph(); 00141 void OnSPReadByTarget(); 00142 00143 public: 00145 HWStackSram(AvrDevice *core, int bitsize, bool initRAMEND = false); 00146 00147 virtual void Push(unsigned char val); 00148 virtual unsigned char Pop(); 00149 virtual void PushAddr(unsigned long addr); 00150 virtual unsigned long PopAddr(); 00151 00152 virtual void Reset(); 00153 00154 IOReg<HWStackSram> sph_reg; 00155 IOReg<HWStackSram> spl_reg; 00156 }; 00157 00159 class ThreeLevelStack: public HWStack, public TraceValueRegister { 00160 00161 protected: 00162 unsigned long *stackArea; 00163 00164 public: 00165 ThreeLevelStack(AvrDevice *core); 00166 ~ThreeLevelStack(); 00167 00168 virtual void Push(unsigned char val); 00169 virtual unsigned char Pop(); 00170 virtual void PushAddr(unsigned long addr); 00171 virtual unsigned long PopAddr(); 00172 00173 virtual void Reset(); 00174 }; 00175 00176 #endif