* .version: bumped version to 2.5.6 (pic14 ABI changed)
[fw/sdcc] / support / scripts / inc2h.pl
index 4283688edc4f0631ca6af71cf742eaee3a4b4773..589f38ce0a8a48e4c5e06c54e04ea31a5a194c9a 100755 (executable)
@@ -129,28 +129,44 @@ while (<DATA>) {
 #      }
 #  }
 
+# Create header for pic${processor}.c file
+$lcproc = "pic" . lc($processor);
+$c_head = <<EOT;
+/* Register definitions for $lcproc.
+ * This file was automatically generated by:
+ *   $programName V$version
+ *   Copyright (c) 2002, Kevin L. Pauba, All Rights Reserved
+ */
+#include <${lcproc}.h>
+
+EOT
+
 #
 # Convert the file.
 #
 $defaultType = 'other';
 $includeFile = $path.$path_delim.'header'.$path_delim.'p'.lc($processor).'.inc';
+$defsFile = "pic" . lc($processor) . ".c";
 open(HEADER, "<$includeFile")
     || die "$programName: Error: Cannot open include file $includeFile ($!)\n";
 
 while (<HEADER>) {
-    if (/^;-* (\S+) Bits/i) {
-       if (defined($alias{$1})) {
-           $defaultType = "bits $alias{$1}";
-       } else {
-           $defaultType = "bits $1";
+    if (/^;-+ (\S+) Bits/i) {
+        # also accept "UIE/UIR Bits"
+       foreach $name (split(/\//, $1)) {
+           if (defined($alias{$name})) {
+               $defaultType = "bits $alias{$name}";
+           } else {
+               $defaultType = "bits $name";
+           }
        }
        s/;/\/\//;
        $body .= "$_";
-    } elsif (/^;-* Register Files/i) {
+    } elsif (/^;-+ Register Files/i) {
        $defaultType = 'sfr';
        s/;/\/\//;
        $body .= "$_";
-    } elsif (/^;=*/i) {
+    } elsif (/^;=+/i) {
        $defaultType = '';
        s/;/\/\//;
        $body .= "$_";
@@ -203,7 +219,8 @@ while (<HEADER>) {
            } else {
                $addresses .= sprintf("#define %s_ADDR\t0x%s\n", $name, $value);
            }
-           $body .= sprintf("sfr  at %-30s %s;$rest\n", "${name}_ADDR", $name);
+           $body .= sprintf("extern sfr  __at %-30s $name;$rest\n", "(${name}_ADDR)" );
+           $c_head .= sprintf("sfr  __at %-30s $name;\n", "(${name}_ADDR)");
            $addr{"p$processor", "$name"} = "0x$value";
        } elsif ($type eq 'volatile') {
            #
@@ -213,7 +230,8 @@ while (<HEADER>) {
            $pragmas .= sprintf("#pragma memmap %s_ADDR %s_ADDR "
                                . "SFR %s\t// %s\n",
                                $name, $name, $bitmask, $name);
-           $body .= sprintf("data at %-30s %s;$rest\n", "${name}_ADDR volatile char", $name);
+           $body .= sprintf("extern data __at %-30s $name;$rest\n", "(${name}_ADDR) volatile char");
+           $c_head .= sprintf("data __at %-30s $name;\n", "(${name}_ADDR) volatile char");
            if (defined $addr{"p$processor", "$name"}) {
                $addresses .= sprintf("#define %s_ADDR\t0x%s\n", $name, $addr{"p$processor", "$name"});
            } else {
@@ -223,7 +241,13 @@ while (<HEADER>) {
            ($junk, $register) = split(/\s/, $type);
            $bit = hex($value);
            $addr = $addr{"$register"};
-           $body .= "BIT_AT(${register}_ADDR,$bit)\t$name;$rest\n";
+           # prepare struct declaration
+           for ($k=0; $k < scalar @{$bits{"$register"}->{oct($bit)}}; $k++) {
+             $name = "" if ($bits{"$register"}->{oct($bit)} eq $name)
+           }
+           if ($name ne "") {
+             push @{$bits{"$register"}->{oct($bit)}}, $name;
+           }
        } else {
            #
            # Other registers, bits and/or configurations.
@@ -249,8 +273,7 @@ while (<HEADER>) {
        #
        $body .= "\n";
     } elsif (/__MAXRAM\s+H'([0-9a-fA-F]+)'/) {
-       $maxram .= "//\n// Memory organization.\n//\n"
-           . sprintf("#pragma maxram 0x%s\n\n", $1);
+       $maxram .= "//\n// Memory organization.\n//\n";
        $pragmas = $maxram
            . $ram{"p$processor"} . "\n"
                . $pragmas;
@@ -287,21 +310,72 @@ $header .= <<EOT;
 #ifndef P${processor}_H
 #define P${processor}_H
 
-#ifndef BIT_AT
-#define BIT_AT(base,bitno) sbit at ((base<<3)+bitno)
-#endif
-
 //
 // Register addresses.
 //
 EOT
 
+$c_head .= <<EOT;
+
+// 
+// bitfield definitions
+// 
+EOT
+
+$structs = "";
+## create struct declarations
+foreach $reg (sort keys %bits)
+{
+  $structs .= "// ----- $reg bits --------------------\n";
+  $structs .= "typedef union {\n";
+  $idx = 0; $max = 1;
+  do {
+    $structs .= "  struct {\n";
+    for ($i=0; $i < 8; $i++)
+    {
+      @names = @{$bits{$reg}->{oct($i)}};
+      if ($max < scalar @names) { $max = scalar @names; }
+      if ($idx >= scalar @names) {
+       $structs .= "    unsigned char :1;\n";
+      } else { # (1 == scalar @names) {
+       $structs .= "    unsigned char " . $names[$idx] . ":1;\n";
+#      } else {
+#      $structs .= "  union {\n";
+#      foreach $name (@names) {
+#        $structs .= "    unsigned char " . $name . ":1;\n";
+#      } # foreach
+#      $structs .= "  };\n";
+      }
+    } # for
+    $structs .= "  };\n";
+    $idx++;
+  } while ($idx < $max);
+  $structs .= "} __${reg}_bits_t;\n";
+  $structs .= "extern volatile __${reg}_bits_t __at(${reg}_ADDR) ${reg}_bits;\n\n";
+  $c_head .= "volatile __${reg}_bits_t __at(${reg}_ADDR) ${reg}_bits;\n";
+  
+  # emit defines for individual bits
+  for ($i=0; $i < 8; $i++)
+  {
+    @names = @{$bits{$reg}->{oct($i)}};
+    foreach $field (@names) {
+      $structs .= sprintf("#define %-20s ${reg}_bits.$field\n", $field);
+    } # foreach
+  }
+  $structs .= "\n";
+} # foreach
+
 print $header
     . $addresses . "\n"
     . $pragmas . "\n\n"
-    . $body
+    . $body . "\n"
+    . $structs
     . "#endif\n";
 
+open(DEFS, ">$defsFile") or die "Could not open $defsFile for writing.";
+print DEFS $c_head . "\n";
+close DEFS;
+
 sub Usage {
        print STDERR <<EOT;
 
@@ -344,37 +418,3 @@ __END__
 
 alias OPTION_REG OPTION
 volatile INDF PCL
-
-#
-# bitmask settings.
-#
-processor 16F84
-    bitmask 0x080 INDF PCL STATUS FSR PCLATH INTCON
-
-processor 16F873, 16F874, 16F877, 16F627, 16F628, 16F876
-    bitmask 0x180 INDF PCL STATUS FSR PCLATH INTCON
-    bitmask 0x100 TMR0 OPTION_REG PORTB TRISB
-
-#
-# Memory maps.
-#
-processor 16F84
-    ram 0x000C 0x004f 0x080
-
-processor 16F627, 16F628
-    ram 0x0020 0x006f 0x000
-    ram 0x0070 0x007f 0x180
-    ram 0x00a0 0x00ef 0x000
-    ram 0x0120 0x014f 0x000
-
-processor 16F876, 16F877
-    ram 0x0020 0x006f 0x000
-    ram 0x0070 0x007f 0x180
-    ram 0x00a0 0x00ef 0x000
-    ram 0x0110 0x016f 0x000
-    ram 0x0190 0x01ef 0x000
-
-processor 16F873, 16F874
-    ram 0x0020 0x007f 0x100
-    ram 0x00a0 0x00ff 0x100
-