d586ed94637042576c24884ef5d6479e8a03c64d
[fw/sdcc] / support / scripts / inc2h.pl
1 #!/usr/bin/perl
2
3 # Copyright (c) 2002 Kevin L. Pauba
4
5
6 # License:
7 #
8 # SDCC is licensed under the GNU Public license (GPL) v2.  Note that
9 # this license covers the code to the compiler and other executables,
10 # but explicitly does not cover any code or objects generated by sdcc.
11 # We have not yet decided on a license for the run time libraries, but
12 # it will not put any requirements on code linked against it. See:
13
14 # http://www.gnu.org/copyleft/gpl.html
15 #
16 # See http://sdcc.sourceforge.net/ for the latest information on sdcc.
17
18
19 $rcsid = q~$Id$~;
20 ($junk, $file, $version, $date, $time, $programmer, $status)
21   = split(/\s+/, $rcsid);
22 ($programName) = ($file =~ /(\S+),v/);
23
24 if ($#ARGV != 0) {
25         Usage();
26 }
27
28 #
29 # Read the symbols at the end of this file.
30 #
31 while (<DATA>) {
32         next if /^\s*#/;
33
34         if (/^alias\s+(\S+)\s+(\S+)/) {
35                 #
36                 # Set an alias for a special function register.
37                 # Some MPASM include files are not entirely consistent
38                 # with sfr names.
39                 #
40                 $alias{$2} = $1;
41         } elsif (/^address\s+(\S+)\s+(\S+)/) {
42                 #
43                 # Set a default address for a special function register.
44                 # Some MPASM include files don't specify the address
45                 # of all registers.
46                 # 
47                 $addr{"$1"} = $2;
48         } elsif (/^(\S+)/) {
49                 $type = $1;
50         } else {
51                 foreach $key (split) {
52                         eval "\$type{'$key'} = $type;";
53                 }
54         }
55 }
56
57 #
58 # Print the header.
59 #
60 print <<EOT;
61 //
62 // This header file was automatically generated by:
63 //
64 //\t$programName V$version
65 //
66 // 
67
68 #ifndef BIT_AT
69 #define BIT_AT(base,bitno) bit at ((base<<3)+bitno)
70 #endif
71
72
73 EOT
74
75 #
76 # Convert the file.
77 #
78 $defaultType = 'other';
79 while (<>) {
80         if (/^;-* (\S+) Bits/i) {
81                 if (defined($alias{$1})) {
82                         $defaultType = "bits $alias{$1}";
83                 } else {
84                         $defaultType = "bits $1";
85                 }
86                 s/;/\/\//;
87                 print "$_";
88         } elsif (/^;-* Register Files/i) {
89                 $defaultType = 'sfr';
90                 s/;/\/\//;
91                 print "$_";
92         } elsif (/^;=*/i) {
93                 $defaultType = '';
94                 s/;/\/\//;
95                 print "$_";
96         } elsif (/^\s*;/) {
97                 #
98                 # Convert ASM comments to C style.
99                 #
100                 print "//$'";
101         } elsif (/^\s*IFNDEF __(\S+)/) {
102                 #
103                 # Processor type.
104                 #
105                 $processor = $1;
106                 print "//$_";
107         } elsif (/^\s*(\S+)\s+EQU\s+H'(.+)'/) {
108                 #
109                 # Useful bit of information.
110                 #
111                 $name = $1;
112                 $value = $2;
113                 $rest = $';
114                 $rest =~ s/;/\/\//;
115                 chomp($rest);
116
117                 if (defined($type{"$name"})) {
118                         $type = $type{"$name"};
119                 } else {
120                         $type = $defaultType;
121                 }
122
123                 if ($type eq 'sfr') {
124                         #
125                         # A special function register.
126                         #
127                         printf("sfr at 0x%s %s;$rest\n", $value, $name);
128                         $addr{"$name"} = "0x$value";
129                 } elsif ($type eq 'volatile') {
130                         #
131                         # A location that can change without 
132                         # direct program manipulation.
133                         #
134                         printf("data at 0x%s volatile char %s;$rest\n", $value, $name);
135                 } elsif ($type =~ /^bits/) {
136                         ($junk, $register) = split(/\s/, $type);
137                         $bit = hex($value);
138                         $addr = $addr{"$register"};
139                         printf("BIT_AT($addr,$bit) $name;$rest\n");
140                 } else {
141                         #
142                         # Other registers, bits and/or configurations.
143                         #
144                         if ($type eq 'other') {
145                                 #
146                                 # A known symbol.
147                                 #
148                                 printf("#define %-20s 0x%s$rest\n", $name, $value);
149                         } else {
150                                 #
151                                 # A symbol that isn't defined in the data
152                                 # section at the end of the file.  Let's 
153                                 # add a comment so that we can add it later.
154                                 #
155                                 printf("#define %-20s 0x%s$rest\n",
156                                        $name, $value);
157                         }
158                 }
159         } elsif (/^\s*$/) {
160                 #
161                 # Blank line.
162                 #
163                 print;
164         } else {
165                 #
166                 # Anything else we'll just comment out.
167                 #
168                 print "// $_";
169         }
170 }
171
172 sub Usage {
173         print STDERR <<EOT;
174
175 inc2h.pl - A utility to convert MPASM include files to header files
176            suitable for the SDCC compiler.
177
178 License: Copyright (c) 2002 Kevin L. Pauba
179
180          SDCC is licensed under the GNU Public license (GPL) v2; see
181          http://www.gnu.org/copyleft/gpl.html See http://sdcc.sourceforge.net/
182          for the latest information on sdcc.
183
184 Usage:   $programName [file]
185
186          where:
187
188          file   A MPASM include file name.  If none is supplied, the
189                 standard input will be used.
190
191          The header file will be written to the standard output.
192
193
194 EOT
195         exit;
196 }
197
198 __END__
199
200 alias OPTION_REG OPTION
201 address OPTION_REG 0x0081
202
203 sfr
204
205 volatile
206         INDF PCL
207
208 bit
209