/****************************************************************************/
/*                                                                          */
/*                      Chaine de CAO & VLSI   Alliance                     */
/*                                                                          */
/*    Produit :  Synthetiseur de FSM                                       */
/*    Fichier :  syf_codage.c                                                  */
/*                                                                          */
/*    (c) copyright 1992 Laboratoire MASI equipe CAO & VLSI                 */
/*    Tous droits reserves                                                  */
/*    Support : e-mail cao-vlsi@masi.ibp.fr                                 */
/*                                                                          */
/*    Auteur(s) :  C. Sarwary                            le : 04/03/1992     */
/*                                                                          */
/*    Modifie par :                                     le : ../../....     */
/*    Modifie par :                                     le : ../../....     */
/*    Modifie par :                                     le : ../../....     */
/*                                                                          */
/***************************************************************************/
#include <stdio.h>
#include MUT_H
#include LOG_H
#include "syf_auto.h"
#include "syf_must.h"
#include "../util/util.h"

codageHorizontal(numBit,AME)
int numBit;
 int AME;
{
 p_groupe Se;
int *tabCode;
int codeVal;
pCode code;
int numState;
int i,j;
FILE *file ;
int *tabPoidEtat;
int *affinity;
int *we;
int *nwS;
int *nwO;
int selectLabel;
char fileName[50];
char name[50] ;
char *s,*chain ;
int cod ;
/*char newline[5] ;*/
int newline ;
int line,numberCod ;

strcpy(fileName,autoSys->name);
/*i = strlen(fileName);
fileName[i-1] = '\0';*/
strcat(fileName,".cod");

if(!(file = fopen(fileName,"rt")))
	{
	printf("syf : can't find %s\n", fileName) ;
	exit(-1) ;
	}

tabCode = (int *)mbkalloc(bit[numBit]* sizeof(int )) ;

if(AME >0)
{
  nwS = (int *)mbkalloc((autoSys->numberState +1 )*(autoSys->numberState +1)  * sizeof(int )) ;
  nwO = (int *)mbkalloc((autoSys->numberState + 1)*(autoSys->numberOut) * sizeof(int )) ;
} 
numState = bit[numBit];
code = autoSys->code;

numberCod = 0 ;
line = 0 ;
cod = -1 ;

chain = (char *)mbkalloc(200) ;

while(fgets(chain,200,file) != NULL)
	{
	s = chain ;
	newline = 0 ;
	while(!strncmp(s," ",1) || !strncmp(s,"	",1))/*elimination des blancs*/
		s++ ;

	line++ ;
	if(strncmp(s,"#",1) && strncmp(s,"\n",1) && strcmp(s,""))
		{
		sscanf(s,"%s %x %x",name, &cod, newline) ;
		/*if(strncmp(newline,"\n",1) || cod < 0)*/
		if(newline != 0 || cod < 0)
			{
			printf("syf : syntax error in %s, Line %d\n",fileName, line) ;
			exit(-1) ;
			}
		for(i = 0; i < autoSys->numberState; i++)
			if(!strcmp(((autoSys->state) + i)->name,name))
				{
				numberCod++ ;
				(code + i)->code = cod ;
  				tabCode[cod] = i;
				break ;
				}
		if(i == autoSys->numberState)
			{
			printf("syf : %s, Line: %d. %s doesn't exist\n",fileName,line,name);
			exit(-1) ;
			}
		}
  	}
	if(numberCod < autoSys->numberState)
		{
		printf("syf : %s is incomplet, some states code are missed\n",fileName);
		exit(-1) ;
		}

if(AME>0) 
{
  mustPoids(nwS,nwO,NULL);
  we = mustCalcul(nwS,nwO,NULL);
  tabPoidEtat = asp_poidEtat();
  affinity =PoidsFutur();
  for(i =0; i<AME; i++)
  {
    Se= stateGeneration();
    while(Se)
    {
    selectLabel = codeModification(we,tabPoidEtat,affinity,tabCode,Se);
    Se = delFromgroup(Se, selectLabel);
    }
	printf("%deme iteration\n",i+1);
  }
/*#ifndef ARCHI_PC*/
free(we);
free(nwS);
free(nwO);
/*#endif*/
}
/*#ifndef ARCHI_PC*/
free(tabCode);
/*#endif*/
autoSys->dc = autoSys->code + autoSys->numberState ;
completCod() ;
}

