rdpascii2rdram.c
8.14 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <mbi.h>
#include <verify.h>
#define ZBUFFER_ADDR 0x00280000 /* hard coded as per rdpvector's spec file */
#define CFB0_ADDR 0x00380000 /* hard coded as per rdpvector's spec file */
#define CFB1_ADDR 0x003a5800 /* hard coded as per rdpvector's spec file */
#define RDPLIST_ADDR 0x00000a00 /* placed above verifyInfo in 1st MB of ram*/
/*
* NOTE: Texture address is read in from the ascii dump.
*/
/*
* rdpascii2rdram.c: convert an ascii version of a set of RDP display lists
* and loaded textures to compressed 2MB binary .rdram files.
*/
main (int argc, char *argv[])
{
int ascii_fd, binary_fd = -1, bytesRead;
unsigned char readbuf[80], *readPtr;
char cmdbuf[160], fname[80], fname_prefix[80];
int bufIndex, eof = 0;
unsigned long long int rdp_dl_word;
int rdp_dl_length; /* Length is in 64 bit words */
unsigned long long int *fourMeg, *texAddr, *rdp_dl_addr;
int dumpingTexture = 0;
VerifyInfo *verify;
if (argc != 2) {
fprintf(stderr, "Usage: %s ascii_rdp_file\n", argv[0]);
exit(1);
}
if ( (ascii_fd = open(argv[1], O_RDONLY)) == -1) {
sprintf(cmdbuf, "Could not open ascii rdp file %s", argv[1]);
perror(cmdbuf);
exit(1);
}
fourMeg = (unsigned long long int *)malloc(0x400000);
if (fourMeg == NULL) {
fprintf(stderr, "Unable to malloc four megabyte .rdram buffer\n");
exit(1);
}
verify = (VerifyInfo *)( (int)(fourMeg) + VERIFY_INFO_PHYSADDR );
/*
* Initialize those fields within the verifyInfo struct which must be
* defined for purposes of RDP simulation. The only fields which may
* change for each display list we read in are:
*
* verify->rdpListSize
* verify->frameBufferSize
* verify->width
* verify->height
*/
verify->rdpListAddr = (unsigned int) (RDPLIST_ADDR);
verify->frameBuffer0Addr = (unsigned int) (CFB0_ADDR);
verify->frameBuffer1Addr = (unsigned int) (CFB1_ADDR);
verify->zBufferAddr = (unsigned int) (ZBUFFER_ADDR);
verify->frameBufferFormat = G_IM_FMT_RGBA;
verify->frameBufferSize = G_IM_SIZ_16b;
verify->width = 320;
verify->height = 240;
verify->magicNumber = MAGICNUMBER;
verify->version = VERSION;
while (!eof) {
bufIndex = 0;
bytesRead = read(ascii_fd, &(readbuf[bufIndex]), 1);
if (bytesRead == 0) {
close(ascii_fd);
/*
* Write out the rdp_dl_length, in bytes.
*/
verify->rdpListSize = (rdp_dl_length * 8);
/*
* Close the previously opened .rdram file, if one had been
* opened. Also run "rdramgclr" and "compress" on the file
* to clear the color & z buffers, as well as to shrink the
* 2MB to something more reasonable.
*/
if (binary_fd > 0) {
write(binary_fd, fourMeg, 0x400000);
if (verify->width == 1024) {
/*
* Make an 8MB file, so that rdramgclr's rdraminit()
* will size the rdram for a 8 MB system, which is
* what we need for a 2MB (1K x 1K) cfb + 2MB z buffer.
*
* The data written to the file here doesn't matter...
*/
write(binary_fd, fourMeg, 0x400000);
}
close(binary_fd);
sprintf(cmdbuf, "rdramgclr %s", fname_prefix);
system(cmdbuf);
sprintf(cmdbuf, "compress -f %s", fname);
system(cmdbuf);
}
exit(0);
} else {
while (readbuf[bufIndex++] != '\n') {
bytesRead = read(ascii_fd, &(readbuf[bufIndex]), 1);
if (bytesRead != 1) {
fprintf(stderr, "short read from ascii rdp file %s\n",
argv[1]);
exit(1);
}
}
/*
* We've read in a line from the ascii file. Check to see if the
* first line contains the word "TEST"; if so, the rest of the line
* will be read and used as the filename for the binary .rdp file
* we are to create from the rdp display list commands which follow.
*
* Otherwise, check to see if the line is a TEXTURE DUMP; if so,
* the intervening lines up to the next "TEST" are texture data,
* and need to be copied into the texture segment of our four meg
* rdram buffer.
*/
if (strncmp(readbuf, "TEST", 4) == 0) {
/*
* Read the third word on this line to determine 16/32 cfb.
*/
readPtr = readbuf + 5;
if (strlen(readPtr) == 0) {
fprintf(stderr,
"Bad filename encountered; unable to open rdp binary file.\n");
exit(1);
}
readPtr = (unsigned char *)strchr(readbuf, ' ');
readPtr++; /* Skip past the space */
readPtr = (unsigned char *)strchr(readPtr, ' ');
readPtr++; /* Skip past the space */
/*
* Terminate the remainder of the line so that we can
* better extract the filename.
*/
*(readPtr - 1) = '\0';
/*
* Write out the rdp_dl_length, in bytes.
*/
verify->rdpListSize = (rdp_dl_length * 8);
/*
* Turn off the "texture dump" mode, if we had been in that
* mode, and reset the rdp display list pointer.
*/
dumpingTexture = 0;
rdp_dl_addr = (unsigned long long int *)
( (int)(fourMeg) + RDPLIST_ADDR );
rdp_dl_length = 0;
/*
* Close the previously opened .rdram file, if one had been
* opened. Also run "rdramgclr" and "compress" on the file
* to clear the color & z buffers, as well as to shrink the
* 2MB to something more reasonable.
*/
if (binary_fd > 0) {
write(binary_fd, fourMeg, 0x400000);
if (verify->width == 1024) {
/*
* Make an 8MB file, so that rdramgclr's rdraminit()
* will size the rdram for a four MB system, which is
* what we need for a 2MB (1K x 1K) cfb + 2MB z buffer.
*
* The data written to the file here doesn't matter...
*/
write(binary_fd, fourMeg, 0x400000);
}
close(binary_fd);
sprintf(cmdbuf, "rdramgclr %s", fname_prefix);
system(cmdbuf);
sprintf(cmdbuf, "compress -f %s", fname);
system(cmdbuf);
}
/*
* Determine cfb size for the DL we are about to read in.
*/
if (strncmp(readPtr, "16", 2) == 0) {
verify->frameBufferSize = G_IM_SIZ_16b;
verify->width = 320;
verify->height = 240;
} else if (strncmp(readPtr, "32", 2) == 0) {
verify->frameBufferSize = G_IM_SIZ_32b;
verify->width = 320;
verify->height = 240;
} else if (strncmp(readPtr, "1024", 4) == 0) {
verify->frameBufferSize = G_IM_SIZ_32b;
verify->width = 1024;
verify->height = 1024;
} else {
fprintf(stderr, "Bogus cfb size %s\n", readPtr);
exit(1);
}
/*
* Skip past the "TEST ", to the start of the filename.
*/
readPtr = readbuf + 5;
if (strlen(readPtr) == 0) {
fprintf(stderr,
"Bad filename encountered; unable to open rdp binary file.\n");
exit(1);
}
/*
* Tack on a .rdram suffix.
*/
strcpy(fname, readPtr);
strcpy(fname_prefix, readPtr);
strcat(fname, ".rdram");
if ( (binary_fd = creat(fname, 0666)) == -1 ) {
sprintf(cmdbuf, "Could not open binary rdp file %s",
fname);
perror(cmdbuf);
exit(1);
}
} else if (strncmp(readbuf, "TEXTURE DUMP", 12) == 0) {
/*
* Texture data will be found on all following lines, up
* to the first line encountered beginning with "TEST".
*
* Skip past the "TEXTURE DUMP ", to the address of the texture
* segment.
*/
readPtr = readbuf + 13;
sscanf(readPtr, "%x", (char *)&texAddr);
dumpingTexture = 1;
texAddr = (unsigned long long int *)
( (int)(fourMeg) + (int)texAddr );
} else {
/*
* Use strchr & sscanf to convert a 64 bit word to binary,
* then write this long long (64 bit) word out to the currently
* open binary file.
*/
readPtr = (unsigned char *)strchr(readbuf, ':');
if (readPtr == NULL) {
fprintf(stderr, "strange line read from ascii rdp file %s\n",
readbuf);
exit(1);
}
/*
* Skip past the ": ", and point directly at the hexnum; terminate
* the string with a NULL character to end the conversion.
*/
readPtr += 2;
*(readPtr + 18) = '\0';
sscanf(readPtr, "%llx", (char *)&rdp_dl_word);
if (dumpingTexture) {
*texAddr++ = rdp_dl_word;
} else {
*rdp_dl_addr++ = rdp_dl_word;
rdp_dl_length++;
}
}
}
}
}