/**
 * TrueReality - endian.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 <stdio.h>

#include "config.h"
#include "n64/type_sizes.h"

#include "endian.h"



#ifdef DEBUG
#       include "debug.h"
#endif





static int get_machine_endianness();






/*******************************************************************************
*                                                                              *
* if endianness is wrong defined in 'Makefile.temp', print error message and   *
* return -1 otherwise print cpu endian and return 0                            *
*                                                                              *
*******************************************************************************/

int check_machine_endian()
{
        int result;
   
   
   
        result = get_machine_endianness();



#ifdef ENDIAN_IS_LITTLE
        if(result == BIG_ENDIAN)
        {
                fprintf(stderr, "\n\n"
                                "You are on a big endian cpu but you defined ENDIAN_IS_LITTLE in config.h\n"
                                "Change that to ENDIAN_IS_BIG and compile again."
                                "\n\n");
                return(-1);
        }
        else
                printf("Machine:                                                          little endian\n");
#endif

#ifdef ENDIAN_IS_BIG
        if(result == LITTLE_ENDIAN)
        {
                fprintf(stderr, "\n\n"
                                "You are on a little endian cpu but you defined ENDIAN_IS_BIG in config.h\n"
                                "Change that to ENDIAN_IS_LITTLE and compile again."
                                "\n\n");
                return(-1);
        }
        else
                printf("Machine:                                                             big endian\n");
#endif


        puts("-------------------------------------------------------------------------------"); fflush(stdout);
   
        return(0);
   
} /* int check_machine_endian() */








/*
** this routine is called from 'int check_machine_endian()'
** check machine endian and return endianness (defined in local endian.h)
*/
   
static int get_machine_endianness()
{
        HWORD endian;


   
        endian = 0x8037;
   
        if( ((BYTE *)(&endian))[0] == 0x80 && ((BYTE *)(&endian))[1] == 0x37 )
        {
                return(BIG_ENDIAN);
        }
        else
        if( ((BYTE *)(&endian))[0] == 0x37 && ((BYTE *)(&endian))[1] == 0x80 )
        {
                return(LITTLE_ENDIAN);
        }
        else
        {
                /*
                **  this should never happen;
                **  if it happens, it might be that 'sizeof(HWORD)' is wrong ... check 'type_sizes.h'!!!
                */
         
                fprintf(stderr, "\n\nget_endianness: Could not get endianness.\n"
                                "This should not happen. Check 'type_sizes.h'\n\n");
                exit(1);
        }
   
} /* static int get_machine_endianness() */






