Ejemplo n.º 1
0
void Area::processEventQueue() {
	bool hasMove = false;
	for (std::list<Events::Event>::const_iterator e = _eventQueue.begin();
	     e != _eventQueue.end(); ++e) {

		if        (e->type == Events::kEventMouseMove) { // Moving the mouse
			hasMove = true;
		} else if (e->type == Events::kEventMouseDown) { // Clicking
			if (e->button.button == SDL_BUTTON_LMASK) {
				checkActive(e->button.x, e->button.y);
				click(e->button.x, e->button.y);
			}
		} else if (e->type == Events::kEventKeyDown) { // Holding down TAB
			if (e->key.keysym.sym == SDLK_TAB)
				highlightAll(true);
		} else if (e->type == Events::kEventKeyUp) {   // Releasing TAB
			if (e->key.keysym.sym == SDLK_TAB)
				highlightAll(false);
		}
	}

	_eventQueue.clear();

	if (hasMove)
		checkActive();
}
Ejemplo n.º 2
0
void sysTask(void *p_arg){

       uint16_t i;
       uint8_t j,p;
    (void)p_arg;
    checktime = 0;
    checkActiveInit();
   uartInit(0,9600,0,'n'); 
    
    for(i=0;i<1000;i++){
       OSTimeDly(3);
         WDG_Clear(feed);
    }
    
   
    while(1 == shellon){
       OSTimeDly(3);
       WDG_Clear(feed); 
    }
       
      gsm_ring = 0;   
    while(1){
    
     OSTimeDly(2);
     checktime++;
     
      if( 10 == gsm_ring){
        ringGet = 1; 
        gsm_ring = 10;   
     }else if(11 == gsm_ring){
          gsm_ring = 10;
     }
     
     if((1==gsm_pw)&&(0 == (PTED & 0x04))){
        gsm_ring++;
     }else{
        gsm_ring = 0;  
     }
     
     
     
     WDG_Clear(feed);
     
     if(checktime >= SYS_CHECK_TICKS){  //如果有任务在一分钟内无变化,则放狗
        checktime = 0;
        if(FALSE == checkActive()){
            sys_sleep = 0;       //不进行睡眠
            feed = 0;
        }
     } 
      
  }
   
}  
Ejemplo n.º 3
0
/**
 * Returns true when a switch was done.
 */
