dumpsave.c 3.42 KB

#include <stdio.h>
#include <string.h>

static int WriteOnce = 0;
char *OutputFileBase = NULL;

void usage(void)
{
    fprintf(stderr, "usage:  cat foo.out | dumpsave -o output [-x] \n");
    fprintf(stderr, "\tcreates output.{mem,tsk} \n");
    fprintf(stderr, "\t-x causes program to exit after doing dump \n");
    exit(0);
}

void parse_args(int argc, char *argv[])
{
    int c;
    extern char *optarg;
    extern int optind;
    char *ofile = NULL;
    
    while ((c = getopt(argc, argv, "o:x")) != EOF)
	switch (c)
	    {
	    case 'o':
		OutputFileBase = strdup(optarg);
		break;
	    case 'x':
		WriteOnce = 1;
		break;
	    case '?':
		usage();
		break;		
	    }    
    
    if (OutputFileBase == NULL)
	usage();	   
}

main(int argc, char *argv[])
{
	int c;
	int  act=0,ccount,count=0;
	char *word1="DUMP_START\n";
	char *word2="\n\n";
	char *word3="\n\n";
	FILE *fp;
	char file1[100],file2[100];
	int num,d,digit,addr=0;
	char dram[0x200000];

	parse_args(argc, argv);
	
	file1[0]='\0';
	strcat(file1,OutputFileBase);
	strcat(file1,".tsk");
	file2[0]='\0';
	strcat(file2,OutputFileBase);
	strcat(file2,".mem");

	while (1) {
		c=getchar();
		while(c==13) c=getchar();
		if (c=='#') {
			fprintf(stderr,"%c",c);
			c=getchar();
			while(c==13) c=getchar();
			while(c!='#') {
				fprintf(stderr,"%c",c);
				c=getchar();
				while(c==13) c=getchar();
			}
			fprintf(stderr,"%c\n",c);
			c=getchar();
			while(c==13) c=getchar();
			ccount=0;
		}
		if (act==1) {
			if (c==word2[ccount]) ccount++;
			else ccount=0;
			if (word2[ccount]=='\0') {
				act=2;
fprintf(stderr,"SWITCH TO SAVING DRAM\n");
				d=0;
				num=0;
				ccount=0;
				fclose(fp);
				if(!(fp=fopen(file2,"w"))) {
					fprintf(stderr,"error opening %s\n",file1);
					exit(0);
				}
				count=0;
				fprintf(stderr,"\ngathering dram for file %s\n",
								file2);
			}
			if (c>='A' && c<='F') c += 'a'-'A';
			digit=-1;
			if (c>='0' && c<='9') digit=c-'0';
			if (c>='a' && c<='f') digit=c-'a'+10;
			if (digit>=0) {
				num *= 16;
				num += digit;
				d++;
			}
			if (d==2) {
				fprintf(fp,"%c",num);
				num=0;
				d=0;
			}
		} else if (act==2) {
			if (c==word3[ccount]) ccount++;
			else ccount=0;
			if (word3[ccount]=='\0') {
fprintf(stderr,"END SAVING DRAM\n");
				d=0;
				num=0;
				act=0;
				fprintf(stderr,"\nwriting file %s\n",
								file2);
				fwrite((void *)&dram[0],sizeof(char),0x200000,fp);
				fclose(fp);
				fprintf(stderr,"done!\n");
                                if (WriteOnce) exit(1);
			}
			if (c=='$') { d=-9; num=0; } /* 8 digit hex addresses */
			if (c>='A' && c<='F') c += 'a'-'A';
			digit=-1;
			if (c>='0' && c<='9') digit=c-'0';
			if (c>='a' && c<='f') digit=c-'a'+10;
			if (digit>=0) {
				num *= 16;
				num += digit;
				d++;
			}
			if (d==-1) {
				fprintf(stderr,"...%08x\n",addr);
				addr=num&0x3fffff;
				fprintf(stderr,"%08x:\n",addr);
				d=0;
				num=0;
			}
			if (d==2) {
				dram[addr]=num;
				/*fprintf(stderr,"%08x:  %x\n",addr,num);*/
				addr++;
				num=0;
				d=0;
			}
		} else {
			if (c==word1[ccount]) ccount++;
			else ccount=0;
			if (word1[ccount]=='\0') {
				act=1;
				d=0;
fprintf(stderr,"SWITCH TO SAVING TASK HEADER\n");
				num=0;
				for(ccount=0;ccount<0x200000;ccount++)
					dram[ccount]=0;
				ccount=0;
				if(!(fp=fopen(file1,"w"))) {
					fprintf(stderr,"error opening %s\n",file1);
					exit(0);
                                }
				fprintf(stderr,"\ndumping to file %s\n",file1);

			}
			fprintf(stderr,"%c",c);
		}
	}
}