Exemple #1
0
// constructor / destructor:
Processor::Processor()
    : Trace(0)
{
    Accumulator(0);
    setInstructionCounter(0);
    InstructionRegister(0);
    setOperationCode(0);
    setOperand(0); // this is an address of cell in the current command
}
Exemple #2
0
void Processor::RunMemoryProg(int _n_trace)
    /*
      MemCell* memory //pointer to an array!,
      Processor* pProc,
      int Trace*/
{
    int  StopFlag = 0;
    char cInput[20];
    char cInTrace[20];

    Trace = _n_trace;
    setInstructionCounter(START_ADDRESS); // set counter to the start == 0
 
    do  
     {
        // copy next memory cell to the Register:
        this->InstructionRegister(memory->GetCellValue(this->InstructionCounter()) );
        // get an operation code and operand from Register:
        this->GetCOPO(); 
        // save an address of memory cell where data is:        
        memory->SetCellCounter(Operand());

        switch ( this->OperationCode() )
        {
         case READ: // 10 - read a word from the terminal to the memory cell
            printf (" Enter value: > ");
            gets(cInput);
            memory->SetCellValue(cInput); //
            this->incInctructionCounter();
            break;

         case WRITE:// 11 - print a word from the memory cell to the terminal
            // memory->PrintCurCellNumber(); 
            printf ("Output: \n");
            printf ("**************\n");
            memory->PrintCellValue();
            printf ("\n**************\n\n");
            this->incInctructionCounter();
            break;
         
         case LOAD: // 20 - save a word from the memory cell to an accumulator
            this->Accumulator( memory->GetCellValue() ); 
            this->incInctructionCounter();
            break;

        case STORE: // 21 - //save a word from accumulator to memory cell
            memory->SetCellValue(this->Accumulator()); 
            this->incInctructionCounter();
            break;

         case ADD:
            this->Accumulator(this->Accumulator() + memory->GetCellValue());
            this->incInctructionCounter();
            break;

         case SUBTRACT:
            this->Accumulator(this->Accumulator() - memory->GetCellValue()); //
            this->incInctructionCounter();
            break;

         case DIVIDE:
            if (this->Accumulator() == 0)
            {
                printf ("ERROR: divide to zero!\n");
                printf ("Program complete by error.\n");
                StopFlag = 1;
            }
            else
            {
                this->Accumulator(memory->GetCellValue() / this->Accumulator());
            }            
            this->incInctructionCounter();
            break;

         case MULTIPLY:
            this->Accumulator(this->Accumulator() * memory->GetCellValue()); //
            this->incInctructionCounter();
            break; 

         case BRANCH: //GOTO
            this->setInstructionCounter(Operand());
            break;

         case BRANCHNEG: // branch if accumulator is negative
            if (this->Accumulator() < 0) 
                this->setInstructionCounter(Operand());
            else
                this->incInctructionCounter();
            break;

         case BRANCHZERO:
            if (this->Accumulator() == 0) 
                this->setInstructionCounter(Operand());
            else
                this->incInctructionCounter();
            break;

         case HALT:
            printf ("Program complete.\n");
            StopFlag = 1;
            break;      
                
         default: 
            printf ("Unexpected value in the address %d\n", Operand());
            StopFlag = 1;
        }// case
        
        if (Trace)
        {
          memory->PrintMemoryDump ();
          this->PrintProcState();
          printf("\nPress Enter to TRACE >> ");
          gets(cInTrace);
        }

    } while (!StopFlag);

    this->PrintProcState();

};
/*************************************************************************
* Date last modified: July 9th, 2013
* Description: Runs program that has already been loaded into memory
* Input parameters: 
* Returns: 
* Precondition: Valid program exists in memory
* Postcondition: Valid result is printed to console
*************************************************************************/
void SimpletronSimulator::runProgram(Simpletron &program)
{
	setAccumulator(0);
	setInstructionCounter(0);
	instructionRegister = program.memory[instructionCounter];
	operationCode = program.getInstruction(instructionRegister);
	while(instructionCounter != -9999 && operationCode != 43)
	{
		operand = program.getLocation(instructionRegister);
		switch(operationCode)
		{
			case 10: // Read word from terminal into memory location
				program.read(operand);
				break;
			case 11: // Write a word from memory location to console
				program.write(operand);
				cout << endl;
				break;
			case 20: // Load a word from memory location into accumulator
				accumulator = program.load(operand);
				break;
			case 21: //Store a word from accumulator into memory location
				program.store(operand, accumulator);
				break;
			case 30: // Add a word from memory location to word in accumulator
				accumulator += program.memory[operand];
				break;
			case 31: // Subtract a word from memory location from word in accumulator
				accumulator -= program.memory[operand];
				break;
			case 32: // Divide a word from memory location into word in accumulator
				if(program.memory[operand] != 0)
				{
					accumulator /= program.memory[operand];
				}
				else
				{
					cout << "***Fatal Error: Attempt to divide by zero***" << endl;
					cout << "***Simpletron execution abnormally terminated***" << endl;
					instructionCounter = -9999;
				}
				break;
			case 33: // Multiply word from memory location with word in accumulator
				accumulator *= program.memory[operand];
				break;
			case 34: // Mod word from memory location into word in accumulator
				accumulator %= program.memory[operand];
				break;
			case 35: // Calculate accumulator to the power of word in memory location
				accumulator = (int)pow(accumulator, program.memory[operand]);
				break;
			case 40: // Branch to a specific location in memory
				instructionCounter = operand-1;
				break;
			case 41: // If accumulator is negative branch to memory location
				if(accumulator < 0)
				{
					instructionCounter = operand-1;
				}
				break;
			case 42: // If accumulator is zero branch to memory location
				if(accumulator == 0)
				{
					instructionCounter = operand-1;
				}
				break;
		}
		if(instructionCounter != -9999)
		{
			instructionCounter++;
			instructionRegister = program.memory[instructionCounter];
			operationCode = program.getInstruction(instructionRegister);
		}
	}
	if(instructionCounter != -9999)
	{
		cout << "***Simpletron Execution Terminated***" << endl;
	}
}