Mano pdp8 VirtualMano.cc

From Minor Miracle Software
Jump to: navigation, search


VirtualMano.cc

// Virtual for Mano Basic Computer
//
// Changes to execute programs one at a time.


#ifndef VIRTUALMANO_CC
#define VIRTUALMANO_CC

#ifndef MANO_H
  #include "mano.h"
#endif

#ifndef ASSEMBLERMANO_CC
	#include "AssemblerMano.cc"
#endif

#ifndef COMPUTERMANO_CC
  #include "ComputerMano.cc"
#endif

#ifndef INSTRUCTIONWORD_CC
	#include "InstructionWord.cc"
#endif

#ifndef LIST_CC
  #include "list.cc"
#endif

#ifndef QUEUE_CC
  #include "queue.cc"
#endif

#ifndef CHAR_STRING_CC
  #include "charString.cc"
#endif

#ifndef SYMBOLTABLE_CC
  #include "SymbolTable.cc"
#endif

#ifndef PROGRAMLINE_CC
  #include "ProgramLine.cc"
#endif

#include <iostream.h>
#include <fstream.h>

class VirtualMano;

istream& operator >>( istream& IS, VirtualMano &VM );

class VirtualMano {

      int i_mEND;            // End program flag
      int i_mHLT;            // HLT=0 no HLT yet, HLT=1 HLT encountered
      int i_LocationCounter; // Location Counter (LC)

	AssemblerMano AM_AssemblerMano;
      ComputerMano Mano_Computer;
      list<SymbolTable> list_SymbolTable;
      queue<ProgramLine> queue_Program;
      queue<InstructionWord> queue_AssemblyCode;

friend istream& operator >>( istream& IS, VirtualMano &VM )
{
// Input portion

if( DEBUG ) cout << "VirtualMano: >> 0" << endl;

     if( IS )
     {
if( DEBUG ) cout << "VirtualMano: >> 1" << endl;

       VM.InPut( IS );
if( DEBUG ) cout << "VirtualMano: >> 2" << endl;

    }

if( DEBUG ) cout << "VirtualMano: >> 3" << endl;

    return IS;
} // >>




void ErrorHandler( int i_Error, short s_location )
{
if( DEBUG ) cout << " VirtualMano: ErrorHandler 0" << endl;

     switch( i_Error )
     {
             case ADDRESS_OVER_MAX:
                  cout << " ERROR: Address greater than 4095. Line Number " << s_location << endl;
                  break;
             case ADDRESS_UNDER_MIN:
                  cout << " ERROR: Address less than 0. Line Number " << s_location << endl;
                  break;
             case INVALID_COMMAND:
                  cout << " ERROR: Invalid Command. Line Number " << s_location << endl;
                  break;
             case INVALID_DEC_INSTRUCTION:
                  cout << " ERROR: Invalid DEC Instruction. Line Number " << s_location << endl;
                  break;
             case INVALID_END_INSTRUCTION:
                  cout << " ERROR: Invalid END Instruction. Line Number " << s_location << endl;
                  break;
             case INVALID_HEX_INSTRUCTION:
                  cout << " ERROR: Invalid HEX Instruction. Line Number " << s_location << endl;
                  break;
             case INVALID_HLT_INSTRUCTION:
                  cout << " ERROR: Invalid HLT Instruction. Line Number " << s_location << endl;
                  break;
             case INVALID_LABEL:
                  cout << " ERROR: Invalid Label. Line Number " << s_location << endl;
                  break;
             case INVALID_NUMBER_FORMAT:
                  cout << " ERROR: Invalid Number Format. Line Number " << s_location << endl;
                  break;
             case INVALID_OCT_INSTRUCTION:
                  cout << " ERROR: Invalid OCT Instruction. Line Number " << s_location << endl;
                  break;
             case INVALID_ORG_INSTRUCTION:
                  cout << " ERROR: Invalid ORG Instruction. Line Number " << s_location << endl;
                  break;
             case INVALID_PSEUDO_INSTRUCTION:
                  cout << " ERROR: Invalid Pseudo Instruction. Line Number " << s_location << endl;
                  break;
             case MEMORY_LOAD_ERROR:
                  cout << " ERROR: Error Allocating Memory. Line Number " << s_location << endl;
                  break;
             case NO_END:
                  cout << " ERROR: No END Command. Line Number " << s_location << endl;
                  break;
             case NO_HALT:
                  cout << " ERROR: No HLT Command. Line Number " << s_location << endl;
                  break;
             case NON_PSEUDO_INSTRUCTION:
                  cout << " ERROR: NON Pseudo Instruction. Line Number " << s_location << endl;
                  break;
             case NUMBER_OVER_MAX:
                  cout << " ERROR: Number Over 32768. Line Number " << s_location << endl;
                  break;
             case NUMBER_UNDER_MINUS_MIN:
                  cout << " ERROR: Number Less Than -32767. Line Number " << s_location << endl;
                  break;
             case ORG_OVER_4095:
                  cout << " ERROR: ORG Past Address 4095. Line Number " << s_location << endl;
                  break;
             case ORG_UNDER_0:
                  cout << " ERROR: ORG Under Address 0. Line Number " << s_location << endl;
                  break;
             case TOO_MANY_LINES:
                  cout << " ERROR: More than 4096 code lines. line number " << s_location << endl;
                  break;
             case VALID_DEC_INSTRUCTION:
                  cout << " ERROR: Valid DEC Instruction. Line Number " << s_location << endl;
                  break;
             case VALID_END_INSTRUCTION:
                  cout << " ERROR: Valid END Instruction line number " << s_location << endl;
                  break;
             case VALID_HEX_INSTRUCTION:
                  cout << " ERROR: Valid HEX Instruction line number " << s_location << endl;
                  break;
             case VALID_HLT_INSTRUCTION:
                  cout << " ERROR: Valid HLT Instruction line number " << s_location << endl;
                  break;
             case VALID_OCT_INSTRUCTION:
                  cout << " ERROR: Valid OCT Instruction line number " << s_location << endl;
                  break;
             case VALID_PSEUDO_INSTRUCTION:
                  cout << " ERROR: Valid Pseudo Instruction line number " << s_location << endl;
                  break;
     default:
         ;
     }
if( DEBUG ) cout << " VirtualMano: ErrorHandler 1" << endl;

} // ErrorHandler


