#include <stdio.h>

int gvst2();
int puiss();
char valeur();

main()
{
 int i;
 

 i= gvst2(8,1,74,0);
}

int gvst2(l,c,n,u)
int l,c,n,u;
{
 FILE *fich;
 int i,j,k;
 char name[12];

 name[0] = 'm';
 name[1] = 'e';
 name[2] = 'm';
 name[3] = '_';
 name[4] = valeur(l);
 name[5] = '_';
 name[6] = valeur(c);
 name[7] = '.';
 name[8] = 'v';
 name[9] = 'b';
 name[10] = 'e';
 name[11] = '\0';
 
 fich = fopen(name, "w");
 fprintf(fich,"ENTITY  mem_%d_%d IS\n",l,c);

 fprintf(fich,"PORT (\n"); 
 fprintf(fich,"   adr	:	IN BIT_VECTOR(%d DOWNTO 0);\n",l+c-1);
 fprintf(fich,"   write :	IN BIT ;\n");
 fprintf(fich,"   ck	:   	IN BIT ;\n");
 if(u==1){
 fprintf(fich,"   din	:	IN BIT_VECTOR(%d DOWNTO 0);\n",n-1);
 fprintf(fich,"   dout	:	OUT BIT_VECTOR(%d DOWNTO 0);\n",n-1);
         }
 else {
 fprintf(fich,"   dout	:	INOUT MUX_BIT_VECTOR(%d DOWNTO 0) BUS;\n",n-1);
      }

 fprintf(fich,"   vdd	:   	linkage BIT ;\n");
 fprintf(fich,"   vss	:   	linkage BIT) ;\n");
 fprintf(fich,"END mem_%d_%d ;\n",l,c);
 fprintf(fich,"\n\n\n"); 
 fprintf(fich,"ARCHITECTURE structural_view OF mem_%d_%d IS \n",l,c);

 fprintf(fich," \n"); 
 fprintf(fich,"COMPONENT dec_%d_%d\n",l,c);
 fprintf(fich,"PORT (\n");
 fprintf(fich,"   adr   :       IN BIT_VECTOR(%d DOWNTO 0);\n",l+c-1);
 fprintf(fich,"   wl    :       OUT BIT_VECTOR(0 TO %d);\n",puiss(l)-1);
 fprintf(fich,"   sel   :       OUT BIT_VECTOR(0 TO %d);\n",puiss(c)-1);
 fprintf(fich,"   write :       IN BIT ;\n");
 fprintf(fich,"   wrt   :       OUT BIT ;\n");
 fprintf(fich,"   rd    :       OUT BIT ;\n");
 fprintf(fich,"   ck    :       IN BIT ;\n");
 fprintf(fich,"   vdd   :       IN BIT ;\n");
 fprintf(fich,"   vss   :       IN BIT) ;\n");
 fprintf(fich,"END COMPONENT;\n");

 fprintf(fich," \n"); 
 fprintf(fich,"COMPONENT rbit_%d_%d\n",l,c);
 fprintf(fich,"PORT (\n");
 fprintf(fich,"   wl    :       IN BIT_VECTOR(0 TO %d);\n",puiss(l)-1);
 fprintf(fich,"   sel   :       IN BIT_VECTOR(0 TO %d);\n",puiss(c)-1);
 fprintf(fich,"   read  :       IN BIT ;\n");
 fprintf(fich,"   write :       IN BIT ;\n");
 if(u==1){
 fprintf(fich,"   ent   :       IN BIT;\n");
 fprintf(fich,"   sor   :       OUT BIT;\n");
         }
 else {
 fprintf(fich,"   sor   :       INOUT MUX_BIT BUS ;\n");
      }
 fprintf(fich,"   vdd   :       IN BIT ;\n");
 fprintf(fich,"   vss   :       IN BIT) ;\n");
 fprintf(fich,"END COMPONENT;\n");

 fprintf(fich," \n"); 
 fprintf(fich," SIGNAL lect	: BIT;\n"); 
 fprintf(fich," SIGNAL ecr2	: BIT;\n"); 
 fprintf(fich," SIGNAL selm	: BIT_VECTOR(0 TO %d);\n",puiss(c)-1); 
 fprintf(fich," SIGNAL wln	: BIT_VECTOR(0 TO %d);\n",puiss(l)-1); 
 fprintf(fich," \n\n\n"); 
 fprintf(fich,"BEGIN\n"); 

 fprintf(fich,"   decod : dec_%d_%d\n",l,c);
 fprintf(fich,"     PORT MAP (\n");
 fprintf(fich,"       adr   => adr,\n");
 fprintf(fich,"       wl    => wln,\n");
 fprintf(fich,"       sel   => selm,\n");
 fprintf(fich,"       write => write,\n");
 fprintf(fich,"       wrt   => ecr2,\n");
 fprintf(fich,"       rd    => lect,\n");
 fprintf(fich,"       ck    => ck,\n");
 fprintf(fich,"       vdd   => vdd,\n");
 fprintf(fich,"       vss   => vss);\n");
 fprintf(fich," \n"); 

 for(i=0;i<n;i++)
 {
  fprintf(fich,"   bit_%d : rbit_%d_%d\n",i,l,c);
  fprintf(fich,"     PORT MAP (\n");
  fprintf(fich,"   wl    => wln, \n");
  fprintf(fich,"   sel   => selm, \n");
  fprintf(fich,"   read  => lect, \n");
  fprintf(fich,"   write => ecr2, \n");
  if(u==1){
           fprintf(fich,"   ent   => din(%d), \n",i);
          }
  fprintf(fich,"   sor   => dout(%d), \n",i);
  fprintf(fich,"   vdd   => vdd, \n");
  fprintf(fich,"   vss   => vss ); \n");
  fprintf(fich," \n"); 
 }
 fprintf(fich," END structural_view;\n");
}

int puiss(i)
int i;
{
 if(i==0) return(1);
 else return(2*puiss(i-1));
}
 
char valeur(i)
int i;
{
 switch(i)
 {
  case 1:{ return('1'); break;}
  case 2:{ return('2'); break;}
  case 3:{ return('3'); break;}
  case 4:{ return('4'); break;}
  case 5:{ return('5'); break;}
  case 6:{ return('6'); break;}
  case 7:{ return('7'); break;}
  case 8:{ return('8'); break;}
  default: break;
 }
}
