home *** CD-ROM | disk | FTP | other *** search
/ Vectronix 2 / VECTRONIX2.iso / FILES_10 / DEVELOP.LZH / DSP / DSPDEBUG / MSGEX / MAIN.C < prev    next >
C/C++ Source or Header  |  1992-10-01  |  13KB  |  660 lines

  1. /*
  2.  
  3. DSP Debug gem pipe communication example.
  4. This programs shows in a graphic window the sine table built in the DSP ROM
  5. received word by word via the gem pipe from the DSP Debugger.
  6.  
  7. Brainstorm 07/92
  8.  
  9. */
  10.  
  11. #include    <stdio.h>
  12. #include    <string.h>
  13. #include    <ctype.h>
  14.  
  15. #include    <ext.h>
  16. #include    <tos.h>
  17. #include    <aes.h>
  18. #include    <vdi.h>
  19.  
  20. typedef unsigned char uchar;
  21. typedef unsigned short ushort;
  22. typedef unsigned long ulong;
  23. typedef unsigned float ufloat;
  24. typedef unsigned double udouble;
  25. typedef int bfint;
  26. typedef unsigned int bfuint;
  27.  
  28. #define    unused(x)    ((x) = (x))
  29. #define Debugger *(char *)NULL=0
  30.  
  31. #define    AP_TERM    50
  32. #define    SH_TERMOK    9
  33. #define    DSPMSG_MAGIC    127
  34.  
  35. typedef    enum {
  36.     STARTRECEIVE=0,
  37.     ENDRECEIVE
  38. } DSPMSG_ENUM;
  39.  
  40. typedef struct {
  41.     char    ttl[160];        /* wind title */
  42.     char    inf[160];        /* wind info */
  43.     short    whandle;        /* wind AES handle */
  44.     short    gflags;            /* wind AES flags */
  45.     GRECT    strt;            /* start rect */
  46.     GRECT    work;            /* curr wind work area (not inc. borders) */
  47.     GRECT    curr;            /* curr wind work area (inc. borders) */
  48. } WIND_STRUCT;
  49.  
  50. void    FreezeWinds(void);
  51. void    ReleaseWinds(void);
  52. void    FreezeMulti(void);
  53. void    ReleaseMulti(void);
  54. void    OnMouse(void);
  55. void    OffMouse(void);
  56. void    FreeMouse(void);
  57. void    BusyMouse(void);
  58.  
  59. void    InitGrafPort(void);
  60. void    ClipOff(GRECT *);
  61. void    ClipOn(GRECT *);
  62.  
  63. void    XY2WH(GRECT *,short *);
  64. void    WH2XY(short *,GRECT *);
  65. short    RectInter(GRECT *,GRECT *,GRECT *);
  66. void    *Rect2Mfdb(MFDB *,GRECT *);
  67.  
  68. void    GetScreenCoords(GRECT *);
  69. void    GetCurrCoords(WIND_STRUCT *,GRECT *);
  70. void    GetWorkCoords(WIND_STRUCT *,GRECT *);
  71. void    GetCurrWindCoords(WIND_STRUCT *);
  72. void    GetWorkWindCoords(WIND_STRUCT *);
  73. void    SetCurrWindCoords(WIND_STRUCT *,GRECT *);
  74.  
  75. WIND_STRUCT    *CreateWind(WIND_STRUCT *,char *,char *);
  76. void    DeleteWind(WIND_STRUCT *);
  77. short    _SetTopWind(short);
  78. void    HideWind(WIND_STRUCT *);
  79. void    ShowWind(WIND_STRUCT *);
  80. void    DoRectWind(WIND_STRUCT *,GRECT *,void(*fct)(WIND_STRUCT *,GRECT *));
  81. WIND_STRUCT    *GetWindByHandle(short whandle);
  82. short    SetTopWind(WIND_STRUCT *);
  83. void    RedrawWind(WIND_STRUCT *,GRECT *);
  84.  
  85. short    GEMInit(void);
  86. void    GEMExit(void);
  87. short    DoEvents(short *);
  88. void    AccInit(void);
  89. void    MainLoop(void);
  90.  
  91. extern    void    Display(long);
  92.  
  93. short    MultiFlag,Apid,Vdih,Planes,ToApid=-1;
  94. WIND_STRUCT    Wind,*CurWind=NULL;
  95. extern    short    _app;
  96. extern    void    *DisplayBuffer;
  97.  
  98. /* #[ Low level AES: */
  99. void    FreezeWinds()
  100. {
  101.     wind_update(BEG_UPDATE);
  102. }
  103.  
  104. void    ReleaseWinds()
  105. {
  106.     wind_update(END_UPDATE);
  107. }
  108.  
  109. void    FreezeMulti()
  110. {
  111.     wind_update(BEG_MCTRL);
  112. }
  113.  
  114. void    ReleaseMulti()
  115. {
  116.     wind_update(END_MCTRL);
  117. }
  118.  
  119. void    GemOff()
  120. {
  121.     FreezeWinds();
  122.     FreezeMulti();
  123. }
  124.  
  125. void    GemOn()
  126. {
  127.     ReleaseMulti();
  128.     ReleaseWinds();
  129. }
  130.  
  131. void    GrafMouse(short mtype,MFORM *mform)
  132. {
  133.     graf_mouse(mtype,mform);
  134. }
  135. void    OnMouse()
  136. {
  137.     GrafMouse(M_ON,NULL);
  138. }
  139.  
  140. void    OffMouse()
  141. {
  142.     GrafMouse(M_OFF,NULL);
  143. }
  144.  
  145. void    FreeMouse()
  146. {
  147.     GrafMouse(ARROW,NULL);
  148. }
  149.  
  150. void    BusyMouse()
  151. {
  152.     GrafMouse(HOURGLASS,NULL);
  153. }
  154.  
  155. void    InitGrafPort()
  156. {
  157.     short    handle=Vdih;
  158.  
  159.     vswr_mode(handle,MD_REPLACE);    /* mode remplacement */
  160.     vsl_type(handle,SOLID);    /* ligne pleine */
  161.     vsl_width(handle,1);    /* epaisseur ligne 1 point */
  162.     vsl_ends(handle,ROUND,ROUND);    /* debut,fin ligne arrondie */
  163.     vsm_type(handle,1);    /* marqueur point */
  164.     vsf_interior(handle,0);    /* motif de remplissage plein blanc */
  165. }
  166.  
  167. void    ClipOn(GRECT *rect)
  168. {
  169.     short    xyarray[4];
  170.  
  171.     WH2XY(xyarray,rect);
  172.     vs_clip(Vdih,1,xyarray);
  173. }
  174.  
  175. void    ClipOff(GRECT *rect)
  176. {
  177.     short    xyarray[4];
  178.  
  179.     WH2XY(xyarray,rect);
  180.     vs_clip(Vdih,0,xyarray);
  181. }
  182.  
  183. /* #] Low level AES: */
  184. /* #[ Rectangles: */
  185. short    min(short a,short b)
  186. {
  187.     if (a<b)
  188.         return a;
  189.     return b;
  190. }
  191.  
  192. short    max(short a,short b)
  193. {
  194.     if (a>b)
  195.         return a;
  196.     return b;
  197. }
  198.  
  199. void    XY2WH(GRECT *rect,short *array)
  200. {
  201.     rect->g_x=array[0];
  202.     rect->g_y=array[1];
  203.     rect->g_w=array[2]-array[0]+1;
  204.     rect->g_h=array[3]-array[1]+1;
  205. }
  206.  
  207. void    WH2XY(short *array,GRECT *rect)
  208. {
  209.     array[0]=rect->g_x;
  210.     array[1]=rect->g_y;
  211.     array[2]=rect->g_w+array[0]-1;
  212.     array[3]=rect->g_h+array[1]-1;
  213. }
  214.  
  215. short    RectInter(GRECT *rs,GRECT *rd,GRECT *inter)
  216. {
  217.     inter->g_x=max(rd->g_x,rs->g_x);
  218.     inter->g_y=max(rd->g_y,rs->g_y);
  219.     inter->g_w=min(rd->g_x+rd->g_w,rs->g_x+rs->g_w);
  220.     inter->g_h=min(rd->g_y+rd->g_h,rs->g_y+rs->g_h);
  221.     if (((inter->g_w-=inter->g_x)>0) && ((inter->g_h-=inter->g_y)>0))
  222.         return(1);
  223.     return(0);
  224. }
  225.  
  226. void    RectInset(short xoffset,short yoffset,GRECT *r)
  227. {
  228.     r->g_w-=xoffset;
  229.     r->g_h-=yoffset;
  230. }
  231.  
  232. void    RectOffset(short xoffset,short yoffset,GRECT *r)
  233. {
  234.     r->g_x+=xoffset;
  235.     r->g_y+=yoffset;
  236. }
  237.  
  238. void    RectCenter(GRECT *rs,short xoffset,short yoffset,GRECT *rd)
  239. {
  240.     *rd=*rs;
  241.     RectInset(xoffset*2,yoffset*2,rd);
  242.     RectOffset(xoffset,yoffset,rd);
  243. }
  244.  
  245. void    *Rect2Mfdb(MFDB *mfdb,GRECT *r)
  246. {
  247.     short    g_w,planes;
  248.     void    *addr;
  249.  
  250.     mfdb->fd_w=r->g_w;
  251.     mfdb->fd_h=r->g_h;
  252.     g_w=r->g_w/16+((r->g_w%16)?1:0);
  253.     mfdb->fd_wdwidth=g_w;
  254.     mfdb->fd_stand=0;
  255.     planes=Planes;
  256.     mfdb->fd_nplanes=planes;
  257.     addr=malloc(g_w*r->g_h*planes);
  258.     mfdb->fd_addr=addr;
  259.     return(addr);
  260. }
  261.  
  262. /* #] Rectangles: */
  263. /* #[ Windows:*/
  264.     /*#[ AES calls:*/
  265. short    _SetTopWind(short handle)
  266. {
  267.     return(wind_set(handle,WF_TOP));
  268. }
  269.  
  270. short    _GetTopWind()
  271. {
  272.     short    top_handle,dummy;
  273.  
  274.     wind_get(0,WF_TOP,&top_handle,&dummy,&dummy,&dummy);
  275.     return(top_handle);
  276. }
  277.  
  278. short    _GetUnderWind(short mx,short my)
  279. {
  280.     return(wind_find(mx,my));
  281. }
  282.  
  283. void    GetScreenCoords(GRECT *spos)
  284. {
  285.     wind_get(0,WF_WORKXYWH,&(spos->g_x),&(spos->g_y),&(spos->g_w),&(spos->g_h));
  286. }
  287.  
  288. void    GetCurrCoords(WIND_STRUCT *wind,GRECT *rect)
  289. {
  290.     wind_get(wind->whandle,WF_CURRXYWH,&(rect->g_x),&(rect->g_y),&(rect->g_w),&(rect->g_h));
  291. }
  292.  
  293. void    GetWorkCoords(WIND_STRUCT *wind,GRECT *rect)
  294. {
  295.     wind_get(wind->whandle,WF_WORKXYWH,&(rect->g_x),&(rect->g_y),&(rect->g_w),&(rect->g_h));
  296. }
  297.  
  298. void    GetIntCoords(WIND_STRUCT *wind,GRECT *from,GRECT *to)
  299. {
  300.     wind_calc(WC_WORK,wind->gflags,from->g_x,from->g_y,from->g_w,from->g_h,&(to->g_x),&(to->g_y),&(to->g_w),&(to->g_h));
  301. }
  302.  
  303. void    GetExtCoords(WIND_STRUCT *wind,GRECT *from,GRECT *to)
  304. {
  305.     wind_calc(WC_BORDER,wind->gflags,from->g_x,from->g_y,from->g_w,from->g_h,&(to->g_x),&(to->g_y),&(to->g_w),&(to->g_h));
  306. }
  307.  
  308. short    SendMsg(short *msg,long msgsize,short msgcode,short apid)
  309. {
  310.     char    buf[64];
  311.  
  312.     msg[0]=msgcode;
  313.     msg[1]=apid;
  314.     if (msgsize>16)
  315.         msg[2]=(short)(msgsize-16);
  316.     else
  317.         msg[2]=0;
  318.     if ((appl_write(apid,(short)msgsize,msg))==0) {
  319.         sprintf(buf,"[1][Error in appl_writing %d|to application %d][OK]",msgcode,apid);
  320.         form_alert(1,buf);
  321.         return(0);
  322.     }
  323.     return(1);
  324. }
  325.  
  326. short    SendMeMsg(short *msg,long msgsize,short msgcode)
  327. {
  328.     return(SendMsg(msg,msgsize,msgcode,Apid));
  329. }
  330.  
  331. short    SendWindMsg(short *msg,long msgsize,short msgcode,WIND_STRUCT *wind)
  332. {
  333.     msg[3]=wind->whandle;
  334.     return(SendMeMsg(msg,msgsize,msgcode));
  335. }
  336.     /*#] AES calls:*/
  337.     /*#[ Coords:*/
  338. void    GetCurrWindCoords(WIND_STRUCT *wind)
  339. {
  340.     GetCurrCoords(wind,&(wind->curr));
  341. }
  342.  
  343. void    GetWorkWindCoords(WIND_STRUCT *wind)
  344. {
  345.     GetWorkCoords(wind,&(wind->work));
  346. }
  347.  
  348. void    SetCurrWindCoords(WIND_STRUCT *wind,GRECT *rect)
  349. {
  350.     wind_set(wind->whandle,WF_CURRXYWH,rect->g_x,rect->g_y,rect->g_w,rect->g_h);
  351.     GetCurrWindCoords(wind);
  352. }
  353.     /*#] Coords:*/
  354.     /*#[ Show/Hide:*/
  355. void    SetWindTtl(WIND_STRUCT *wind,char *ttl)
  356. {
  357.     char    *buf;
  358.  
  359.     if (wind->gflags&NAME) {
  360.         buf=wind->ttl;
  361.         *buf++=' ';
  362.         strcpy(buf,ttl);
  363.         buf+=strlen(buf);
  364.         *buf++=' ';
  365.         *buf++=0;
  366.         wind_set(wind->whandle,WF_NAME,wind->ttl);
  367.     }
  368. }
  369.  
  370. void    SetWindInf(WIND_STRUCT *wind,char *inf)
  371. {
  372.     char    *buf;
  373.  
  374.     if (wind->gflags&INFO) {
  375.         buf=wind->inf;
  376.         *buf++=' ';
  377.         strcpy(buf,inf);
  378.         buf+=strlen(buf);
  379.         *buf++=' ';
  380.         *buf++=0;
  381.         wind_set(wind->whandle,WF_INFO,wind->inf);
  382.     }
  383. }
  384.  
  385. void    ShowWind(WIND_STRUCT *wind)
  386. {
  387.     GRECT    *strt,open;
  388.  
  389.     strt=&(wind->strt);
  390.     GetExtCoords(wind,strt,&open);
  391.     OffMouse();
  392.     wind_open(wind->whandle,strt->g_x,strt->g_y,open.g_w,open.g_h);
  393.     OnMouse();
  394. }
  395.  
  396. void    HideWind(WIND_STRUCT *wind)
  397. {
  398.     GetCurrWindCoords(wind);
  399.     OffMouse();
  400.     wind_close(wind->whandle);
  401.     OnMouse();
  402. }
  403.  
  404.     /*#] Show/Hide:*/
  405.     /*#[ Manage:*/
  406. WIND_STRUCT    *GetWindByHandle(short whandle)
  407. {
  408.     WIND_STRUCT    *wind=CurWind;
  409.  
  410.     if (wind) {
  411.         if (wind->whandle==whandle)
  412.             return(wind);
  413.     }
  414.     return(NULL);
  415. }
  416.  
  417. WIND_STRUCT    *CreateWind(WIND_STRUCT *wind,char *ttl,char *inf)
  418. {
  419.     short    whandle;
  420.     GRECT    *strt,*curr;
  421.  
  422.     wind->gflags=CLOSER|MOVER|NAME|INFO;
  423.     strt=&(wind->strt);
  424.     curr=&(wind->curr);
  425.     GetExtCoords(wind,strt,curr);
  426.     whandle=wind_create(wind->gflags,strt->g_x,strt->g_y,curr->g_w,curr->g_h);
  427.     wind->whandle=whandle;
  428.     if (whandle<0) {
  429.         return(NULL);
  430.     }
  431.     SetWindTtl(wind,ttl);
  432.     SetWindInf(wind,inf);
  433.     ShowWind(wind);
  434.     return(wind);
  435. }
  436.  
  437. void    DeleteWind(WIND_STRUCT *wind)
  438. {
  439.     if (wind) {
  440.         HideWind(wind);
  441.         wind_delete(wind->whandle);
  442.     }
  443. }
  444.  
  445. short    SetTopWind(WIND_STRUCT *wind)
  446. {
  447.     short    err;
  448.  
  449.     if ((err=_SetTopWind(wind->whandle))!=0) {
  450.         GetCurrWindCoords(wind);
  451.     }
  452.     return(err);
  453. }
  454.  
  455. void    _GenDraw(WIND_STRUCT *wind,GRECT *inter_rect)
  456. {
  457.     GRECT    *work=&(wind->work);
  458.     GRECT    org={0,0,128,128};
  459.     MFDB    orgmfdb,screen;
  460.     short    pxyarray[8],colors[2]={2,0};
  461.  
  462.     orgmfdb.fd_addr=&(DisplayBuffer);
  463.     orgmfdb.fd_w=128;
  464.     orgmfdb.fd_h=128;
  465.     orgmfdb.fd_wdwidth=8;
  466.     orgmfdb.fd_stand=0;
  467.     orgmfdb.fd_nplanes=1;
  468.     screen.fd_addr=NULL;
  469.     WH2XY(pxyarray,&org);
  470.     WH2XY(&(pxyarray[4]),work);
  471.     ClipOn(inter_rect);
  472.     OffMouse();
  473.     vrt_cpyfm(Vdih,MD_REPLACE,pxyarray,&orgmfdb,&screen,colors);
  474.     OnMouse();
  475.     ClipOff(inter_rect);
  476. }
  477.  
  478. void    DoRectWind(WIND_STRUCT *wind,GRECT *redraw_rect,void(*fct)(WIND_STRUCT *,GRECT *))
  479. {
  480.     short    whandle;
  481.     GRECT    wpos,inter_rect;
  482.  
  483.     FreezeWinds();
  484.     whandle=wind->whandle;
  485.     GetWorkWindCoords(wind);
  486.     InitGrafPort();
  487.     wind_get(whandle,WF_FIRSTXYWH,&(wpos.g_x),&(wpos.g_y),&(wpos.g_w),&(wpos.g_h));
  488.     while ((wpos.g_w!=0) || (wpos.g_h!=0)) {
  489.         if (RectInter(redraw_rect,&wpos,&inter_rect)) {
  490.             fct(wind,&inter_rect);
  491.         }
  492.         wind_get(whandle,WF_NEXTXYWH,&wpos.g_x,&wpos.g_y,&wpos.g_w,&wpos.g_h);
  493.     }
  494.     ReleaseWinds();
  495. }
  496.  
  497. void    RedrawWind(WIND_STRUCT *wind,GRECT *redraw_rect)
  498. {
  499.     DoRectWind(wind,redraw_rect,_GenDraw);
  500. }
  501.  
  502. void    ManageWinds(short *msg)
  503. {
  504.     short    whandle;
  505.     WIND_STRUCT    *wind;
  506.     GRECT    *ncoords=(GRECT *)&msg[4];
  507.  
  508.     whandle=msg[3];
  509.     wind=GetWindByHandle(whandle);
  510.     if (wind==NULL)
  511.         return;
  512.     switch (msg[0]) {
  513.         case WM_CLOSED:
  514.         break;
  515.         case WM_TOPPED:
  516.             SetTopWind(wind);
  517.         break;
  518.         case WM_REDRAW:
  519.             RedrawWind(wind,ncoords);
  520.         break;
  521.         case WM_MOVED:
  522.             SetCurrWindCoords(wind,ncoords);
  523.         break;
  524.     }
  525. }
  526.  
  527. short    DoEvents(short *msg)
  528. {
  529.     short    event,bstate=1,key,mx,my,mbutton,mstate,mclicks;
  530.     long    dspword;
  531.  
  532.     event=evnt_multi(MU_MESAG,
  533.             2,1,bstate,
  534.             0,0,0,0,0,
  535.             0,0,0,0,0,
  536.             msg,
  537.             1,0,
  538.             &mx,&my,
  539.             &mbutton,&mstate,
  540.             (short *)&key,&mclicks);
  541.     if (event&MU_MESAG) {
  542.         switch (msg[0]) {
  543.             case DSPMSG_MAGIC:    /* DSP word in msg[3]<<16|msg[4] */
  544.                 if (ToApid!=-1) {
  545.                     dspword=((long)(msg[3])<<16)+msg[4];
  546.                     Display(dspword);    /* put in buffer */
  547.                     if (CurWind)        /* redraw display window */
  548.                         RedrawWind(CurWind,&(CurWind->work));
  549.                 }
  550.             break;
  551.             case AP_TERM:
  552.                 msg[0]=AC_CLOSE;
  553.             break;
  554.             default:
  555.                 ManageWinds(msg);
  556.             break;
  557.         }
  558.     }
  559.     return(event&MU_MESAG);
  560. }
  561.     /*#] Manage:*/
  562. /* #] Windows:*/
  563. short    GEMInit()
  564. {
  565.     short    dum;
  566.     short    win[11]={0,1,1,1,1,1,1,1,1,1,2};
  567.     short    wout[57];
  568.  
  569.     if ((Apid=appl_init())>=0) {
  570.         win[0]=Getrez()+2;
  571.         if (_GemParBlk.global[1]==-1) {    /* AES 4.0 */
  572.             MultiFlag=1;
  573.             win[0]=_GemParBlk.global[13];
  574.             shel_write(SH_TERMOK,1,0,NULL,NULL);
  575.         }
  576.         if (_app==0) {
  577.             if (menu_register(Apid,"  DSPDisplay ")==-1) {
  578.                 appl_exit();
  579.                 return(-1);
  580.             }
  581.         }
  582.         Vdih=graf_handle(&dum,&dum,&dum,&dum);
  583.         v_opnvwk(win,&Vdih,wout);
  584.         vq_extnd(Vdih,1,wout);
  585.         Planes=wout[4];
  586.     }
  587.     else {
  588.         Cconws("Appl_init error - Press a key\r\n");
  589.         Bconin(2);
  590.     }
  591.     return(Apid);
  592. }
  593.  
  594. void    GEMExit()
  595. {
  596.     v_clsvwk(Vdih);
  597.     appl_exit();
  598. }
  599.  
  600. void    AccInit()
  601. {
  602.     short    msg[8];
  603.     GRECT    *strt=&(Wind.strt);
  604.  
  605.     if ((ToApid==-1) && (ToApid=appl_find("DSPDEBUG"))!=-1) {
  606.         msg[3]=STARTRECEIVE;    /* Init DSP receive catch */
  607.         msg[4]=Apid;        /* send to Appl Id */
  608.         SendMsg(msg,sizeof(msg),DSPMSG_MAGIC,ToApid);    /* appl write it */
  609.     }
  610.     if (CurWind==NULL) {
  611.         GetScreenCoords(strt);
  612.         strt->g_w=128;
  613.         strt->g_h=128;
  614.         CurWind=CreateWind(&Wind,"DSP","Sine table");
  615.         Display(0L);            /* Init X coords line */
  616.     }
  617.     else
  618.         SetTopWind(CurWind);
  619. }
  620.  
  621. void    MainLoop()
  622. {
  623.     short    evnt,msg[8];
  624.  
  625.     if (_app)    /* If program, init as accessory */
  626.         AccInit();
  627.     while (1) {
  628.         evnt=DoEvents(msg);
  629.         if (evnt) {
  630.             switch (msg[0]) {
  631.                 case AC_OPEN:
  632.                     AccInit();    /* if acc, open */
  633.                 break;
  634.                 case WM_CLOSED:
  635.                 case AC_CLOSE:
  636.                     DeleteWind(CurWind);
  637.                     CurWind=NULL;
  638.                     if (ToApid!=-1) {
  639.                         msg[3]=ENDRECEIVE;    /* Exit DSP receive catch */
  640.                         SendMsg(msg,sizeof(msg),DSPMSG_MAGIC,ToApid);
  641.                         ToApid=-1;
  642.                     }
  643.                     if (_app)    /* If program, exit */
  644.                         return;    /* else loop */
  645.             }
  646.         }
  647.     }
  648. }
  649.  
  650. main()
  651. {
  652.     if (GEMInit()>=0) {
  653.         FreeMouse();
  654.         MainLoop();
  655.         GEMExit();
  656.     }
  657.     return(0);
  658. }
  659.  
  660.