#

static char *_S_NKmain = "@(#)NKmain.c	1.9";


/*
COPYRIGHT:  1980, by Ford Aerospace and Communications Corp.
ADDRESS:    Ford Aerospace and Communications Corp.
            Western Development Laboratories
            3939 Fabian Way
            Palo Alto, California 94303
            Attention: Software Technology Dept.
            Mail Stop: V02 

CPCI:  NKSR
	
CPC:  CSUP
	
TAG:  
MODULE NAME:  NKmain
REFERENCES-
  A Specs:  none
  B5 Specs:  none	
HISTORY:
  AUTHOR:   Strafford Wentworth
  VERSION:  1.9 of 12/15/80
  MODULE TYPE: 
PRIVILEGES:  none
SPECIAL NOTES:  This procedure is called from the crt0.o for the NKSR.
	This procedure then calls main passing along the argc and
	argv values.
	
*/

#include  "emuniv.h"

/* A Place holder to make ld happy. This var is not used */
int ESC_hd;

/* Pointer to arg block */
char *arg_blk;


/*-----------------------------------------------------------------------
    PROCEDURE   NKmain

    PURPOSE     Do necessary initialization and turn control
		over to real main.

------------------------------------------------------------------------*/


NKmain (argc, argv)
    int   argc;
    char *argv[];
{
    int i;
    int prev_lev;
    int *arg_addr;
    int  arg_size;

    termInit ();

    /* Calculate bottom of arg block */
    arg_addr = 0177776;
    arg_size = *arg_addr;
    arg_blk  = arg_addr - arg_size;


    EEV_Init (EventChannelTable, EventChannelTableSize, NIL, NIL);

    EDI_Init ();

    init_PIV ();

    K_set_pil (base_lev, &prev_lev);

    main (argc, argv);

    /* This should not be needed... There is a bug somewhere */
    K_release_process (null_seid);
}



/***************************************************************************

    PROCEDURE   panic


    PURPOSE     To relay the reason for crashing.

****************************************************************************/

panic (arglist)
{
    printf ("PANIC: %r",&arglist);
    printf ("\n");
    K_release_process (null_seid);
}

/***************************************************************************

    PROCEDURE   warning


    PURPOSE     To relay that somthing unexpected happened.

****************************************************************************/
warning (arglist)
{
    printf ("WARNING: %r",&arglist);
    printf ("\n");
}




/***************************************************************************

    PROCEDURE   Umemvalid

    PURPOSE     Place holder for NKSR code. In the emulator code this
		procedure checks if the address in the user domain is
		valid
***************************************************************************/


UMemValid (space, buffer, nbytes)

    mem_type                space;
    register char           *buffer;
    register unsigned int   nbytes;
{

    return (TRUE);
}


ECCpi (pi_level)

    register int pi_level;

{

    register pseudo_int_vector     *pi_pointer;
    register int                    pi_event;

    int                             old_level;
    char                            good_event;



    pi_pointer = &(((pseudo_int_vector *) PI_ADDRESS)[pi_level]);

    pi_event = pi_pointer->int_msg.EM_Type & 0377;

    if (pi_level < io_lev)
	K_set_pil (io_lev, &old_level);


    good_event = TRUE;

    switch (pi_level)
    {
	case ipc_lev:
	    break;

	case time_lev:
	    break;

	case sig_lev:
	    break;

	case io_lev:
	    break;

	case hard_lev:
	    printf ( "Hard level interrupt -- interrupted `PS': %o, `PC': %o, event #: %d\n"
		   , ((pseudo_int_vector *) PI_ADDRESS)[pi_level].interrupted_ps
		   , ((pseudo_int_vector *) PI_ADDRESS)[pi_level].interrupted_pc
		   , pi_event
		   );
	    panic ("Hard level interrupt\n");

	default:
	    panic ("Bad pseudo interrupt level\n");
    }

    if (good_event)
	/* Route the message */
	ECCdemux (&pi_pointer->int_msg);

    if (pi_level == hard_lev)
	K_set_pil (io_lev, &old_level);

    /* Pick up any pending IPC's */
    ECCreceiver (FALSE);


    K_set_pil (pi_level, &old_level);
}
