#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <exec/memory.h>
#include <powerup/ppclib/interface.h>
#include <powerup/gcclib/powerup_protos.h>

#define	TEXT	"This is a test\n"

UWORD	M68kProgram[]=
{
/*
Color:
;d0=color
	move.l	d2,-(a7)
	moveq	#-1,d1
	moveq	#20,d2
0$:
	move.w	d0,$dff180
	dbra	d1,0$
	dbra	d2,0$
	move.l	(a7)+,d2
	rts
*/

  0x2f02,
  0x72ff,
  0x7414,
  0x33c0,
  0x00df,
  0xf180,
  0x51c9,
  0xfff8,
  0x51ca,
  0xfff4,
  0x241f,
  0x4e75
};


int Function(APTR	Function)
{
BPTR	MyFile;
struct Caos	*MyCaos;
ULONG		DataArray[10];

  if (MyFile=PPCOpen("con:0/0/640/200/WindowOpenedByPPC/CLOSE",MODE_NEWFILE))
  {
    PPCWrite(MyFile,
             TEXT,
             strlen(TEXT));

    DataArray[0]	=	0x100;
    DataArray[1]	=	(ULONG) PPCAllocVec(DataArray[0],MEMF_PUBLIC|MEMF_CLEAR);

    PPCRawDoFmt("Allocated %ld Bytes at Address 0x%lx\n",
                &DataArray,
                1,				/* 0=Buffer,1=serial <> NOT supported yet */
                NULL);

    if (MyCaos = (struct Caos*) PPCAllocVec(sizeof(struct Caos),MEMF_PUBLIC|MEMF_CLEAR))
    {
      MyCaos->caos_Un.Function	=	(APTR) &M68kProgram;
      /* Look at the function above and you see that this
         function doesn`t touch any memory the PPC uses too or
         will use after the function returns.
         BUT let`s assume you would call AllocMem now and pass back
         that ptr to the PPC program you would need to set CacheFlush
         TRUE. Attention..you`re aren`t allowed to use normal AllocMem
         areas anyway because of the needed CacheLine Alignment only
         the ppc.library functions offer.
       */
      MyCaos->M68kCacheMode	=	IF_CACHEFLUSHNO;
      MyCaos->PPCCacheMode	=	IF_CACHEFLUSHNO;
      MyCaos->d0		=	0x0ff;
      PPCCallM68k(MyCaos);
      PPCFreeVec(MyCaos);
    }
    if (DataArray[1])
    {
      PPCFreeVec((APTR) DataArray[1]);
    }
    PPCClose(MyFile);
    return(1);
  }
  return(0);
}