codageAleatoire(numBit,AME)
int numBit;
 int AME;
{
 p_groupe Se;
int *tabPoidEtat;
 int *affinity;
 int *we;
 int *nwS;
 int *nwO;
 int selectLabel;
int *tabCode;
int codeVal;
pCode code;
int numState;
int i,j;

numState = bit[numBit];
tabCode = (int*)mbkalloc(numState * sizeof(int)) ;

srand(getpid());
code = autoSys->code;
for(i=0; i < numState; i++)
     tabCode[i] = numState; /*mettre 100000 au lieu de 0 
                                  a fin de pouvoir faire le teste
                                  facilement  */ 
i = 0;
while(i < numState)
{
codeVal = (rand()/256) %numState;
if (tabCode[codeVal] == numState)
   {
     tabCode[codeVal] = i; 
     code->code = codeVal;
     code++;
     i++;
   }
}
 
if(AME>0)
{
nwS = (int *)mbkalloc((autoSys->numberState +1 )*(autoSys->numberState +1)  * sizeof(int )) ;
nwO = (int *)mbkalloc((autoSys->numberState + 1)*(autoSys->numberOut) * sizeof(int )) ;
 
mustPoids(nwS,nwO,NULL);
we = mustCalcul(nwS,nwO,NULL);
tabPoidEtat = asp_poidEtat();
affinity =PoidsFutur();
	printf("debut de l'amelioration \n");

for(i =0; i<AME; i++)
{
  Se= stateGeneration();
  while(Se)
  {
  selectLabel = codeModification(we,tabPoidEtat,affinity,tabCode,Se);
  Se = delFromgroup(Se, selectLabel);
  }
	printf("%deme iteration\n",i+1);
}

/*#ifndef ARCHI_PC*/
free(we);
free(nwS);
free(nwO);
/*#endif*/
}
/*#ifndef ARCHI_PC*/
free(tabCode);
/*#endif*/
autoSys->dc = autoSys->code + autoSys->numberState ;
}

codageVertical(numBit)
int numBit;
{
int *tabCode;
int Val;
int codeVal;
pCode code;
int i,k;

if(!(tabCode = (int*)calloc(bit[numBit] , sizeof(int))))
        {
        printf("auto.c,initializeAuto : allocation impossible\n") ;
        exit(-1) ;
        }
srand(getpid());
code = autoSys->code;

for(k = 0; k < numBit; k++)
 {
i = 0;
while(i < bit[numBit -1])
{
Val = (rand()/256)%bit[numBit];
codeVal = (code + Val)->code;
if ((codeVal < bit[k])&&(tabCode[codeVal] < bit[numBit - k -1]))
   {
     (code + Val)->code += bit[k]; 
     tabCode[codeVal]++;
     i++;
   }
}
  for(i = 0; i< bit[k]; i++) tabCode[i] = 0;
}
/*#ifndef ARCHI_PC*/
free(tabCode);
/*#endif*/
}
/*-------------------------------------------------------------------------
completCod : Complete le code des etats fontomes.
------------------------------------------------------
parametre  : rien.
-----------------------------------------------
return     : rien.
----------------------------------------------------------------------------*/
completCod()
{
int i, j, k, possible ;
pCode cod ;

k = autoSys->numberState ;
cod = autoSys->code ;

for(i = 0; i < bit[autoSys->numberReg]; i++)
	{
	possible = 1 ;
	for(j = 0; j < autoSys->numberState; j++)
		if((cod + j)->code == i)
			{
			possible = 0 ;
			break ;
			}
	if(possible)
		{
		(cod + k)->code = i ;
		k++ ;
		}
	}
}
