Beispiel #1
0
static void maybe_read(struct thread *thread)
{
    obj_t *fp = thread->fp;
    int fd = fixnum_value(fp[-9]);
    int nfound, res;
    obj_t *old_sp;

    nfound = input_available(fd);
    if (nfound < 0) {
        old_sp = pop_linkage(thread);
        thread->sp = old_sp + 2;
        old_sp[0] = obj_False;
        old_sp[1] = make_fixnum(errno);
        do_return(thread, old_sp, old_sp);
    }
    else if (nfound == 0)
        wait_for_input(thread, fd, maybe_read);
    else {
        res = mindy_read(fd,
                         (char *)(buffer_data(fp[-8]) + fixnum_value(fp[-7])),
                         fixnum_value(fp[-6]));

        results(thread, pop_linkage(thread), res, make_fixnum(res));
    }
}
Beispiel #2
0
static void fd_input_available(obj_t self, struct thread *thread, obj_t *args)
{
    int fd = fixnum_value(args[0]);
    int res = input_available(fd);

    results(thread, args-1, res, res ? obj_True : obj_False);
}
Beispiel #3
0
void new_game(void) {
	char c = 0;
	cancel_software_timer(foodTimerNum);
	cancel_software_timer(ratsTimerNum);
	empty_display();

	//wait for space
	while(c != ' ' && c != 'l' && c != 'L'){
		if(input_available())
			c = fgetc(stdin);

		if(c == 'M' || c == 'm'){
			toggle_sound();
			display_sound_status();
			c = 0;
		}
	}
	
	init_display();
	
	if(c == 'l' || c == 'L'){
		if(load_state())
			render_board();
		else {
			/* Initialise internal representations. */
			init_board();
			init_score();
		}
	}
	else {
		/* Initialise internal representations. */
		init_board();
		init_score();
	}
	clear_terminal();

	//Place scores
	update_score();

	//place sound status
	display_sound_status();

	show_instruction(PLAYING);

	/* Make food blink 5 times a second. We should call blink_food
	** 10 times a second which is 100ms
	*/
	foodTimerNum = execute_function_periodically(BLINKRATE, blink_food);
	ratsTimerNum = execute_function_periodically(RATSPEED, move_rats);

	/* Debug *
	move_cursor(0, TITLEY-1);
	printf_P(PSTR("new_game %u"), get_score());
	wait_for(1000);
	//*/
}
Beispiel #4
0
// This is a replacement for the old `WaitForChar` function in os_unix.c
static InbufPollResult inbuf_poll(int32_t ms)
{
  if (input_available()) {
    return kInputAvail;
  }

  if (event_poll(ms)) {
    return eof && rstream_available(read_stream) == 0 ?
      kInputEof :
      kInputAvail;
  }

  return kInputNone;
}
Beispiel #5
0
void pause_game() {
	/*
	** status = 0, game is running
	** status = 1, game is paused
	*/
	static uint8_t status = 0;
	char c = 0;

	if(status) {
		move_cursor(28, TITLEY);
		clear_to_end_of_line();
		show_instruction(PLAYING);
		render_board();
		foodTimerNum = execute_function_periodically(BLINKRATE, blink_food);
		ratsTimerNum = execute_function_periodically(RATSPEED, move_rats);
		status = 0;
	}
	else {
		show_instruction(PAUSE);
		cancel_software_timer(foodTimerNum);
		cancel_software_timer(ratsTimerNum);
		empty_display();
		status = 1;

		while(c != 'p' && c != 'P'){
			if(input_available())
				c = fgetc(stdin);

			if(c == 's' || c == 'S'){
				save_state();
				//move_cursor(0, TITLEY);
				//clear_to_end_of_line();
				move_cursor(28, TITLEY);
				printf_P(PSTR("State has been saved."));
				c = 0;
			}

		}
		pause_game();

	}
}
Beispiel #6
0
MINDY_NORETURN
static void getc_or_wait(struct thread *thread)
{
    // if (FBUFEMPTYP(stdin) && !feof(stdin)) {
    if (!feof(stdin)) {
        int fd = fileno(stdin);
        int nfound = input_available(fd);

        if (nfound < 0) {
            switch (errno) {
              case EBADF:
                error("Tried to getc with stdin broken.");
              case EINTR:
                wait_for_input(thread, fd, getc_or_wait);
              case EINVAL:
                lose("select failed with EINVAL?");
            }
        }
        else if (nfound == 0)
            wait_for_input(thread, fd, getc_or_wait);
    }

    {
        obj_t *old_sp;
        int c = mindy_getchar();

        old_sp = pop_linkage(thread);

        if (c != EOF)
            *old_sp = int_char(c);
        else
            *old_sp = obj_False;

        thread->sp = old_sp + 1;

        do_return(thread, old_sp, old_sp);
    }
}
Beispiel #7
0
int client(){
    int msg_id = msgget(KEY, 0666 | IPC_CREAT | IPC_NOWAIT);
    perror("Creating IPC");
    if (msg_id < -1){
        return(-1);
    }

    while(1){
        if (feof(stdin)){
            exit(0);
        }
        if (input_available()){
            text_message msg;
            msg.type = getpid();

            fgets(msg.text, MAX_TEXT, stdin);
            int rv = msgsnd(msg_id, &msg,  MAX_TEXT, IPC_NOWAIT);
            if (rv < 0){
                perror("msgsnd()");
                return(-1);
            }
        }

        text_message msg;
        int rv = msgrcv(msg_id, &msg, MAX_TEXT, getpid(), IPC_NOWAIT | MSG_EXCEPT);
        if (rv < 0 && errno != ENOMSG){
            perror("msgrcv()");
            return(-1);
        } else if (rv > 0){
            printf("rcv %d > %s\n", msg.type, msg.text);
        }
        usleep(100);
    }

    return 0;
}
Beispiel #8
0
static void check_fds(bool block)
{
    fd_set readfds, writefds;
    int nfound, fd;
    struct timeval tv, *tvp;

    if (NumFds == 0) {
        if (block) {
          sigset_t set;
          sigemptyset(&set);
          sigsuspend(&set);
        }
        return;
    }

    memcpy(&readfds, &Readers.fds, sizeof(readfds));
    memcpy(&writefds, &Writers.fds, sizeof(writefds));

#ifdef WIN32
    do {
            for (fd = 0; fd < NumFds; fd++) {
            if (FD_ISSET(fd, &Readers.fds) && input_available(fd)) {
                event_broadcast(Readers.events[fd]);
                FD_CLR(fd, &Readers.fds);
                block = 0;
            }
            if (FD_ISSET(fd, &Writers.fds) && output_writable(fd)) {
                event_broadcast(Writers.events[fd]);
                FD_CLR(fd, &Writers.fds);
                block = 0;
            }
        }
    } while (block);
    for (fd = NumFds - 1; fd >= 0; fd--)   /* Adjust NumFds */
        if (FD_ISSET(fd, &Readers.fds) || FD_ISSET(fd, &Writers.fds))
            break;
    NumFds = fd+1;
#else
    if (block)
        tvp = NULL;
    else {
        tv.tv_usec = 0;
        tv.tv_sec = 0;
        tvp = &tv;
    }

    nfound = select(NumFds, &readfds, &writefds, NULL, tvp);

    if (nfound < 0) {
        switch (errno) {
          case EBADF:
            /* One of the file descriptors when bad.  Wake everyone up */
            /* and let the individual threads figure out who is selecting */
            /* on a bogus fd. */
            for (fd = 0; fd < NumFds; fd++) {
                if (FD_ISSET(fd, &Readers.fds))
                    event_broadcast(Readers.events[fd]);
                if (FD_ISSET(fd, &Writers.fds))
                    event_broadcast(Writers.events[fd]);
            }
            FD_ZERO(&Readers.fds);
            FD_ZERO(&Writers.fds);
            NumFds = 0;
            break;

          case EINTR:
            break;
          case EINVAL:
            lose("select failed with EINVAL?");
        }
    }
    else if (nfound > 0) {
        for (fd = 0; fd < NumFds; fd++) {
            if (FD_ISSET(fd, &readfds)) {
                event_broadcast(Readers.events[fd]);
                FD_CLR(fd, &Readers.fds);
            }
            if (FD_ISSET(fd, &writefds)) {
                event_broadcast(Writers.events[fd]);
                FD_CLR(fd, &Writers.fds);
            }
        }
        for (fd = NumFds - 1; fd >= 0; fd--)
            if (FD_ISSET(fd, &Readers.fds) || FD_ISSET(fd, &Writers.fds))
                break;
        NumFds = fd+1;
    }
#endif
}
void main(){

// Configure the timers
 e_ctimer_set(E_CTIMER_0, E_CTIMER_CLK, E_CTIMER_MAX); 

 // Start the timer (countdown from 0xFFFFFFFF)
 e_ctimer_start(E_CTIMER_0, E_CTIMER_CLK);

	init();
	
	int j = 0;
	int consumed_elements = 0;

	// wait for the go (which means actors are connected)
	while(Mailbox.pGo[me.corenum] == 0);

timerValue = e_ctimer_get(E_CTIMER_0);
Mailbox.pTimer0[me.corenum] = E_CTIMER_MAX - timerValue;
e_ctimer_stop(E_CTIMER_0);

e_ctimer_set(E_CTIMER_1, E_CTIMER_CLK, E_CTIMER_MAX); 
e_ctimer_start(E_CTIMER_1, E_CTIMER_CLK);

	while(j < IN_BUFFER_SIZE)
	{
		
		// action #1
		if(transpose.rcount < 64){

			int row = (transpose.rcount >> 3);
			int quad = (transpose.rcount >> 2) & 1;
			int i;
	
			if(quad == 0){

				for(i = 0; i < 4; i++){

					if(input_available(&X)){

						switch(consumed_elements){
						case 0:
							transpose.mem[transpose.select][row][0] = port_read(&X);
							break;
						case 1:
							transpose.mem[transpose.select][row][7] = port_read(&X);
							break;
						case 2:
							transpose.mem[transpose.select][row][3] = port_read(&X);
							break;
						case 3:
							transpose.mem[transpose.select][row][4] = port_read(&X);
							transpose.rcount = transpose.rcount + 4;
							break;
						default:
							break;
						}
					
						// if we consumed 4 elements, get out of for loop
						if(consumed_elements == 3)
						  i = 4;

						consumed_elements = (consumed_elements + 1) % 4;
					}
				}
			}
			else{
				 for(i = 0; i < 4; i++){
                                        if(input_available(&X)){
                                                switch(consumed_elements){
                                                case 0:
                                                        transpose.mem[transpose.select][row][1] = port_read(&X);
                                                        break;
                                                case 1:
                                                        transpose.mem[transpose.select][row][6] = port_read(&X);
                                                        break;
                                                case 2:
                                                        transpose.mem[transpose.select][row][2] = port_read(&X);
                                                        break;
                                                case 3:
                                                        transpose.mem[transpose.select][row][5] = port_read(&X);
                                                        transpose.rcount = transpose.rcount + 4;
                                                        break;
                                                default:
							break;
                                                }

						// if we consumed 4 elements, get out of for loop
						if(consumed_elements == 3)
						  i = 4;

                                                consumed_elements = (consumed_elements + 1) % 4;
                                        }
                                }
			}
	
		}
		// end of action #1

		// action #2
		if(transpose.ccount > 0){


			int a, b;
			int col = (64 - transpose.ccount) >> 3;
			int pair = ((64 - transpose.ccount) >> 1) & 3;
			int k = transpose.select ^ 1;

			if(pair == 0){
				a = 0;
				b = 4;
			}
			else if(pair == 1){
				a = 2;
				b = 6;
			}
			else if(pair == 2){
				a = 1;
				b = 7;
			}
			else{
				a = 5;
				b = 3;
			}

#if 1
			port_write(&Y, transpose.mem[k][a][col]); j++;
			port_write(&Y, transpose.mem[k][b][col]); j++;
#endif



#if 0
			// DEBUG
			Mailbox.pOutputBuffer[j] = transpose.mem[k][a][col]; j++;
			Mailbox.pOutputBuffer[j] = transpose.mem[k][b][col]; j++;
#endif

			transpose.ccount = transpose.ccount - 2;


		}
Beispiel #10
0
/**
stop_search():
This checks for time and input to see if need to stop searching.
Created 091006; last modified 030709
**/
BOOL stop_search(void)
{
	CMD_RESULT cmd_result;
	BOOL stop = FALSE;
	GAME_ENTRY *current;

	if (zct->stop_search)
		stop = TRUE;
	/* Check for input, because we might enter this function more than once
		before fully stopping when unwinding the stack. */
	else if (zct->input_buffer[0])
		stop = TRUE;
	else if (time_is_up())
		stop = TRUE;
	else if (input_available())
	{
		/* Return to the root position. Some commands, such as "display" work
			on the root position instead of the current one. */
		current = board.game_entry;
		while (board.game_entry > root_entry)
			unmake_move();
		/* When pondering, the root entry is after the ponder move. */
		if (zct->engine_state == PONDERING)
			unmake_move();
		/* Read the command and parse it. */
		read_line();
		cmd_result = command(zct->input_buffer);
		/* Command() returns CMD_STOP_SEARCH when we need to exit the search to
			handle a command. */
		if (cmd_result == CMD_STOP_SEARCH)
			stop = TRUE;
		/* If the command was a move, either stop or make the move. */
		else if (zct->engine_state != NORMAL && cmd_result == CMD_BAD &&
			input_move(zct->input_buffer, INPUT_CHECK_MOVE))
		{
			/* If we are pondering, check if the move entered was the ponder
				move. If so, we don't need to stop. */
			if (zct->engine_state == PONDERING && input_move(zct->input_buffer,
				INPUT_GET_MOVE) == zct->ponder_move)
			{
				zct->input_buffer[0] = '\0';
				make_move(zct->ponder_move);
				zct->engine_state = NORMAL;
				update_clocks();
			}
			else
				stop = TRUE;
		}
		else if (cmd_result == CMD_BAD)
		{
			zct->input_buffer[0] = '\0';
			if (zct->protocol != UCI)
				print("Error.\n");
		}

		/* Check for ping here in UCI mode. */
		if (zct->ping && zct->protocol == UCI)
		{
			print("readyok\n");
			zct->ping = 0;
		}
		/* Go back to the position we were in before checking. This is kind of
			hacky in that it relies on the game history after undoing all the
			moves to be the same. */
		while (board.game_entry < current)
			make_move(board.game_entry->move);
	}

	/* If we're stopping the search, set a flag to make sure we don't come
		back here and decide to keep going, thereby introducing bugs... */
	if (stop)
		zct->stop_search = TRUE;

	return stop;
}
Beispiel #11
0
void event() {

   while (!SearchInfo->stop && input_available()) loop_step();
}
Beispiel #12
0
/*
 * main -- Main program.
 */
int main(void) {
	uint8_t chars_into_escape_sequence = 0;
	int8_t moveStatus = 0;

	char c;

	/* Initialise our main clock */
	init_timer();

	/* Initialise serial I/O */
	init_serial_stdio(19200, 0);

	/* Make the display_row() function be called every 2ms.
	** (This function returns a timer number, but we ignore 
	** this since we'll never do anything with it.)
	*/
	execute_function_periodically(2, display_row);

	/* Register the time_increment() function to be called every 500ms.
	** This function just sets a variable (timePassedFlag).
	*/
	mainTimerNum = execute_function_periodically(500, time_increment);

	//4209435
	/* setup AVR to handle sounds*/
	init_sound();

	/*
	** Turn on interrupts (needed for timer and serial input/output to work)
	*/
	sei();
	
	/*
	** Display splash screen 
	*/
	splash_screen();
	show_instruction(NEWGAME);
	
	/*
	** Perform necessary initialisations for a new game.
	*/
	new_game();
		
	/*
	** Event loop - wait for a certain amount of time to pass or wait
	** for a character to arrive from standard input. The time_passed_flag
	** is set within the function time_increment() below - which is setup
	** to be called periodically.
	*/
	for(;;) {
		if(timePassedFlag) {
			moveStatus = move_snake();
			timePassedFlag = 0;
		} else if(input_available()) {
			/* Read the input from our terminal and handle it */
			c = fgetc(stdin);			
			if(chars_into_escape_sequence == 0 && c == '\x1b') {
				/*
				** Received ESCAPE character - we're one character into
				** an escape sequence
				*/
				chars_into_escape_sequence = 1;
			} else if(chars_into_escape_sequence == 1 && c == '[') {
				/* 
				** We're now two characters into an escape sequence
				*/
				chars_into_escape_sequence = 2;
			} else if (chars_into_escape_sequence == 2) {
				/* We're two characters into an escape sequence and
				** have received another - see if it is as expected.
				*/
				if (c == 'C') {
					/* Cursor right key pressed - Set next direction to
					** be moved to RIGHT */
					set_snake_dirn(RIGHT);
				}  
				if (c == 'D') {
					/* Cursor left key pressed - Set next direction to
					** be moved to LEFT */
					set_snake_dirn(LEFT);
				}  
				if (c == 'A') {
					/* Cursor up key pressed - Set next direction to
					** be moved to UP */
					set_snake_dirn(UP);
				}  
				if (c == 'B') {
					/* Cursor down key pressed - Set next direction to
					** be moved to DOWN */
					set_snake_dirn(DOWN);
				}

				/* else, unknown escape sequence */

				/* We're no longer part way through an escape sequence */
				chars_into_escape_sequence = 0; 
			} else if(chars_into_escape_sequence != 0) {
				/*
				** We started an escape sequence but didn't get a character
				** we recognised - discard it and assume that we're not
				** in an escape sequence.
				*/
				chars_into_escape_sequence = 0;
			} else if (c == ' ') {
				/* Space character received - move snake immediately */
				moveStatus = move_snake();
			} else {					
				if(c == 'N' || c == 'n'){	
					show_instruction(NEWGAME);				
					new_game();
				} else if(c == 'P' || c == 'p'){
					moveStatus = 0;
					pause_game();
				} else if(c == 'M' || c == 'm'){
					toggle_sound();
					display_sound_status();
				}
			}
		}

		switch(moveStatus){
			case ATE_FOOD:
				if(sound_status())
					play_sound();
				moveStatus = MOVE_OK;
				break;
		}

		if(moveStatus < 0) {
			/* Move failed - game over */
			handle_game_over();
			moveStatus = 0;
			update_score();
		}
	}
}