gbi2mem.c
3.38 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *OutputFileBase = NULL;
char *InputFile = NULL;
void usage(void)
{
fprintf(stderr, "usage: gbi2mem -i input.out -o output \n");
fprintf(stderr, "\tcreates output.{mem,tsk} \n");
fprintf(stderr, "\tinput.out can be created with: gload | tee input.out\n");
fprintf(stderr, "\t(hit ^c when ===... appears;\n");
fprintf(stderr, "\t use guDumpGbiDL or guDumpTurbo in program)\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, "i:o:")) != EOF) {
switch (c)
{
case 'i':
InputFile = strdup(optarg);
break;
case 'o':
OutputFileBase = strdup(optarg);
break;
default:
usage();
break;
}
}
if (OutputFileBase == NULL) printf("NO OUT FILE\n");
else printf("OUTFILE=%s\n",OutputFileBase);
if (InputFile == NULL) printf("NO IN FILE\n");
else printf("INFILE=%s\n",InputFile);
if (OutputFileBase == NULL)
usage();
}
#define MAXLINE 20
void getline(char *buf, FILE *fp)
{
int i=0,c;
while ((c=getc(fp)) != '\n') {
if (i<MAXLINE-1) buf[i++]=c;
}
buf[i]='\0';
}
main(int argc, char *argv[])
{
char buf[MAXLINE];
FILE *fp;
char file1[100],file2[100];
char dram[0x400000];
int done=0,addr,taskaddr,num,tasklen=0,*dp;
parse_args(argc, argv);
if (InputFile) {
if(!(fp=fopen(InputFile,"r"))) {
fprintf(stderr,"error opening %s\n",file2);
exit(0);
}
} else {
fp=stdin;
}
file1[0]='\0';
strcat(file1,OutputFileBase);
strcat(file1,".tsk");
file2[0]='\0';
strcat(file2,OutputFileBase);
strcat(file2,".mem");
while(1) {
getline(buf,fp);
if (!strcmp(&buf[0],"GBI_DUMP_START:"))
break;
}
while(!done) {
getline(buf,fp);
switch (buf[0]) {
case '$' : /* task header address */
sscanf(&buf[2],"%x",&taskaddr);
tasklen=0;
if (1) fprintf(stderr,"dumping task header\n");
else
case '@' : /* display list address */
if (1) fprintf(stderr,"dumping display list\n");
else
case '&' : /* dma data address */
if (1) fprintf(stderr,"dumping dma\n");
else
case '?' : /* code/code data address */
if (1) fprintf(stderr,"dumping code/code data\n");
else
case '%' : /* texture address */
if (1) fprintf(stderr,"dumping texture\n");
sscanf(&buf[2],"%x",&addr);
break;
case '<' : /* task header data */
tasklen += 4;
case '>' : /* 1st half of display list command */
case '-' : /* 2nd half of display list command */
case '+' : /* dma data */
case '*' : /* texture data */
case '|' : /* code/code data */
sscanf(&buf[1],"%x",&num);
dp=(int *)&dram[addr];
*dp=num;
addr += 4;
break;
case '^' :
done=1;
break;
}
}
if (InputFile) {
fclose(fp);
}
/*
* save .tsk file
*/
if(!(fp=fopen(file2,"w"))) {
fprintf(stderr,"error opening %s\n",file2);
exit(0);
}
fprintf(stderr,"\nwriting file %s\n", file2);
fwrite((void *)&dram[0],sizeof(char),0x400000,fp);
fclose(fp);
/*
* save .tsk file
*/
for (addr=taskaddr; addr<taskaddr+tasklen; addr += 4)
dram[addr] &= 0x7f;
if(!(fp=fopen(file1,"w"))) {
fprintf(stderr,"error opening %s\n",file1);
exit(0);
}
fprintf(stderr,"\nwriting file %s\n", file1);
fwrite((void *)&dram[taskaddr],sizeof(char),tasklen,fp);
fclose(fp);
fprintf(stderr,"done!\n");
}