3 # Copyright (c) 2002 Kevin L. Pauba
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:
14 # http://www.gnu.org/copyleft/gpl.html
16 # See http://sdcc.sourceforge.net/ for the latest information on sdcc.
20 ($junk, $file, $version, $date, $time, $programmer, $status)
21 = split(/\s+/, $rcsid);
22 ($programName) = ($file =~ /(\S+),v/);
24 if ($#ARGV < 0 || $#ARGV > 1 ) {
27 $processor = uc(shift);
30 if ($^O eq 'MSWin32') {
32 if (defined($path = $ENV{'GPUTILS_HEADER_PATH'}) || defined($path = $ENV{'GPUTILS_LKR_PATH'})) {
36 die "Could not find gpasm includes.\n";
42 # Nathan Hurst <njh@mail.csse.monash.edu.au>: find gputils on Debian
44 if ( -x "/usr/share/gputils") {
45 $path = "/usr/share/gputils";
46 } elsif ( -x "/usr/share/gpasm") {
47 $path = "/usr/share/gpasm";
49 die "Could not find gpasm includes.\n";
56 # Read the symbols at the end of this file.
61 if (/^\s*alias\s+(\S+)\s+(\S+)/) {
63 # Set an alias for a special function register.
64 # Some MPASM include files are not entirely consistent
68 } elsif (/^\s*address\s+(\S+)\s+(\S+)/) {
70 # Set a default address for a special function register.
71 # Some MPASM include files don't specify the address
75 foreach $device (split(/[,\s]+/, $devices)) {
76 $addr{"p$device", "$1"} = $2;
78 } elsif (/^\s*bitmask\s+(\S+)\s+/) {
80 # Set the bitmask that will be used in the 'memmap' pragma.
83 foreach $register (split(/\s+/, $')) {
84 $bitmask{"$register"} = $bitmask;
86 } elsif (/^\s*ram\s+(\S+)\s+(\S+)\s+(\S+)/) {
87 # This info is now provided in "include/pic/pic14devices.txt".
91 #foreach $device (split(/[,\s]+/, $devices)) {
92 # $ram{"p$device"} .= "#pragma memmap $lo $hi RAM $bitmask$'";
94 } elsif (/^\s*processor\s+/) {
97 } elsif (/^\s*(\S+)/) {
100 foreach $key (split) {
101 eval "\$type{'$key'} = $type;";
104 foreach $key (split) {
105 eval "\$type{'$key'} = $type;";
111 # Read the linker file.
113 # $linkFile = "$path/lkr/" . lc $processor . ".lkr";
114 # open(LINK, "<$linkFile")
115 # || die "$programName: Error: Cannot open linker file $linkFile ($!)\n";
117 # if (/^(\S+)\s+NAME=(\S+)\s+START=(\S+)\s+END=(\S+)\s+(PROTECTED)?/) {
122 # $protected = 1 if ($5 =~ /protected/i);
124 # if ($type =~ /(SHAREBANK)|(DATABANK)/i) {
125 # $ram{"p$processor"} .=
126 # sprintf("#pragma memmap %7s %7s RAM 0x000\t// $name\n",
129 # } elsif (/^SECTION\s+NAME=(\S+)\s+ROM=(\S+)\s+/) {
133 # Create header for pic${processor}.c file
134 $lcproc = "pic" . lc($processor);
136 /* Register definitions for $lcproc.
137 * This file was automatically generated by:
138 * $programName V$version
139 * Copyright (c) 2002, Kevin L. Pauba, All Rights Reserved
141 #include <${lcproc}.h>
148 $defaultType = 'other';
149 $includeFile = $path.$path_delim.'header'.$path_delim.'p'.lc($processor).'.inc';
150 $defsFile = "pic" . lc($processor) . ".c";
151 open(HEADER, "<$includeFile")
152 || die "$programName: Error: Cannot open include file $includeFile ($!)\n";
155 if (/^;-+ (\S+) Bits/i) {
156 # also accept "UIE/UIR Bits"
157 foreach $name (split(/\//, $1)) {
158 if (defined($alias{$name})) {
159 $defaultType = "bits $alias{$name}";
161 $defaultType = "bits $name";
166 } elsif (/^;-+ Register Files/i) {
167 $defaultType = 'sfr';
176 # Convert ASM comments to C style.
179 } elsif (/^\s*IFNDEF __(\S+)/) {
185 } elsif (/^\s*(\S+)\s+EQU\s+H'(.+)'/) {
187 # Useful bit of information.
195 if (defined($type{"p$processor", "$name"})) {
196 $type = $type{"p$processor", "$name"};
197 } elsif (defined($type{"$name"})) {
198 $type = $type{"$name"};
200 $type = $defaultType;
203 if (defined($bitmask{"p$processor", "$name"})) {
204 $bitmask = $bitmask{"p$processor", "$name"};
205 # } elsif (defined($bitmask{"$name"})) {
206 # $bitmask = $bitmask{"$name"};
211 if ($type eq 'sfr') {
213 # A special function register.
215 $pragmas .= sprintf("#pragma memmap %s_ADDR %s_ADDR "
217 $name, $name, $bitmask, $name);
218 if (defined $addr{"p$processor", "$name"}) {
219 $addresses .= sprintf("#define %s_ADDR\t0x%s\n", $name, $addr{"p$processor", "$name"});
221 $addresses .= sprintf("#define %s_ADDR\t0x%s\n", $name, $value);
223 $body .= sprintf("extern __sfr __at %-30s $name;$rest\n", "(${name}_ADDR)" );
224 $c_head .= sprintf("__sfr __at %-30s $name;\n", "(${name}_ADDR)");
225 $addr{"p$processor", "$name"} = "0x$value";
226 } elsif ($type eq 'volatile') {
228 # A location that can change without
229 # direct program manipulation.
231 $pragmas .= sprintf("#pragma memmap %s_ADDR %s_ADDR "
233 $name, $name, $bitmask, $name);
234 $body .= sprintf("extern __data __at %-30s $name;$rest\n", "(${name}_ADDR) volatile char");
235 $c_head .= sprintf("__data __at %-30s $name;\n", "(${name}_ADDR) volatile char");
236 if (defined $addr{"p$processor", "$name"}) {
237 $addresses .= sprintf("#define %s_ADDR\t0x%s\n", $name, $addr{"p$processor", "$name"});
239 $addresses .= sprintf("#define %s_ADDR\t0x%s\n", $name, $value);
241 } elsif ($type =~ /^bits/) {
242 ($junk, $register) = split(/\s/, $type);
244 $addr = $addr{"$register"};
245 # prepare struct declaration
246 for ($k=0; $k < scalar @{$bits{"$register"}->{oct($bit)}}; $k++) {
247 $name = "" if ($bits{"$register"}->{oct($bit)} eq $name)
250 push @{$bits{"$register"}->{oct($bit)}}, $name;
254 # Other registers, bits and/or configurations.
256 if ($type eq 'other') {
260 $body .= sprintf("#define %-20s 0x%s$rest\n", $name, $value);
263 # A symbol that isn't defined in the data
264 # section at the end of the file. Let's
265 # add a comment so that we can add it later.
267 $body .= sprintf("#define %-20s 0x%s$rest\n",
276 } elsif (/__MAXRAM\s+H'([0-9a-fA-F]+)'/) {
277 $maxram .= "//\n// Memory organization.\n//\n";
279 . $ram{"p$processor"} . "\n"
284 # Anything else we'll just comment out.
291 // Register Declarations for Microchip $processor Processor
294 // This header file was automatically generated by:
296 //\t$programName V$version
298 //\tCopyright (c) 2002, Kevin L. Pauba, All Rights Reserved
300 //\tSDCC is licensed under the GNU Public license (GPL) v2. Note that
301 //\tthis license covers the code to the compiler and other executables,
302 //\tbut explicitly does not cover any code or objects generated by sdcc.
303 //\tWe have not yet decided on a license for the run time libraries, but
304 //\tit will not put any requirements on code linked against it. See:
306 //\thttp://www.gnu.org/copyleft/gpl/html
308 //\tSee http://sdcc.sourceforge.net/ for the latest information on sdcc.
311 #ifndef P${processor}_H
312 #define P${processor}_H
315 // Register addresses.
322 // bitfield definitions
327 ## create struct declarations
328 foreach $reg (sort keys %bits)
330 $structs .= "// ----- $reg bits --------------------\n";
331 $structs .= "typedef union {\n";
334 $structs .= " struct {\n";
335 for ($i=0; $i < 8; $i++)
337 @names = @{$bits{$reg}->{oct($i)}};
338 if ($max < scalar @names) { $max = scalar @names; }
339 if ($idx >= scalar @names) {
340 $structs .= " unsigned char :1;\n";
341 } else { # (1 == scalar @names) {
342 $structs .= " unsigned char " . $names[$idx] . ":1;\n";
344 # $structs .= " union {\n";
345 # foreach $name (@names) {
346 # $structs .= " unsigned char " . $name . ":1;\n";
348 # $structs .= " };\n";
353 } while ($idx < $max);
354 $structs .= "} __${reg}_bits_t;\n";
355 $structs .= "extern volatile __${reg}_bits_t __at(${reg}_ADDR) ${reg}_bits;\n\n";
356 $c_head .= "volatile __${reg}_bits_t __at(${reg}_ADDR) ${reg}_bits;\n";
358 # emit defines for individual bits
359 for ($i=0; $i < 8; $i++)
361 @names = @{$bits{$reg}->{oct($i)}};
362 foreach $field (@names) {
363 $structs .= sprintf("#define %-20s ${reg}_bits.$field\n", $field);
376 open(DEFS, ">$defsFile") or die "Could not open $defsFile for writing.";
377 print DEFS $c_head . "\n";
383 inc2h.pl - A utility to convert MPASM include files to header files
384 suitable for the SDCC compiler.
386 License: Copyright (c) 2002 Kevin L. Pauba
388 SDCC is licensed under the GNU Public license (GPL) v2; see
389 http://www.gnu.org/copyleft/gpl.html See http://sdcc.sourceforge.net/
390 for the latest information on sdcc.
392 Usage: $programName processor [path]
396 processor The name of the processor (16f84, 16f877, etc.)
398 path The path to the parent of the "header" and "lkr"
399 directories. The default is "/usr/share/gpasm".
401 The header file will be written to the standard output.
411 # processor <processor_name>
412 # address <register_name> <hex_address>
413 # bitmask <bitmask> <register_list>
414 # ram <lo_address> <hi_address> <bitmask>
415 # sfr <register_list>
416 # volatile <address_list>
420 alias OPTION_REG OPTION