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

#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/file.h>
#include <signal.h>
#include <vax/mtpr.h>

char *malloc();
char *realloc();
char *copyofstr();
char *copyofblk();
int free();
#define NEW(type) ((type *) malloc(sizeof(type)))
#define OLD(x) free((char *) x)

#include "../sys/rciext/kaioctl.h"
#include "../sys/rciext/kamsgs.h"
#include "../sys/rciext/kacomm.h"

#include "getmapplace.h"

char *sbrk();

char device[] = "/dev/ka1";
int cpuno = 1;
int kafd;
struct blkdesc desc;
char msg[1024];
extern char etext;
extern char edata;
extern char end;
char Share[2048];
char *share;
int *value;

int Xint;

slavecrashed()
{
	CRASHINFO ci;
	char errmsg[256];

	ioctl(kafd,KAIOC_GETCRASH,&ci);
	crashMessage(errmsg,&ci,0);
	printf("slave crash: %s\n",errmsg);
	exit(0);
}

int slavemain();
int slaveast();

getpfn()
{
 register int p0br; /* r11 */

 p0br = P0BR;
 asm("mfpr r11,r11");
 Xint = ((int *)p0br)[Xint>>9];
}

main()
{
 kafd = open(device,O_RDWR,0);
 if (kafd < 0)
  { perror(device);
    exit(1);
  }
 share = (char *) ((((int)&Share[0])+1023)&~1023);
 value = (int *) share;
 mth$int_random();
 sendclear(kafd);
 sendtext(kafd);
 senddata(kafd);
 sendexec(kafd,slavemain);
	signal(SIGXCPU,slavecrashed);
	 { int sig;
	   sig = SIGXCPU;
	   ioctl(kafd,KAIOC_ONCRASH,&sig);
	 }
 while (1)
  { sleep(1);
  }
/* exit(0); */
}

static int foo = 0;
static int bar = -1;

slaveast()
{
 foo ++;
}

recurse (i)
{
	int k = i;
	if (i==10)
	 { *(int*)0xfffffff0 = 0;
	 }
	else
	 { k++;
	   recurse (k, k, k, k, k, k, k);
	 }
}

crash2()
{
	*(int*)0xfffffff0 = 0;
}

crash1()
{
	crash2(0x100, 0x200, 0x300);
}

slavemain()
{
 int i;

 printf ("\nAbout to crash\n");

 recurse (1, 1, 1, 1, 1, 1, 1);

 crash1(0x1, 0x2);

 printf ("down\n");

 while (1)
  { if (foo != bar)
     { bar = foo;
       printf("%d\n",foo);
       fflush(stdout);
     }
  }
}
