07-13-09-Mem t

From Msim

Jump to: navigation, search

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

memory.h

The following prototypes must be provided in memory.h

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

memory.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 mem_t * source)
{
 return operator<<(out,*source);
}
std::ostream & operator << (std::ostream & out, const mem_t & source)
{
 out << "/* writing `" << (int)source.page_count << "' memory pages... */" << std::endl;
 out << "(" << source.page_count << ", 0x" << std::hex << source.ld_brk_point << ", 0x" << std::hex << source.ld_stack_min << ")" << std::endl;

 out << std::endl;
 out << "/* text segment specifiers (base & size) */" << std::endl;
 out << "(0x" << std::hex << source.ld_text_base << ", " << std::dec << source.ld_text_size << ")" << std::endl;

 out << std::endl;
 out << "/* data segment specifiers (base & size) */" << std::endl;
 out << "(0x" << std::hex << source.ld_data_base << ", " << std::dec << source.ld_data_size << ")" << std::endl;

 out << std::endl;
 out << "/* stack segment specifiers (base & size) */" << std::endl;
 out << "(0x" << std::hex << source.ld_stack_base << ", " << std::dec << source.ld_stack_size << ")" << std::endl;

 out << std::endl;
 for(unsigned int i = 0;i<source.ptab.size();i++)
 {
  for(mem_pte_t *pte=source.ptab[i];pte;pte=pte->next)
  {
   out << "(0x";
   unsigned long long mem_pte_addr((pte->tag << (MD_LOG_PAGE_SIZE + MEM_LOG_PTAB_SIZE)) | (i << MD_LOG_PAGE_SIZE));
   out << std::hex << mem_pte_addr << ", ";
   //out << "{" << pte->page << "}<\n";
   out << "{" << std::dec << MD_PAGE_SIZE << "}<\n";

   bool newline(false);
   for(unsigned int j=0;j<MD_PAGE_SIZE;j++)
   {
    if((j!=0) && ((j%38)==0))
    {
     out << "\n";
     newline = true;
    }
    unsigned int toprint = (unsigned int)pte->page[j];
    out << std::hex << (toprint/16) << std::hex << (toprint%16);
   }
   if(!newline)
   {
    out << "\n";
   }
   out << ">)\n" << std::dec << std::endl;
  }
 }
 return out;
}
std::istream & operator >> (std::istream & in, mem_t * target)
{
 return operator>>(in,*target);
}

std::istream & operator >> (std::istream & in, mem_t & target)
{
 std::string mem_header("' memory pages... */");
 std::string text_header("/* text segment specifiers (base & size) */");
 std::string data_header("/* data segment specifiers (base & size) */");
 std::string stack_header("/* stack segment specifiers (base & size) */");
 
 std::string buf;
 char c_buf;
 in >> buf >> buf >> c_buf >> target.page_count;
 getline(in,buf);
 if(buf!=mem_header)
 {
  std::cerr << "Failed reading memory header, read: " << buf << std::endl;
  std::cerr << "Wanted to read: " << mem_header << std::endl;
  exit(-1);
 }
 in >> c_buf >> target.page_count >> c_buf >> std::hex >> target.ld_brk_point >> c_buf >> std::hex >> target.ld_stack_min;
 getline(in,buf);
 getline(in,buf);
 getline(in,buf);
 if(buf!=text_header)
 {
  std::cerr << "Failed reading text header, read: " << buf << std::endl;
  std::cerr << "Wanted to read: " << text_header << std::endl;
  exit(-1);
 }
 in >> c_buf >> std::hex >> target.ld_text_base >> c_buf >> std::dec >> target.ld_text_size;
 getline(in,buf);
 getline(in,buf);
 getline(in,buf);
 if(buf!=data_header)
 {
  std::cerr << "Failed reading data header, read: " << buf << std::endl;
  std::cerr << "Wanted to read: " << data_header << std::endl;
  exit(-1);
 }
 in >> c_buf >> std::hex >> target.ld_data_base >> c_buf >> std::dec >> target.ld_data_size;
 getline(in,buf);
 getline(in,buf);
 getline(in,buf);
 if(buf!=stack_header)
 {
  std::cerr << "Failed reading stack header, read: " << buf << std::endl;
  std::cerr << "Wanted to read: " << stack_header << std::endl;
  exit(-1);
 }
 in >> c_buf >> std::hex >> target.ld_stack_base >> c_buf >> std::dec >> target.ld_stack_size;
 getline(in,buf);
 getline(in,buf);
 
 int page_count(target.page_count);
 for(int i=0; i < page_count; i++)
 {
  in >> c_buf;
  unsigned long long page_addr;
  in >> std::hex >> page_addr >> c_buf >> c_buf;
  unsigned long long page_size;
  in >> std::dec >> page_size;
  if(page_size!=MD_PAGE_SIZE)
  {
   std::cerr << page_size << " does not match " << MD_PAGE_SIZE << std::endl;
   exit(-1);
  }
  getline(in,buf);
  
  for(int j=0; j < MD_PAGE_SIZE; j++)
  {
   char t[3];
   t[2] = NULL;
   in >> t[0] >> t[1];
   unsigned int val = strtol(t,NULL,16);
   //unchecked access...
   MEM_WRITE_BYTE(&target, page_addr, val);
   page_addr++;
  }
  in >> c_buf;
  if(c_buf!='>')
  {
   std::cerr << "Failed finding end of memory page" << std::endl;
   exit(-1);
  }
  getline(in,buf);
  getline(in,buf);
 }
 return in;
}
Personal tools