⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loadsig.c

📁 hspice toolbox with matlab.
💻 C
📖 第 1 页 / 共 3 页
字号:
	    }

          /* test to see if more ascii header remains */
          if (strcmp(simsource,"hspice_win") == 0)
	    {
             if (term_seq == 0x23252624)
                break;
	    }
          else
	    {
             if (term_seq == 0x24262523)
                break;
	    }
	  /* old method:
          if (ibuf[1] == 2048) 
	     break;
          */
	  }
        ahdrsize += 20*i;

        if (i == MAX_HEADER_BLOCKS)
	  {
           mexPrintf("error in hs_allocate_sf:  too many header blocks!\n");
           mexPrintf("  must increase MAX_HEADER_BLOCKS value in source code\n");
           return(NULL);
	  }
        rewind(fp);
        fseek(fp,16,SEEK_CUR);


        if ((ahdr = (char *) mxCalloc(ahdrsize+1,sizeof(char))) == NULL)   
           {   
           mexPrintf("error in hs_allocate_sf:  mxCalloc call failed - \n");   
           mexPrintf("  not enough memory!!\n");   
           return(NULL);   
           }    
	if(fread(ahdr, sizeof(char), ahdrsize, fp) != ahdrsize) {
	     mexPrintf("error in hs_allocate_sf:  EOF reading block1\n");
             return(NULL);
	}
	ahdr[ahdrsize] = '\0';
	/* remove false '\0' characters */
        for (k = 0; k < ahdrsize; k++)
           if (ahdr[k] == '\0')
              ahdr[k] = ' ';

	/* remove block information from header */
        char_position = 0;
        for (j = 0; j < i; j++)
	  {
          char_position += header_seg_length[j];
          for (k = 0; k < 20; k++)
            ahdr[char_position+k] = ' ';
          char_position += 20;
	  }


	if(fread_int_values(simsource,ibuf,1, fp) != 1) {
	     mexPrintf("error in hs_allocate_sf: EOF reading block1 trailer\n");
             return(NULL);
	}
	if(ibuf[0] != header_seg_length[i]) {
	     mexPrintf("error in hs_allocate_sf:  block1 trailer mismatch\n");
             return(NULL);
	}


	if(fread_int_values(simsource,ibuf, 4, fp) != 4) 
            {
	    mexPrintf("error in rdhdr_hsbin:  EOF reading block2 header\n");
	    return(NULL);
	    }
	if(ibuf[0] != 4 || ibuf[2] != 4) 
            {
	    mexPrintf("error in rdhdr_hsbin:  unexepected values in block2 header\n");
	    return(NULL);
	    }
        block_size = ibuf[3]/sizeof(float);





	/* mexPrintf("block size = %d\n",block_size); */

	/* ahdr is an ascii header that describes the variables in
	 * much the same way that the first lines of the ascii format do,
	 * except that there are no newlines
	 */

        strncpy(version, &ahdr[16], 4);
        version[4] = '\0';
        strncpy(full_version, &ahdr[16], 43);
        full_version[43] = '\0';

	if (strncmp(full_version,"9601    Output Signals from C++ Simulation.",43) == 0)
          sprintf(simsource,"cppsim");
        else if (strncmp(full_version, "9601",4) == 0)
	  ;  /* no action necessary */
	else 	/* wrong version of post format */
	  {
           mexPrintf("error in hs_allocate_sf:  wrong binary format\n");
           mexPrintf("   only hspice post format '9601' or cppsim output is supported\n");
           mexPrintf("   in this case, the input file is version '%s'\n",version);
           return(NULL);
	  }

	strncpy(nbuf, &ahdr[0], 4);
	nbuf[4] = 0;
	nauto = atoi(nbuf);	/* number of automaticly-included variables,
				   first one is independent variable */
	strncpy(nbuf, &ahdr[4], 4);
	nbuf[4] = 0;
	nprobe = atoi(nbuf);	/* number of user-requested columns */
	/* mexPrintf("nprobe = %d, nauto = %d\n",nprobe,nauto);
	   return(NULL); */
        num_signals = nprobe + nauto;

	/* 
        mexPrintf("num_signals = %d, nprobe = %d, nauto = %d\n",num_signals,
            nprobe,nauto); 
        */
        /* mexPrintf("%s",ahdr); */
        strncpy(nbuf, &ahdr[8], 4);
        nbuf[4] = 0;
        nsweep_names = atoi(nbuf);
        data_rows = hs_determine_num_data_rows(&ahdr[256],num_signals,nauto,
             &sig_type,nsweep_names,&nsweep_var);
        mxFree(ahdr);


	/* count data lines */
        i = 0;
        sweep_count = 0;
        col_count = 0;
        while(1)
          {
          val_flag = hs_validate_column(fp,data_rows,i,nsweep_var,block_size,
                          simsource,col_count++);
          if (val_flag == 1)
             break;
          else if (val_flag == 2)
	    {
	      /* mexPrintf("i = %d\n",i); */
             if (sweep_count == 0)
                data_cols = i;  
             else
	       {
                if (data_cols > i)
                   data_cols = i;
	       }
             i = -1;
             fseek(fp,20,SEEK_CUR);
             sweep_count += 1;
	    }
          i++;
          } 
        if (sweep_count < 1)
	  {
          data_cols = i;
          sweep_count=1;
	  }
        return(init_SpiceData(num_signals,nauto,data_rows,data_cols,sweep_count,nsweep_var,nsweep_names,block_size));
}


