data2rdram.c
3.65 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
#ifdef __sgi__
#include <bstring.h>
#endif
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define LINE_SIZE (80)
#define MAIN_SIZE (2*1024*1024)
#define HIDDEN_SIZE (MAIN_SIZE/8)
#define MAIN 0
#define HIDDEN 1
static int lineNumber = 0;
static void usage(void);
static void syntax(void);
main(int argc, char *argv[])
{
int c;
FILE *f;
int numberRdrams = 1;
char line[LINE_SIZE];
unsigned long long *mainBits;
unsigned char *hiddenBits;
int mainRdramSize, hiddenRdramSize;
int maxAddress;
unsigned int address;
int mainOrHidden = MAIN;
unsigned long l0, l1;
unsigned long long ll;
unsigned int b;
while ((c = getopt(argc, argv, "r:")) != EOF) {
switch (c) {
case 'r':
numberRdrams = atoi(optarg);
break;
case '?':
usage();
exit(1);
}
}
if ((argc - optind) > 1) {
usage();
exit(1);
}
if ((numberRdrams < 1) || (numberRdrams > 2)) {
fprintf(stderr, "data2rdram: number of rdrams must be 1 or 2\n");
exit(1);
}
if ((argc - optind) == 1) {
if ((f = fopen(argv[optind], "r")) == NULL) {
fprintf(stderr, "data2rdram: %s: cannot open (%s)\n",
argv[optind], strerror(errno));
exit(1);
}
} else {
f = stdin;
}
mainRdramSize = numberRdrams * MAIN_SIZE;
hiddenRdramSize = maxAddress = numberRdrams * HIDDEN_SIZE;
mainBits = (unsigned long long *)malloc(mainRdramSize);
hiddenBits = (unsigned char *)malloc(hiddenRdramSize);
if ((mainBits == NULL) || (hiddenBits == NULL)) {
fprintf(stderr, "data2rdram: malloc failed (%s)\n",
strerror(errno));
exit(1);
}
bzero(mainBits, mainRdramSize);
bzero(hiddenBits, hiddenRdramSize);
while (fgets(line, LINE_SIZE, f) != NULL) {
lineNumber++;
if ((line[0] == '\n') || ((line[0] == '/') && (line[1] == '/'))) {
continue;
}
if (sscanf(line, "@%x", &address) == 1) {
mainOrHidden = MAIN;
} else if (sscanf(line, "%%%x", &address) == 1) {
mainOrHidden = HIDDEN;
} else {
if (mainOrHidden == MAIN) {
if (address >= maxAddress) {
fprintf(stderr,
"data2rdram: address (0x%x) out of range at line %d\n",
address, lineNumber);
exit(1);
}
if (sscanf(line, "%x_%x", &l0, &l1) == 2) {
#ifdef __sgi__
mainBits[address++] = (long long)l0 << 32LL | (long long)l1;
#else
l0 = ntohl(l0);
l1 = ntohl(l1);
mainBits[address++] = (long long)l1 << 32LL | (long long)l0;
#endif
} else if (sscanf(line, "%llx", &ll) == 1) {
#ifdef __sgi__
mainBits[address++] = ll;
#else
l1 = (unsigned long) (ll >> 32);
l0 = (unsigned long) (ll & 0xffffffff);
l1 = ntohl(l1);
l0 = ntohl(l0);
mainBits[address++] = (long long)l0 << 32LL | (long long)l1;
#endif
} else {
syntax();
}
} else {
if (address >= maxAddress) {
fprintf(stderr,
"data2rdram: address (0x%x) out of range at line %d\n",
address, lineNumber);
exit(1);
}
if (sscanf(line, "%x", &b) == 1) {
hiddenBits[address++] = (unsigned char)b;
} else {
syntax();
}
}
}
}
if ((write(1, mainBits, mainRdramSize) != mainRdramSize) ||
(write(1, hiddenBits, hiddenRdramSize) != hiddenRdramSize)) {
fprintf(stderr, "data2rdram: %s: cannot write rdram file\n");
exit(1);
}
}
static void
usage(void)
{
fprintf(stderr, "usage: data2rdram [-r <num rdrams>] [.data file]\n");
exit(1);
}
static void
syntax(void)
{
fprintf(stderr, "data2rdram: syntax error on line %d\n", lineNumber);
exit(1);
}