home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / technical / autopcb / !AutoPCB / c / PCBVIEW < prev    next >
Encoding:
Text File  |  1991-03-17  |  19.7 KB  |  484 lines

  1. /*
  2. ** printed circuit board displayer, Copyright (C) Randy Nevin 1989.
  3. **
  4. ** you may give this software to anyone, make as many copies as you like, and
  5. ** post it on public computer bulletin boards and file servers. you may not
  6. ** sell it or charge any fee for distribution (except for media and postage),
  7. ** remove this comment or the copyright notice from the code, or claim that
  8. ** you wrote this code or anything derived from it. you may modify the code as
  9. ** much as you want (please document clearly with comments, and maintain the
  10. ** coding style), but programs which are derived from this one are subject to
  11. ** the conditions stated here. i am providing this code so that people can
  12. ** learn from it, so if you distribute it, please include source code, not
  13. ** just executables. contact me to report bugs or suggest enhancements; i do
  14. ** not guarantee support, but i will make an effort in good faith to help you,
  15. ** and i want to act as a central clearing house for future versions. you
  16. ** should contact me before undertaking a significant development effort, to
  17. ** avoid reinventing the wheel. if you come up with an enhancement you
  18. ** consider particularly useful, i would appreciate being informed so that it
  19. ** can be incorporated in future versions. my address is: Randy Nevin,
  20. ** 1731 211th PL NE, Redmond, WA 98053. this code is available directly from
  21. ** the author; just send a floppy and a self-addressed floppy mailer with
  22. **
  23. ** HISTORY
  24. ** (name                date            description)
  25. ** ----------------------------------------------------
  26. ** randy nevin          2/1/89          initial version
  27. ** randy nevin          2/11/89         released version 1.00
  28. ** pete goodwin         17/3/91         adapted for archimedes
  29. */
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. /* #include <io.h> */
  34. /* #include <conio.h> */
  35. #include <string.h>
  36. /* #include <dos.h> */
  37. /* #include <signal.h> enable this to catch ^C */
  38. #include "bbc.h"
  39.  
  40. #include "cell.h"
  41.  
  42. /*
  43. ** ^C handling is disabled because on my system enabling it seems to interfere
  44. ** with int 10h processing, which is needed to plot pixels. you should enable
  45. ** it and see what happens on your system.
  46. */
  47.  
  48. /* WARNING: the code below assumes 640x350 16-color ega */
  49.  
  50. /* 0=black      1=blue          2=green         3=light blue            */
  51. /* 4=red        5=purple        6=brown         7=grey                  */
  52. /* 8=black      9=bright blue   A=bright green  B=bright light blue     */
  53. /* C=scarlet    D=purple        E=yellow        F=white                 */
  54.  
  55. /*
  56. ** the colors below work fine for me, but may not for your particular ega and
  57. ** monitor. if bright blue and light blue look the same to you, or some traces
  58. ** appear to be missing, you may want to change these constants.
  59. **
  60. ** on some egas, there appear to be gaps in the traces; i don't know why. on
  61. ** other egas, the traces look fine. this happened to me on the maximum zoom,
  62. ** but not on any other zoom level. apparently some problem with the int 10h
  63. ** interface.
  64. */
  65.  
  66. /* colors of objects */
  67. #define H       0x1     /* hole color; scarlet                  */
  68. #define F       0x2     /* frontside trace color; bright blue   */
  69. #define B       0x4     /* backside trace color; light blue     */
  70. #define E       0x7     /* edge color; yellow                   */
  71.  
  72. /* screen limits */
  73. #define MINHORZ 0       /* left-most pixel      */
  74. #define MAXHORZ 1280     /* right-most pixel     */
  75. #define MINVERT 0       /* top-most pixel       */
  76. #define MAXVERT 1024     /* bottom-most pixel    */
  77.  
  78. static int mode; /* for saving original screen mode */
  79. static int sides = 3; /* 0=holes only, 1=front only, 2=back only, 3=all */
  80.  
  81. #define MAXZOOM 3       /* maximum zoom number; minimum is 0 */
  82.  
  83. #define ZOOM0   3       /* 3x3 pixels per cell          */
  84. #define ZOOM1   6       /* 6x6 pixels per cell          */
  85. #define ZOOM2   10      /* 10x10 pixels per cell        */
  86. #define ZOOM3   18      /* 18x18 pixels per cell        */
  87.  
  88. static int zoom = 1; /* 0=3x3, 1=6x6, 2=10x10, 3=18x18 */
  89. static int zoomsize[MAXZOOM+1] = { ZOOM0, ZOOM1, ZOOM2, ZOOM3 };
  90.  
  91. /* current lower-left position */
  92. static int Xrow = 0;
  93. static int Xcol = 0;
  94.  
  95. int justboard = 1; /* only need the board data structure */
  96.  
  97. /* board dimensions */
  98. extern int Nrows;
  99. extern int Ncols;
  100.  
  101. extern void InitBoard( void );
  102. extern long GetCell( int, int, int );
  103. extern void SetCell( int, int, int, long );
  104. extern int GetMode( void );
  105. extern void SetMode( int );
  106. extern void Dot( int, int, int );
  107.  
  108. /* static void control_C( void ); enable this to catch ^C */
  109. static void doedges( void );
  110. static void doboard( void );
  111. static void map( int, int, long, long );
  112. static void plot0( int, int, char [ZOOM0][ZOOM0], int );
  113. static void plot1( int, int, char [ZOOM1][ZOOM1], int );
  114. static void plot2( int, int, char [ZOOM2][ZOOM2], int );
  115. static void plot3( int, int, char [ZOOM3][ZOOM3], int );
  116.  
  117. int main ( argc, argv ) /* input routed board, display it on the screen */
  118.         int argc;
  119.         char *argv[];
  120.         {
  121.         char ch, *self, *p;
  122.         int r, c, rp, cp, i1, i2, i3, i4;
  123.         FILE *fp;
  124.         long x;
  125.  
  126.         printf( "Copyright (C) Randy Nevin, 1989. Version 1.00\n" );
  127.         printf( "See source code for rights granted.\n\n" );
  128.         self = argv[0];
  129.         /* get rid of initial part of path */
  130.         if ((p = strrchr( self, '\\' )) || (p = strrchr( self, ':' )))
  131.                 self = ++p;
  132.         if (argc != 2) { /* need infile */
  133.                 fprintf( stderr, "usage: %s infile\n", self );
  134.                 exit( -1 );
  135.                 }
  136.         if (!(fp = fopen( argv[1], "rb" ))) {
  137.                 fprintf( stderr, "can't open %s\n", argv[1] );
  138.                 exit( -1 );
  139.                 }
  140.         /* fetch the board dimensions */
  141.         if ((rp = getc( fp )) == EOF || (cp = getc( fp )) == EOF) {
  142.                 fprintf( stderr, "premature eof\n" );
  143.                 exit( -1 );
  144.                 }
  145.         Nrows = (rp & 0xFF) | ((cp << 8) & 0xFF00);
  146.         if ((rp = getc( fp )) == EOF || (cp = getc( fp )) == EOF) {
  147.                 fprintf( stderr, "premature eof\n" );
  148.                 exit( -1 );
  149.                 }
  150.         Ncols = (rp & 0xFF) | ((cp << 8) & 0xFF00);
  151.         InitBoard(); /* allocate memory for data structures */
  152.         for (r = 0; r < Nrows; r++) { /* read in the board, row by column */
  153.                 for (c = 0; c < Ncols; c++) {
  154.                         /* first, do frontside */
  155.                         if ((i1 = getc( fp )) == EOF
  156.                                 || (i2 = getc( fp )) == EOF
  157.                                 || (i3 = getc( fp )) == EOF
  158.                                 || (i4 = getc( fp )) == EOF) {
  159.                                 fprintf( stderr, "premature eof\n" );
  160.                                 exit( -1 );
  161.                                 }
  162.                         x = (long)i1 | (((long)i2) << 8)
  163.                                 | (((long)i3) << 16) | (((long)i4) << 24);
  164.                         SetCell( r, c, TOP, x );
  165.                         /* then do backside */
  166.                         if ((i1 = getc( fp )) == EOF
  167.                                 || (i2 = getc( fp )) == EOF
  168.                                 || (i3 = getc( fp )) == EOF
  169.                                 || (i4 = getc( fp )) == EOF) {
  170.                                 fprintf( stderr, "premature eof\n" );
  171.                                 exit( -1 );
  172.                                 }
  173.                         x = (long)i1 | (((long)i2) << 8)
  174.                                 | (((long)i3) << 16) | (((long)i4) << 24);
  175.                         SetCell( r, c, BOTTOM, x );
  176.                         }
  177.                 }
  178.         /* tell user what commands are available */
  179.         printf( "\t0   = holes only\n" );
  180.         printf( "\t1   = holes and top traces\n" );
  181.         printf( "\t2   = holes and bottom traces\n" );
  182.         printf( "\t3   = holes and all traces\n" );
  183.         printf( "\tz/Z = zoom one level / maximum zoom\n" );
  184.         printf( "\ts/S = shrink one level / minimum shrink\n" );
  185.         printf( "\tl/L = move left by one / move left by ten\n" );
  186.         printf( "\tr/R = move right by one / move right by ten\n" );
  187.         printf( "\tu/U = move up by one / move up by ten\n" );
  188.         printf( "\td/D = move down by one / move down by ten\n" );
  189.         printf( "\tany other key exits the program\n" );
  190.         printf( "\nPress ENTER to continue, or ^C to exit " );
  191.         bbc_get();
  192.         mode = GetMode(); /* save mode so can restore later */
  193.         /* signal( SIGINT, control_C ); enable this to catch ^C */
  194.         SetMode( 0x10 ); /* 640x350 16-color mode */
  195.         doedges(); /* display board edges */
  196.         doboard(); /* display the board */
  197.         for (;;) { /* process until unrecognized keystroke */
  198.                 ch = bbc_get();
  199.                 switch (ch) { /* determine what it is */
  200.                 case '0': /* just show holes */
  201.                         if (sides == 0)
  202.                                 continue;
  203.                         sides = 0;
  204.                         break;
  205.                 case '1': /* show holes and top-side traces */
  206.                         if (sides == 1)
  207.                                 continue;
  208.                         sides = 1;
  209.                         break;
  210.                 case '2': /* show holes and bottom-side traces */
  211.                         if (sides == 2)
  212.                                 continue;
  213.                         sides = 2;
  214.                         break;
  215.                 case '3': /* show holes and all traces */
  216.                         if (sides == 3)
  217.                                 continue;
  218.                         sides = 3;
  219.                         break;
  220.                 case 'Z': /* zoom to the limit */
  221.                         if (zoom == MAXZOOM)
  222.                                 continue;
  223.                         zoom = MAXZOOM;
  224.                         break;
  225.                 case 'z': /* zoom by one */
  226.                         if (zoom == MAXZOOM)
  227.                                 continue;
  228.                         zoom++;
  229.                         break;
  230.                 case 'S': /* shrink to the limit */
  231.                         if (zoom == 0)
  232.                                 continue;
  233.                         zoom = 0;
  234.                         break;
  235.                 case 's': /* shrink by one */
  236.                         if (zoom == 0)
  237.                                 continue;
  238.                         zoom--;
  239.                         break;
  240.                 case 'L': /* left by 10 */
  241.                         if (Xcol == 0)
  242.                                 continue;
  243.                         if (Xcol <= 10)
  244.                                 Xcol = 0;
  245.                         else
  246.                                 Xcol -= 10;
  247.                         break;
  248.                 case 'l': /* left by one */
  249.                         if (Xcol == 0)
  250.                                 continue;
  251.                         Xcol--;
  252.                         break;
  253.                 case 'R': /* right by 10 */
  254.                         if (Xcol == Ncols-1)
  255.                                 continue;
  256.                         if (Xcol >= Ncols-11)
  257.                                 Xcol = Ncols-1;
  258.                         else
  259.                                 Xcol += 10;
  260.                         break;
  261.                 case 'r': /* right by one */
  262.                         if (Xcol == Ncols-1)
  263.                                 continue;
  264.                         Xcol++;
  265.                         break;
  266.                 case 'U': /* up by 10 */
  267.                         if (Xrow == Nrows-1)
  268.                                 continue;
  269.                         if (Xrow >= Nrows-11)
  270.                                 Xrow = Nrows-1;
  271.                         else
  272.                                 Xrow += 10;
  273.                         break;
  274.                 case 'u': /* up by one */
  275.                         if (Xrow == Nrows-1)
  276.                                 continue;
  277.                         Xrow++;
  278.                         break;
  279.                 case 'D': /* down by 10 */
  280.                         if (Xrow == 0)
  281.                                 continue;
  282.                         if (Xrow <= 10)
  283.                                 Xrow = 0;
  284.                         else
  285.                                 Xrow -= 10;
  286.                         break;
  287.                 case 'd': /* down by one */
  288.                         if (Xrow == 0)
  289.                                 continue;
  290.                         Xrow--;
  291.                         break;
  292.                 default:
  293.                         SetMode( mode ); /* restore original screen mode */
  294.                         exit( 0 );
  295.                         break;
  296.                         }
  297.                 bbc_cls(); /* clear screen */
  298.                 doedges(); /* display board edges */
  299.                 doboard(); /* display the board */
  300.                 }
  301.         }
  302.  
  303. /*
  304. ** enable this to catch ^C
  305. **
  306. **static void control_C () { / * handle ^C * /
  307. **      SetMode( mode ); / * restore original screen mode * /
  308. **      exit( 0 );
  309. **      }
  310. */
  311.  
  312. static void doedges () { /* display the board edges */
  313.         int r1, c1, r2, c2, i, z;
  314.  
  315.         z = zoomsize[zoom];
  316.         /* first, calculate their virtual screen positions */
  317.         r1 = MAXVERT+(Xrow*z); /* bottom edge */
  318.         c1 = MINHORZ-(Xcol*z); /* left edge */
  319.         r2 = MAXVERT-1-((Nrows-Xrow)*z); /* top edge */
  320.         c2 = MINHORZ+1+((Ncols-Xcol)*z); /* right edge */
  321.         if (r1 >= MINVERT && r1 <= MAXVERT) /* draw bottom edge */
  322.                 for (i = c1; i <= c2; i++)
  323.                         if (i >= MINHORZ && i <= MAXHORZ)
  324.                                 Dot( E, r1, i );
  325.         if (c1 >= MINHORZ && c1 <= MAXHORZ) /* draw left edge */
  326.                 for (i = r1; i >= r2; i--)
  327.                         if (i >= MINVERT && i <= MAXVERT)
  328.                                 Dot( E, i, c1 );
  329.         if (r2 >= MINVERT && r2 <= MAXVERT) /* draw top edge */
  330.                 for (i = c1; i <= c2; i++)
  331.                         if (i >= MINHORZ && i <= MAXHORZ)
  332.                                 Dot( E, r2, i );
  333.         if (c2 >= MINHORZ && c2 <= MAXHORZ) /* draw right edge */
  334.                 for (i = r1; i >= r2; i--)
  335.                         if (i >= MINVERT && i <= MAXVERT)
  336.                                 Dot( E, i, c2 );
  337.         }
  338.  
  339. static void doboard () { /* display the board on the screen, row by column */
  340.         int r, c, rp, cp, rpd, cpd, z;
  341.         long x, y;
  342.  
  343.         z = zoomsize[zoom];
  344.         rpd = MINVERT+z; /* top-most plottable row */
  345.         cpd = MAXHORZ-z; /* right-most plottable column */
  346.         for (r = Xrow, rp = MAXVERT-1; r < Nrows && rp >= rpd; r++, rp -= z) {
  347.                 for (c = Xcol, cp = MINHORZ+1; c < Ncols && cp <= cpd;
  348.                                 c++, cp += z) {
  349.                         x = GetCell( r, c, TOP );
  350.                         y = GetCell( r, c, BOTTOM );
  351.                         if (x || y) /* only map if something is there */
  352.                                 map( rp, cp, x, y );
  353.                         }
  354.                 }
  355.         }
  356.  
  357. struct x { /* group the bit templates for an object */
  358.         long t;                 /* the object type      */
  359.         char t0[ZOOM0][ZOOM0];  /* tiny zoom template   */
  360.         char t1[ZOOM1][ZOOM1];  /* small zoom template  */
  361.         char t2[ZOOM2][ZOOM2];  /* medium zoom template */
  362.         char t3[ZOOM3][ZOOM3];  /* large zoom template  */
  363.         };
  364.  
  365. extern struct x y1[];  /* hole templates                */
  366. extern struct x y2[];  /* hole-related templates        */
  367. extern struct x y3[];  /* non-hole-related templates    */
  368.  
  369. extern int z1;  /* number of hole types                 */
  370. extern int z2;  /* number of hole-related types         */
  371. extern int z3;  /* number of non-hole-related types     */
  372.  
  373. #define domap1(v,c)     { for (i = 0; i < z1; i++) { \
  374.                                 if (v & (y1[i].t)) { \
  375.                                         if (zoom == 0) \
  376.                                                 plot0( rp, cp, y1[i].t0, c );\
  377.                                         else if (zoom == 1) \
  378.                                                 plot1( rp, cp, y1[i].t1, c );\
  379.                                         else if (zoom == 2) \
  380.                                                 plot2( rp, cp, y1[i].t2, c );\
  381.                                         else if (zoom == 3) \
  382.                                                 plot3( rp, cp, y1[i].t3, c );\
  383.                                         } \
  384.                                 } } \
  385.  
  386. #define domap2(v,c)     { for (i = 0; i < z2; i++) { \
  387.                                 if (v & (y2[i].t)) { \
  388.                                         if (zoom == 0) \
  389.                                                 plot0( rp, cp, y2[i].t0, c );\
  390.                                         else if (zoom == 1) \
  391.                                                 plot1( rp, cp, y2[i].t1, c );\
  392.                                         else if (zoom == 2) \
  393.                                                 plot2( rp, cp, y2[i].t2, c );\
  394.                                         else if (zoom == 3) \
  395.                                                 plot3( rp, cp, y2[i].t3, c );\
  396.                                         } \
  397.                                 } } \
  398.  
  399. #define domap3(v,c)     { for (i = 0; i < z3; i++) { \
  400.                                 if (v & (y3[i].t)) { \
  401.                                         if (zoom == 0) \
  402.                                                 plot0( rp, cp, y3[i].t0, c );\
  403.                                         else if (zoom == 1) \
  404.                                                 plot1( rp, cp, y3[i].t1, c );\
  405.                                         else if (zoom == 2) \
  406.                                                 plot2( rp, cp, y3[i].t2, c );\
  407.                                         else if (zoom == 3) \
  408.                                                 plot3( rp, cp, y3[i].t3, c );\
  409.                                         } \
  410.                                 } } \
  411.  
  412. static void map ( rp, cp, v0, v1 ) /* map a cell */
  413.         int rp, cp;
  414.         long v0, v1;
  415.         {
  416.         int i;
  417.  
  418.         if (v0 & HOLE) {
  419.                 domap1( v0, H ); /* plot the hole */
  420.                 if (v1 && (sides & 2)) /* plot backside? */
  421.                         domap2( v1, B );
  422.                 if (v0 && (sides & 1)) /* plot frontside? */
  423.                         domap2( v0, F );
  424.                 }
  425.         else {
  426.                 if (v1 && (sides & 2)) /* plot backside? */
  427.                         domap3( v1, B );
  428.                 if (v0 && (sides & 1)) /* plot frontside? */
  429.                         domap3( v0, F );
  430.                 }
  431.         }
  432.  
  433. static void plot0 ( rp, cp, obj, color ) /* plot a 3x3 template */
  434.         int rp, cp;
  435.         char obj[ZOOM0][ZOOM0];
  436.         int color;
  437.         {
  438.         int r, c;
  439.  
  440.         for (r = 0; r < ZOOM0; r++)
  441.                 for (c = 0; c < ZOOM0; c++)
  442.                         if (obj[r][c])
  443.                                 Dot( color, rp-r, cp+c );
  444.         }
  445.  
  446. static void plot1 ( rp, cp, obj, color ) /* plot a 6x6 template */
  447.         int rp, cp;
  448.         char obj[ZOOM1][ZOOM1];
  449.         int color;
  450.         {
  451.         int r, c;
  452.  
  453.         for (r = 0; r < ZOOM1; r++)
  454.                 for (c = 0; c < ZOOM1; c++)
  455.                         if (obj[r][c])
  456.                                 Dot( color, rp-r, cp+c );
  457.         }
  458.  
  459. static void plot2 ( rp, cp, obj, color ) /* plot a 10x10 template */
  460.         int rp, cp;
  461.         char obj[ZOOM2][ZOOM2];
  462.         int color;
  463.         {
  464.         int r, c;
  465.  
  466.         for (r = 0; r < ZOOM2; r++)
  467.                 for (c = 0; c < ZOOM2; c++)
  468.                         if (obj[r][c])
  469.                                 Dot( color, rp-r, cp+c );
  470.         }
  471.  
  472. static void plot3 ( rp, cp, obj, color ) /* plot an 18x18 template */
  473.         int rp, cp;
  474.         char obj[ZOOM3][ZOOM3];
  475.         int color;
  476.         {
  477.         int r, c;
  478.  
  479.         for (r = 0; r < ZOOM3; r++)
  480.                 for (c = 0; c < ZOOM3; c++)
  481.                         if (obj[r][c])
  482.                                 Dot( color, rp-r, cp+c );
  483.         }
  484.