06-16-09-Mem t

From Msim

Jump to: navigation, search

This page discusses the code added to memory.c and memory.h to provide the output operator (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, const mem_t & target);
std::istream & operator >> (std::istream & in, const mem_t * target);

memory.c

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

#include<iomanip>

The output operator is defined as follows (input is stubbed here):

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, const mem_t * target)
{
 return operator>>(in,*target);
}

std::istream & operator >> (std::istream & in, const mem_t & target)
{
 
 return in;
}
Personal tools