int hs_determine_num_data_rows(char *line, int num_signals, int nauto, 
    int *ind_sig_type, int num_sweep_names, int *num_sweep_vars)
{
	char *cp;
	char *signam;
	int i;
	int hstype;
        int signal_type;
        int current_row;

        current_row = 0;

	/*	 mexPrintf("%s\n",line);  */
/* type of independent variable */
	cp = strtok(line, " \t\n");

	if(!cp) 
           {
	    mexPrintf("error in hs_determine_num_data_rows:  initial vartype not found on header line\n");
	    return(-1);
	   }

	hstype = atoi(cp);
	switch(hstype) {
	case 1:
		signal_type = TIME;
		break;
	case 2:
		signal_type = FREQUENCY;
		break;
	case 3:
		signal_type = VOLTAGE;
		break;
	default:
		signal_type = UNKNOWN;
		break;
	}
        *ind_sig_type = signal_type;
        current_row++;

/* dependent variable types */
	for(i = 1; i < num_signals; i++) 
           {
		cp = strtok(NULL, " \t\n");
		if(!cp) {
		    mexPrintf("error in hs_determine_num_data_rows: not enough vartypes on header line\n");
		    return(-1);
		}
		if(!isdigit(cp[0])) {
		    mexPrintf("error in hs_determine_num_data_rows:  bad vartype %d [%s] on header line\n", i, cp);
		    return(-1);
		}
		/* how many columns comprise this variable? */
		current_row++;
		if(i < nauto && signal_type == FREQUENCY) 
                    current_row++;
	  }

/* independent variable name */
	signam = strtok(NULL, " \t\n"); 
	if(!signam) {
		mexPrintf("error in hs_determine_num_data_rows: no IV name found on header line");
		return(-1);
	}

 /* dependent variable names */
	for(i = 1; i < num_signals; i++) 
           {
	    if((signam = strtok(NULL, " \t\n")) == NULL) 
                {
		mexPrintf("error in hs_determine_num_data_rows:  not enough DV names found on header line\n");
                mexPrintf(" num_signals = %d, got to %d\n",num_signals-1,i);
		return(-1);
		}
	    /*    mexPrintf("(%d) dv = %s\n",i,signam);   */
	   }
        if (num_sweep_names > 0)
	  {
	   if((signam = strtok(NULL, " \t\n")) == NULL) 
               {
	       mexPrintf("error in hs_determine_num_data_rows:  not enough sweep parameter names found on header line\n");
	       return(-1);
	       }
           if (strcmp(signam,"MONTE_CARLO") == 0)
               *num_sweep_vars = num_sweep_names;
           else if (num_sweep_names > 1)
               *num_sweep_vars = num_sweep_names-1;
           else
               *num_sweep_vars = num_sweep_names;
	  }
        else
           *num_sweep_vars = 0;

return(current_row);
}



