00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef __EMULATORUTILITY_OPEMULATIONSTATE_H__
00033 #define __EMULATORUTILITY_OPEMULATIONSTATE_H__
00034
00035 #include "Interface/OpStateIF.h"
00036
00037 #include "Interface/OpInfo.h"
00038 #include "Emu/Utility/CommonOpInfo.h"
00039
00040 namespace Onikiri {
00041 namespace EmulatorUtility {
00042
00043 class ProcessState;
00044
00045 class OpEmulationState {
00046 private:
00047 OpStateIF* m_opState;
00048 OpInfo* m_opInfo;
00049 EmulatorUtility::ProcessState* m_processState;
00050
00051
00052 static const int MaxSrcCount = 8;
00053 static const int MaxDstCount = 3;
00054
00055
00056
00057
00058 boost::array<u64, MaxSrcCount> m_src;
00059 boost::array<u64, MaxDstCount> m_dst;
00060
00061 u64 m_PC;
00062 u64 m_takenPC;
00063 bool m_taken;
00064 int m_pid;
00065 int m_tid;
00066
00067
00068 private:
00069 template<typename TOpInfo, typename TRegObtainer>
00070 void InitOperands(TRegObtainer obtainer)
00071 {
00072 TOpInfo* opInfo = static_cast<TOpInfo*>(m_opInfo);
00073 {
00074 int immNum = opInfo->GetImmNum();
00075 for (int i = 0; i < immNum; i ++) {
00076 m_src[ opInfo->GetImmOpMap(i) ] = opInfo->GetImm(i);
00077 }
00078 }
00079
00080 {
00081 int srcRegNum = opInfo->GetSrcRegNum();
00082 for (int i = 0; i < srcRegNum; i ++) {
00083 m_src[ opInfo->GetSrcRegOpMap(i) ] = obtainer(i);
00084 }
00085 }
00086 }
00087 public:
00088
00089 struct RegFromOpState : public std::unary_function<int, u64>
00090 {
00091 RegFromOpState(Onikiri::OpStateIF* opState) : m_opState(opState) {}
00092
00093 u64 operator()(int index) {
00094 return m_opState->GetSrc(index);
00095 }
00096 Onikiri::OpStateIF* m_opState;
00097 };
00098
00099
00100 template<typename TOpInfo>
00101 struct RegFromRegArray : public std::unary_function<int, u64>
00102 {
00103 RegFromRegArray(TOpInfo* opInfo, u64* regArray) : m_opInfo(opInfo), m_regArray(regArray) {}
00104
00105 u64 operator()(int index) {
00106 return m_regArray[ m_opInfo->GetSrcReg(index) ];
00107 }
00108 TOpInfo* m_opInfo;
00109 u64* m_regArray;
00110 };
00111
00112
00113 template<typename TOpInfo>
00114 OpEmulationState(Onikiri::OpStateIF* opState, TOpInfo* opInfo, EmulatorUtility::ProcessState* processState)
00115 : m_opState(opState), m_opInfo(opInfo), m_processState(processState)
00116 {
00117 Addr addr = m_opState->GetTakenPC();
00118 m_takenPC = addr.address;
00119 m_taken = false;
00120 m_pid = addr.pid;
00121 m_tid = addr.tid;
00122
00123 m_PC = m_opState->GetPC().address;
00124
00125 InitOperands<TOpInfo, RegFromOpState>( RegFromOpState(opState) );
00126 }
00127
00128
00129
00130 template<typename TOpInfo>
00131 OpEmulationState(Onikiri::OpStateIF* opState, TOpInfo* opInfo, EmulatorUtility::ProcessState* processState, PC pc, u64 takenAddr, u64* regArray)
00132 : m_opState(opState), m_opInfo(opInfo), m_processState(processState)
00133 {
00134 m_takenPC = pc.address+4;
00135 m_taken = false;
00136 m_pid = pc.pid;
00137 m_tid = pc.tid;
00138
00139 m_PC = pc.address;
00140
00141 InitOperands<TOpInfo, RegFromRegArray<TOpInfo> >( RegFromRegArray<TOpInfo>(opInfo, regArray) );
00142 }
00143
00144
00145 template<typename TOpInfo>
00146 void ApplyEmulationState()
00147 {
00148 TOpInfo* opInfo = static_cast<TOpInfo*>(m_opInfo);
00149 m_opState->SetTaken(m_taken);
00150 m_opState->SetTakenPC( Addr(m_pid, m_tid, m_takenPC) );
00151
00152
00153 int dstRegNum = opInfo->GetDstRegNum();
00154 for (int i = 0; i < dstRegNum; i ++) {
00155 m_opState->SetDst( i, m_dst[ opInfo->GetDstRegOpMap(i) ]);
00156 }
00157 }
00158
00159
00160
00161 template<typename TOpInfo>
00162 void ApplyEmulationStateToRegArray(u64* regArray)
00163 {
00164 TOpInfo* opInfo = static_cast<TOpInfo*>(m_opInfo);
00165
00166
00167 int dstRegNum = opInfo->GetDstRegNum();
00168 for (int i = 0; i < dstRegNum; i ++) {
00169 regArray[ opInfo->GetDstReg(i) ] = m_dst[ opInfo->GetDstRegOpMap(i) ];
00170 }
00171 }
00172
00173 void SetDst(int index, u64 value)
00174 {
00175 m_dst[index] = value;
00176 }
00177 u64 GetSrc(int index)
00178 {
00179 return m_src[index];
00180 }
00181 u64 GetDst(int index)
00182 {
00183 return m_dst[index];
00184 }
00185
00186
00187 void SetTaken(bool t)
00188 {
00189 m_taken = t;
00190 }
00191 bool GetTaken() const
00192 {
00193 return m_taken;
00194 }
00195
00196 Onikiri::u64 GetPC() const
00197 {
00198 return m_PC;
00199 }
00200
00201 void SetTakenPC(Onikiri::u64 pc)
00202 {
00203 m_takenPC = pc;
00204 }
00205
00206 Onikiri::u64 GetTakenPC() const
00207 {
00208 return m_takenPC;
00209 }
00210
00211 int GetPID() const
00212 {
00213 return m_pid;
00214 }
00215
00216 int GetTID() const
00217 {
00218 return m_tid;
00219 }
00220
00221 OpStateIF* GetOpState()
00222 {
00223 return m_opState;
00224 }
00225 OpInfo* GetOpInfo()
00226 {
00227 return m_opInfo;
00228 }
00229 EmulatorUtility::ProcessState* GetProcessState()
00230 {
00231 return m_processState;
00232 }
00233 };
00234
00235 }
00236 }
00237
00238 #endif