07-13-09-Regs t

From Msim

Jump to: navigation, search

This page discusses the code added to regs.c and regs.h to provide the input and output operator (operator>> and operator<<) for regs_t. The operator output matches the output desired by the eio requirements.

regs.h

The following prototypes must be provided in regs.h

std::ostream & operator << (std::ostream & out, const regs_t & source);
std::istream & operator >> (std::istream & in, regs_t & target);

regs.c

In order to output data in hex we use the iomanip library.

#include<iomanip>

The operators are defined as follows:

std::ostream & operator << (std::ostream & out, const regs_t & source)
{
 out << "/* misc regs icnt, PC, NPC, etc... */" << std::endl;
 
 //sim_num_insn is always zero... instructions counts can't be stored here.
 out << "(" << 0 << ", ";
 
 //regs_PC, regs_NPC, regs_C.fpcr, regs_C.uniq
 out << "0x" << std::hex << source.regs_PC << ", ";
 out << "0x" << source.regs_NPC << ", ";
 out << std::dec << source.regs_C.fpcr << ", ";
 out << source.regs_C.uniq << ")" << std::endl;
 
 out << std::endl;
 out << "/* integer regs */" << std::endl;
 out << "(";
 for(unsigned int i = 0;i<source.regs_R.size();i++)
 {
  out << "0x" << std::hex << source.regs_R[i];
  if((i+1)<source.regs_R.size())
  {
   out << ", ";
  }
 }
 out << ")" << std::endl;
 
 out << std::endl;
 out << "/* FP regs (integer format) */" << std::endl;
 out << "(";
 for(unsigned int i = 0;i<source.regs_F.size();i++)
 {
  out << "0x" << std::hex << source.regs_F[i].q;
  if((i+1)<source.regs_F.size())
  {
   out << ", ";
  }
 }
 out << ")" << std::endl;
 
 out << std::dec << std::endl;
 return out;
}

std::istream & operator >> (std::istream & in, regs_t & target)
{
 std::string misc_header("/* misc regs icnt, PC, NPC, etc... */");
 std::string int_header("/* integer regs */");
 std::string fp_header("/* FP regs (integer format) */");
 
 std::string buf;
 char c_buf;
 std::getline(in,buf);
 
 if(buf!=misc_header)
 {
  std::cout << "Malformed EIO file, could not read initial header, read: " << buf << std::endl;
  std::cout << "Needed to read: " << misc_header << std::endl;
  exit(-1);
 }
 
 in >> buf;
 if(buf!="(0,")
 {
  std::cout << "Failed reading sim_num_insn from control register data" << std::endl;
  exit(-1);
 }
 
 in >> std::hex >> target.regs_PC >> c_buf;
 in >> std::hex >> target.regs_NPC >> c_buf;
 in >> std::dec >> target.regs_C.fpcr >> c_buf;
 in >> std::dec >> target.regs_C.uniq;
 std::getline(in,buf);
 std::getline(in,buf);
 std::getline(in,buf);
 
 if(buf!=int_header)
 {
  std::cout << "Malformed EIO file, could not read int reg header, read: " << buf << std::endl;
  std::cout << "Needed to read: " << int_header << std::endl;
  exit(-1);
 }
 in >> c_buf;
 
 for(unsigned int i = 0;i<target.regs_R.size();i++)
 {
  in >> std::hex >> target.regs_R[i];
  if((i+1)<target.regs_R.size())
  {
   in >> c_buf;
  }
 }
 in >> std::dec;
 std::getline(in,buf);
 std::getline(in,buf);
 std::getline(in,buf);
 
 if(buf!=fp_header)
 {
  std::cout << "Malformed EIO file, could not read fp reg header, read: " << buf << std::endl;
  std::cout << "Needed to read: " << fp_header << std::endl;
  exit(-1);
 }
 in >> c_buf;
 
 for(unsigned int i = 0;i<target.regs_F.size();i++)
 {
  in >> std::hex >> target.regs_F[i].q;
  if((i+1)<target.regs_F.size())
  {
   in >> c_buf;
  }
 }
 in >> std::dec;
 std::getline(in,buf);
 std::getline(in,buf);
 return in;
}
Personal tools