#include <stdio.h>
#include <libraries/dos.h>


int   *vkadump;
int   maxspclen;
int   *manyptr;
APTR  *libptr;

int finish = 0;

CallInit()
{
FILE *fp;

fp = fopen("RAM:MCAinit","r");
if(fp == NULL) {
   Help("There is no routine RAM:MCAinit !\n");
   return;
}
fclose(fp);
SysCall("RAM:MCAinit");
}

ReadMCA()
{
FILE *fp;

fp = fopen("RAM:ReadMCA","r");
if(fp == NULL) {
   Help("There is no routine RAM:ReadMCA !\n");
   return;
}
fclose(fp);
SysCall("RAM:ReadMCA");
}

WriteMCA()
{
FILE *fp;

fp = fopen("RAM:WriteMCA","r");
if(fp == NULL) {
   Help("There is no routine RAM:WriteMCA !\n");
   return;
}
fclose(fp);
SysCall("RAM:WriteMCA");
}

ADC_ON()
{
FILE *fp;

fp = fopen("RAM:ADC_ON","r");
if(fp == NULL) {
   Help("There is no routine RAM:ADC_ON !\n");
   return;
}
fclose(fp);
SysCall("RAM:ADC_ON");
}

ADC_OFF()
{
FILE *fp;

fp = fopen("RAM:ADC_OFF","r");
if(fp == NULL) {
   Help("There is no routine RAM:ADC_OFF !\n");
   return;
}
fclose(fp);
SysCall("RAM:ADC_OFF");
}

MCA_exit()
{
finish = 1;
}


main(argc,argv)
int argc;
char *argv[];
{
int n,i;
char s[80];

   if(argc != 5) {
      fprintf(stderr,"Sorry, this programm must be started from MCAah!\n");
      fprintf(stderr,"You can change the entry \"HANDLER=\" in the Tooltypes\n");
      fprintf(stderr,"of MCAah to \"HANDLER=DemoHandler\"\n");
      fprintf(stderr,"This allows you to have 5 separate \"stupid\" programs\n");
      fprintf(stderr,"in RAM: which will be started to communicate with your\n");
      fprintf(stderr,"Hardware. These programms are:\n");
      fprintf(stderr,"RAM:MCAinit         to do some initialization\n");
      fprintf(stderr,"RAM:ReadMCA         to read out the Hardware\n");
      fprintf(stderr,"RAM:WriteMCA        to write back an old spectrum\n");
      fprintf(stderr,"RAM:ADC_ON          to start a new measurement\n");
      fprintf(stderr,"RAM:ADC_OFF         to stop your measurement\n");
      fprintf(stderr,"There are two Amiga environment variables, which\n");
      fprintf(stderr,"you will need:\n");
      fprintf(stderr,"env:VKADUMP         contains the Hex address of\n");
      fprintf(stderr,"                    the \"Display\" Buffer of MCAah\n");
      fprintf(stderr,"env:MAXSPCLEN       contains the length of this buffer\n");
      exit(0);
   }
   vkadump = (int *) xtoi(argv[1]);    /* reading buffer address */
   maxspclen = atoi(argv[2]);          /* length of buffer */
   manyptr = (int *) xtoi(argv[3]);    /* hardware address */
   libptr = (APTR *) xtoi(argv[4]);    /* "library" pointer */
   libptr[0] = (APTR) *ReadMCA;        /* fill in routines */
   libptr[1] = (APTR) *WriteMCA;
   libptr[2] = (APTR) *ADC_ON;
   libptr[3] = (APTR) *ADC_OFF;
   libptr[4] = (APTR) *MCA_exit;

   setenv2("VKADUMP",argv[1]);
   setenv2("MAXSPCLEN",argv[2]);

   CallInit();

/* now going to endless loop */
   finish = 0;
   while(finish == 0) Delay(100L);
}


SysCall(str)
char *str;
{
int i,n,m;
struct FileHandle *input, *output;
FILE *fp;

fp = fopen("c:runback","r"); /* check for existance of c:runback */
if(fp == NULL) {
   Help("Sorry, you don't have RUNBACK\nin your c: path.\nThis way you will not\nhave much fun with me\n");
   return;
}
fclose(fp);

input = (struct FileHandle *) Open("nil:",MODE_OLDFILE);
output = (struct FileHandle *) Open("nil:",MODE_NEWFILE);
   Execute(str,input,output);
Close(input);
Close(output);
}

xtoi(s)
char s[];
{
int i,m,l,e;
char c;

   l=strlen(s); e=0;
   for(i=0;i<l;i++) {
      c=s[i]; c = toupper(c);
      m=c-'0';
      if(m>9) m=m-7;
      e=16*e+m;
   }
   return(e);
}

setenv2(var,val)
char *var;
char *val;
{
FILE *fp;
char *s;
int n,m,i;

   s = (char *) malloc(80);
   strcpy(s,"env:"); strcat(s,var);
   fp = fopen(s,"w");
   if(fp != NULL) {
      fprintf(fp,"%s\n",val);
      fclose(fp);
   }
   free(s);
}

Help(s)
char *s;
{
FILE *fp1;

   fp1 = fopen("con:30/30/400/50/Message from DemoHandler:","r+");
   fprintf(fp1,"\n  %s",s);
   Delay(200);
   fclose(fp1);
}