-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharmdsp-mkbin.c
156 lines (132 loc) · 2.74 KB
/
armdsp-mkbin.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "armdsp.h"
int armdsp_sram_used;
int armdsp_dram_used;
int
parse_hex6x (char const *filename)
{
FILE *f;
char buf[10000];
unsigned int count, addr;
char *p;
int i;
int val;
unsigned char volatile *base;
int size;
if ((f = fopen (filename, "r")) == NULL)
return (-1);
while (fgets (buf, sizeof buf, f) != NULL) {
p = buf;
if (*p != 'S')
continue;
p++;
switch (*p) {
case '0':
continue;
case '7':
goto done;
case '3':
break;
default:
goto bad;
}
p++;
if (sscanf (p, "%2x%8x", &count, &addr) != 2)
goto bad;
p += 5 * 2;
count -= 5;
if (addr >= ARMDSP_SRAM_BASE
&& addr < ARMDSP_SRAM_BASE + ARMDSP_SRAM_SIZE) {
addr -= ARMDSP_SRAM_BASE;
base = armdsp_sram;
size = ARMDSP_SRAM_SIZE;
if (addr + count > armdsp_sram_used)
armdsp_sram_used = addr + count;
} else if (addr >= ARMDSP_DRAM_BASE
&& addr < ARMDSP_DRAM_BASE + ARMDSP_DRAM_SIZE) {
addr -= ARMDSP_DRAM_BASE;
base = armdsp_dram;
size = ARMDSP_DRAM_SIZE;
if (addr + count > armdsp_dram_used)
armdsp_dram_used = addr + count;
} else {
printf ("invalid addr 0x%x\n", addr);
exit (1);
}
if (addr + count > size)
goto bad;
for (i = 0; i < count; i++) {
if (sscanf (p, "%2x", &val) != 1)
goto bad;
p += 2;
base[addr + i] = val;
}
}
done:
fclose (f);
return (0);
bad:
fclose (f);
fprintf (stderr, "%s: invalid srecord\n", filename);
return (-1);
}
void
usage (void)
{
fprintf (stderr, "usage: armdsp-mkobj infile outfile\n");
exit (1);
}
void write_file (char *ext, void *base, int size);
char *inname;
int
main (int argc, char **argv)
{
int c;
while ((c = getopt (argc, argv, "")) != EOF) {
switch (c) {
default:
usage ();
}
}
if (optind >= argc)
usage ();
inname = argv[optind++];
if (optind != argc)
usage ();
armdsp_sram = malloc (ARMDSP_SRAM_SIZE);
armdsp_dram = malloc (ARMDSP_DRAM_SIZE);
if (armdsp_sram == NULL || armdsp_dram == NULL) {
fprintf (stderr, "out of memory\n");
exit (1);
}
if (parse_hex6x (inname) < 0) {
fprintf (stderr, "error reading %s\n", inname);
exit (1);
}
write_file (".sram", armdsp_sram, armdsp_sram_used);
write_file (".dram", armdsp_dram, armdsp_dram_used);
return (0);
}
void
write_file (char *ext, void *base, int size)
{
char outname[1000], *p;
FILE *outf;
snprintf (outname, sizeof outname - 10, "%s", inname);
if ((p = strrchr (outname, '.')) != NULL)
*p = 0;
strcat (outname, ext);
remove (outname);
if ((outf = fopen (outname, "w")) == NULL) {
fprintf (stderr, "can't create %s\n", outname);
exit (1);
}
fwrite (base, 1, size, outf);
fclose (outf);
}