#include MUT_H 
#include MLO_H 
#include MLU_H
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "max.h"
#include "extern.h"
#include "foncspi.h"
#define PARSE_TRACE NO

extern chain_list *already_drived;
extern FILE *file_out;
#define HASH_MAX 100


spi_triplet_list *hashtable[HASH_MAX];

void spicesavelofig(lofig)
lofig_list *lofig;
{
char file_out_name[80];

strcpy(file_out_name,lofig -> NAME);
strcat(file_out_name, ".");
strcat(file_out_name, OUT_LO);
file_out = (FILE *) mbkfopen(lofig->NAME, OUT_LO, WRITE_TEXT);
	if(file_out == NULL){
	fprintf(stderr, "*** mbk error *** can't open file %s\n", file_out_name);
        EXIT(-1);}

        fprintf(file_out, "****spice description of %s\n", lofig -> NAME);
        fprintf(file_out, "****driver version 606\n\n\n");
 

spi_drive_dic(file_out,lofig);
spi_drive_mod(lofig);
freechain(already_drived);
already_drived = (chain_list *)NULL;
}

spi_drive_dic(pt_out,ptlofig)
lofig_list *ptlofig;
FILE *pt_out;
{
	losig_list *pt_aux = (losig_list *) NULL;
	for(pt_aux=ptlofig->LOSIG;pt_aux!=NULL;pt_aux=pt_aux->NEXT)
	{
	chain_list *chain ;

        if(pt_aux->TYPE == 'E')
        {
        locon_list *locon ;

        for(locon = ptlofig->LOCON ; locon->SIG != pt_aux ; locon = locon->NEXT);
	fprintf(pt_out,"* net %d = %s\n",pt_aux->INDEX,spi_busname(locon->NAME));
        }
        else
        for(chain = pt_aux->NAMECHAIN ; chain != NULL ; chain = chain->NEXT)
	if((strcmp((char *)chain->DATA,"") != 0) && (strncmp((char *)chain->DATA,"mbk",3) != 0))
	fprintf(pt_out,"* net %d = %s\n",pt_aux->INDEX,spi_busname((char *)chain->DATA));
	}
}

void spi_drive_mod(lofig)
lofig_list *lofig;
{
chain_list *pt_aux =(chain_list *) NULL;
char drived_cell();
void drive_basic_cells();
 
drive_basic_cells(lofig);
for(pt_aux=lofig->MODELCHAIN; pt_aux!=NULL; pt_aux=pt_aux->NEXT)
        {
        lofig_list *pt_aux1 =(lofig_list *) NULL;
        if(!drived_cell(already_drived,namealloc((char*)pt_aux->DATA)))
                        {
        pt_aux1 = getlofig(pt_aux->DATA,'A');
        if(pt_aux1 == NULL)
                {
                fprintf(stdout,"*******MBK ERROR : FIGURE %s DOESN'T EXIST******\n",pt_aux->DATA);
                exit(1);
                }
        spi_drive_mod(pt_aux1);
                        }
        }
        spi_drive_subckt(lofig);
}
 