	void manual()
	{
		// manual for Virtual Mano Computer
		if( DEBUG ) cout << " VirtualMano: manual 0" << endl;

        cout << "TABLE 5,6 Control Functions and Microoperations for the Basic Computer" << endl;
	    cout << "Fetch                    R'T0: AR<-PC"  << endl;
	    cout << "                         R'T1: IR <- M[AR], PC<-PC + 1" << endl;
	    cout << endl;
	    cout << "Decode	               R'T2: D0,...,D7<-Decode IR(12-14)," << endl;
	     cout << "                               AR<-IR(O-11), I<-IR(15)" << endl;
	     cout << "Indirect	           D'7IT3:	AR<-M[AR]" << endl;
	     cout << "Interrupt:" << endl;
	     cout << "	T'0T'1T'2(IEN)(FGI + FGO):	R<-l" << endl;
	     cout << "	                      RT0:  AR<-O, TR<-PC" << endl;
	     cout << "                        RT1:  M[AR]<-TR, PC<-O" << endl;
	     cout << "                        RT2:  PC<-PC+l, IEN<-O, R<-O, SC<-0" << endl;
	     cout << "Memory-reference:" << endl;
	     cout << " AND                     DoT4:  DR <-M[AR]" << endl;
	     cout << "                         DoT5:  AC-ACADR, SC<-O" << endl;
	     cout << " ADD                     D1T4:  DR<-M[AR]" << endl;
	     cout << "                         D1T5:  AC-AC+DR, E<-cout, SC<-0" << endl;
	     cout << " LDA                     D2T4:  DR<-M[AR]" << endl;
	     cout << "                         D2T5:  AC<-DR, SC<-0" << endl;
	     cout << " STA                     D3T4:  M[AR]<-AC, SC<-0" << endl;
	     cout << " BUN                     D4T4:  PC<-AR, SC<-0" << endl;
	     cout << " BSA                     D5T4:  M[AR]<-PC, AR<-AR+1" << endl;
	     cout << "                         D5T5:  PC<-AR, SC<-0" << endl;
	     cout << " ISZ                     D6T4:  DR<-M[AR]" << endl;
	     cout << "                         D6T5:  DR<-DR+1" << endl;
	     cout << "                         D6T6:  M[AR]<-DR, if(DR=0)then(PC<-PC+1), SC<-0" << endl;
	     cout << "Register-reference:" << endl;
	     cout << endl;
	     cout << "                       D7I'T3 = r (common to all register-reference instructions)" << endl;
	     cout << "                       IR(i) = Bi (i = 0,1,2,...,11)" << endl;
	     cout << "                            r:  SC<-0" << endl;
	     cout << " CLA                     rB11:  AC-0" << endl;
	     cout << " CLE                     rB10:  E<-0" << endl;
	     cout << " CMA                      rB9:  AC<-AC (complement)" << endl;
	     cout << " CME                      rB8:  E<-E (complement)" << endl;
	     cout << " CIR                      rB7:  AC-shr AC, AC(15)<-E, E<-AC(0)" << endl;
	     cout << " CIL                      rB6:  AC<-shl AC, AC(0)<-E, E<-AC(15)" << endl;
	     cout << " INC                      rB5:  AC<-AC + 1" << endl;
	     cout << " SPA                      rB4:  If (AC(15) = 0) then (PC <-PC + 1)" << endl;
	     cout << " SNA                      rB3:	If (AC(15) = 1) then (PC <-PC + 1)" << endl;
	     cout << " SZA                      rB2:	If (AC = 0) then PC<-PC + 1)" << endl;
	     cout << " SZE                      rB1:	If (E = 0) then (PC<-PC + 1)" << endl;
	     cout << " HLT                      rB0:	S<-0" << endl;
	     cout << "Input-0utput:" << endl;
	     cout << "                        D7IT3 = p (common to all input-output instructions)" << endl;
	     cout << "                        IR(i) = Bi (i = 6,7,8,9,10,11)" << endl;
	     cout << "                            p:   SC-0" << endl;
	     cout << " INP                      pB11:	AC(0-7)<-INPR, FGI<-0" << endl;
	     cout << " 0UT                      pB10:	0UTR<-AC(0-7), FG0<-0" << endl;
	     cout << " SKI                      pB9:  If (FGI = 1) then (PC<-PC + 1)" << endl;
	     cout << " SK0                      pB8:	If (FG0	1) then (PC<-PC + 1)" << endl;
	     cout << " I0N                      pB7:	IEN<-1" << endl;
	     cout << " I0F                      pB6:	IEN<-0" << endl;
	}



public:

