/*
 *
 * hcount.c -- source for example layer postprocessor for PCLO
 * Copyright (c) 1986 - SoftCircuits, Inc. - Imported by QUARTEX
 *
 */

/*
 * Assembly language calls:
 *     vcheck(layer);              determine extent of structures
 *     encode(rdmuse,layer);       de-encrypt layer info
 */

/* The following are the definitions for the data found in a board cell */

#define C_BLANK 0    /* empty cell */
#define C_FAT   1    /* solidly filled cell*/
#define C_TEBW  2    /* cell with 2 diagonal traces, top east, bottom west */
#define C_TWBE  3    /* cell with 2 diagonal traces, top west, bottom east */
#define C_VERT  4    /* vertical trace */
#define C_HORZ  5    /* horizontal trace */
#define C_NW    6    /* NorthWest diagonal trace */
#define C_SE    7    /* SouthEast diagonal trace */
#define C_SW    8    /* SouthWest diagonal trace */
#define C_NE    9    /* NorthEast diagonal trace */
#define C_TOPT  10   /* horizontal trace with connection to the top */
#define C_BOTT  11   /* horizontal trace with connection to the bottom */
#define C_LEFTT 12   /* vertical trace with connection to the left */
#define C_RITET 13   /* vertical trace with connection to the right */
#define C_FULL  14   /* connections everywhere; like a plus sign */

/* All of the following codes are "cylinders", they exist on all layers */

#define C_PAD   15   /* standard pad; used for ic pins, etc */
#define C_VIA   16   /* feedthrough pad; used for circuit connections only */
#define C_EDGE  17   /* shearline indicator; used to indicate edge of bd */
#define C_DRILL 18   /* drill hole marker; used for tooling mounting holes */
#define C_ONE   19   /* pin one pad; used to indicate orientation of items */

#include "exec/types.h"
#include "exec/nodes.h"
#include "exec/lists.h"
#include "exec/ports.h"
#include "exec/libraries.h"
#include "exec/devices.h"
#include "exec/io.h"
#include "devices/serial.h"
#include "lattice/stdio.h"
#include "workbench/startup.h"

UBYTE tname[80];  /* temp area to hold full path/filename */
UBYTE fname[80];  /* filename to process */
UBYTE path[80];   /* path that BoardLib directory resides under */
UBYTE *layer;     /* pointer to area where board data is kept */
int rdmuse;       /* file encryptor key storage */
float CostPerHole;/* used for input from user */
extern struct WBStartup *WBenchMsg; /* place for WorkBench to pass us info */


main(argc,argv)
   int argc;
   unsigned char *argv[];
{
   int x,y,cell,hcount,hotload;
   struct WBArg *arg;
      hcount=0;            /* no holes found yet */
      hotload=0;           /* assume not parameterized on startup */
      strcpy(&path[0],""); /* assume not a Wb startup, no path needed */
      if (argc == 0)       /* workbench load */
         {
            if(WBenchMsg->sm_NumArgs == 2)
               {
                  arg = WBenchMsg->sm_ArgList;
                  ++arg;
                  strcpy(&fname[0],arg->wa_Name);
                  strcpy(&path[0],":");
                  hotload = 1;
               }
         }
      else /* cli load */
        {
           if (argc == 2) /* there was an argument */
               {
                  strcpy(&fname[0],argv[1]);
                  hotload = 1;
               }
         }
      if (hotload == 0)
         {
            printf("Enter the name of the board to be checked: ");
            scanf("%s",&fname[0]);
         }
      printf("Enter the cost per hole for this estimate: ");
      scanf("%f",&CostPerHole);
      if ((layer=(UBYTE*)calloc(65536,1))==NULL) /* get memory for layer */
         {
            printf("layer memory allocation failed -- Not Enough Memory");
            jexit(20);
         }
      if (loadbd() != 1) jexit(20); /* load board specified in "fname" */
      printf("counting holes;\n");
      for (x=0; x<256; x++)
         {
            for (y=0; y<256; y++)
               {
                  cell=readbd(x,y);
                  if (cell > C_FULL) /* Want: C_DRILL, C_PAD, C_VIA, C_ONE */
                     {
                        if (cell != C_EDGE)
                           {
                              hcount++;
                           }
                     }
               }
         }
      printf("\nThe board %s contains %d holes\n",&fname[0],hcount);
      printf("estimated prototype cost is $%1.2f\n",hcount*CostPerHole);
      jexit(0);
   }

jexit(parm)
   int parm;
   {
   int i;
      for (i=0; i<500000; i++);
      exit(parm);
   }

/*
 * read a cell from the board:
*/
readbd(xpos,ypos)
   int xpos,ypos;
   {
   int data;
      data=(int)layer[xpos+(ypos*256)];
      return(data);
   }

/*
 * board load: 
 */
loadbd()
   {
   FILE *fp;

      strcpy(&tname[0],&path[0]);    /* setup base path for BoardLib */
      strcat(&tname[0],"BoardLib/"); /* setup Directory for layer */
      strcat(&tname[0],&fname[0]);   /* prefix full path to filename */
      strcat(&tname[0],".layer1");   /* add extension to filename */

      if (fp = fopen(&tname[0],"r")) /* try and open layer1 file */
         {
            printf("Please wait -- loading '%s'\n",&tname[0]);
               {
                  if (fread(&rdmuse,4,1,fp) != 1)   /* get file encryptor */
                     {
                        printf("file error - entire file not loaded\n");
                        return(0);
                     }
                  if (fread(layer,65536,1,fp) != 1)
                     {
                        printf("file error - entire file not loaded\n");
                        return(0);
                     }
                  else
                     {
                        printf("file '%s' loaded successfully\n",&tname[0]);
                     }
               }
            fclose(fp);
            printf("stand by - normalizing record\n");
            encode(rdmuse,layer);
            return(1);
         }
      else
         {
            printf("'%s' file not found\n",&tname[0]);
            return(0);
         }
   }
