
/*
	GIER simulator - core dump

	(C) Copyright 2001-2010 by Mogens Kjaer


   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

void main(int argc, char **argv)
{
  FILE *fp;
  unsigned long long Ferrit[1024];
  int i,j,from,to,is1,first,first1;
  char buf[1024];
  char *bp,*cp;

  if(argc<4)
  {
    fprintf(stderr, "Usage: coredump file.gier from to\n");
    exit(-1);
  }

  fp = fopen(argv[1], "r");

  if(fp == NULL)
  {
    fprintf(stderr, "Could not open %s for reading.\n", argv[1]);
    exit(-1);
  }

  from=atoi(argv[2]);

  if(from<0 || from>1023)
  {
    fprintf(stderr, "From must be >= 0 and <=1023.\n");
    exit(-1);
  }
  to=atoi(argv[3]);
  if(to<0 || to>1023)
  {
    fprintf(stderr, "To must be >= 0 and <=1023.\n");
    exit(-1);
  }

  for(i=0;i<1024;i++) Ferrit[i] = 0ULL;

  while(1)
  {
    bp = fgets(buf, sizeof(buf), fp);
    if(bp == NULL) break;

    cp=strtok(bp, "\t\n");
    if(!cp) continue;

    if(!strncmp(cp, "Ferrit[", 7))
    {
      sscanf(cp+7, "%d", &i);
      cp=strtok(0, "\t\n");
#ifdef WINDOWS
      sscanf(cp, "%I64x", Ferrit+i);
#else
      sscanf(cp, "%llx", Ferrit+i);
#endif
    }
  }

  fclose(fp);

  for(i=from;i<=to;i++)
  {
    printf("%4d: ",i);
    is1=0;
    first=1;
    for(j=0;j<42;j++)
    {
      if((1ULL<<(41-j))&Ferrit[i])
      {
	if(!is1)
	{
	  if(!first) printf(",");
	  first=0;
	  printf("%d",j);
	  first1=j;
	  is1=1;
	}
      }
      else
      {
	if(is1)
	{
	  if(j>(first1+1))
	    printf("-%d",j-1);
	  is1=0;
	}
      }
    }
    if(is1&first1<41) printf("-41");
    printf("\n");
  }
}
