/* $Header: show.m,v 1.1 91/08/23 16:28:15 chu mp310d $

                    Copyright (c) 1991
                    MasPar Computer Corporation
                    All Rights Reserved

    This program has been licensed subject to restrictions
    on reproduction, use and disclosure and this notice
    does not imply that the program or the related ideas
    or algorithms are in the public domain.
*/

#include <stdio.h>
#include <mpl.h>
#define MIN(a,b)  (((a) < (b)) ? (a) : (b))
#define MAX(a,b)  (((a) > (b)) ? (a) : (b))
#define NBY(n)    ((( (n) + nyproc - 1 ) >> lnyproc))
#define NBX(n)    ((( (n) + nxproc - 1 ) >> lnxproc))
#define BLOCKY(i) ((i) >> lnyproc)
#define BLOCKX(i) ((i) >> lnxproc)
#define POINTY(i) ((i) & (nyproc - 1))
#define POINTX(i) ((i) & (nxproc - 1))
#define Ablk(i,j) a[(i)*nbx + (j)]

getsubmatrix(a, m, n, ioff, joff, temp)  /*  temp = a offset 
                                              i in y and j in x */
plural double a[], *temp;
int ioff, joff, m, n;
{
 register int ipoff, iboff, jpoff, jboff;
 int nby, nbx;
 plural register int jyproc, jxproc;
 plural double register temp1;

   jyproc = iyproc;
   jxproc = ixproc;
   nby = NBY(m);
   nbx = NBX(n);
   iboff = BLOCKY(ioff);
   jboff = BLOCKX(joff);
   ipoff = POINTY(ioff);
   jpoff = POINTX(joff);
   
   *temp = 0;
   temp1 = Ablk(iboff,jboff);
   if (ipoff > 0) temp1 = xnetS[ipoff].temp1;
   if (iyproc < nyproc-ipoff & ixproc < nxproc - jpoff) 
                  *temp = xnetE[jpoff].temp1;
   
   if (ipoff > 0 && iboff < nby-1) {
      temp1 = Ablk((1+iboff) , jboff);
      temp1 = xnetN[nyproc -ipoff].temp1;
      if (iyproc >= nyproc-ipoff & ixproc < nxproc - jpoff) 
                  *temp = xnetE[jpoff].temp1;
      }
   
   if (jpoff > 0 && jboff < nbx-1) {
      temp1 = Ablk((iboff) , jboff + 1);
      if (ipoff > 0) temp1 = xnetS[ipoff].temp1;
      if (iyproc < nyproc-ipoff & ixproc >= nxproc - jpoff) 
                  *temp = xnetW[nxproc - jpoff].temp1;
      }
   
   if (jpoff > 0 && ipoff > 0 && jboff < nbx-1 && iboff < nby-1) {
      temp1 = Ablk((1 + iboff) , jboff + 1);
      temp1 = xnetN[nyproc -ipoff].temp1;
      if (iyproc >= nyproc-ipoff & ixproc >= nxproc - jpoff) 
                  *temp = xnetW[nxproc - jpoff].temp1;
      }
}

show(a, m, n, msg)  /*  interactive display of a */
plural double a[];
int m, n;
char * msg;
{
#define NPRIN 8
#define SMALL 4
#define LARGE 8
#define ON 1
#define OFF 0
 static int npr = NPRIN;
 static int skipcalls = 0;
 static int prec = SMALL;
 static int fixed = 0, i=0, j=0;   /*  upper left corner to print  */
 register int ii, jj;
 plural double temp;
 char direction, bitbuck;


   if (skipcalls > 0) {  /* skip this call */
       skipcalls--;
       return;
       }
   else {   /*   dont skip this call  */
   if (!fixed) {
      i = 0; j = 0;
   }
   printf("%s\n",msg);
   direction = '0';
   while (  direction != 'x' ) {
      all{ getsubmatrix(a, m, n, i, j, &temp);
      printf("  cols:  ");
      for (jj=0; jj<npr; jj++) 
         printf("%5d ", j+jj);
      printf("\n");
      for (ii=0; ii<npr; ii++) {
         printf("row: %2d :", ii+i);
         if (prec == SMALL) {
             for (jj=0; jj<npr; jj++) {
                 printf("%5.1f ", proc[ii][jj].temp);}
         } else {
             for (jj=0; jj<npr; jj++) {
                 printf("%4.8g ", proc[ii][jj].temp);}
         }
         printf("\n");
      }}
      getdir:
         printf("show (? for help) >>>  ");
         scanf("%c",&direction);
         if      (direction == 'h') j = MAX(0, j-npr);
         else if (direction == 'j') i = MAX(0, i-npr);
         else if (direction == 'k') i = MIN(m-npr, i+npr);
         else if (direction == 'l') j = MIN(n-npr, j+npr);
         else if (direction == 'H') j = MAX(0, j-nxproc);
         else if (direction == 'J') i = MAX(0, i-nyproc);
         else if (direction == 'K') i = MIN(m-npr, i+nyproc);
         else if (direction == 'L') j = MIN(n-npr, j+nxproc);
         else if (direction == 'o') {i = 0; j = 0;}
         else if (direction == 't') i = 0; 
         else if (direction == 'b') i = m-npr; 
         else if (direction == 'a') j = 0;
         else if (direction == 's') j = n-npr;
         else if (direction == 'w') {npr += 4;}
         else if (direction == 'n') {npr -= 4;}
         else if (direction == 'P') {prec = LARGE;}
         else if (direction == 'p') {prec = SMALL;}
         else if (direction == 'F') {fixed = ON;}
         else if (direction == 'f') {fixed = OFF;}
         else if (direction == 'x') j = j;
         else if (direction == 'm') {
            printf("%s\n",msg);
            goto getdir;
         }
         else if (direction == 'z') {
             printf("  Skip how many calls to show? ");
             scanf("%d", &skipcalls);
             printf("  Skipping next %d calls\n",skipcalls);
             direction = 'x';
         }
         else if (direction == 'r') {
             printf("  Goto row: ? ");
             scanf("%d", &i);
         }
         else if (direction == 'c') {
             printf("  Goto column: ? ");
             scanf("%d", &j);
         }
         else if (direction == '?') {
           printf(" h  --  move window left\n");
           printf(" j  --  move window up\n");
           printf(" k  --  move window down\n");
           printf(" l  --  move window right\n");
           printf(" H  --  move window left  by nxproc\n");
           printf(" J  --  move window up    by nyproc\n");
           printf(" K  --  move window down  by nyproc\n");
           printf(" L  --  move window right by nxproc\n");
           printf(" o  --  move window to (0,0) position\n");
           printf(" t  --  move window to top\n");
           printf(" b  --  move window to bottom\n");
           printf(" a  --  move window to left edge\n");
           printf(" s  --  move window to right edge\n");
           printf(" r  --  move window to row\n");
           printf(" c  --  move window to column\n");
           printf(" w  --  narrower window\n");
           printf(" n  --  wider window\n");
           printf(" P  --  more precision\n");
           printf(" p  --  less precision\n");
           printf(" F  --  freeze window coordinates between calls\n");
           printf(" f  --  window always starts at (0,0)\n");
           printf(" m  --  repeat the show title\n");
           printf(" x  --  Stop the show!\n");
           printf(" z  --  Skip next several shows (prompt follows)\n");
           goto getdir;
         }
         else goto getdir;
      }
         scanf("%c",&direction);   /* eat last cr */
   }
}
int pause()
{  char c;
   printf("  any key to continue \n");
   scanf("%c",&c);
   if (c == 'q') return 1;
   return 0;
}
