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

#define text    "Text sent by PPC processor\n"
#define	DEBUG	1

struct StartupData
{
	ULONG	MsgCount;
};

BPTR    MyFile;
void printf(char *String);

int	main(void)
{
struct TagItem		MyTags[10];
struct StartupData	*StartupData;
void			*PPCPort;
void			*ReplyPort;
void			*M68kMsg;
void			*Body;
ULONG			result;
ULONG			MsgCount;
ULONG			i;

  StartupData	=(struct StartupData *) PPCGetTaskAttr(PPCTASKTAG_STARTUP_MSGDATA);

  MsgCount	=	StartupData->MsgCount;

#if DEBUG
  if (MyFile = PPCOpen("con:0/0/640/200/MessageDemo - PPC output/CLOSE", MODE_NEWFILE))
  {
#endif

    if (PPCPort=(void*) PPCGetTaskAttr(PPCTASKTAG_MSGPORT))
    {
#if DEBUG
      printf("Waiting for M68k message\n");
#endif
      for (i=0;i<MsgCount;i++)
      {
        PPCWaitPort(PPCPort);

#if DEBUG
        printf("Getting message\n");
#endif
        if (M68kMsg = PPCGetMessage(PPCPort))
        {
#if DEBUG
          printf("Message: ");
          printf((char*) PPCGetMessageAttr(M68kMsg, PPCMSGTAG_DATA));
#endif
          PPCReplyMessage(M68kMsg);
        }
        else
        {
#if DEBUG
          printf("Did not get m68k msg\n");
#endif
        }
      }
    }
    else
    {
#if DEBUG
      printf("Could not find PPC Task`s msgport\n");
#endif
    }

#if DEBUG
    printf("Closing output\n");
    PPCClose(MyFile);
  }
#endif
}

void printf(char *String)
{
  PPCWrite(MyFile, String, strlen(String));
}

