/* Check amount and type of memory and place in appropriate order */
/* Execute SetPatch with option [r] as appropriate */
/* © Neil G. Matthews, Henley Beach, South Australia */

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/exec.h>
#include <exec/execbase.h>
#include <exec/execname.h>
#include <libraries/dos.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <string.h>

void main() /* Version 1.1 */

/* Write used instead of printf to reduce size of executable */

{


  char *address;
  int success;
  long EB;
  struct ExecBase *ExBase;
  
  EB = OpenLibrary(EXECNAME,0);

  ExBase = (struct ExecBase *) EB;

/* Check ExecBase Max Local Memory value to determine chip memory size */
  if (ExBase->MaxLocMem == 0x100000)
	success = system("sys:C/SetPatch r");
  else
  if (ExBase->MaxLocMem == 0x80000)
	success = system("sys:C/SetPatch");
  else
    {
	Write(Output(),
	"\x9B7;3;32;43mUnknown Memory Configuration!\x9B0m\n",44);
	exit(20);
    }

  if(success == 0) /* i.e. SetPatch was successful */
    {
     /* Check for existence of memory at $C80000 */
     if ((ULONG)ExBase->MaxExtMem == 0xc80000)
       {
	/*  Attempt allocation of 512kB of Fast Memory */
	/*  Assumes < 512kB of Non Chip, Non Fast Memory */
	if ((address = AllocMem(1<<19,MEMF_FAST)) == 0)
	  {
	   Write(Output(),
		"\x9B7;3;32;43mNo Fast Memory found.\x9B0m\n",36);
	   exit(10);
	  }
	else
	  {
	    FreeMem(address,1<<19);
	    /* Move $C0000 to last in Memory free list */
	   success = system("sys:system/FastMemFirst");
	   if (success == 0)
	     {
		Write(Output(),
   "\x9B7;3;32;43mFast Memory allocated before $C0000 memory.\x9B0m\n",58);
	     }
	  }
       }
    } /* no need to use else clause as SetPatch gives message */
      /* "SetPatch has already been installed." */
}
