1 /* Second attempt to create the ccs version file for testing the rom boot loader */
2 /* No i2c blocking is performed */
4 #include <stdio.h>
5 #include <malloc.h>
8 int asciiByte (unsigned char c)
9 {
10 if ((c >= '0') && (c <= '9'))
11 return (1);
13 if ((c >= 'A') && (c <= 'F'))
14 return (1);
16 return (0);
17 }
19 int toNum (unsigned char c)
20 {
21 if ((c >= '0') && (c <= '9'))
22 return (c - '0');
24 return (c - 'A' + 10);
26 }
29 void stripLine (FILE *s)
30 {
31 char iline[132];
33 fgets (iline, 131, s);
35 }
37 /* Read a .b file. */
38 int readBFile (FILE *s, unsigned char *data, unsigned maxSize)
39 {
40 unsigned char x, y;
41 int byteCount = 0;
43 /* Strip the 1st two lines */
44 stripLine (s);
45 stripLine (s);
47 for (;;) {
49 /* read the 1st ascii char */
50 do {
51 x = fgetc (s);
52 if (x == (unsigned char)EOF)
53 return (byteCount);
55 } while (!asciiByte(x));
57 /* Read the next ascii char */
58 y = fgetc (s);
59 if (y == (unsigned char)EOF)
60 return (byteCount);
61 if (asciiByte(y))
62 data[byteCount++] = (toNum(x) << 4) | toNum (y);
64 if (byteCount >= maxSize) {
65 fprintf (stderr, "Max input array size exceeded\n");
66 return (-1);
67 }
69 }
72 }
75 int copyBlock (unsigned char *source, int idx, int maxSize, unsigned char *dest, int count)
76 {
77 int i;
79 for (i = 0; i < count; i++) {
80 if (idx >= maxSize)
81 break;
82 dest[i] = source[idx++];
83 }
85 return (i);
87 }
89 unsigned dwordConvert (unsigned char *data, int idx, int iMax)
90 {
91 unsigned value;
92 unsigned char c[4];
93 int i;
95 c[0] = c[1] = c[2] = c[3] = 0;
97 for (i = 0; i < 4; i++) {
98 if (idx >= iMax)
99 break;
100 c[i] = data[idx++];
101 }
103 value = c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24);
105 return (value);
107 }
111 #define SIZE 0x10000 /* max array size */
113 int main (int argc, char *argv[])
114 {
115 FILE *strin;
116 FILE *strout;
118 unsigned char *dataSet1;
120 unsigned pIn;
121 unsigned pOut;
123 int inSize;
124 int i;
126 /* Arg check */
127 if (argc != 3) {
128 fprintf (stderr, "usage: %s infile outfile\n", argv[0]);
129 return (-1);
130 }
132 /* Open the input file */
133 strin = fopen (argv[1], "r");
134 if (strin == NULL) {
135 fprintf (stderr, "%s: Could not open file %s for reading\n", argv[0], argv[1]);
136 return (-1);
137 }
139 /* Allocate the two data set memories */
140 dataSet1 = malloc (SIZE * sizeof (unsigned char));
141 if (dataSet1 == NULL) {
142 fprintf (stderr, "%s: Malloc failure\n", argv[0]);
143 return (-1);
144 }
146 /* Read the data into the byte stream */
147 if ((inSize = readBFile (strin, dataSet1, SIZE)) < 0)
148 return (inSize);
149 fclose (strin);
151 /* Copy the resulting data set into the output file in ccs format */
152 strout = fopen (argv[2], "w");
153 if (strout == NULL) {
154 fprintf (stderr, "%s: Could not open %s for writing\n", argv[0], argv[2]);
155 return (-1);
156 }
158 /* Write the ccs header. The number of lines must be computed from the
159 * array size */
160 fprintf (strout, "1651 1 b000 1 %x\n", (inSize + 3) / 4);
162 for (i = 0; i < inSize; i += 4)
163 fprintf (strout, "0x%08x\n", dwordConvert (dataSet1, i, inSize));
165 fclose (strout);
167 return (0);
169 }