2007-03-03 19:24:42 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
#include "MipsTestEngine.h"
|
|
|
|
#include "StdStream.h"
|
|
|
|
#include "lexical_cast_ex.h"
|
|
|
|
#include "xml/Node.h"
|
|
|
|
#include "xml/Parser.h"
|
|
|
|
#include "xml/FilteringNodeIterator.h"
|
|
|
|
#include "xml/Utils.h"
|
|
|
|
|
|
|
|
using namespace Framework;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
CMipsTestEngine::CMipsTestEngine(const char* sPath)
|
|
|
|
{
|
|
|
|
Xml::CNode* pRootNode;
|
|
|
|
pRootNode = Xml::CParser::ParseDocument(&CStdStream(fopen(sPath, "rb")));
|
|
|
|
|
|
|
|
if(pRootNode == NULL)
|
|
|
|
{
|
|
|
|
throw exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadInputs(pRootNode->Select("Test/Inputs"));
|
|
|
|
LoadOutputs(pRootNode->Select("Test/Outputs"));
|
2007-03-12 06:24:13 +00:00
|
|
|
LoadInstances(pRootNode->Select("Test/Instances"));
|
2007-03-03 19:24:42 +00:00
|
|
|
|
|
|
|
delete pRootNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
CMipsTestEngine::~CMipsTestEngine()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-03-12 22:28:01 +00:00
|
|
|
CMipsTestEngine::OutputsType::iterator CMipsTestEngine::GetOutputsBegin()
|
|
|
|
{
|
|
|
|
return m_Outputs.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
CMipsTestEngine::OutputsType::iterator CMipsTestEngine::GetOutputsEnd()
|
|
|
|
{
|
|
|
|
return m_Outputs.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
CMipsTestEngine::CValueSet* CMipsTestEngine::GetInput(unsigned int nId)
|
|
|
|
{
|
|
|
|
InputsByIdMapType::iterator itInput;
|
|
|
|
itInput = m_InputsById.find(nId);
|
|
|
|
return (itInput != m_InputsById.end()) ? (itInput->second) : (NULL);
|
|
|
|
}
|
|
|
|
|
2007-03-13 23:43:35 +00:00
|
|
|
CMipsTestEngine::CInstance* CMipsTestEngine::GetInstance(unsigned int nId)
|
|
|
|
{
|
|
|
|
InstancesByIdMapType::iterator itInstance;
|
|
|
|
itInstance = m_InstancesById.find(nId);
|
|
|
|
return (itInstance != m_InstancesById.end()) ? (itInstance->second) : (NULL);
|
|
|
|
}
|
|
|
|
|
2007-03-03 19:24:42 +00:00
|
|
|
void CMipsTestEngine::LoadInputs(Xml::CNode* pInputsNode)
|
|
|
|
{
|
2007-03-12 06:24:13 +00:00
|
|
|
if(pInputsNode == NULL)
|
2007-03-03 19:24:42 +00:00
|
|
|
{
|
|
|
|
throw exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
for(Xml::CFilteringNodeIterator itNode(pInputsNode, "ValueSet"); !itNode.IsEnd(); itNode++)
|
|
|
|
{
|
2007-03-12 22:28:01 +00:00
|
|
|
CValueSet* pInput(new CValueSet(*itNode));
|
|
|
|
m_Inputs.push_back(pInput);
|
|
|
|
m_InputsById[pInput->GetInputId()] = pInput;
|
2007-03-03 19:24:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMipsTestEngine::LoadOutputs(Xml::CNode* pOutputsNode)
|
|
|
|
{
|
2007-03-12 06:24:13 +00:00
|
|
|
if(pOutputsNode == NULL)
|
2007-03-03 19:24:42 +00:00
|
|
|
{
|
|
|
|
throw exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
for(Xml::CFilteringNodeIterator itNode(pOutputsNode, "ValueSet"); !itNode.IsEnd(); itNode++)
|
|
|
|
{
|
|
|
|
m_Outputs.push_back(new CValueSet(*itNode));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-12 06:24:13 +00:00
|
|
|
void CMipsTestEngine::LoadInstances(Xml::CNode* pInstancesNode)
|
|
|
|
{
|
|
|
|
if(pInstancesNode == NULL)
|
|
|
|
{
|
|
|
|
throw exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
for(Xml::CFilteringNodeIterator itNode(pInstancesNode, "Instance"); !itNode.IsEnd(); itNode++)
|
|
|
|
{
|
2007-03-13 23:43:35 +00:00
|
|
|
CInstance* pInstance(new CInstance(*itNode));
|
|
|
|
m_Instances.push_back(pInstance);
|
|
|
|
m_InstancesById[pInstance->GetId()] = pInstance;
|
2007-03-12 06:24:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-03 19:24:42 +00:00
|
|
|
////////////////////////////////////////////////////
|
|
|
|
// CValueSet implementation
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
CMipsTestEngine::CValueSet::CValueSet(Xml::CNode* pValueSetNode)
|
|
|
|
{
|
2007-03-12 06:24:13 +00:00
|
|
|
m_nInputId = 0;
|
|
|
|
m_nInstanceId = 0;
|
|
|
|
|
|
|
|
Xml::GetAttributeIntValue(pValueSetNode, "InputId", reinterpret_cast<int*>(&m_nInputId));
|
|
|
|
Xml::GetAttributeIntValue(pValueSetNode, "InstanceId", reinterpret_cast<int*>(&m_nInstanceId));
|
|
|
|
|
2007-03-03 19:24:42 +00:00
|
|
|
for(Xml::CNode::NodeIterator itNode(pValueSetNode->GetChildrenBegin());
|
|
|
|
itNode != pValueSetNode->GetChildrenEnd(); itNode++)
|
|
|
|
{
|
|
|
|
Xml::CNode* pNode(*itNode);
|
|
|
|
if(!pNode->IsTag()) continue;
|
|
|
|
|
|
|
|
//Check the value type
|
|
|
|
if(!strcmp(pNode->GetText(), "Register"))
|
|
|
|
{
|
|
|
|
m_Values.push_back(new CRegisterValue(pNode));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw runtime_error(string("Unknown value type '") + pNode->GetText() + string("' encountered."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CMipsTestEngine::CValueSet::~CValueSet()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-03-12 22:28:01 +00:00
|
|
|
unsigned int CMipsTestEngine::CValueSet::GetInputId() const
|
|
|
|
{
|
|
|
|
return m_nInputId;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int CMipsTestEngine::CValueSet::GetInstanceId() const
|
|
|
|
{
|
|
|
|
return m_nInstanceId;
|
|
|
|
}
|
|
|
|
|
2007-03-13 23:43:35 +00:00
|
|
|
void CMipsTestEngine::CValueSet::AssembleLoad(CMIPSAssembler& Assembler)
|
|
|
|
{
|
|
|
|
for(ValueListType::iterator itValue(m_Values.begin());
|
|
|
|
itValue != m_Values.end(); itValue++)
|
|
|
|
{
|
|
|
|
itValue->AssembleLoad(Assembler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-14 01:28:22 +00:00
|
|
|
bool CMipsTestEngine::CValueSet::Verify(CMIPS& Context)
|
|
|
|
{
|
|
|
|
bool nResult = true;
|
|
|
|
|
|
|
|
for(ValueListType::iterator itValue(m_Values.begin());
|
|
|
|
itValue != m_Values.end(); itValue++)
|
|
|
|
{
|
|
|
|
nResult &= itValue->Verify(Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nResult;
|
|
|
|
}
|
|
|
|
|
2007-03-12 06:24:13 +00:00
|
|
|
////////////////////////////////////////////////////
|
|
|
|
// CInstance implementation
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
CMipsTestEngine::CInstance::CInstance(Xml::CNode* pInstanceNode)
|
|
|
|
{
|
|
|
|
if(!Xml::GetAttributeIntValue(pInstanceNode, "Id", reinterpret_cast<int*>(&m_nId)))
|
|
|
|
{
|
|
|
|
throw runtime_error("No Id declared for instance.");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_sSource = pInstanceNode->GetInnerText();
|
|
|
|
}
|
|
|
|
|
|
|
|
CMipsTestEngine::CInstance::~CInstance()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-03-13 23:43:35 +00:00
|
|
|
unsigned int CMipsTestEngine::CInstance::GetId()
|
|
|
|
{
|
|
|
|
return m_nId;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* CMipsTestEngine::CInstance::GetSource()
|
|
|
|
{
|
|
|
|
return m_sSource.c_str();
|
|
|
|
}
|
|
|
|
|
2007-03-03 19:24:42 +00:00
|
|
|
////////////////////////////////////////////////////
|
|
|
|
// CValue implementation
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
CMipsTestEngine::CValue::~CValue()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
// CRegisterValue implementation
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
CMipsTestEngine::CRegisterValue::CRegisterValue(Xml::CNode* pNode)
|
|
|
|
{
|
|
|
|
const char* sName;
|
|
|
|
const char* sValue;
|
|
|
|
if(!Xml::GetAttributeStringValue(pNode, "Name", &sName))
|
|
|
|
{
|
|
|
|
throw runtime_error("RegisterValue: Couldn't find attribute 'Name'.");
|
|
|
|
}
|
|
|
|
|
2007-03-13 23:43:35 +00:00
|
|
|
m_nRegister = CMIPSAssembler::GetRegisterIndex(sName);
|
2007-03-03 19:24:42 +00:00
|
|
|
if(m_nRegister == -1)
|
|
|
|
{
|
|
|
|
throw runtime_error("RegisterValue: Invalid register name.");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_nValue0 = 0;
|
|
|
|
m_nValue1 = 0;
|
|
|
|
|
|
|
|
if(Xml::GetAttributeStringValue(pNode, "Value0", &sValue))
|
|
|
|
{
|
|
|
|
m_nValue0 = lexical_cast_hex<string>(sValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Xml::GetAttributeStringValue(pNode, "Value1", &sValue))
|
|
|
|
{
|
|
|
|
m_nValue1 = lexical_cast_hex<string>(sValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CMipsTestEngine::CRegisterValue::~CRegisterValue()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-03-13 23:43:35 +00:00
|
|
|
void CMipsTestEngine::CRegisterValue::AssembleLoad(CMIPSAssembler& Assembler)
|
2007-03-03 19:24:42 +00:00
|
|
|
{
|
2007-03-13 23:43:35 +00:00
|
|
|
if((m_nValue0 == 0) && (m_nValue1 == 0))
|
|
|
|
{
|
|
|
|
Assembler.ADDIU(m_nRegister, 0, 0x0000);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uint16 nHalf[4];
|
|
|
|
uint32 nSignExtension;
|
|
|
|
|
|
|
|
nHalf[0] = (m_nValue0 >> 0) & 0xFFFF;
|
|
|
|
nHalf[1] = (m_nValue0 >> 16) & 0xFFFF;
|
|
|
|
nHalf[2] = (m_nValue1 >> 0) & 0xFFFF;
|
|
|
|
nHalf[3] = (m_nValue1 >> 16) & 0xFFFF;
|
|
|
|
|
|
|
|
Assembler.LUI(m_nRegister, nHalf[1]);
|
|
|
|
Assembler.ORI(m_nRegister, m_nRegister, nHalf[0]);
|
|
|
|
|
|
|
|
nSignExtension = ((m_nValue0 & 0x80000000) == 0) ? (0x00000000) : (0xFFFFFFFF);
|
2007-03-03 19:24:42 +00:00
|
|
|
|
2007-03-13 23:43:35 +00:00
|
|
|
if(m_nValue1 != nSignExtension)
|
|
|
|
{
|
|
|
|
Assembler.DSRL(m_nRegister, m_nRegister, 4);
|
|
|
|
Assembler.ORI(m_nRegister, m_nRegister, nHalf[2]);
|
|
|
|
Assembler.DSRL(m_nRegister, m_nRegister, 4);
|
|
|
|
Assembler.ORI(m_nRegister, m_nRegister, nHalf[3]);
|
|
|
|
}
|
|
|
|
}
|
2007-03-03 19:24:42 +00:00
|
|
|
}
|
|
|
|
|
2007-03-14 01:28:22 +00:00
|
|
|
bool CMipsTestEngine::CRegisterValue::Verify(CMIPS& Context)
|
2007-03-03 19:24:42 +00:00
|
|
|
{
|
2007-03-14 01:28:22 +00:00
|
|
|
if(Context.m_State.nGPR[m_nRegister].nV[0] != m_nValue0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Context.m_State.nGPR[m_nRegister].nV[1] != m_nValue1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-03 19:24:42 +00:00
|
|
|
|
2007-03-14 01:28:22 +00:00
|
|
|
return true;
|
2007-03-03 19:24:42 +00:00
|
|
|
}
|