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