bool
Controls::useSwitch()
{
    bool result = false;
    if (m_active != m_units.end()) {
        if (!(*m_active)->willMove()) {
            checkActive();
        }

        if (m_switch && m_active != m_units.end()) {
            m_locker->ensurePhases(3);
            (*m_active)->activate();
            result = true;
        }
    }
    m_switch = false;
    return result;
}
Ejemplo n.º 4
0
void Area::processEventQueue() {
	bool hasMove = false;
	for (std::list<Events::Event>::const_iterator e = _eventQueue.begin();
	     e != _eventQueue.end(); ++e) {

		if        (e->type == Events::kEventMouseMove) {
			hasMove = true;
		} else if (e->type == Events::kEventKeyDown) {
			if (e->key.keysym.sym == SDLK_TAB)
				highlightAll(true);
		} else if (e->type == Events::kEventKeyUp) {
			if (e->key.keysym.sym == SDLK_TAB)
				highlightAll(false);
		}
	}

	_eventQueue.clear();

	if (hasMove)
		checkActive();
}
Ejemplo n.º 5
0
void Area::notifyCameraMoved() {
	checkActive();
}
Ejemplo n.º 6
0
void AgroShield::SDI12(uint8_t mode)
{
	switch( mode )
	{
	case	CHECK_ALL			:	mySDI12.begin(); 
									delay(500); 						// Tempo para que mySDI12 seja iniciado.
									
									/*
										Efectua um Scan rápido de todos os endereços.
									*/
									Serial.println("                    | SEARCH FOR SENSORS");
									Serial.println("                    | Scanning all addresses, please wait...");
									for(byte i = '0'; i <= '9'; i++) if(checkActive(i)) setTaken(i);   // Scan dos endereços entre 0-9

									for(byte i = 'a'; i <= 'z'; i++) if(checkActive(i)) setTaken(i);   // Scan dos endereços entre a-z

									for(byte i = 'A'; i <= 'Z'; i++) if(checkActive(i)) setTaken(i);   // Scan dos endereços entre A-Z

									/*
										Imprime a informação actual de cada sensor activo.
									*/
								  
									// Scan dos endereços entre 0-9
									for(char i = '0'; i <= '9'; i++) if(isTaken(i)){
									
										Serial.println("");
										getDateTime();
										found_check_all = true;
									
										Serial.print("Sensor ");
										Serial.print(i);
										Serial.print(":");
										printInfo(i);   
										//Serial.println(); 
									}
								  
									// Scan dos endereços entre a-z
									for(char i = 'a'; i <= 'z'; i++) if(isTaken(i)){
									
										Serial.println("");
										getDateTime();
										found_check_all = true;
									
										Serial.print("Sensor ");
										Serial.print(i);
										Serial.print(": ");
										printInfo(i);   
										//Serial.println(); 
									} 
								  
									// Scan dos endereços entre A-Z
									for(char i = 'A'; i <= 'Z'; i++) if(isTaken(i)){
									
										Serial.println("");
										getDateTime();
										found_check_all = true;
									
										Serial.print("Sensor ");
										Serial.print(i);
										Serial.print(":");
										printInfo(i);   
										//Serial.println();  
									};
									
									
									
									if(!found_check_all) {
										Serial.println("                    | No sensors found, please check connections and restart the Arduino.\n"); 
										//while(true);
										//break;
									} // stop here
									
									
									
									mySDI12.end();
									delay(500);
									found_check_all = false;
									break;

									
	case	WILD_CARD			:	mySDI12.begin();
									delay(500);							// Tempo para que mySDI12 seja iniciado.
									mySDI12.sendCommand(wild_card_Command); 
									delay(300);							// Espera um tempo pela resposta.
									Serial.println("                    | SENSORS CONNECTED");
									getDateTime();
									
									while(mySDI12.available()){			// Escreve a resposta no Serial Monitor.
										Serial.write(mySDI12.read());
										
										//Serial.print("                    | ");
										
									}
									Serial.println("");
									mySDI12.end();
									delay(500);
									break;
									
									
	case	SIMPLE_LOGGER		:	mySDI12.begin();
									delay(500); 						// Tempo para que mySDI12 seja iniciado.
									//////////////////////////////////////////////
									Serial.println();
									Serial.println("                    | SENSORS INFORMATION AND MEASUREMENTS "); 
									Serial.println("                    | Scanning all addresses, please wait..."); 
									
									/*
									  Efectua um Scan rápido de todos os endereços.
									*/

									for(byte i = '0'; i <= '9'; i++) if(checkActive(i)) setTaken(i);   // Scan dos endereços entre 0-9

									for(byte i = 'a'; i <= 'z'; i++) if(checkActive(i)) setTaken(i);   // Scan dos endereços entre a-z

									for(byte i = 'A'; i <= 'Z'; i++) if(checkActive(i)) setTaken(i);   // Scan dos endereços entre A-Z

									/*
									  Verificar se existem sensores activos.
									*/
									
//									for(byte i = 0; i < 62; i++){
//										if(isTaken(i)){
//										  found = true;
//										  break;
//										}
//									}
									
									for(char i = '0'; i <= '9'; i++){
										if(isTaken(i)){
											found = true;
											
											//Serial.print("Checking address ");
											//Serial.print(i);
											//Serial.print("..."); 
											
											break;
										}
									}
									for(char i = 'a'; i <= 'z'; i++){
										if(isTaken(i)){
											found = true;
											
											//Serial.print("Checking address ");
											//Serial.print(i);
											//Serial.print("..."); 
											
											break;
										}
									}
									for(char i = 'A'; i <= 'Z'; i++){
										if(isTaken(i)){
											found = true;
											
											//Serial.print("Checking address ");
											//Serial.print(i);
											//Serial.print("..."); 
											
											break;
										}
									}

									if(!found) {
										Serial.println("                    | No sensors found, please check connections and restart the Arduino.\n"); 
										//while(true);
										break;
									} // stop here

									Serial.println();
									Serial.println("                    | Time Elapsed (s), Sensor Address and ID");
									Serial.println("                    | Sensor Address, Measurement 1, Measurement 2, ... etc."); 
									Serial.println("                    | -------------------------------------------------------------------------------");
									//////////////////////////////////////////////
									// Scan dos endereços entre 0-9
									for(char i = '0'; i <= '9'; i++) if(isTaken(i)){
									
										getDateTime();
									
										Serial.print(millis()/1000);
										Serial.print(",");
										printInfo(i); 

										//getDateTime();
										
										takeMeasurement(i);
										
										Serial.println("");
									}

									// Scan dos endereços entre a-z
									for(char i = 'a'; i <= 'z'; i++) if(isTaken(i)){
									
										getDateTime();
									
										Serial.print(millis()/1000);
										Serial.print(",");
										printInfo(i); 

										//getDateTime();
										
										takeMeasurement(i);
										
										Serial.println("");
									} 

									// Scan dos endereços entre A-Z
									for(char i = 'A'; i <= 'Z'; i++) if(isTaken(i)){
									
										getDateTime();
									
										Serial.print(millis()/1000);
										Serial.print(",");
										printInfo(i); 

										//getDateTime();
										
										takeMeasurement(i);
										
										Serial.println("");
									};   
									//////////////////////////////////////////////
									mySDI12.end();
									delay(500);
									break;									

	case	INTERFACE			:	mySDI12.begin();
									delay(500);							// Tempo para que mySDI12 seja iniciado.
									mySDI12.sendCommand("");
									
									Serial.println("                    | INTERFACE COM SENSORES SDI-12");
									Serial.println("                    | Insira o comando a enviar (''exit'' para sair)...");
									Serial.println("");
									
									while(estado){
										if(Serial.available() > 0)
										{
											myCommand = Serial.readStringUntil('\n');
											//x = Serial.parseInt();

											if (myCommand == "exit")
											{
											  Serial.println("                    | Saiu do envio de comandos");
											  Serial.println("");
											  estado = false;
											}

											if (estado){    
												//if ( (myCommand[0] >= '0' && myCommand[0] <= '9' || myCommand[0] >= 'a' && myCommand[0] <= 'z' || 
												//myCommand[0] >= 'A' && myCommand[0] <= 'Z' || myCommand[0] == '?') && (myCommand[1] == 'I' || 
												//myCommand[1] == 'M' || myCommand[1] == 'D' || myCommand[1] == 'A') && (myCommand[2] == '!' || 
												//(myCommand[3] == '!' && (myCommand[2] >= '0' && myCommand[2] <= '9' || myCommand[2] >= 'a' && 
												//myCommand[2] <= 'z' || myCommand[2] >= 'A' && myCommand[2] <= 'Z')) ) )
												if ( (myCommand[0] >= '0' && myCommand[0] <= '9' || myCommand[0] >= 'a' && myCommand[0] <= 'z' || 
												myCommand[0] >= 'A' && myCommand[0] <= 'Z' || myCommand[0] == '?') && (myCommand[1] == '!' || 
												myCommand[1] == 'I' || myCommand[1] == 'M' || myCommand[1] == 'D' || myCommand[1] == 'A') && 
												(myCommand[1] == '!' || myCommand[2] == '!' || (myCommand[3] == '!' && (myCommand[2] >= '0' && 
												myCommand[2] <= '9' || myCommand[2] >= 'a' && myCommand[2] <= 'z' || myCommand[2] >= 'A' && 
												myCommand[2] <= 'Z')) ) ) 
												{

													getDateTime();
													Serial.println(myCommand);

													for(int j = 0; j < 1; j++){
													  mySDI12.sendCommand(myCommand);
													  //      mySDI12.flush();
													  delay(300); 
													  if(mySDI12.available()>1) break;
													  if(mySDI12.available()) mySDI12.read(); 
													}

													delay(300);


													//mySDI12.read(); //consume sensor address (you can keep it if you'd like)

													if(mySDI12.available()){
														getDateTime();
													}
													
//													while(mySDI12.available()){
//														comandorecebido = (String)mySDI12.read();
//														if(comandorecebido == "\n"){
//															Serial.println("                    | ");
//															Serial.println(comandorecebido);
//														}
//														Serial.println(comandorecebido);
//														delay(5); 
//													}
													
													while(mySDI12.available()){
														Serial.write(mySDI12.read()); 
														delay(5); 
													}
													
													Serial.println("");
												}

												else{
													Serial.println("                    | Comando Invalido");
													Serial.println("");
												}
											}
										}
									}
									

//									if(!estado)
//										while(!estado){
//											if(Serial.available() > 0)
//											{
//												myCommand = Serial.readStringUntil('\n');
//												//if (myCommand == "run sdi monitor")
//												if (myCommand == "run")
//												{      
//													estado = true;
//													Serial.println("Entrou do envio de comandos");  
//												}
//											}
//										}
																		
								
									
									mySDI12.end();
									delay(500);
									estado = true;
									break;
									
									
									
	default						:	;							
	}
}
Ejemplo n.º 7
0
int main(int argc, char *argv[])
{
  unsigned i, j, k;
  systemConfig *SYS;
  contextConfig *CNT;
  hyperContextConfig *HCNT;
  /*clusterTemplateConfig *CLUT;*/

  systemT *system;
  contextT *context;
  hyperContextT *hypercontext;
  /*clusterT *cluster;*/
  /*unsigned *tempP, clust;*/
  unsigned bundleCount;
  unsigned long long *bundleCountP;

  /*unsigned curClustTemplate, curClustInstance;*/
  /*unsigned sGPROffset, sFPROffset, sVROffset, sPROffset;*/
  /* signal catch (SIGSEGV and SIGUSR1) */
  pid_t pid;
  struct sigaction sigusr1_action;
  sigset_t block_mask;
  unsigned int STACK_SIZE_ARG = 0;

  isLLVM = 0;
  cycleCount = 0;
  char *versionNumber = "Insizzle_Revision";
  similarIRAM = 0;
  suppressOOB = 1;
  STACK_SIZE = 8; /* Default stack size to 8 KiB */
  PRINT_OUT = 0;
  SINGLE_STEP = 0;
  STAGGER = 0;
  printf("Insizzle (%s)\n", versionNumber);

  /* signal catch (SIGSEGV and SIGUSR1) */
  if((pid = getpid()) >= 0) {
    printf("PID: %d\n", pid);
    printf("(send SIGUSR1 signal to produce state dump)\n");
  }
  signal(SIGSEGV, sigsegv_debug);
  sigfillset(&block_mask);
  sigusr1_action.sa_handler = sigusr1_debug;
  sigusr1_action.sa_mask = block_mask;
  sigusr1_action.sa_flags = 0;
  sigaction(SIGUSR1, &sigusr1_action, NULL);

#ifndef INSIZZLEAPI
  if(argc < 2) {
    printf("you need to specify an xml machine model file\n");
    return -1;
  }

  if(readConf(argv[1]) == -1) {
    printf("error reading config\n");
    return -1;
  }
  /* all setup perfomed
     reset rand()
  */
  srand(1);

  printf("Galaxy setup completed.\n");
  /* for each other argv */
  for(i=2;i<(unsigned)argc;i++) {
	  if(!strncmp(argv[i], "-similarIRAM", 12)) {
      similarIRAM = 1;
    }
    else if(!strncmp(argv[i], "-suppressOOB", 12)) {
      suppressOOB = 1;
    }
    else if(!strncmp(argv[i], "-stack", 6)) {
      STACK_SIZE_ARG = atoi(argv[++i]);
    }
    else if(!strncmp(argv[i], "-printout", 9)) {
      PRINT_OUT = 1;
    }
    else if(!strncmp(argv[i], "-singlestep", 11)) {
      SINGLE_STEP = 1;
    }
    else if(!strncmp(argv[i], "-stagger", 8)) {
      STAGGER = atoi(argv[++i]);
    }
    else if(!strncmp(argv[i], "-calls_list", 11)) {
      /* setup calls list */
      callsList = NULL;
      if(setupCallsList(argv[i+1]) != 0) {
	return -1;
      }
      i++;
    }
    else if(!strncmp(argv[i], "-llvm", 5)) {
      isLLVM = 1;
    }
    else {
      printf("Unknown argument: %s\n", argv[i]);
    }
  }

  /* call config to setup the galaxy */
  if(setupGalaxy() == -1) {
    printf("error setting up galaxy\n");
    return -1;
  }

  /* then run through like we do */

  /* for each system
        for each context
	  for each hypercontext
  */


  SYS = (systemConfig *)((size_t)SYSTEM + (0 * sizeof(systemConfig)));
  system = (systemT *)((size_t)galaxyT + (0 * sizeof(systemT)));
  context = (contextT *)((size_t)system->context + (0 * sizeof(contextT)));

  /* check stack size */
  if((SYS->STACK_SIZE == 0) && (STACK_SIZE_ARG == 0)) {
    /* this means it isn't set in the XML file or command line */
    printf("Using default Stack Size / hypercontext of %d KiB\n", STACK_SIZE);
  }
  else if(STACK_SIZE_ARG != 0){
    /* set at command line, this overrides others */
    printf("Using Stack Size / hypercontext of %d KiB\n", STACK_SIZE_ARG);
    if(STACK_SIZE_ARG != SYS->STACK_SIZE) {
      printf("\tThis is different from Stack Size set in XML config file\n");
    }
    STACK_SIZE = STACK_SIZE_ARG;
  }
  else if(SYS->STACK_SIZE != 0) {
    /* set in XML */
    printf("Using Stack Size / hypercontext of %d KiB\n", SYS->STACK_SIZE);
    STACK_SIZE = SYS->STACK_SIZE;
  }

#ifdef VTHREAD
  /* need to set single hypercontext to active
     c 0 hc 0
  */
  for(i=0;i<(GALAXY_CONFIG & 0xff);i++)
    {
      SYS = (systemConfig *)((size_t)SYSTEM + (i * sizeof(systemConfig)));
      system = (systemT *)((size_t)galaxyT + (i * sizeof(systemT)));

      /* loop through contexts */
#if 0
      for(j=0;j<(SYS->SYSTEM_CONFIG & 0xff);j++)
	{
	  context = (contextT *)((size_t)system->context + (j * sizeof(contextT)));
	  hypercontext = (hyperContextT *)((size_t)context->hypercontext + (0 * sizeof(hyperContextT)));
	  hypercontext->VT_CTRL |= RUNNING << 3;
	}
#else
      context = (contextT *)((size_t)system->context + (0 * sizeof(contextT)));
      hypercontext = (hyperContextT *)((size_t)context->hypercontext + (0 * sizeof(hyperContextT)));
      hypercontext->VT_CTRL |= RUNNING << 3;
#endif
    }

  /* Initialise ThreadControlUnit */
  ThreadControlUnit.status = 0;
#endif

  int totalHC = 0;

  /* loop through systems in the galaxy */
  for(i=0;i<(GALAXY_CONFIG & 0xff);i++)
    {
      SYS = (systemConfig *)((size_t)SYSTEM + (i * sizeof(systemConfig)));
      system = (systemT *)((size_t)galaxyT + (i * sizeof(systemT)));
      /*printf("dram_size: 0x%x\n", (((SYS->DRAM_SHARED_CONFIG >> 8) & 0xffff) * 1024));*/

      /* loop through contexts */
      for(j=0;j<(SYS->SYSTEM_CONFIG & 0xff);j++)
	{
	  CNT = (contextConfig *)((size_t)SYS->CONTEXT + (j * sizeof(contextConfig)));
	  context = (contextT *)((size_t)system->context + (j * sizeof(contextT)));

	  /* loop through hypercontexts */
	  for(k=0;k<((CNT->CONTEXT_CONFIG >> 4) & 0xf);k++)
	    {
	      HCNT = (hyperContextConfig *)((size_t)CNT->HCONTEXT + (k * sizeof(hyperContextConfig)));
	      hypercontext = (hyperContextT *)((size_t)context->hypercontext + (k * sizeof(hyperContextT)));

	      /*printf("hypercontext: %d\n", k);*/
	      hypercontext->initialStackPointer = (((SYS->DRAM_SHARED_CONFIG >> 8) & 0xffff) * 1024) - ((STACK_SIZE * 1024) * totalHC);
	      *(hypercontext->S_GPR + (size_t)1) = ((((SYS->DRAM_SHARED_CONFIG >> 8) & 0xffff) * 1024) - 256) - ((STACK_SIZE * 1024) * totalHC);
	      *(hypercontext->pS_GPR + (size_t)1) = ((((SYS->DRAM_SHARED_CONFIG >> 8) & 0xffff) * 1024) - 256) - ((STACK_SIZE * 1024) * totalHC);
	      /*printf("\tr1: 0x%x\n", *(hypercontext->S_GPR + (unsigned)1));*/
	      /*printf("\tr1: 0x%x\n", *(hypercontext->pS_GPR + (unsigned)1));*/
	      totalHC++;
	    }

	}
    }

#endif
  start = time(NULL);
#endif

#ifdef INSIZZLEAPI
  int insizzleAPIClock(galaxyConfigT *galaxyConfig, hcTracePacketT gTracePacket[][MASTERCFG_CONTEXTS_MAX][MASTERCFG_HYPERCONTEXTS_MAX])
  {
    unsigned i, j, k;

    systemConfig *SYS;
    contextConfig *CNT;
    hyperContextConfig *HCNT;
    /*clusterTemplateConfig *CLUT;*/

    systemT *system;
    contextT *context;
    hyperContextT *hypercontext;
    unsigned bundleCount;
    unsigned long long *bundleCountP;
    unsigned bundlePos;
#else
  while(checkActive())
    {
      if(PRINT_OUT) {
	printf("------------------------------------------------------------ end of cycle %lld\n", cycleCount);
      }
#endif

#ifdef INSDEBUG
      printf("galaxy: 0\n");
#endif
      /* loop through systems in the galaxy */
      for(i=0;i<(GALAXY_CONFIG & 0xff);i++)
	{
#ifdef INSDEBUG
	  printf("\tsystem: %d\n", i);
#endif
	  SYS = (systemConfig *)((size_t)SYSTEM + (i * sizeof(systemConfig)));
	  system = (systemT *)((size_t)galaxyT + (i * sizeof(systemT)));

	  /* loop through contexts */
	  for(j=0;j<(SYS->SYSTEM_CONFIG & 0xff);j++)
	    {
#ifdef INSDEBUG
	      printf("\t\tcontext: %d\n", j);
#endif
	      CNT = (contextConfig *)((size_t)SYS->CONTEXT + (j * sizeof(contextConfig)));
	      context = (contextT *)((size_t)system->context + (j * sizeof(contextT)));

	      /* loop through hypercontexts */
	      for(k=0;k<((CNT->CONTEXT_CONFIG >> 4) & 0xf);k++)
		{
#ifdef INSDEBUG
		  printf("\t\t\thypercontext: %d\n", k);
#endif
		  HCNT = (hyperContextConfig *)((size_t)CNT->HCONTEXT + (k * sizeof(hyperContextConfig)));
		  hypercontext = (hyperContextT *)((size_t)context->hypercontext + (k * sizeof(hyperContextT)));
#ifdef INSIZZLEAPI
		  bundlePos = 0;
#endif
#ifdef INSDEBUG
		  printf("\t\t\t\thypercontext totalWidth: %d\n", hypercontext->totalWidth);
#endif
#ifdef INSDEBUG
		  printf("hypercontext cycleCount: %lld\n", hypercontext->cycleCount);
#endif
		  /* TODO: here will be the status check of the hypercontext to see what
		     state it is in
		  */
		  /* check the state VT_CTRL */
#ifdef INSDEBUG
		  printf("hypercontext->VT_CTRL: 0x%08x\n", hypercontext->VT_CTRL);
#endif

		  unsigned int deepstate = (hypercontext->VT_CTRL >> 3) & 0xff;
		  unsigned int sstep = (hypercontext->VT_CTRL >> 2) & 0x1;
		  unsigned int debug = (hypercontext->VT_CTRL >> 1) & 0x1;
		  /*unsigned int kernel = hypercontext->VT_CTRL & 0x1;*/

#ifdef INSDEBUG
		  printf("\tdeepstate: %d\n", deepstate);
		  printf("\tsstep:     %d\n", sstep);
		  printf("\tdebug:     %d\n", debug);
		  /*printf("\tkernel:    %d\n", kernel);*/
#endif

#ifdef INSIZZLEAPI
		  /* full 32 bit register */
		  /*gTracePacket[i][j][k].vt_ctrl = hypercontext->VT_CTRL;*/
		  /* just the deepstate */
		  gTracePacket[i][j][k].vt_ctrl = (hypercontext->VT_CTRL >> 3) & 0xff;
#endif
		  if(debug)
		    {

#ifdef INSDEBUG
		      printf("HC STATE: DEBUG\n");
#endif
		      /* nothing to do here */
		    }
		  else
		    {
		      /* TODO: need to implement this
			 it will be dependant on having something to say go
		       */
		      if(sstep)
			{
			  int sstmode = 0;
			  do {
			      printf("SINGLE STEP MODE: ");
			      if(scanf("%d", &sstmode) == EOF)
				return -1;
			  } while(!sstmode);

			  if(deepstate == READY)
			    printf("HC STATE: SSTEP_READY\n");
			  else if(deepstate == RUNNING)
			    printf("HC STATE: SSTEP_RUNNING\n");
			  else if(deepstate == BLOCKED_MUTEX_LOCK)
			    printf("HC STATE: SSTEP_BLOCKED_MUTEX_LOCK\n");
			  else if(deepstate == TERMINATED_SYNC)
			    printf("HC STATE: SSTEP_TERMINATED_SYNC\n");
			  else
			    {
			      printf("HC STATE: UNKNOWN\n");
			      return -1;
			    }
			}
		      else
			{
			  if(deepstate == READY)
			    {
#ifdef INSDEBUG
			      printf("HC STATE: READY\n");
#endif
			      hypercontext->idleCount++;
			    }
			  else if(deepstate == RUNNING)
			    {
#ifdef INSDEBUG
			      printf("HC STATE: RUNNING\n");
			      printf("%d:%d:%d (system:context:hypercontext)\n", i, j, k);
#endif

			      /* TODO: check the microarchitectural state of the hypercontext */
			      /* stalled, running */
			      bundleCount = 0;
			      if(hypercontext->stalled > 0)
				{
				  hypercontext->stallCount++;
				  hypercontext->stalled--;
				}
			      else
				{
				  if(hypercontext->checkedPC != (int)hypercontext->programCounter)
				    {
				      hypercontext->checkedPC = (int)hypercontext->programCounter;
#ifdef INGDEBUG
				      printf("checking bundle for PC: 0x%x\n", hypercontext->programCounter);
#endif
				      instructionPacket this;
				      unsigned startPC = hypercontext->programCounter;
				      unsigned endPC = hypercontext->programCounter;

				      do {
					this = fetchInstruction(context, endPC, CNT);
					if((this.op >> 31) & 0x1)
					  {
					    if(this.immValid)
					      {
						endPC += 4;
					      }
					    break;
					  }
					endPC  = this.nextPC;
				      } while(!(this.op >> 31) & 0x1);
#ifdef INSDEBUG
				      printf("startPC: 0x%x\n", startPC);
				      printf("endPC: 0x%x\n", endPC);
#endif

				      unsigned cB = checkBundle(hypercontext, startPC, endPC);
				      if(cB > 0) {
					if(cB > hypercontext->memoryStall) {
					  hypercontext->decodeStallCount += cB;
#ifdef NOSTALLS
					  hypercontext->stallCount += cB;
#else
					  unsigned long long *bundleCountP;
					  bundleCountP = (unsigned long long *)((size_t)hypercontext->bundleCount);
					  *bundleCountP = *bundleCountP + 1;

					  hypercontext->stalled += (cB-1);
					  hypercontext->cycleCount++;
					  hypercontext->stallCount++;
					  continue;
#endif					
					}
					else {
					  /* don't need to add extra stall, because the memory stalls hides it */
					}
				      }
				    }

				  /* Memory Stalls here */
				  if(hypercontext->memoryStall > 0) {
				    hypercontext->memoryStall--;
				    hypercontext->cycleCount++;
				    continue;
				  }

#ifdef INSDEBUG
				  printf("already checked bundle for PC: 0x%x\n", hypercontext->programCounter);
#endif
				  /*if(cycle(context, hypercontext, (HCNT->HCONTEXT_CONFIG & 0xf)) == -1)
				    {
				    printf("Error\n");
				    return -1;
				    }*/
				  instructionPacket this;
				  instruction inst;
				  /* TODO: possibly zero these out */
				  do {
				    bundleCount++;
				    this = fetchInstruction(context, hypercontext->programCounter, CNT);

#ifdef INSDEBUG
				    printf("PC: 0x%x, this.op: 0x%08x\n", hypercontext->programCounter, this.op);
				    if(this.immValid)
				      printf("PC: 0x%x, this.imm: 0x%08x\n", (hypercontext->programCounter + 4), this.imm);
#endif

				    inst = instructionDecode(this.op, this.imm, /*system->dram,*/ hypercontext, system, /*context, hypercontext->VT_CTRL, */(((SYS->DRAM_SHARED_CONFIG >> 8) & 0xffff) * 1024));

				    /*printf("%d : %d : ", *(hypercontext->S_GPR + 60), *(hypercontext->pS_GPR + 60));*/
  if((inst.packet.addr == 0) && (inst.packet.target == 1)) {
    /*    printf("\nsetting stack register\n");
    printf("initialStackPointer: 0x%x\n", hypercontext->initialStackPointer);
    printf("stackSize: %d\n", STACK_SIZE);
    printf("diff: %d\n", (hypercontext->initialStackPointer - inst.packet.data));*/
    if((hypercontext->initialStackPointer - inst.packet.data) >= (STACK_SIZE * 1024)) {
      /*
      printf("POSSIBLY OUT OF STACK?\n");
      printf("diff: %d\n", (hypercontext->initialStackPointer - inst.packet.data));*/
      int system_id = (((hypercontext->VT_CTRL) >> 24) & 0xff);
      int context_id = (((hypercontext->VT_CTRL) >> 16) & 0xff);
      int hypercontext_id = (((hypercontext->VT_CTRL) >> 12) & 0xf);
      printf("\tStack Overflow detected in [%d][%d][%d]:\n", system_id, context_id, hypercontext_id);
      printf("\t\tStack Pointer initially: 0x%x (%d)\n", hypercontext->initialStackPointer, hypercontext->initialStackPointer);
      printf("\t\tStack Size: 0x%x (%d)\n", (STACK_SIZE * 1024), (STACK_SIZE * 1024));
      printf("\t\tStack Pointer now: 0x%x (%d)\n", inst.packet.data, inst.packet.data);
      printf("\t\tTry setting stack pointer to %d K ( -stack=%d )\n", (hypercontext->initialStackPointer - inst.packet.data),
	     ((int)ceil((hypercontext->initialStackPointer - inst.packet.data) / 1024) + 1));
    }
  }
				    if(PRINT_OUT) {
#ifndef INSIZZLEAPI
				      printOut(inst, this, hypercontext, cycleCount);
#else
				      printOut(inst, this, hypercontext, 0);
#endif
				    }
				    if(SINGLE_STEP) {
				      stateDumpToTerminal(inst, hypercontext, cycleCount);
				      stateDump();
				      printf("Press enter to continue.");
				      getchar();

				    }

#ifdef INSIZZLEAPI
				    /* populate the gTracePacketT here */
				    gTracePacket[i][j][k].bundle[bundlePos].executed = inst.packet.executed;
				    gTracePacket[i][j][k].bundle[bundlePos].syll = this.op;
				    /*gTracePacket[i][j][k].bundle[bundlePos].syllValid = 1;*/
				    gTracePacket[i][j][k].bundle[bundlePos].pc = hypercontext->programCounter;

				    if(this.immValid) {
				      gTracePacket[i][j][k].bundle[bundlePos].imm = this.imm;
				      /*gTracePacket[i][j][k].bundle[bundlePos].immValid = this.immValid;*/
				    }
				    else {
				      gTracePacket[i][j][k].bundle[bundlePos].imm = 0;
				      /*gTracePacket[i][j][k].bundle[bundlePos].immValid = this.immValid;*/
				    }

				    gTracePacket[i][j][k].bundle[bundlePos].maddr = inst.packet.maddr;
				    gTracePacket[i][j][k].bundle[bundlePos].maddrValid = inst.packet.maddrValid;

				    /* source register 1 */
				    /*gTracePacket[i][j][k].bundle[bundlePos].rs1.regType = inst.packet.source[0].target;*/
				    gTracePacket[i][j][k].bundle[bundlePos].rs1.reg = inst.packet.source[0].reg;
				    gTracePacket[i][j][k].bundle[bundlePos].rs1.valid = inst.packet.source[0].valid;
				    gTracePacket[i][j][k].bundle[bundlePos].rs1.val = inst.packet.source[0].value;
				    gTracePacket[i][j][k].bundle[bundlePos].rs1.cluster = inst.packet.source[0].cluster;

				    /* source register 2 */
				    /*gTracePacket[i][j][k].bundle[bundlePos].rs2.regType = inst.packet.source[1].target;*/
				    gTracePacket[i][j][k].bundle[bundlePos].rs2.reg = inst.packet.source[1].reg;
				    gTracePacket[i][j][k].bundle[bundlePos].rs2.valid = inst.packet.source[1].valid;
				    gTracePacket[i][j][k].bundle[bundlePos].rs2.val = inst.packet.source[1].value;
				    gTracePacket[i][j][k].bundle[bundlePos].rs2.cluster = inst.packet.source[1].cluster;

				    /* source register 3 */
				    /*gTracePacket[i][j][k].bundle[bundlePos].rs3.regType = inst.packet.source[2].target;*/
				    gTracePacket[i][j][k].bundle[bundlePos].rs3.reg = inst.packet.source[2].reg;
				    gTracePacket[i][j][k].bundle[bundlePos].rs3.valid = inst.packet.source[2].valid;
				    gTracePacket[i][j][k].bundle[bundlePos].rs3.val = inst.packet.source[2].value;
				    gTracePacket[i][j][k].bundle[bundlePos].rs3.cluster = inst.packet.source[2].cluster;

				    /* source register 4 */
				    /*gTracePacket[i][j][k].bundle[bundlePos].rs4.regType = inst.packet.source[3].target;*/
				    gTracePacket[i][j][k].bundle[bundlePos].rs4.reg = inst.packet.source[3].reg;
				    gTracePacket[i][j][k].bundle[bundlePos].rs4.valid = inst.packet.source[3].valid;
				    gTracePacket[i][j][k].bundle[bundlePos].rs4.val = inst.packet.source[3].value;
				    gTracePacket[i][j][k].bundle[bundlePos].rs4.cluster = inst.packet.source[3].cluster;

				    /* source register 5 */
				    /*gTracePacket[i][j][k].bundle[bundlePos].rs5.regType = inst.packet.source[4].target;*/
				    gTracePacket[i][j][k].bundle[bundlePos].rs5.reg = inst.packet.source[4].reg;
				    gTracePacket[i][j][k].bundle[bundlePos].rs5.valid = inst.packet.source[4].valid;
				    gTracePacket[i][j][k].bundle[bundlePos].rs5.val = inst.packet.source[4].value;
				    gTracePacket[i][j][k].bundle[bundlePos].rs5.cluster = inst.packet.source[4].cluster;

				    /* source register 6 */
				    /*gTracePacket[i][j][k].bundle[bundlePos].rs6.regType = inst.packet.source[5].target;*/
				    gTracePacket[i][j][k].bundle[bundlePos].rs6.reg = inst.packet.source[5].reg;
				    gTracePacket[i][j][k].bundle[bundlePos].rs6.valid = inst.packet.source[5].valid;
				    gTracePacket[i][j][k].bundle[bundlePos].rs6.val = inst.packet.source[5].value;
				    gTracePacket[i][j][k].bundle[bundlePos].rs6.cluster = inst.packet.source[5].cluster;

				    /* source register 7 */
				    /*gTracePacket[i][j][k].bundle[bundlePos].rs7.regType = inst.packet.source[6].target;*/
				    gTracePacket[i][j][k].bundle[bundlePos].rs7.reg = inst.packet.source[6].reg;
				    gTracePacket[i][j][k].bundle[bundlePos].rs7.valid = inst.packet.source[6].valid;
				    gTracePacket[i][j][k].bundle[bundlePos].rs7.val = inst.packet.source[6].value;
				    gTracePacket[i][j][k].bundle[bundlePos].rs7.cluster = inst.packet.source[6].cluster;

				    /* destination register 1 */
				    /*gTracePacket[i][j][k].bundle[bundlePos].rd1.regType = inst.packet.dest[0].target;*/
				    gTracePacket[i][j][k].bundle[bundlePos].rd1.chk = inst.packet.dest[0].chk;
				    gTracePacket[i][j][k].bundle[bundlePos].rd1.reg = inst.packet.dest[0].reg;
				    gTracePacket[i][j][k].bundle[bundlePos].rd1.valid = inst.packet.dest[0].valid;
				    gTracePacket[i][j][k].bundle[bundlePos].rd1.cluster = inst.packet.dest[0].cluster;
				    gTracePacket[i][j][k].bundle[bundlePos].rd1.res = inst.packet.dest[0].res;

				    /* destination register 2 */
				    /*gTracePacket[i][j][k].bundle[bundlePos].rd2.regType = inst.packet.dest[1].target;*/
				    gTracePacket[i][j][k].bundle[bundlePos].rd2.chk = inst.packet.dest[1].chk;
				    gTracePacket[i][j][k].bundle[bundlePos].rd2.reg = inst.packet.dest[1].reg;
				    gTracePacket[i][j][k].bundle[bundlePos].rd2.valid = inst.packet.dest[1].valid;
				    gTracePacket[i][j][k].bundle[bundlePos].rd2.cluster = inst.packet.dest[1].cluster;
				    gTracePacket[i][j][k].bundle[bundlePos].rd2.res = inst.packet.dest[1].res;

				    bundlePos++;
#endif
				    if(inst.packet.newPCValid)
				      {
					hypercontext->programCounter = inst.packet.newPC;
					inst.packet.newPCValid = 0;
					/* TODO: add stalls for control flow change */
#ifndef INSIZZLEAPI
#ifdef NOSTALLS
					hypercontext->stallCount += PIPELINE_REFILL;
#else
					hypercontext->stalled += PIPELINE_REFILL;
#endif
#else
					hypercontext->stallCount += PIPELINE_REFILL;
#endif
#ifdef INSDEBUG
					printf("control flow change!\n");
#endif
					hypercontext->controlFlowChange++;
				      }
				    else
				      {
					hypercontext->programCounter = this.nextPC;
				      }

				    if(inst.packet.opcode == NOP)
				      {
					hypercontext->nopCount++;
				      }
#if 0
				    else {
				      /* set previous stall count to zero */
				      if(hypercontext->MemoryStall > 0) {
					hypercontext->MemoryStall--;
				      }
				    }
#endif
				  } while(!(this.op >> 31) & 0x1);
				}
			      /* TODO bundleCount */
			      bundleCountP = (unsigned long long *)((size_t)hypercontext->bundleCount + (bundleCount * (sizeof(unsigned long long))));
			      *bundleCountP = *bundleCountP + 1;

			      hypercontext->cycleCount++;
			    }
			  else if(deepstate == BLOCKED_MUTEX_LOCK)
			    printf("HC STATE: BLOCKED_MUTEX_LOCK\n");
			  else if(deepstate == TERMINATED_ASYNC_HOST)
			    {
			      /* TODO: this is the vthread_join thing */
#if 1
#ifdef INSDEBUG
			      printf("HC STATE: TERMINATED_ASYNC_HOST\n");
			      printf("\twaiting for: 0x%x\n", hypercontext->joinWaiting);
#endif
			      hypercontext->cycleCount++;
#endif
			    }
			  else if(deepstate == TERMINATED_ASYNC)
			    printf("HC STATE: TERMINATED_ASYNC\n");
			  else if(deepstate == TERMINATED_SYNC)
			    printf("HC STATE: TERMINATED_SYNC\n");
			  else
			    {
			      printf("HC STATE: UNKNOWN\n");
			      return -1;
			    }
			}
		    }
		}
Ejemplo n.º 8
0
/**
 * @brief  Parses Telnet Options negotiations
 * @param c
 * @return
 */
unsigned char TelnetManager::telnetOptionParse(unsigned char c)
{
    // TEL-OPT Parser
    switch(m_teloptStage)
    {
            // Find IAC
        case 0:
            // Check first Char in Fresh Sequence
            if(c != IAC)
            {
                // Add character to output buffer.
                return c;
            }
            else
            {
                m_teloptStage++;
            }
            break;

            // Find Command
        case 1:
            if(c == IAC)
            {
                // If binary, then we expect double IAC for actual characer
                if (m_is_active_bin)
                {
                    std::cout << "\r\n Got double IAC w/ Binary!!\r\n" << std::endl;
                    m_teloptStage = 0;
                    return IAC;
                }
                else
                {
                    // Restart on next character
                    // Double IAC = IAC character, not valid so skip it for now.
                    // Telnet states binary mode, double IAC mean pass through
                    // Char 255.  But this doesn't equal any text in ExtASCII
                    // So we going to stuff it.
                    std::cout << "\r\n Got double IAC!!\r\n" << std::endl;
                    break;
                }
            }
            if(c != IAC)
            {
                switch(c)
                {
                        // Catch Pass-through commands.
                    case GA:    //     249        /* you may reverse the line */
                    case EL:    //     248        /* erase the current line */
                    case EC:    //     247        /* erase the current character */
                    case AYT:   //     246        /* are you there */
                    case AO:    //     245        /* abort output--but let prog finish */
                    case IP:    //     244        /* interrupt process--permanently */
                    case BREAK: //     243        /* break */
                    case DM:    //     242        /* data mark--for connect. cleaning */
                    case NOP:   //     241        /* nop */
                    case SE:    //     240        /* end sub negotiation */
                    case EOR:   //     239        /* end of record (transparent mode) */
                    case ABORT: //     238        /* Abort process */
                    case SUSP:  //     237        /* Suspend process */
                    case xEOF:  //     236        /* End of file: EOF is already used... */
                        // Pass Through commands that don't need Response.
                        std::cout << "[IAC] [" << (int)c << "] PASS-THROUGH" << std::endl;
                        m_teloptStage = 0;
                        break;

                    default:
                        // Move to Command Parsing.
                        m_teloptCommand = c;
                        m_teloptStage++;
                        break;
                }
            }
            break;

            // Find Option
        case 2:
            m_teloptStage = 0;
            // Catch if were getting Invalid Commands.
            if(TELCMD_OK(m_teloptCommand))
                std::cout << "[IAC] [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
            else
            {
                // Hopefully won't get here!
                std::cout << "[IAC] [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                m_teloptStage = 0;
                break;
            }
            switch(m_teloptCommand)
            {
                    // No responses needed, just stuff these.
                case DONT:
                    std::cout << "[IAC] RECEIVED DONT [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                    switch(c)
                    {
                        case IAC :
                            m_teloptStage = 1;
                            break;

                        default:
                            // Only Response Once, if we've already received this, then ignore it a second time!
                            if (!checkActive(m_teloptCommand))
                            {
                                std::cout << "[IAC] DONT -> WONT [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                                telnetSendIAC(telnetOptionAcknowledge(m_teloptCommand),c);
                                m_teloptStage = 0;

                                // Only Response once to Don't!
                                addActive(m_teloptCommand);
                            }
                            break;
                    }
                    break;

                case DO: // Replies WILL / WON'T
                    std::cout << "[IAC] RECEIVED DO [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                    switch(c)
                    {
                        case TELOPT_ECHO:
                            if (!m_isECHOCompleted)
                            {
                                std::cout << "[IAC] DO TELOPT_ECHO [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                                telnetSendIAC(telnetOptionDeny(m_teloptCommand),c);
                                m_is_active_echo = false;
                                m_isECHOCompleted = false;
                            }
                            break;

                        case TELOPT_BINARY:
                            if (!m_isBINCompleted)
                            {
                                std::cout << "[IAC] DO TELOPT_BINARY [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                                telnetSendIAC(telnetOptionAcknowledge(m_teloptCommand),c);
                                m_isBINCompleted = true;
                                m_is_active_bin = true;
                            }
                            break;

                        case TELOPT_SGA:
                            if (!m_isSGACompleted)
                            {
                                std::cout << "[IAC] DO TELOPT_SGA [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                                telnetSendIAC(telnetOptionAcknowledge(m_teloptCommand),c);
                                m_isSGACompleted = true;
                                m_is_active_sga = true;
                            }
                            break;

                        case TELOPT_TTYPE:
                            std::cout << "[IAC] DO TELOPT_TTYPE [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                            telnetSendIAC(telnetOptionAcknowledge(m_teloptCommand),c);
                            m_is_active_ttype = true;
                            break;

                        case TELOPT_NAWS:
                            std::cout << "[IAC] DO TELOPT_NAWS [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                            telnetSendIAC(telnetOptionAcknowledge(m_teloptCommand),c);
                            telnetOptionNawsReply();
                            m_is_active_naws = true;
                            break;
                            /*
                            case IAC :
                                printf("\r\n [DO - INCORRECT IAC Received, resetting.] \r\n");
                                stage = 1;
                                return 255;
                            */

                        default:
                            // Only Response Once, if we've already received this, then ignore it a second time!
                            if (!checkActive(m_teloptCommand))
                            {
                                std::cout << "[IAC] DO -> WONT [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                                telnetSendIAC(telnetOptionDeny(m_teloptCommand),c);
                                addActive(m_teloptCommand);
                            }
                            break;
                    }
                    m_teloptStage = 0;
                    break;

                    // WILL means the Server Will DO IT!
                    // We reply Fine, do it!
                case WILL: // Replies DO And DONT
                    std::cout << "[IAC] RECEIVED WILL [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                    // Don't response to WILL Requests.
                    switch(c)
                    {
                        case TELOPT_ECHO:
                            if(!m_isECHOCompleted)
                            {
                                std::cout << "[IAC] WILL TELOPT_ECHO [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                                telnetSendIAC(telnetOptionDeny(m_teloptCommand),c);
                                m_is_active_echo = false;
                                m_isECHOCompleted = true;
                            }
                            break;

                        case TELOPT_BINARY :
                            if(!m_isBINCompleted)
                            {
                                std::cout << "[IAC] WILL TELOPT_BINARY [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                                telnetSendIAC(telnetOptionAcknowledge(m_teloptCommand),c);
                                m_is_active_bin = true;
                                m_isBINCompleted = true;
                            }
                            break;

                        case TELOPT_SGA :
                            if(!m_isSGACompleted)
                            {
                                std::cout << "[IAC] WILL TELOPT_SGA [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                                telnetSendIAC(telnetOptionAcknowledge(m_teloptCommand),c);
                                m_is_active_sga = true;
                                m_isSGACompleted = true;
                            }
                            break;
                            /*
                            case IAC :
                                stage = 1;
                                return 255;
                            */
                        default :
                            // Only Response Once, if we've already received this, then ignore it a second time!
                            if (!checkActive(m_teloptCommand))
                            {
                                std::cout << "[IAC] WILL -> DONT [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                                telnetSendIAC(telnetOptionDeny(m_teloptCommand),c);
                                addActive(m_teloptCommand);
                            }
                            break;
                    }
                    m_teloptStage = 0;
                    break;

                case WONT:
                    // Don't respond to WONT
                    std::cout << "[IAC] RECEIVED WONT [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                    //telnetSendIAC(telnetOptionAcknowledge(m_teloptCommand),c);
                    //printf("\r\n [WONT - responded DONT %i] \r\n",c);
                    m_teloptStage = 0;
                    break;

                    // Start of Sub Negotiations and Stages 3 - 4
                case SB: // 250
                    std::cout << "[IAC] TELNET_STATE_SB [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                    if(c == TELOPT_TTYPE)
                    {
                        m_currentOption = c;
                        m_teloptStage = 3;
                    }
                    else
                    {
                        std::cout << "[IAC] TELNET_STATE_SB INVALID [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                        // Invalid, reset back.
                        m_teloptStage = 0;
                    }
                    break;

                default:
                    // Options or Commands Not Parsed, RESET.
                    std::cout << "[IAC] INVALID [" << (int)m_teloptCommand << "] [" << (int)c << "]" << std::endl;
                    m_teloptStage = 0;
                    break;
            }
            break;

        case 3:
            std::cout << "--> STAGE 3 [" << (int)c << "]" << std::endl;
            //Options will be 1 After SB
            //IAC SB TTYPE SEND IAC SE
            switch(m_currentOption)
            {
                case TELOPT_TTYPE:
                    if(c == TELQUAL_SEND)  // SEND
                    {
                        std::cout << "[IAC] TELQUAL_SEND [" << (int)m_currentOption << "] [" << (int)c << "]" << std::endl;
                        m_teloptStage = 4;
                    }
                    else
                        m_teloptStage = 0;
                    break;

                default:
                    //printf("\r\n [Stage 3 - unregistered stuff it] - %i, %i \r\n",opt, c);
                    if(c == SE)
                    {
                        std::cout << "[IAC] SB END [" << (int)m_currentOption << "] [" << (int)c << "]" << std::endl;
                        m_teloptStage = 0;
                    }
                    else
                    {
                        // reset
                        m_teloptStage = 0;
                    }
                    break;
            }
            break;

            // Only Gets here on TTYPE Sub-Negotiation.
        case 4:
            std::cout << "--> STAGE 4 [" << (int)c << "]" << std::endl;
            switch(c)
            {
                case IAC:
                    std::cout << "[IAC] TTYPE TELNET_STATE_SB IAC [" << (int)m_currentOption << "] [" << (int)c << "]" << std::endl;
                    break;

                case SE:
                    std::cout << "[IAC] TTYPE TELNET_STATE_SB SE [" << (int)m_currentOption << "] [" << (int)c << "]" << std::endl;

                    // Send TTYPE After End of Complete Sequence is Registered.
                    telnetOptionTerminalTypeReply();
                    m_teloptStage = 0;
                    break;
            }
            break;
    }

    //std::cout << "[IAC] null return [" << (int)c << "] " << std::endl;
    return '\0';
}