	VirtualMano()
	{
	     i_mEND = 0;
	     i_mHLT = 0;
	     i_LocationCounter = 0;
	}

	~VirtualMano(){}

	VirtualMano(const VirtualMano& M )
	{
	      i_mEND = M.i_mEND;
	      i_mHLT = M.i_mHLT;
	      i_LocationCounter = M.i_LocationCounter;

	      Mano_Computer = M.Mano_Computer;
	      list_SymbolTable = M.list_SymbolTable;
	      queue_Program = M.queue_Program;
	      queue_AssemblyCode = M.queue_AssemblyCode;
	}


	istream& InPut( istream& IFstream )
	{
	
	      short s_LineCounter = 0;     // Counts Absolute Program Lines
                                     // including comments and blanks.
          short s_ProgramLine = 0;   // Code lines

		// Input portion
		if( DEBUG ) cout << " Virtual: InPut 0" << endl;

	    if( IFstream.good() )
	    {
	        while( IFstream.good() )
	        {
		        s_LineCounter++; // increment file line counter
	            charString charString_Temp;
if( DEBUG ) cout << " Virtual: InPut 0" << endl;
	            IFstream >> charString_Temp;

                charString_Temp.purge();
if( DEBUG ) cout << " Virtual: InPut 1.0 " << charString_Temp << endl;
	            if( charString_Temp.Search( "/" ) ) // skip comments
	                continue;
if( DEBUG ) cout << " Virtual: InPut 1.1 " << charString_Temp << endl;
				
				charString_Temp.trim(); // clear white space
if( DEBUG ) cout << " Virtual: InPut 1.2 " << charString_Temp << endl;

	            if( charString_Temp.length() == 0 ) // skip blanks
	                continue;
if( DEBUG ) cout << " Virtual: InPut 1.3" << charString_Temp << endl;

				s_ProgramLine++;
				
				ProgramLine PL = ProgramLine( charString_Temp, s_ProgramLine, s_LineCounter );
if( DEBUG ) cout << " Virtual: InPut 1.4 "  << endl;
				
                queue_Program.enqueue( PL );
if( DEBUG ) cout << " Virtual: InPut 1.5 "  << endl;
	         } // while loop

if( DEBUG ) cout << " Virtual: InPut 3.0 Program Lines " << s_ProgramLine << endl;
if( DEBUG ) cout << " Virtual: InPut 3.1 queue size " << queue_Program.census() << endl;
             if( AM_AssemblerMano.AssembleProgram( queue_Program ) )
             {
				if( DEBUG ) cout << " Virtual: InPut 3.2" << endl;
				queue_AssemblyCode = AM_AssemblerMano.GetAssemblyCode();

				if( DEBUG ) cout << " Virtual: InPut 3.3 " << queue_AssemblyCode.census() << endl;
				Mano_Computer.load( queue_AssemblyCode  );
    	        Mano_Computer.run();
    	     }
			if( DEBUG ) cout << " Virtual: InPut 4" << endl;
	    }
//	    else
//	        manual( );

	// Output portion
//	    cout << Mano_Computer;
		if( DEBUG ) cout << " Virtual: InPut 5 " << s_LineCounter << " lines" << endl;

	    return IFstream;
	} // InPut


	void OutPut()
	{
		if( DEBUG )
		cout << " VirtualMano: OutPut 0" << endl;

	    cout << queue_Program;
	    cout << Mano_Computer;
	} // OutPut
};
#endif // VirtualMano_CC

Internal Links

Parent Article: Mano PDP-8 Simulation Project Source Code