source: rtems/c/src/lib/libbsp/arm/nds/tools/bin2s.c @ 9eac014

4.104.115
Last change on this file since 9eac014 was a36b649, checked in by Ralf Corsepius <ralf.corsepius@…>, on 11/30/09 at 04:16:52

2009-11-30 Ralf Corsépius <ralf.corsepius@…>

  • bin2s.c: Remove CVS-$Log's.
  • Property mode set to 100644
File size: 4.9 KB
Line 
1/*---------------------------------------------------------------------------------
2        $Id$
3
4        bin2s: convert a binary file to a gcc asm module
5        for gfx/foo.bin it'll write foo_bin (an array of char)
6        foo_bin_end, and foo_bin_len (an unsigned int)
7        for 4bit.chr it'll write _4bit_chr, _4bit_chr_end, and
8        _4bit_chr_len
9
10
11        Copyright 2003 - 2005 Damian Yerrick
12
13        Permission is hereby granted, free of charge, to any person obtaining
14        a copy of this software and associated documentation files (the
15        "Software"), to deal in the Software without restriction, including
16        without limitation the rights to use, copy, modify, merge, publish,
17        distribute, sublicense, and/or sell copies of the Software, and to
18        permit persons to whom the Software is furnished to do so, subject to
19        the following conditions:
20
21        The above copyright notice and this permission notice shall be
22        included in all copies or substantial portions of the Software.
23
24        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25        EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
26        OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27        NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28        BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
29        AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
30        OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31        IN THE SOFTWARE.
32
33---------------------------------------------------------------------------------*/
34
35
36
37/*
38.align
39.global SomeLabel_len
40.int 1234
41.global SomeLabel
42.byte blah,blah,blah,blah...
43*/
44
45#include <stdio.h>
46#include <stdlib.h>
47#include <ctype.h>
48
49/*---------------------------------------------------------------------------------
50   Print the closest valid C identifier to a given word.
51---------------------------------------------------------------------------------*/
52void strnident(FILE *fout, const char *src) {
53//---------------------------------------------------------------------------------
54        char got_first = 0;
55
56        while(*src != 0) {
57
58                int s = *src++;
59
60                /* initial digit  */
61                if(isdigit(s) && !got_first)
62                fputc('_', fout);  /* stick a '_' before an initial digit */
63
64                /* convert only out-of-range characters */
65                if(!isalpha(s) && !isdigit(s) && (s != '_')) {
66                        if(s == '-' || s == '.' || s == '/') s = '_';
67                else
68                        s = 0;
69                }
70
71                if(s) {
72                        fputc(s, fout);
73                        got_first = 1;
74                }
75        }
76}
77
78
79//---------------------------------------------------------------------------------
80int main(int argc, char **argv) {
81//---------------------------------------------------------------------------------
82        FILE *fin;
83        size_t filelen;
84        int linelen;
85        int arg;
86        int alignment = 4;
87        if(argc < 2) {
88                fputs(  "bin2s - convert binary files to assembly language\n"
89                                                "typical usage: bin2s foo.bin bar.bin baz.bin > foo.s\n", stderr);
90                return 1;
91        }
92
93  for(arg = 1; arg < argc; arg++) {
94
95                if (argv[arg][0] == '-')
96                {
97                        switch (argv[arg][1])
98                        {
99                                case 'a':
100
101                                        alignment = (argc > arg) ? strtoul(argv[++arg], 0, 0) : 0;
102
103                                        if ( alignment == 0 ) alignment =4;
104                                        break;
105                        }
106                        continue;
107                }
108
109
110        fin = fopen(argv[arg], "rb");
111
112        if(!fin) {
113                fputs("bin2s: could not open ", stderr);
114                perror(argv[arg]);
115                return 1;
116        }
117
118                fseek(fin, 0, SEEK_END);
119                filelen = ftell(fin);
120                rewind(fin);
121
122                if(filelen == 0) {
123                        fclose(fin);
124                        fprintf(stderr, "bin2s: warning: skipping empty file %s\n", argv[arg]);
125                        continue;
126                }
127
128                char *ptr = argv[arg];
129                char chr;
130                char *filename = NULL;
131
132                while ( (chr=*ptr) ) {
133
134                        if ( chr == '\\' || chr == '/') {
135
136                                filename = ptr;
137                        }
138
139                        ptr++;
140                }
141
142                if ( NULL != filename ) {
143                        filename++;
144                } else {
145                        filename = argv[arg];
146                }
147
148                /*---------------------------------------------------------------------------------
149                        Generate the prolog for each included file.  It has two purposes:
150
151                        1. provide length info, and
152                        2. align to user defined boundary, default is 32bit
153
154                ---------------------------------------------------------------------------------*/
155                fprintf(        stdout, "/* Generated by BIN2S - please don't edit directly */\n"
156                                                "\t.section .rodata\n"
157                                                "\t.balign %d\n"
158                                                "\t.global ", alignment);
159                strnident(stdout, filename);
160                fputs("_size\n", stdout);
161                strnident(stdout, filename);
162                printf("_size: .int %lu\n\t.global ", (unsigned long)filelen);
163                strnident(stdout, filename);
164                fputs("\n", stdout);
165                strnident(stdout, filename);
166                fputs(":\n\t.byte ", stdout);
167
168                linelen = 0;
169
170                while(filelen > 0) {
171                        unsigned char c = fgetc(fin);
172
173                        printf("%3u", (unsigned int)c);
174                        filelen--;
175
176                        /* don't put a comma after the last item */
177                        if(filelen) {
178
179                                /* break after every 16th number */
180                                if(++linelen >= 16) {
181                                        linelen = 0;
182                                        fputs("\n\t.byte ", stdout);
183                                } else {
184                                        fputc(',', stdout);
185                                }
186                        }
187                }
188
189                fputs("\n\n\t.global ", stdout);
190                strnident(stdout, filename);
191                fputs("_end\n", stdout);
192                strnident(stdout, filename);
193                fputs("_end:\n\n", stdout);
194
195
196                fclose(fin);
197        }
198
199        return 0;
200}
201
Note: See TracBrowser for help on using the repository browser.