Ejemplo n.º 1
0
void gettime()
{
    serial_print(gethour());
    serial_print(":");

    serial_print(getminute());
    serial_print(":");

    serial_println(getsecond());
    serial_println("");
}
Ejemplo n.º 2
0
void accept_cli(threadargs *newargs)
{
  LList *userlist=newargs->list;
  int cli_socket=newargs->sock;
  int cli_sock2;
  int recvn;	//num of recv bytes
  char buf[bufsize+1]="";
  char buf2[bufsize+1]="";
  char cmd;
  client newcli;
  client lastuser;	//the user which client talk to last time
  bzero(&newcli,sizeof(client));
  bzero(&lastuser,sizeof(client));
  if(-1==Recv(cli_socket,buf))
    pthread_exit(NULL);
  printf("%s",buf);
  if(-1==Send(cli_socket,"Server OCP v0.0.1\n"))
    pthread_exit(NULL);
  bzero(buf,bufsize);
  if(-1==Recv(cli_socket,buf))
    pthread_exit(NULL);
  Rtrim(buf);
  while(useratlist(userlist,buf)==0) //username has been used
  {
    if(-1==Send(cli_socket,":x"))
      pthread_exit(NULL);
    bzero(buf,bufsize);
    if(-1==Recv(cli_socket,buf))
      pthread_exit(NULL);

    Rtrim(buf);
  }
  Send (cli_socket,"Longin Successfully\n");

  strncpy(newcli.nick,buf,strlen(buf));
  newcli.sock=cli_socket;
  ListInsert(userlist,newcli);


  while(1)
  {
    LNode *node=userlist->head->next; //use in :a
    bzero(buf,bufsize);
    if(Recv(cli_socket,buf)==-1)	//client offline
    {
      ListDelete(userlist,cli_socket);
      pthread_exit(NULL);
    }
    if((cmd=iscmd(buf))==0)	//if message body contains only message(not have a command)
    {
      if(useratlist(userlist,lastuser.nick)==0)

      {
        cli_sock2=lastuser.sock;
        domessage(buf2,newcli.nick,buf);
        if(-1==Send(cli_sock2,buf2))
          pthread_exit(NULL);
      }

      else
      {
        if(-1==Send(cli_socket,"The user you want to talk isn't online\n"))
          pthread_exit(NULL);
      }
      continue;
    }
    switch(cmd)
    {
    case 'l':
      bzero(buf,bufsize);
      LNode *user=userlist->head->next;
      while(user!=NULL)
      {
        strcat(buf,user->e.nick);
        strcat(buf,"\n");
        user=user->next;
      }

      if(-1==Send(cli_socket,buf))
        pthread_exit(NULL);
      break;
    case 'u':	//client change user which will talk to
      if(getuser(buf,lastuser.nick,userlist)!=-1)	//buf client's message //buf2 username
      {cli_sock2=findclientsock(userlist,lastuser.nick);
        lastuser.sock=cli_sock2;
        if(getthird(buf)!=NULL)
        {
          domessage(buf2,newcli.nick,getthird(buf));
          if(-1==Send(cli_sock2,buf2))
            pthread_exit(NULL);
        }
      }
      else
      {
        if(-1==Send(cli_socket,"You doesn't specify a user,or the user you want to talk to isn't online\n"))
          pthread_exit(NULL);
      }
      break;
    case 'q':	//client quit
      if(-1==Send(cli_socket,buf))
        pthread_exit(NULL);
      ListDelete(userlist,cli_socket);
      close(cli_socket);
      pthread_exit(NULL);
      break;
    case 'a':	//client talk to all user

      while(node!=NULL)
      {
        client user=node->e;

        cli_sock2=user.sock;
        if (cli_sock2!=cli_socket)	//don't send the message to your
          {   if(getsecond(buf)!=NULL)	//if the message body only contains the :a string

            domessage(buf2,newcli.nick,getsecond(buf));
          if(-1==Send(cli_sock2,buf2))
            pthread_exit(NULL);
        }

        node=node->next;
      }
      break;
    default :
      if(-1==Send(cli_socket,"Sever can't recognize your command\n"))
        pthread_exit(NULL);

    }
  }

}
int main(void)
{
	int memCounter = 0;
    char input;
    int numCountRef, firstNumRef, secondNumRef, intRestRef;
    
    printf("CS 350 Lab 8, Andrey Danilkovich\nFull SDC Simulator\n\n");
	initCPU();
	readMemory();
    
	printf("\nBeginning execution:\n");
	printf("At the > prompt, press return to execute the next instruction (or q to quit or h or ? for help).\n");
	char prompt[] = "> ";
	printf("%s", prompt);
	char command[80];
	fgets(command, sizeof command, stdin);	// Read past end of current line.
    
    scanf("%c", &input);
    
	do
    {
        while(input != '\n' && getchar() != '\n' ){ /* Do Nothing to clear \n from a char */ } // clear out the extra space after the char
        
        numCountRef = getcount(mem, memCounter);
        firstNumRef = getfirst(mem, memCounter, numCountRef);
        secondNumRef = getsecond(mem, memCounter, numCountRef);
        intRestRef = getrest(mem, memCounter, numCountRef);
        
        if(firstNumRef == 0)
        {
            memCounter++;
        }
        else if(input == '\r' | input == '\n')
        {
            instruction_cycle(mem, regs, memCounter);
            memCounter++;
            printf("%s", prompt);
            scanf("%c", &input);
            
            if(firstNumRef == 8 & regs[secondNumRef] > 0)
            {
                memCounter = intRestRef;
            }
            
        }
        else if(input == 'h')
        {
            helpMsg();              // call help message
            printf("%s", prompt);
            scanf("%c", &input);
        }
        else if(input == 'q')
        {
            printf("Quitting program.\n");
        }
        else
        {
            printf("Unknown command; ignoring it.\n");
            printf("%s", prompt);
            scanf("%c", &input);
        }
        
    }
    while (memCounter != 99 & input != 'q');
    
    // Finish Program
    // Print Halting message, diplay registers and memory
    printf("At 00 instr 0 0 00: HALT\n\nHalting\n");
	printf("\nRegisters:\n");
	dumpRegisters(regs);
    
	printf("\nMemory:\n");
	dumpMemory(mem);
}
int instruction_cycle(int mem[], int regs[], int memCounter)
{
    int intCount, firstNum, secondNum, intRest;
    char readInput, futureInput;
    
    intCount = getcount(mem, memCounter);
    firstNum = getfirst(mem, memCounter, intCount);
    secondNum = getsecond(mem, memCounter, intCount);
    intRest = getrest(mem, memCounter, intCount);
    
    // TEST CASE: printf("** This is the first digit: %d **\n", firstNum);
    if (firstNum != 0)
        printf("At %02d instr %d %d %02d: ", memCounter, firstNum, secondNum, intRest);
    
     
    switch (firstNum)
    {
        case 0: // DONE!!!
        {
            // HALT execution
            // Microcode: Running ← false
            memCounter++;
            break;
        }
        case 1: // DONE!!!
        {
            // LD
            // EXAMPLE: LD   R0 <- M[02] = 9225
            // Microcode: Reg[R]←Mem[MM]
            
            regs[secondNum] = mem[intRest];
            printf("LD   R%d <- M[%02d] = %d\n", secondNum, intRest, regs[secondNum]);
            break;
        }
        case 2: // DONE!!!
        {
            // ST
            // EXAMPLE: ST   M[22] <- R3 = -1
            // Microcode: Mem[MM]←Reg[R]
            
            mem[intRest] = regs[secondNum];
            printf("ST   M[%02d] <- R%d = %d\n", intRest, secondNum, mem[intRest]);
            break;
        }
        case 3: // DONE!!!
        {
            // ADD
            // EXAMPLE: ADD  R1 <- R1 + M[22] = 3 + -1 = 2
            // Microcode: Reg[R]←Reg[R] + Mem[MM]
            
            printf("ADD  R%d <- R%d + M[%02d] = %d + %d", secondNum, secondNum, intRest, regs[secondNum], mem[intRest]);
            regs[secondNum] = (regs[secondNum] + mem[intRest]) % 10000;
            
            printf(" = %d\n", regs[secondNum]);
            break;
        }
        case 4: // DONE!!!
        {
            // NEG
            // EXAMPLE: NEG  R3 <- -(R3) = -1
            // Microcode: Reg[R]←–Reg[R]
            regs[secondNum] = (-1 * regs[secondNum]);
            printf("NEG  R%d <- -(R%d) = %d\n", secondNum, secondNum, regs[secondNum]);
            break;
        }
        case 5: // DONE!!!
        {
            // ST
            // EXAMPLE: LDM  R3 <- 1
            // Microcode: Reg[R] ← MM
            regs[secondNum] = intRest;
            printf("LDM   R%d <- %d\n", secondNum, intRest);
            break;
        }
        case 6: // DONE!!!
        {
            // ADDM
            // EXAMPLE: ADDM R0 <- R0 + 01 = 9225 + 1 = 9226
            // Microcode: Reg[R]←Reg[R]+MM
            
            regs[secondNum] = regs[secondNum] + 1;
            printf("ADDM R%d <- R%d + 01 = %d + 1 = %d\n", secondNum, secondNum, (regs[secondNum] - 1), regs[secondNum]);
            break;
        }
        case 7: // EQUAL 0?
        {  
            // BR
            // EXAMPLE: BR   10
            // Microcode: PC←MM
            
            printf("BR   %d\n", intRest);
            break;
        }
        case 8:
        {
            // BRP
            // EXAMPLE: BRP  13 if R1 = 2 > 0: Yes
            // Microcode: if Reg[R]>0 then PC ← MM
            if(regs[secondNum] > 0)
            {
                memCounter = intRest;
                printf("BRP  %02d if R%d = %d > 0: Yes\n", intRest, secondNum, regs[secondNum]);
                return(memCounter);
            }
            else
            {
                printf("BRP  %02d if R%d = %d > 0: No\n", intRest, secondNum, regs[secondNum]);
            }
            break;
        }
        case 9:
        {
            // Read a character and copy its ASCII representation into R0.
            if (secondNum == 0)  
            {
                printf("I/O  Read char\nEnter a char (and press return): ");
                readInput = getchar();
                //scanf(" %c", &readInput);
                while(readInput != '\n' && getchar() != '\n' )
                { /* Do Nothing */ }
                printf("R%d <- %d\n", secondNum, readInput);
                
                futureInput = readInput;

                regs[secondNum] = readInput;  // set register
            }
            
            // Print the character whose ASCII representation is in R0.
            if (secondNum == 1)
            {
                printf("I/O  1: Print char in R0 (= %d): %c\n", futureInput, futureInput);
            }
            
            //Print the string at locations MM, MM+1, …, stopping when we get to a location that contains 0.
            if (secondNum == 2)
            {
                printf("I/O  2: Print string: Hello, world!\n");
            }
            // Print out the values of the registers.
            if (secondNum == 3)
            {
                printf("I/O  3: Dump Registers\n");
                dumpRegisters(regs);
            }
            //Print out the values in memory as a 10 by 10 table
            if (secondNum == 4)
            {
                printf("I/O  4: Dump Memory\n");
                dumpMemory(mem);
            }
            break;
        }
        default:
        {
            printf("\nAdd operaions here\n");
            break;
        }
    }
    return 0;
}