/* Read spice-type file header - hspice binary */
int hs_determine_variables(FILE *fp, SpiceData *sf, char *simsource)
{

	char *ahdr;
	int ahdrsize;
	int nprobe,nsweep_var;
	char nbuf[16];
        int char_position, header_seg_length[MAX_HEADER_BLOCKS],i,j,k;  	
	unsigned int ibuf[4],term_seq;

        rewind(fp);

	if(fread_int_values(simsource,ibuf, 4, fp) != 4) 
           {
	    mexPrintf("error in hs_determine_variables: EOF reading block1 header\n");
            return(0);
	   } 

        ahdrsize = 0;
        for (i = 0; i < MAX_HEADER_BLOCKS; i++)
	  {
          header_seg_length[i] =ibuf[3];
          ahdrsize += header_seg_length[i];
          fseek(fp,header_seg_length[i]-8,SEEK_CUR);
 	  if(fread_int_values(simsource,&term_seq,1, fp) != 1) 
             {
	     mexPrintf("error in hs_determine_variables: EOF reading block trailer\n");
             return(0);
	     }
          fseek(fp,4,SEEK_CUR);

 	  if(fread_int_values(simsource,ibuf,1, fp) != 1) 
             {
	     mexPrintf("error in hs_determine_variables: EOF reading block trailer\n");
             return(0);
	     }
          if (ibuf[0] != header_seg_length[i])
	    {
	     mexPrintf("error in hs_determine_variables:  block trailer mismatch\n");
             return(0);
	    }
    	  if(fread_int_values(simsource,ibuf, 4, fp) != 4) 
            {
	    mexPrintf("error in hs_determine_variables:  EOF reading block header\n");
	    return(0);
	    }
	  if(ibuf[0] != 4 || ibuf[2] != 4) 
            {
	    mexPrintf("error in hs_determine_variables:  unexepected values in block header\n");
	    return(0);
	    }
          /* test to see if more ascii header remains */
          if (strcmp(simsource,"hspice_win") == 0)
	    {
             if (term_seq == 0x23252624)
                break;
	    }
          else
	    {
             if (term_seq == 0x24262523)
                break;
	    }
	  /* old method:
          if (ibuf[1] == 2048) 
	     break;
          */
	  }
        ahdrsize += 20*i;

        if (i == MAX_HEADER_BLOCKS)
	  {
           mexPrintf("error in hs_determine_variables:  too many header blocks!\n");
           mexPrintf("  must increase MAX_HEADER_BLOCKS value in source code\n");
           return(0);
	  }
        rewind(fp);
        fseek(fp,16,SEEK_CUR);



        if ((ahdr = (char *) mxCalloc(ahdrsize+1,sizeof(char))) == NULL)   
           {   
           mexPrintf("error in hs_determine_variables:  mxCalloc call failed - \n");   
           mexPrintf("  not enough memory!!\n");   
           return(0);   
           }    
	if(fread(ahdr, sizeof(char), ahdrsize, fp) != ahdrsize) {
	     mexPrintf("error in hs_determine_variables:  EOF reading block1\n");
             return(0);
	}
	ahdr[ahdrsize] = '\0';
	/* remove false '\0' characters */
        for (k = 0; k < ahdrsize; k++)
           if (ahdr[k] == '\0')
              ahdr[k] = ' ';

	/* remove block information from header */
        char_position = 0;
        for (j = 0; j < i; j++)
	  {
          char_position += header_seg_length[j];
          for (k = 0; k < 20; k++)
            ahdr[char_position+k] = ' ';
          char_position += 20;
	  }


	if(fread_int_values(simsource,ibuf,1, fp) != 1) {
	     mexPrintf("error in hs_determine_variables: EOF reading block1 trailer\n");
             return(0);
	}
	if(ibuf[0] != header_seg_length[i]) {
	     mexPrintf("error in hs_determine_variables:  block1 trailer mismatch\n");
             return(0);
	}

	hs_process_header(&ahdr[256], sf);
	
	if(fread_int_values(simsource,ibuf, 4, fp) != 4) {
	    mexPrintf("error in hs_determine_variables:  EOF reading block2 header\n");
            return(0);
	}

	sf->current_column = 0;
        mxFree(ahdr);
        return(1);
}


void hs_process_header(char *line, SpiceData *sf)
{
	char *cp;
	char *signam;
	int i,j;
	int hstype;
        int current_row;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -