home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Applications / Mic-1 v1.0 / Project and Source / Source / mic_memory.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-15  |  2.0 KB  |  104 lines  |  [TEXT/CWIE]

  1. /*
  2.     "mic_memory.cpp"
  3.     
  4.     This file contains Mic-1 objects which exist solely as memory, be it registers
  5.     or massive control store arrays.
  6. */
  7.  
  8. #include <iostream.h>
  9. #include <fstream.h>
  10. #include <iomanip.h>
  11.  
  12. #include "mic_main.h"
  13. #include "mic_memory.h"
  14. #include "mic_global_functions.h"
  15.  
  16. short InitMicMemory (Mic_1_Class& Mic)
  17. {
  18.     Str32 fileNameStr;
  19.     char *fileNameP;
  20.     GetIndString(fileNameStr, 129, 1);
  21.     fileNameP = P2CStr(fileNameStr);
  22.     ifstream MemInitFile(fileNameP);
  23.     
  24.     for (int n=0; n<kMaxRAM; n++)
  25.         Mic.Memory.putNthWord (n,0);
  26.             
  27.     if (MemInitFile.is_open())
  28.     {
  29.         MemInitFile >> Mic.Memory;
  30.     }
  31.     else
  32.     {
  33.         ofstream newMemFile(fileNameP);
  34.         if (newMemFile.is_open())
  35.         {
  36.             newMemFile << Mic.Memory;
  37.             return 130;
  38.         }
  39.         else
  40.             return 131;
  41.     }
  42.     return 0;
  43. }
  44.  
  45. Boolean MemoryClass::putNthWord (short n, unsigned short newWord)
  46. {
  47.     if ((n>=0) && (n<kMaxRAM))
  48.     {
  49.         mem[n] = newWord;
  50.         return true;
  51.     }
  52.     else
  53.         return false;
  54. }
  55.  
  56. void MemoryClass::output (Mic_1_Class& Mic)
  57. {
  58.     Mic.MBR.input_Memory(getNthWord(addr));
  59. }
  60.  
  61. ostream& operator << (ostream& s, MemoryClass& m)
  62. {
  63.     int extra = (kMaxRAM%10>0) ? 1:0;
  64.     int numRows = 10;
  65.     int numColumns;
  66.     
  67.     s << "MEMORY: " << endl;
  68.     for (int i=0; i<numRows; i++)
  69.     {
  70.         s << setw(4) << setfill(' ') << right << i*10 << ": ";
  71.         numColumns = (kMaxRAM - i*10 >= 10) ? 10:kMaxRAM - i*10;
  72.         s << hex << uppercase;
  73.         for (int j=0; j < numColumns; j++)
  74.         {
  75.             s << setw(4) << setfill('0') << right << m.getNthWord(i*10 + j);
  76.             if (j < 10 - 1)
  77.                 s << " ";
  78.         }
  79.         s << resetiosflags(ios::hex | ios::uppercase);
  80.         if (i < numRows - 1)
  81.             s << endl;
  82.     }
  83.     if (numRows < kMaxRAM/10 + extra)
  84.         s << endl << "<abbreviated>" << endl;
  85.     return s;
  86. }
  87.  
  88. istream& operator >> (istream& s, MemoryClass& m)
  89. {
  90.     unsigned short numwords, memword;
  91.     short counter = 0;
  92.     
  93.     s >> numwords;
  94.     while (counter < numwords)
  95.     {
  96.         s >> memword;
  97.         s >> hex >> uppercase >> memword;
  98.         s >> resetiosflags(ios::hex | ios::uppercase);
  99.         m.putNthWord(counter++, memword);
  100.     }
  101.     for (int i=counter; i<kMaxRAM; i++)
  102.         m.putNthWord(i, 0);
  103.     return s;
  104. }