/**
 * TrueReality - n64/controller.c
 * Copyright (C) 1998 Niki W. Waibel
 *
 * This program is free software; you can redistribute it and/
 * or modify it under the terms of the GNU General Public Li-
 * cence as published by the Free Software Foundation; either
 * version 2 of the Licence, or any later version.
 *
 * This program is distributed in the hope that it will be use-
 * ful, but WITHOUT ANY WARRANTY; without even the implied war-
 * ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public Licence for more details.
 *
 * You should have received a copy of the GNU General Public
 * Licence along with this program; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
 * USA.
 *
 * Information about me (the author):
 *   Niki W. Waibel, Reichenau 20, 6890 Lustenau, Austria - EUROPE
 *   niki.waibel@gmx.net
**/





/* include section */

#include <stdio.h>

#include "type_sizes.h"
#include "memory_extern.h"
#include "controller.h"
#include "../dispatch.h"
#include "../parser_extern.h"





/* routine section */

void controller_check()
{
        int     i;
        int     contr = CONTR_1;
        WORD    state;


        
        for(i=0; i<MAX_CONTR; i++)
        {
                //printf("\nxxx: %lx\n\n", ((WORD *)mem.pi_ram_w)[2*i]);

                switch( ((WORD *)mem.pi_ram_w)[2*i] & 0xfff )
                //switch( ((WORD *)mem.pi_ram)[2*i] & 0xfff )
                {
                    case 0x300:   /* init */
                        if(contr & prefs.plugged_controller)   /* controller is plugged in */
                        {
                                ((WORD *)mem.pi_ram)[2*i]   = 0x00000000;
                                ((WORD *)mem.pi_ram)[2*i+1] = 0x01000000;
#ifdef DEBUG_CONTROLLER
                                if(prefs.debug & DBG_CONTR_PIF)
                                        printf("DBG_CONTR_PIF: init #%d (plugged in)\n", i);
#endif
                        }
                        else                                   /* controller is NOT plugged in */
                        {
                                ((WORD *)mem.pi_ram)[2*i]   = 0x00008000;
                                ((WORD *)mem.pi_ram)[2*i+1] = 0x00000000;
#ifdef DEBUG_CONTROLLER
                                if(prefs.debug & DBG_CONTR_PIF)
                                        printf("DBG_CONTR_PIF: init #%d (NOT plugged in)\n", i);
#endif
                        }
                        break;

                    case 0x401:   /* get data */
                        if(contr & prefs.plugged_controller)   /* controller is plugged in */
                        {
                                ((WORD *)mem.pi_ram)[2*i]   = 0x00000000;
                                
                                state = i;
                                dispatch(CONTROLLER_GET_STATUS, &state);
                                ((WORD *)mem.pi_ram)[2*i+1] = state;
#ifdef DEBUG_CONTROLLER
                                if(prefs.debug & DBG_CONTR_PIF)
                                        printf("DBG_CONTR_PIF: get #%d (%08lx)\n", i, ((WORD *)mem.pi_ram)[2*i+1]);
#endif
                        }
                        else                                   /* controller is NOT plugged in */
                        {
                                ((WORD *)mem.pi_ram)[2*i]   = 0x00008000;
                                ((WORD *)mem.pi_ram)[2*i+1] = 0x00000000;
                                //((WORD *)mem.pi_ram)[2*i+2] = 0x00000000;
#ifdef DEBUG_CONTROLLER
                                if(prefs.debug & DBG_CONTR_PIF)
                                        printf("DBG_CONTR_PIF: get #%d (NOT plugged in)\n", i);
#endif
                        }
                        break;
                
                } /* switch( ((WORD *)mem.pi_ram)[2*i] & 0xfff ) */
                
                contr <<= 1;   /* switch to next controller */
                
        } /* for(i=0; i<MAX_CONTR; i++) */


} /* void controller_check() */





void InitController(int num)
{
        prefs.plugged_controller = num;

} /* void InitController(int num) */






