/* program to re-order .vec files from .ord 
*/

#include <stdio.h>
#include <ctype.h>
#define LDEBUG 0
#define maxsigs 144
#define SG sigs[i]
/* SGM is the remapped version of SG */
#define SGM sigs[map[i]]
#define fpf fprintf
#define foris for(i=0;i<sindex;i++)
#define g_s() get_sym(s,&t)


/* Parser constants etc */
char cmt_start_ch      = '{',
     cmt_end_ch        = '}',
     line_cmt_ch       = '#',
     x_id_start_chs[]  = {'$','_','\0'},
     x_id_chs[]        = {'$','_','\0'},
     x_space_chs[]     = {';',':','@','=','\0'},
     op_chs[]          = {'+','*','/','\0'};
char *keys[] = { "VSS","VDD","I","O","B","END", "INPUT",
                 "OUTPUT", "PIN", "BIDIR", "DEFPINS" };
#include "procs6.h"

/* program vars */
int   sindex=0, vecnum=0;
struct sig { char v,ov,rv,dir,odir,vt, name[N_L]; int pin; } sigs[maxsigs];
int map[maxsigs]; /* xref the new and old position of signals */

FILE *fo,*fs;

main(argc,argv)
int argc; char *argv[];
{int i=0;
 if (argc<2)
  error("USAGE: ordvec <designname> [-no_group|-ng] [-id]\n");
 if (arg_num(argc,argv,"-i*d")) {printf("Version FEB/02/89\n"); exit(0);}
 fin=efopen(cf(argv[1],".vec"),"r");
 init();

 if (arg_num(argc,argv,"-s*tdout")) fo=stdout;
   else fo= efopen(cf(argv[1],".ov"),"w");


/*------------------------------------------------------------------*/

fpf(fo,"# ORDVEC Output\n\nDEFPINS\n"); /* write the .ov header */

expect_sym("DEFPINS");

while (strneq(s,"END") && !feof(fin))
{
 g_s();
 if (strneq(s,"PIN") && strneq(s,"END"))
   error("PIN or END expected in defpins");
  else
  {
   if (strequ(s,"PIN"))
   {i=get_int();
    if (i>maxsigs || i<=0) error("invalid pin range");
    sigs[sindex].pin=i;
    g_s();
    if (strequ(s,"VSS") || strequ(s,"VDD"))
      fpf(fo," PIN %d %s;\n",i,s);
    else
    {
     if (t!=ident) error("Signal name expected");
     if (sig_exists(s)) error("Duplicate signal found in sigdefs");
     strcpy(sigs[sindex].name,s);
     g_s();
     if (s[0] != 'I' && s[0] != 'O' && s[0] != 'B')
      error("INPUT, OUTPUT, or BIDIR expected");
     sigs[sindex].vt = s[0];
     sindex++;                 }}
                                 }
                                   } /* while */

/*------- parse in the .ord new signal order file -------------------*/

fclose(fin);
fin=efopen(cf(argv[1],".ord"),"r");
init();
i=0;
g_s(); while(!feof(fin)&& t!=ident) g_s(); /* get the first sig */
while (!feof(fin))
{ map[i++]=sig_index(s);  g_s(); while(!feof(fin)&& t!=ident) g_s();}
if(i!=sindex) error("insuffient signals in .ord file");
fclose(fin);

/* reopen .vec file */
fin=efopen(cf(argv[1],".vec"),"r");
init();
g_s();
while(strneq(s,"END")) g_s(); /* read till end of defpins */

/*------- Output the header        ----------------------------------*/

foris fpf(fo," PIN %d %s %c;\n",SGM.pin,SGM.name,SGM.vt);
fpf(fo,"END;\n\n");
pr_header();

/*------- Init the pins            ----------------------------------*/

/* init values */
foris SG.v='-';


if(LDEBUG) foris
  {printf("%s %c %c %c %d -> ",SG.name,SG.vt,SG.v,SG.dir,SG.pin);
   printf("%s %c %c %c %d\n",SGM.name,SGM.vt,SGM.v,SGM.dir,SGM.pin);}


/*-------- Read and deal with vectors ------------------------------*/

xget_vline();
while (!feof(fin))  /* deal with vector */
{
 vecnum++;
 foris fpf(fo,"%c",SGM.v);
 fpf(fo," {%d}\n",vecnum);
 xget_vline();
} /* END of vector while */

fpf(stdout,"-I-  %d vectors \n",vecnum);

} /* main */

/*------------------------------------------------------------------*/

cv(c)
char c;
{ switch (c) {
  case 'L': return('0');
  case 'H': return('1');
  default : return(c);
 }
}


vt(c)
char c;
{ c=uc(c);
  if (c == 'L' || c == 'H') return('I'); else
   {if (c == '0' || c == '1' || c == 'X') return('O');
    else return('U'); }}

iv(c)
char c;
{ if(c=='0') return('L'); if(c=='1') return('H'); return(c); }


/*------------------------------------------------------------------*/

get_value()
{ /* fpf(fo,"gv\n"); */
 do {
     xgetch();
     if (ch=='#') { while (ch != '\n') xgetch(); }
     if (ch=='{') { while (ch != '}')  xgetch(); }
     if (ch >= 'a' && ch <= 'z') ch = toupper(ch);
               } while (cinstring(ch,"HL10X") == FALSE && ch != EOF);
 return(ch);
}

/*------------------------------------------------------------------*/

xget_vline()
{ int i; char c;
  foris
  { c = get_value();
    if (SG.vt == 'O' && (c == 'H' || c == 'L'))
                            error("Expected output value");
    if (SG.vt == 'I' && (c == '0' || c == '1' || c == 'X'))
                            error("Expected input value");
    if (SG.vt == 'B') SG.dir = vt(c);
    SG.v = c;
  } /* for */
}

/*------------------------------------------------------------------*/

pr_header()
{ int i=0,j=0,l=0;
  fpf(fo,"{\n");
  foris if(strlen(SGM.name)>l) l=strlen(SGM.name);
  l--;
  for(;j<=l;j++)
   { foris if(strlen(SGM.name)>j)
       fpf(fo,"%c",SGM.name[j]); else fpf(fo,"%c",' ');
     fpf(fo,"\n");
   }
  fpf(fo,"}\n");
}

sig_exists(sg)
char *sg;
{int i;
 for(i = 0;i!=sindex && strneq(sg,sigs[i].name);i++) ;
 if (strequ(sg,sigs[i].name)) return(TRUE);
  else return(FALSE);
}


sig_index(sg)
char *sg;
{int i;
 for(i = 0;i!=sindex && strneq(sg,sigs[i].name);i++) ;
 if (strneq(sg,sigs[i].name))
 {fpf(stderr,"Signal %s in .ord file not found in .vec file\n",sg);
  error("Signal not found"); }
 return i;
}