dumpsave.c
3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#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);
}
}
}