/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by John E. Lloyd.			    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

/*---------------------------------------------------------------------------
 *		
 * MODULE NAME:	auxMonitor
 *
 * DESCRIPTION:	this file contains the entry points and monitor routines 
 *		specific to the auxiliary processors.
 *
 * AUTHOR/DATE:	John Lloyd, Fall 1987
 *
 * MODIFICATION HISTORY:
 *
 *--------------------------------------------------------------------------*/

#if defined(uVAX_CFG)

#include <stdio.h>

#include "../h/rciConfig.h"
#include "../sys.uVAX/rciext/kacomm.h"		/* UNIX auxiliary CPU interface */

#include "../h/robot_jls.h"
#include "../rci/create.h"
#include "../auxkernel/getmapplace.h"
#include "../auxkernel/uapage.h"
#include "../rci/monitorCom.h"
#include "../rci/monitorDefs.h"

extern unsigned char *rciBus;
extern unsigned char *rciIOpage;
extern int rciCpu;
extern int rciNumTasks;

extern float rciClockRes;
extern int rciClockNum;

extern int *_rciMaskAsts;
extern int *_rciAstsPending;
extern int (*_rciRestartAstsFxn)();

extern int (*_rciSignalAuxFxn)();
extern int (*_rciSchedAstFxn)();
extern int *_rciClockAddrs[];
extern int *_rciClockHosts[];

extern int (*_rciSignalFxn)();
extern int (*_rciUprintfFxn)();
extern int (*_rciPrintfFxn)();
extern int (*_rciBadAddrFxn)();
extern int (*_rciWallClockFxn)();
extern int (*_rciSetTraceFxn)();
extern int (*_rciClearTraceFxn)();
extern int (*_rciKutCallFxn)();

extern char moncomCmd[];
extern int moncomPhysmem;

static struct kabboard *bboards[MAX_CPUS];
static UAPAGE *uapage;

extern TASK_ENTRY *residentTaskQueue;

#include <varargs.h>

static printffxn(va_alist)
va_dcl
{
	/* Ugly ... the reason for this routine is that the aux. kernel
	   right now will not do a flush after "\n" is written to stdout.
	 */
	va_list ap;
	char *fmt;
	va_start(ap);
	fmt = va_arg(ap,char*);
	_doprnt(fmt,ap,stdout);
	fflush(stdout);
	va_end(ap);
}
	
auxMain(cpu)
int cpu;
{
	/* The first thing that is executed on an RCI slave CPU
	   after we have loaded it with our program.
	 */

	int k;
	extern int schedast();
	extern int clearast();
	extern void *getmem();
	extern monitorSignalHandler();
	extern int badaddr();
	extern int sendsignal();

	/* aux monitor setup */

	rciCpu = cpu;
	rciBus = (unsigned char *)getmapplace(MAP_QBUS);
	rciIOpage = (unsigned char *)getmapplace(MAP_IOPAGE);
	rciNumTasks = 0;
	_rciSchedAstFxn = schedast;
	_rciRestartAstsFxn = clearast;
	_rciSignalAuxFxn = (int(*)())NULL;
	rciMallocInit (getmem, 1024, NULL, 0);

	_rciSignalFxn = sendsignal;
	_rciUprintfFxn = printffxn;
	_rciPrintfFxn = printffxn;
	_rciBadAddrFxn = badaddr;
	_rciWallClockFxn = NULLF;
	_rciSetTraceFxn = NULLF;
	_rciClearTraceFxn = NULLF;

	_rciKutCallFxn = NULLF;

	for (k=0; k<MAX_CPUS; k++)
	 { bboards[k] = (struct kabboard *)getmapplace(MAP_BBOARD(k));
	 }
	for (k=0; k<MAX_CPUS; k++)
	 { _rciClockAddrs[k] = &(bboards[cpu]->time_rt[k]);
	   _rciClockHosts[k] = &(bboards[k]->time_rt[k]);
	 }
	uapage = (UAPAGE*)getmapplace(MAP_UAPAGE);
	_rciMaskAsts = &(uapage->block_asts);
	_rciAstsPending = &(uapage->astqsize);
	handlesig (MON_COMSIG, monitorSignalHandler);
	mutexInit(cpu);

	moncomPhysmem = physmem();

	printf ("Greetings from cpu %d!\n", cpu);
	fflush (stdout);

	while (1)
	 { if (moncomCmd[cpu])
	    { monitorCommand (moncomCmd[cpu]);
	      moncomCmd[cpu] = 0;
	    }
	   monitorRun(residentTaskQueue);	/* call with interrupt dev undefined */
	 }
}

#else
static int keepRanlibHappy;
#endif
