vsig_gram.y 3.71 KB
/* vsig_gram.y - Grammar for vsig  */


%{

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <values.h>

#include "vsig.h"

/*
 * tokenString is global holding last ident, string, literal
 * shorn of quotes
 */

#define TOKENSIZ  	256

typedef union _NameType {
   char name[TOKENSIZ];
   int  ival;
} NameType;

#define YYSTYPE NameType

/* Forward References */

void    install(char *);
void    install_number(char *, int);
void	print_vsig( void );

/* globals */

extern FILE *Vecfp; /* memory vectors */
extern char *yysptr;

int EverynVecs = 20;  /* number of vectors before printing */

static SigDef Signal[MAX_SIGNALS];
static int nSigs = 0;  /* number of signals */
static int nVecs = 0;  /* number of vectors */
static int CurSig    = 0; /* current signal */
static int ClkSig = 0; /* clock signal flag */
static int line_num = 0;
static int *VecArray = NULL;

%}

%start Input

/* Reserved Keywords */


/* symbols */

%token tCOLON
%token tEOL
%token tLBRACK
%token tRBRACK
%token tPERCENT
%token tQUOTE
%token tINPUT
%token tOUTPUT
%token tBIDIR
%token tTRI
%token tEDGE
%token tSTROBE
%token tCLOCK
%token tVALID

/* patterns */

%token tNUMBER
%token tHEX
%token tIDENT
%token tJUNK

%%

Input		: /* EMPTY */
		| Input Statement
;

Statement	: tEOL
		{
		  if(VecArray == NULL)
		    VecArray = (int *) malloc(nSigs*sizeof(int));
		}
		| Signal tEOL
		{
		  if(!ClkSig)
		    nSigs++;
		  else
		    ClkSig = 0;
		}
		| Vector tEOL
		{
		  int i;
		  if(!(nVecs % EverynVecs))
		    print_vsig();
		  fprintf(Vecfp,"  ");
		  for(i = 0; i < nSigs; i++) {
		    if(Signal[i].hex_dig)
		      fprintf(Vecfp,"0x%0*x ", (Signal[i].size+3) / 4, VecArray[i]);
		    else
		      fprintf(Vecfp,"%0x ", VecArray[i]);
		  }
		  fprintf(Vecfp, "\n");
		  CurSig = 0; /* reset */
		  nVecs++;
		}
;

		  	
Signal		: Bus
		{
		}
		| tIDENT
		{
		  strcpy(Signal[nSigs].sig_name, $1.name);
		  Signal[nSigs].size    = 1;
		  Signal[nSigs].msb     = 0;
		  Signal[nSigs].lsb     = 0;
		  Signal[nSigs].hex_dig = 0;
		}
		| Signal tINPUT 
		{
		}
		| Signal tOUTPUT
		{
		}
		| Signal tBIDIR tIDENT tNUMBER
		{
		}
		| Signal tVALID
		{
		}
		| Signal tVALID tIDENT
		{
		}
		| Signal tCLOCK tNUMBER tNUMBER
		{
		  ClkSig = 1;
		}
		| Signal tEDGE tNUMBER
		{
		}
		| Signal tSTROBE tNUMBER
		{
		}
		| error
		{
		  printf("Unrecognized signal syntax: %s\n", yylval.name);
		}
;

Bus		: tIDENT tLBRACK tNUMBER tCOLON tNUMBER tRBRACK
		{
		  sprintf(Signal[nSigs].sig_name, "%s\0",$1.name);
		  Signal[nSigs].msb     = $3.ival;
		  Signal[nSigs].lsb     = $5.ival;
		  Signal[nSigs].size    = $3.ival - $5.ival + 1;
		  Signal[nSigs].hex_dig = (Signal[nSigs].size + 3) / 4;
		}
;

Vector		: VecNumber
		{
		  VecArray[CurSig++] = $1.ival;
		}
		| Vector VecNumber
		{
		  VecArray[CurSig++] = $2.ival;
		}
;

VecNumber	: tNUMBER
		| tHEX
;


%%

#include "vsig_scan.c"

void
  print_vsig( void )
{
    int i,j,k;
    int stop[MAX_SIGNALS];

    /* print signal names vertically */
    for(i = 0; i < nSigs; i++)
        stop[i] = 0;

    if(nSigs > 0) 
    {
      for(j = 0; 1; j++)
      {
        fprintf(Vecfp,"# ");
        for(i = 0; i < nSigs; i++)
        {
          if(stop[i] == 0)
          {
            if(Signal[i].sig_name[j] == 0)
            {
              stop[i] = 1;
              fprintf(Vecfp," ");
            }
            else
              fprintf(Vecfp,"%c",Signal[i].sig_name[j]);
           }
           else
             fprintf(Vecfp," ");
	   if(Signal[i].hex_dig)
	     fprintf(Vecfp,"  ");
           for(k = 0; k < (Signal[i].size+3)/4; k++)
             fprintf(Vecfp," ");
        }
        fprintf(Vecfp,"\n");
        for(i = 0; i < nSigs; i++)
          if(stop[i]==0)
            break;
        if(i == nSigs)
          break;
    }
  }
}