Ejemplo n.º 1
0
bool ControlUnit::processControlInstruction(){
	int operation = iRInput.read().range(13,9).to_int();
	int src1 = iRInput.read().range(5,3).to_int();
	switch(operation){
		case 0: // PC = address
			{
				//cout<<__FILE__<<"::"<<__LINE__<<endl;

				loadsARWithPc();

				resetAllLoads();

				//PC = DR
				//cout<<"\nPC = DR\n";
				ulaOp = ulaOperation_doNothing;
				ulaInAMuxSel = UlaInputSelection_DR; //DR
				loadRA = 1;
				wait(1); //espera RA
				ulaOutDemuxSel = UlaOutputSelection_PC; //PC
				loadPC = 1; //espera PC
				wait(1);
				return true;
			}
			break;
		case 1: // if status[rel] == 1 then PC = Address end
			{
				//cout<<__FILE__<<"::"<<__LINE__<<endl;
				if(statusBit){
					
					loadsARWithPc();

					resetAllLoads();

					//PC = DR
					//cout<<"\nPC = DR\n";
					ulaOp = ulaOperation_doNothing;
					ulaInAMuxSel = UlaInputSelection_DR; //DR
					loadRA = 1;
					wait(1); //espera RA
					ulaOutDemuxSel = UlaOutputSelection_PC; //PC
					loadPC = 1; //Loads PC
					wait(1); //espera PC

					return true;				
				}
				else{
					//PC = PC + 1 para pular o address
					//
					//cout<<"\nstatus bit not set PC = PC +1\n";
					incrementPC();
				}
				return true;
			}
			break;
	}
	return false;
}
Ejemplo n.º 2
0
void ControlUnit::controlUnitBehavior(){
	while(true){
		resetAllLoads();
		//cout<<"\nchecking actual state\n"<<endl;
		switch(state){
			
			/*instruction fetching IR = mem[PC] begin*/
			case 0:
				//AR = PC
				//cout<<"\nfetching new instruction 1.1\n"<<endl;
				loadsARWithPc();
				state ++;
				break;
			case 1:
				//IR = DR
				//cout<<"\nfetching new instruction 1.2\n"<<endl;
				ulaOp = ulaOperation_doNothing;
				ulaInAMuxSel = UlaInputSelection_DR; //DR
				ulaOutDemuxSel = UlaOutputSelection_IR; //IR
				loadRA = 1; //loads RA = DR
				wait(1);
				loadIR = 1; //Loads IR = RA
				wait(1);
				state ++;
				break;
				/*instruction fetching IR = mem[PC] end*/
			case 2:
				/*increment pc PC = PC+1 begin*/
				incrementPC();
				state ++;
				break;
				/*increment pc PC = PC+1 end*/
			case 3:
				//cout<<"\nprocess instruction\n"<<endl;
				if(processInstruction())
					state = 0;
				break;
		}
	}
}
Ejemplo n.º 3
0
bool ControlUnit::processMemoryInstruction(){
	
	int operation = iRInput.read().range(13,9).to_int();
	//cout<<__FILE__<<"::"<<__LINE__<<endl;
	switch (operation){
		case 0: //RF[dest] = address
			{
				//cout<<__FILE__<<"::"<<__LINE__<<endl;
				int dest = iRInput.read().range(8,6).to_int();
				//guarda o endereço onde será guardado address
				rfSel = dest; 

				//pega o dado de AR em pc atual
				loadsARWithPc();

				//RF[dest] = PC
				ulaOp = ulaOperation_doNothing;
				ulaInAMuxSel = UlaInputSelection_DR; //Address
				loadRA = 1;
				wait(1); //espera RA
				ulaOutDemuxSel = UlaOutputSelection_RF; //RF
				rfReadWriteBit = 1; //carrega RF com o valor em rfSel
				wait(1);//espera RF
				resetAllLoads();
			
				incrementPC();

				return true;
			}
			break;
		case 1: //RF[dest] = Mem[address]
			{
				//cout<<__FILE__<<"::"<<__LINE__<<endl;
				//guarda o endereço onde será guardado address
				int dest = iRInput.read().range(8,6).to_int();
				rfSel = dest; 

				loadsARWithPc();
				
				resetAllLoads();
				
				//RA = DR
				ulaOp = ulaOperation_doNothing;
				ulaInAMuxSel = UlaInputSelection_DR; //DR
				loadRA = 1;
				wait(1);//espera RA

				//AR = DR
				ulaOutDemuxSel = UlaOutputSelection_AR; //AR
				loadAR = 1; //Loads AR
				wait(1); //wait for AR
				writeMemory = 0; //read from memory
				loadDR = 1;
				wait(1);//espera DR
				wait(1);//delay da memoria

				resetAllLoads();

				//RF[dest] = DR
				ulaOp = ulaOperation_doNothing;
				ulaInAMuxSel = UlaInputSelection_DR; //DR
				loadRA= 1; //Loads RA
				wait(1);//espera RA

				ulaOutDemuxSel = UlaOutputSelection_RF; //RF
				rfReadWriteBit = 1; //carrega RF com o valor em rfSel
				wait(1); //espera RF

				incrementPC();

				return true;
			}
			break;
		case 2: //RF[dest] = Mem[Mem[address]]
			{
				//guarda o endereço onde será guardado address
				int dest = iRInput.read().range(8,6).to_int();
				rfSel = dest; 

				loadsARWithPc();
				
				resetAllLoads();
				
				//RA = DR
				ulaOp = ulaOperation_doNothing;
				ulaInAMuxSel = UlaInputSelection_DR; //DR
				loadRA = 1;
				wait(1);//espera RA

				//AR = DR
				ulaOutDemuxSel = UlaOutputSelection_AR; //AR
				loadAR = 1; //Loads AR
				wait(1); //wait for AR
				writeMemory = 0; //read from memory
				loadDR = 1;
				wait(1);//espera DR
				wait(1);//delay da memoria

				resetAllLoads();

				//RA = DR
				ulaOp = ulaOperation_doNothing;
				ulaInAMuxSel = UlaInputSelection_DR; //DR
				loadRA = 1;
				wait(1);//espera RA

				//AR = DR
				ulaOutDemuxSel = UlaOutputSelection_AR; //AR
				loadAR = 1; //Loads AR
				wait(1); //wait for AR
				writeMemory = 0; //read from memory
				loadDR = 1;
				wait(1);//espera DR
				wait(1);//delay da memoria

				//RF[dest] = DR
				ulaOp = ulaOperation_doNothing;
				ulaInAMuxSel = UlaInputSelection_DR; //DR
				loadRA= 1; //Loads RA
				wait(1);//espera RA

				ulaOutDemuxSel = UlaOutputSelection_RF; //RF
				rfReadWriteBit = 1; //carrega RF com o valor em rfSel
				wait(1); //espera RF

				incrementPC();

				return true;
			}
			break;
		case 3: //Mem[address] = RF[src1]
			{
				//guarda o endereço onde será guardado address
				int src1 = iRInput.read().range(5,3).to_int();
				rfSel = src1; 

				loadsARWithPc();

				resetAllLoads();

				//RA = DR
				ulaOp = ulaOperation_doNothing;
				ulaInAMuxSel = UlaInputSelection_DR; //DR
				loadRA = 1;
				wait(1);//espera RA

				//AR = DR
				ulaOutDemuxSel = UlaOutputSelection_AR; //AR
				loadAR = 1; //Loads AR
				wait(1);

				resetAllLoads();
				
				//DR = RF[rfSel]
				ulaOp = ulaOperation_doNothing;
				ulaInAMuxSel = UlaInputSelection_RF; //RF
				loadRA = 1;
				wait(1);

				ulaOutDemuxSel = UlaOutputSelection_DR; //DR
				dRinMuxSel = 0;
				rfReadWriteBit = 0; //lê valor de RF[rfSel]
				loadDR = 1; //Loads DR
				wait(1);

				resetAllLoads();
				//Mem[AR] = DR
				writeMemory = 1;
				dRoutDemuxSel = 1;

				wait(1);
				wait(1);//delay da memoria
				
				incrementPC();

				return true;
			}
			break;
		case 4: //Mem[Mem[address]] = RF[src1]
			{
				//guarda o endereço onde será guardado address
				int src1 = iRInput.read().range(5,3).to_int();
				rfSel = src1; 

				loadsARWithPc();

				resetAllLoads();

				//cout<<"\nAR = DR\n";
				//RA = DR
				ulaOp = ulaOperation_doNothing;
				ulaInAMuxSel = UlaInputSelection_DR; //DR
				loadRA = 1;
				wait(1);//espera RA

				//AR = DR
				ulaOutDemuxSel = UlaOutputSelection_AR; //AR
				loadAR = 1; //Loads AR
				wait(1); //wait for AR
				writeMemory = 0; //read from memory
				loadDR = 1;
				wait(1);//espera DR
				wait(1);//delay da memoria

				resetAllLoads();

				//cout<<"\nAR = DR\n";
				//RA = DR
				ulaOp = ulaOperation_doNothing;
				ulaInAMuxSel = UlaInputSelection_DR; //DR
				loadRA = 1;
				wait(1);//espera RA

				//AR = DR
				ulaOutDemuxSel = UlaOutputSelection_AR; //AR
				loadAR = 1; //Loads AR
				wait(1); //wait for AR
				writeMemory = 0; //read from memory
				loadDR = 1;
				wait(1);//espera DR
				wait(1);//delay da memoria


				resetAllLoads();

				//cout<<"\nDR = RF[src1]\n";
				//DR = RF[rfSel]
				ulaOp = ulaOperation_doNothing;
				ulaInAMuxSel = UlaInputSelection_RF; //RF
				loadRA = 1;
				wait(1);

				ulaOutDemuxSel = UlaOutputSelection_DR; //DR
				dRinMuxSel = 0;
				rfReadWriteBit = 0; //lê valor de RF[rfSel]
				loadDR = 1; //Loads DR
				wait(1);

				resetAllLoads();
				//Mem[AR] = DR
				//cout<<"\nmem[AR] = DR\n";
				writeMemory = 1;
				dRoutDemuxSel = 1;

				wait(1);
				wait(1);//delay da memoria
				

				incrementPC();

				return true;
			}
			break;
	}
	return false;
}
Ejemplo n.º 4
0
bool ControlUnit::processRegisterInstruction(){
	//cout<<__FILE__<<"::"<<__LINE__<<endl;
	int operation = iRInput.read().range(13,9).to_int();
	switch (operation){
		case 0: //RF[dest] = CTE
			{
				//cout<<__FILE__<<"::"<<__LINE__<<endl;
				int dest = iRInput.read().range(8,6).to_int();
				//guarda o endereço onde será guardado address
				rfSel = dest; 
				//pega o valor da constante
				//cout<<"\nGetting CTE value from mem\n";
				loadsARWithPc();
				resetAllLoads();

				//incrementa o pc para descartar o dado
				//cout<<"\n\n";
				incrementPC();
				resetAllLoads();

				//cout<<"\n\n";
				//RF[rfSel] = DR
				//
				//cout<<"DR to RF[dest]"<<endl;
				ulaOp = ulaOperation_doNothing;
				ulaInAMuxSel = UlaInputSelection_DR; //RA = DR
				loadRA = 1;
				wait(1);

				ulaOutDemuxSel = UlaOutputSelection_RF; //RF[dest] = RA
				rfReadWriteBit = 1; //escreve em RF
				wait(1);

				return true;
			}
			break;
		default: //RF [dest] = RF[src1] op RF[src2]
			{
				int dest = iRInput.read().range(8,6).to_int();
				int src1 = iRInput.read().range(5,3).to_int();
				int src2 = iRInput.read().range(2,0).to_int();

				//RA = RF(src1)
				ulaInAMuxSel = UlaInputSelection_RF;
				ulaOp = operation;
				rfSel = src1;//
				rfReadWriteBit = 0;
				//cout<<"loading RA with RF(src1)"<<endl;
				wait(1);//espera por RF
				loadRA = 1;
				wait(1);//espera por loadRA
				resetAllLoads();

				//RB = RF(src2)
				ulaInBMuxSel = UlaInputSelection_RF;
				rfSel = src2;//
				rfReadWriteBit = 0;
				//cout<<"loading RB with RF(src1)"<<endl;
				wait(1); //espera por RF
				loadRB = 1;
				wait(1);//espera por loadRB
				resetAllLoads();

				//RF(dest) = ulaOut
				ulaOutDemuxSel = UlaOutputSelection_RF;
				rfSel = dest;
				rfReadWriteBit = 1;
				wait(1);

				return true;
			}
			break;
	}
}
Ejemplo n.º 5
0
 void simulateToRead()
 {
     if (_debug)
         printf("%*s% 7.2lf ", _indent*8, "", _tNextStop/(400.0*256.0));
     int pc = _pch | _memory[2];
     int op = _program->op(pc);
     incrementPC();
     UInt16 r;
     if ((op & 0x800) == 0) {
         _f = op & 0x1f;
         if ((op & 0x400) == 0) {
             bool d = ((op & 0x20) != 0);  // true if destination is f, false if destination is W
             char dc = d ? 'f' : 'W';
             switch (op >> 6) {
                 case 0x0:
                     if (!d)
                         switch (_f) {
                             case 0:
                                 if (_debug) { printf("NOP             "); printf("\n"); }
                                 _f = -1;
                                 break;
                             case 2:
                                 if (_debug) { printf("OPTION          "); printf("\n"); }
                                 _f = -1;
                                 _option = _w;
                                 break;
                             case 3:
                                 if (_debug) { printf("SLEEP           "); printf("\n"); }
                                 _f = -1;
                                 throw Exception(String("SLEEP not supported"));
                                 break;
                             case 4:
                                 if (_debug) { printf("CLRWDT          "); printf("\n"); }
                                 _f = -1;
                                 throw Exception(String("CLRWDT not supported"));
                                 break;
                             case 6:
                                 if (_debug) { printf("TRIS GPIO       "); printf("\n"); }
                                 _f = 0x20;
                                 _data = _w;
                                 break;
                             default:
                                 unrecognizedOpcode(op);
                                 break;
                         }
                     else {
                         if (_debug) { printf("MOVWF 0x%02x      ", _f); printf("\n"); }
                         _data = _w;
                     }
                     break;
                 case 0x1:
                     if (_debug) {
                         if (!d)
                             printf("CLRW            ");
                         else
                             printf("CLRF 0x%02x       ", _f);
                          printf("\n");
                     }
                     storeZ(0, d);
                     break;
                 case 0x2:
                     if (_debug) { printf("SUBWF 0x%02x, %c   ", _f, dc); printf("\n"); }
                     {
                         UInt8 m = readMemory(_f);
                         r = m - _w;
                         if (r & 0x100)
                             _memory[3] |= 1;
                         else
                             _memory[3] &= 0xfe;
                         if ((m & 0xf) - (_w & 0xf) != (r & 0xf))
                             _memory[3] |= 2;
                         else
                             _memory[3] &= 0xfd;
                         storeZ(r, d);
                     }
                     break;
                 case 0x3:
                     if (_debug) { printf("DECF 0x%02x, %c    ", _f, dc); printf("\n"); }
                     storeZ(readMemory(_f) - 1, d);
                     break;
                 case 0x4:
                     if (_debug) { printf("IORWF 0x%02x, %c   ", _f, dc); printf("\n"); }
                     storeZ(readMemory(_f) | _w, d);
                     break;
                 case 0x5:
                     if (_debug) { printf("ANDWF 0x%02x, %c   ", _f, dc); printf("\n"); }
                     storeZ(readMemory(_f) & _w, d);
                     break;
                 case 0x6:
                     if (_debug) { printf("XORWF 0x%02x, %c   ", _f, dc); printf("\n"); }
                     storeZ(readMemory(_f) ^ _w, d);
                     break;
                 case 0x7:
                     if (_debug) { printf("ADDWF 0x%02x, %c   ", _f, dc); printf("\n"); }
                     {
                         UInt8 m = readMemory(_f);
                         r = m + _w;
                         if (r & 0x100)
                             _memory[3] |= 1;
                         else
                             _memory[3] &= 0xfe;
                         if ((_w & 0xf) + (m & 0xf) != (r & 0xf))
                             _memory[3] |= 2;
                         else
                             _memory[3] &= 0xfd;
                         storeZ(r, d);
                     }
                     break;
                 case 0x8:
                     if (_debug) { printf("MOVF 0x%02x, %c    ", _f, dc); printf("\n"); }
                     storeZ(readMemory(_f), d);
                     break;
                 case 0x9:
                     if (_debug) { printf("COMF 0x%02x, %c    ", _f, dc); printf("\n"); }
                     storeZ(~readMemory(_f), d);
                     break;
                 case 0xa:
                     if (_debug) { printf("INCF 0x%02x, %c    ", _f, dc); printf("\n"); }
                     storeZ(readMemory(_f) + 1, d);
                     break;
                 case 0xb:
                     if (_debug) { printf("DECFSZ 0x%02x, %c  ", _f, dc); printf("\n"); }
                     r = readMemory(_f) - 1;
                     store(r, d);
                     if (r == 0) {
                         incrementPC();
                         _skipping = true;
                     }
                     break;
                 case 0xc:
                     if (_debug) { printf("RRF 0x%02x, %c     ", _f, dc); printf("\n"); }
                     r = readMemory(_f) | ((_memory[3] & 1) << 8);
                     setCarry((r & 1) != 0);
                     store(r >> 1, d);
                     break;
                 case 0xd:
                     if (_debug) { printf("RLF 0x%02x, %c     ", _f, dc); printf("\n"); }
                     r = (readMemory(_f) << 1) | (_memory[3] & 1);
                     setCarry((r & 0x100) != 0);
                     store(r, d);
                     break;
                 case 0xe:
                     if (_debug) { printf("SWAPF 0x%02x, %c   ", _f, dc); printf("\n"); }
                     r = readMemory(_f);
                     store((r >> 4) | (r << 4), d);
                     break;
                 case 0xf:
                     if (_debug) { printf("INCFSF 0x%02x, %c  ", _f, dc); printf("\n"); }
                     r = readMemory(_f) + 1;
                     store(r, d);
                     if (r == 0) {
                         incrementPC();
                         _skipping = true;
                     }
                     break;
             }
         }