home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #4 / Amiga Plus CD - 2000 - No. 4.iso / PowerPC / Dev / PPCRelease / Examples / IPC / Msg6.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-07  |  4.8 KB  |  173 lines

  1. /* Msg6 and Msg6PPC.elf
  2.  *
  3.  * This test program sends 1000 messages to the M68k and
  4.  * shows how long this takes.
  5.  * The Messages are send asynchron and every msg must
  6.  * be replied after another.
  7.  * Each Message has the Body "Text sent by PPC processor\n"
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <exec/nodes.h>
  12. #include <exec/lists.h>
  13. #include <exec/memory.h>
  14. #include <utility/tagitem.h>
  15. #include <powerup/ppclib/interface.h>
  16. #include <powerup/ppclib/message.h>
  17. #include <powerup/ppclib/tasks.h>
  18. #include <powerup/proto/ppc.h>
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "time.h"
  24. #include "time_protos.h"
  25.  
  26. #define    MSGCOUNT    1000
  27. #define    DEBUG        0
  28.  
  29. struct StartupData
  30. {
  31.     void    *MsgPort;
  32.     ULONG    MsgCount;
  33. };
  34.  
  35. extern struct Library    *SysBase;
  36.  
  37. int    main(void)
  38. {
  39. struct Library        *PPCLibBase;
  40. struct TagItem        MyTags[10];
  41. void            *M68kPort;
  42. void            *StartupMsg;
  43. void            *PPCMsg;
  44. void            *ElfObject;
  45. void            *Task;
  46. struct StartupData    *StartupData;
  47. void            *MyTimerObject;
  48. ULONG            i;
  49.  
  50.   Printf("Opening ppc.library\n");
  51.   if (PPCLibBase = OpenLibrary("ppc.library", 44))
  52.   {
  53.     if (MyTimerObject=TimerCreateObject())
  54.     {
  55.       Printf("Loading PPC object\n");
  56.       if (ElfObject=PPCLoadObject("PROGDIR:Msg6PPC.elf"))
  57.       {
  58.         Printf("Creating message port...");
  59.         MyTags[0].ti_Tag    =    TAG_DONE;
  60.         if (M68kPort = PPCCreatePort(MyTags))
  61.         {
  62.           if (StartupMsg = PPCCreateMessage(M68kPort, 0))
  63.           {
  64.             Printf("Allocating StartupData\n");
  65.             if (StartupData = PPCAllocVec(sizeof(struct StartupData), MEMF_ANY))
  66.             {
  67.               StartupData->MsgPort    =    M68kPort;
  68.               StartupData->MsgCount    =    MSGCOUNT;
  69.  
  70.               Printf("Creating PPC task\n");
  71.               MyTags[0].ti_Tag        =    PPCTASKTAG_STARTUP_MSG;
  72.               MyTags[0].ti_Data        =(ULONG) StartupMsg;
  73.  
  74.               MyTags[1].ti_Tag        =    PPCTASKTAG_STARTUP_MSGDATA;
  75.               MyTags[1].ti_Data        =(ULONG) StartupData;
  76.  
  77.               MyTags[2].ti_Tag        =    PPCTASKTAG_STARTUP_MSGLENGTH;
  78.               MyTags[2].ti_Data        =    0;
  79.  
  80.               MyTags[3].ti_Tag        =    PPCTASKTAG_STARTUP_MSGID;
  81.               MyTags[3].ti_Data        =    0;
  82.  
  83.               MyTags[4].ti_Tag        =    PPCTASKTAG_MSGPORT;
  84.               MyTags[4].ti_Data        =    TRUE;
  85.  
  86.               MyTags[5].ti_Tag        =    TAG_DONE;
  87.  
  88.               if (Task = PPCCreateTask(ElfObject, MyTags))
  89.               {
  90.                 Printf("Receive 1000 messages with a *Body* and reply them...");
  91.                 TimerSetAttr(MyTimerObject,TIMERTAG_START);
  92.                 i    =    0;
  93.                 while (i<MSGCOUNT)
  94.                 {
  95.                   PPCWaitPort(M68kPort);
  96.                   while (i<MSGCOUNT && (PPCMsg = PPCGetMessage(M68kPort)))
  97.                   {
  98. #if DEBUG
  99.                     MsgID    =    PPCGetMessageAttr(PPCMsg, PPCMSGTAG_MSGID);
  100.                     Printf("Received 0x%lx(%ld)=%ld Port 0x%lx\n",
  101.                            PPCMsg,
  102.                            MsgID,
  103.                            i,
  104.                            ((struct Message*) PPCMsg)->mn_ReplyPort);
  105. #endif
  106.                     PPCReplyMessage(PPCMsg);
  107.                     i++;
  108.                   }
  109.                 }
  110.                 TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  111.                 TimerShow(MyTimerObject);
  112.  
  113.                 Printf("Waiting for Task Finish Msg...\n");
  114.                 for (;;)
  115.                 {
  116.                   if ((PPCMsg=PPCGetMessage(M68kPort)) == StartupMsg)
  117.                   {
  118.                     Printf("Ahh..the expected Startup=Finish Msg was received.\nNow we can savely free all resources.\n");
  119.                     break;
  120.                   }
  121.                   else
  122.                   {
  123.                     Printf("Some non replied Msg 0x%lx was found..wait\n",
  124.                            PPCMsg);
  125.                     PPCWaitPort(M68kPort);
  126.                   }
  127.                 }
  128.  
  129.                 Printf("Deleting message...\n");
  130.               }
  131.               else
  132.               {
  133.                 Printf("Could not create PPC task\n");
  134.               }
  135.               PPCFreeVec(StartupData);
  136.             }
  137.             else
  138.             {
  139.               Printf("Could not alloc Startup Data\n");
  140.             }
  141.             PPCDeleteMessage(StartupMsg);
  142.           }
  143.           else
  144.           {
  145.             Printf("Could not create Startup message\n");
  146.           }
  147.           Printf("Deleting message port...");
  148.           while (PPCDeletePort(M68kPort) == FALSE);
  149.         }
  150.         else
  151.         {
  152.           Printf("Could not create M68k MsgPort\n");
  153.         }
  154.         Printf("Unloading PPC object\n");
  155.         PPCUnLoadObject(ElfObject);
  156.       }
  157.       else
  158.       {
  159.         Printf("Could not load the elfobject\n");
  160.       }
  161.       TimerDeleteObject(MyTimerObject);
  162.     }
  163.     Printf("Closing ppc.library\n");
  164.     CloseLibrary(PPCLibBase);
  165.   }
  166.   else
  167.   {
  168.     Printf("Could not open ppc.library v44+\n");
  169.   }
  170.   return(0);
  171. }
  172.  
  173.