home *** CD-ROM | disk | FTP | other *** search
/ Dream 55 / Amiga_Dream_55.iso / RISCOS / APPS / SCI / ELECTRON / AUTOPC.ZIP / !AutoPCB / Source / c / BOARD < prev    next >
Text File  |  1991-03-23  |  9KB  |  254 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include "heap.h"
  5.  
  6. #include "cell.h"
  7.  
  8. #define LIMIT   0x10000         /* 64k */
  9.  
  10. /* Needed to defeat potty C compilers in mess-doss */
  11.  
  12. #define far
  13.  
  14. /* board dimensions */
  15. int Nrows = ILLEGAL;
  16. int Ncols = ILLEGAL;
  17.  
  18. int InitBoardDone = 0; /* sanity check */
  19.  
  20. /* memory usage */
  21. long Ltotal = 0; /* for board */
  22. long Itotal = 0; /* for dist */
  23. long Ctotal = 0; /* for dir */
  24.  
  25. /*
  26. ** memory is allocated in blocks of rows. as many rows as will fit in 64k are
  27. ** allocated in each block. blocks are linked together by pointers. the last
  28. ** block has a null 'next' pointer. if you want to route some *really* big
  29. ** boards (so big that 640k is not sufficient), you should change the
  30. ** algorithms below to test for Lotus-Intel-Microsoft expanded memory (LIM 3.2
  31. ** or 4.0) and use it if present. this would be a major enhancement, so if you
  32. ** do it i hope you will send it back to me so that it can be incorporated in
  33. ** future versions.
  34. */
  35.  
  36. struct lmem { /* a block of longs */
  37.         struct lmem far *next;   /* ptr to next block */
  38.         long             mem[1]; /* more than 1 is actually allocated */
  39.         };
  40.  
  41. struct imem { /* a block of ints */
  42.         struct imem far *next;   /* ptr to next block */
  43.         int              mem[1]; /* more than 1 is actually allocated */
  44.         };
  45.  
  46. struct cmem { /* a block of chars */
  47.         struct cmem far *next;   /* ptr to next block */
  48.         char             mem[1]; /* more than 1 is actually allocated */
  49.         };
  50.  
  51. struct lhead { /* header of blocks of longs */
  52.         int              numrows; /* number of rows per block */
  53.         struct lmem far *side[2]; /* ptr to first block of each chain */
  54.         };
  55.  
  56. struct ihead { /* header of blocks of ints */
  57.         int              numrows; /* number of rows per block */
  58.         struct imem far *side[2]; /* ptr to first block of each chain */
  59.         };
  60.  
  61. struct chead { /* header of blocks of chars */
  62.         int              numrows; /* number of rows per block */
  63.         struct cmem far *side[2]; /* ptr to first block of each chain */
  64.         };
  65.  
  66. static struct lhead Board = { 0, {NULL,NULL} }; /* 2-sided board */
  67. static struct ihead Dist = { 0, {NULL,NULL} }; /* path distance to cells */
  68. static struct chead Dir = { 0, {NULL,NULL} }; /* pointers back to source */
  69.  
  70. extern int justboard;
  71.  
  72. void InitBoard( void );
  73. long GetCell( int, int, int );
  74. void SetCell( int, int, int, long );
  75. void OrCell( int, int, int, long );
  76. int GetDist( int, int, int );
  77. void SetDist( int, int, int, int );
  78. int GetDir( int, int, int );
  79. void SetDir( int, int, int, int );
  80.  
  81. void InitBoard () { /* initialize the data structures */
  82.         long lx, ly; /* for calculating block sizes */
  83.         struct lmem far * far *ltop;     /* for building board chain */
  84.         struct lmem far * far *lbottom;  /* for building board chain */
  85.         struct imem far * far *itop;     /* for building dist chain */
  86.         struct imem far * far *ibottom;  /* for building dist chain */
  87.         struct cmem far * far *ctop;     /* for building dir chain */
  88.         struct cmem far * far *cbottom;  /* for building dir chain */
  89.         int r, c, i, j, k; /* for calculating number of rows per block */
  90.  
  91.         InitBoardDone = 1; /* we have been called */
  92. /* allocate Board (longs) */
  93.         for (lx = (long)Ncols*sizeof(long), ly = 0, i = 0;
  94.                 i < Nrows && ly <= LIMIT - sizeof(long far *); ly += lx, i++)
  95.                 ; /* calculate maximum number of rows per block */
  96.         Board.numrows = --i;
  97.         ltop = &(Board.side[TOP]);
  98.         lbottom = &(Board.side[BOTTOM]);
  99.         for (j = Nrows; j > 0; j -= i) {
  100.                 k = (j > i) ? i : j;
  101.                 ly = ((long)k * lx) + sizeof(long far *);
  102.                 *ltop = (struct lmem far *)heap_alloc( ly );
  103.                 *lbottom = (struct lmem far *)heap_alloc( ly );
  104.                 Ltotal += 2*ly;
  105.                 ltop = (struct lmem far * far *)(*ltop);
  106.                 lbottom = (struct lmem far * far *)(*lbottom);
  107.                 }
  108.         *ltop = *lbottom = NULL;
  109.         if (!justboard) {
  110. /* allocate Dist (ints) */
  111.                 for (lx = (long)Ncols*sizeof(int), ly = 0, i = 0;
  112.                         i < Nrows && ly <= LIMIT - sizeof(int far *);
  113.                         ly += lx, i++)
  114.                         ; /* calculate maximum number of rows per block */
  115.                 Dist.numrows = --i;
  116.                 itop = &(Dist.side[TOP]);
  117.                 ibottom = &(Dist.side[BOTTOM]);
  118.                 for (j = Nrows; j > 0; j -= i) {
  119.                         k = (j > i) ? i : j;
  120.                         ly = ((long)k * lx) + sizeof(int far *);
  121.                         *itop = (struct imem far *)heap_alloc( ly );
  122.                         *ibottom = (struct imem far *)heap_alloc( ly );
  123.                         Itotal += 2*ly;
  124.                         itop = (struct imem far * far *)(*itop);
  125.                         ibottom = (struct imem far * far *)(*ibottom);
  126.                         }
  127.                 *itop = *ibottom = NULL;
  128. /* allocate Dir (chars) */
  129.                 for (lx = (long)Ncols*sizeof(char), ly = 0, i = 0;
  130.                         i < Nrows && ly <= LIMIT - sizeof(char far *);
  131.                         ly += lx, i++)
  132.                         ; /* calculate maximum number of rows per block */
  133.                 Dir.numrows = --i;
  134.                 ctop = &(Dir.side[TOP]);
  135.                 cbottom = &(Dir.side[BOTTOM]);
  136.                 for (j = Nrows; j > 0; j -= i) {
  137.                         k = (j > i) ? i : j;
  138.                         ly = ((long)k * lx) + sizeof(char far *);
  139.                         *ctop = (struct cmem far *)heap_alloc( ly );
  140.                         *cbottom = (struct cmem far *)heap_alloc( ly );
  141.                         Ctotal += 2*ly;
  142.                         ctop = (struct cmem far * far *)(*ctop);
  143.                         cbottom = (struct cmem far * far *)(*cbottom);
  144.                         }
  145.                 *ctop = *cbottom = NULL;
  146.                 }
  147. /* initialize everything to empty */
  148.         for (r = 0; r < Nrows; r++) {
  149.                 for (c = 0; c < Ncols; c++) {
  150.                         SetCell( r, c, TOP, (long)EMPTY );
  151.                         SetCell( r, c, BOTTOM, (long)EMPTY );
  152.                         if (!justboard) {
  153.                                 SetDist( r, c, TOP, EMPTY );
  154.                                 SetDist( r, c, BOTTOM, EMPTY );
  155.                                 SetDir( r, c, TOP, EMPTY );
  156.                                 SetDir( r, c, BOTTOM, EMPTY );
  157.                                 }
  158.                         }
  159.                 }
  160.         }
  161.  
  162. long GetCell( r, c, s ) /* fetch board cell */
  163.         int r, c, s;
  164.         {
  165.         struct lmem far *p;
  166.  
  167.         p = Board.side[s];
  168.         while (r >= Board.numrows) {
  169.                 p = p->next;
  170.                 r -= Board.numrows;
  171.                 }
  172.         return( p->mem[r*Ncols+c] );
  173.         }
  174.  
  175. void SetCell( r, c, s, x ) /* store board cell */
  176.         int r, c, s;
  177.         long x;
  178.         {
  179.         struct lmem far *p;
  180.  
  181.         p = Board.side[s];
  182.         while (r >= Board.numrows) {
  183.                 p = p->next;
  184.                 r -= Board.numrows;
  185.                 }
  186.         p->mem[r*Ncols+c] = x;
  187.         }
  188.  
  189. void OrCell( r, c, s, x ) /* augment board cell */
  190.         int r, c, s;
  191.         long x;
  192.         {
  193.         struct lmem far *p;
  194.  
  195.         p = Board.side[s];
  196.         while (r >= Board.numrows) {
  197.                 p = p->next;
  198.                 r -= Board.numrows;
  199.                 }
  200.         p->mem[r*Ncols+c] |= x;
  201.         }
  202.  
  203. int GetDist( r, c, s ) /* fetch distance cell */
  204.         int r, c, s;
  205.         {
  206.         struct imem far *p;
  207.  
  208.         p = Dist.side[s];
  209.         while (r >= Dist.numrows) {
  210.                 p = p->next;
  211.                 r -= Dist.numrows;
  212.                 }
  213.         return( p->mem[r*Ncols+c] );
  214.         }
  215.  
  216. void SetDist( r, c, s, x ) /* store distance cell */
  217.         int r, c, s, x;
  218.         {
  219.         struct imem far *p;
  220.  
  221.         p = Dist.side[s];
  222.         while (r >= Dist.numrows) {
  223.                 p = p->next;
  224.                 r -= Dist.numrows;
  225.                 }
  226.         p->mem[r*Ncols+c] = x;
  227.         }
  228.  
  229. int GetDir( r, c, s ) /* fetch direction cell */
  230.         int r, c, s;
  231.         {
  232.         struct cmem far *p;
  233.  
  234.         p = Dir.side[s];
  235.         while (r >= Dir.numrows) {
  236.                 p = p->next;
  237.                 r -= Dir.numrows;
  238.                 }
  239.         return( (int)(p->mem[r*Ncols+c]) );
  240.         }
  241.  
  242. void SetDir( r, c, s, x ) /* store direction cell */
  243.         int r, c, s, x;
  244.         {
  245.         struct cmem far *p;
  246.  
  247.         p = Dir.side[s];
  248.         while (r >= Dir.numrows) {
  249.                 p = p->next;
  250.                 r -= Dir.numrows;
  251.                 }
  252.         p->mem[r*Ncols+c] = (char)x;
  253.         }
  254.