/**
 * TrueReality - main.c
 * Copyright (C) 1998, 1999 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 <stdlib.h>

#include <config.h>
#include <ansi_esc_codes.h>
#include <dispatch.h>
#include <parser_extern.h>
#ifdef NATIVE
        #include <native.h>
#endif

#include "parser.h"
#include "file_loader.h"

#include "N64/rsp_registers.h"

#include "main.h"

#ifdef DEBUG
#       include <debug.h>
#else
        #include "N64/cpu.h"
#endif

#ifdef HALT
        #include "N64/registers.h"
#endif




static void COPYRIGHT_AND_VERSION();










int main(int argc, char *argv[])
{
        COPYRIGHT_AND_VERSION();     /* print COPYRIGHT and VERSION  message */



        if(parse_args(argc, argv) < 0)                    /* from 'parser.c' */
                exit(1);



        if(dispatch(CHECK_MACHINE_ENDIAN, NULL) < 0)
                exit(1);



        if(dispatch(MEMORY_ALLOC, NULL) < 0)
                exit(1);



        if(load_files() < 0)                         /* from 'file_loader.c' */
        {
                dispatch(MEMORY_FREE, NULL);
                exit(1);
        }



        if(copy_code() < 0)                          /* from 'file_loader.c' */
        {
                dispatch(MEMORY_FREE, NULL);
                exit(1);
        }



        dispatch(DISPLAY_OPEN, NULL);



        if(dispatch(JOYSTICK_OPEN, NULL) < 0)
        {
                dispatch(MEMORY_FREE, NULL);
                exit(1);
        }



        dispatch(AUDIO_OPEN, NULL);



        dispatch(CATCH_SIGNALS, NULL);



        dispatch(CPU_RESET, NULL);



#ifdef NATIVE
        if(prefs.load_what == N64_IMAGE)
                Create_Native_Table();
#endif



#ifdef DEBUG
        if(prefs.load_what == N64_IMAGE)
                n64_cpu_debugger();                        /* from 'debug.c' */
        else
        {
                rsp_reg.halt = 0;
                n64_rsp_debugger();
        }
#else
        for(;;)
        {
#ifdef HALT
                if(reg.halt)
                        break;
#endif
                cpu_step();
        }
        dispatch(SHUTDOWN, NULL);
#endif


        if(prefs.symbols == USE_SYMBOLS)
                free(prefs.symtab);

        return(0);

} /* int main(int argc, char *argv[]) */





static void COPYRIGHT_AND_VERSION()
{
        char message[] =
        {
                "-------------------------------------------------------------------------------\n"
                ST_AT_B
                ST_FG_GRE
                "TrueReality\n"
                ST_FG_DEF
                ST_AT_NB
                "Copyright (C) 1998, 1999 Niki W. Waibel\n\n"
                "This program is free software; you can redistribute it and/\n"
                "or modify it under the terms of the GNU General Public Li-\n"
                "cence as published by the Free Software Foundation; either\n"
                "version 2 of the Licence, or any later version.\n\n"
                "This program is distributed in the hope that it will be use-\n"
                "ful, but WITHOUT ANY WARRANTY; without even the implied war-\n"
                "ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
                "See the GNU General Public Licence for more details.\n\n"
                "You should have received a copy of the GNU General Public\n"
                "Licence along with this program; if not, write to the Free\n"
                "Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
                "USA.\n"
                "-------------------------------------------------------------------------------\n"
                "Version: " VERSION "\n"
                "-------------------------------------------------------------------------------\n"
        };



        fputs(message, stdout);

} /* static void COPYRIGHT_AND_VERSION() */

