src/jtag/drivers/ep93xx: fix GCC 12 warning
[fw/openocd] / contrib / gen-stellaris-part-header.pl
1 #!/usr/bin/perl
2 # SPDX-License-Identifier: GPL-2.0-or-later
3
4 # Automatically generates the StellarisParts struct in src/flash/nor/stellaris.c
5 # Uses the header files from TI/Luminary's StellarisWare complete Firmware Development Package
6 # available from: http://www.luminarymicro.com/products/software_updates.html
7
8 $comment = "// Autogenerated by contrib/gen-stellaris-part-header.pl
9 // From Stellaris Firmware Development Package revision";
10
11 $struct_header = "static const struct {
12         uint8_t class;
13         uint8_t partno;
14         const char *partname;
15 } StellarisParts[] = {
16 ";
17
18 $struct_footer = "\t{0xFF, 0x00, \"Unknown Part\"}\n};\n";
19
20 $#ARGV == 1 || die "Usage: $0 <inc directory> <output file>\n";
21 -d $ARGV[0] || die $ARGV[0]." is not a directory\n";
22 $dir = $ARGV[0];
23 -f $ARGV[1] || die $ARGV[1]." is not a file\n";
24 $file = $ARGV[1];
25 print STDERR "Scanning $dir, Updating $file\n";
26
27 opendir(DIR, $dir) || die "can't open $dir: $!";
28 @files = readdir(DIR);
29 closedir(DIR);
30
31 @header_files = sort(grep(/lm.+\.h/, @files));
32
33 $ver = 0;
34 $new_struct = $struct_header;
35 process_file(@header_files);
36 $new_struct .= $struct_footer;
37
38 $dump = "$comment $ver\n$new_struct";
39 {
40         local($/, *INPUT);
41         open(INPUT, $file) || die "can't open $file: $!";
42         $contents = <INPUT>;
43         close(INPUT);
44 }
45
46 $old_struct = qr/((^\/\/.*?\n)*)\Q$struct_header\E.*?$struct_footer/sm;
47 $contents =~ s/$old_struct/$dump/;
48 open(OUTPUT, ">$file") || die "can't open file $file for writing: $!";
49 print OUTPUT $contents;
50 close(OUTPUT);
51
52 sub process_file {
53         foreach $h_file (@_) {
54                 ($base) = ($h_file =~ m/lm..(.{3,7})\.h/ig);
55                 $base = uc($base);
56                 local($/, *FILE);
57                 open(FILE, "$dir/$h_file");
58                 $content = <FILE>;
59                 close(FILE);
60                 $invalid = 0;
61                 if ($content =~ /This is part of revision (\d+) of/) {
62                         if ($ver != 0 and $ver != $1) {
63                                 print STDERR "File version mismatch: $ver != $1\n";
64                                 $ver = max($ver, $1);
65                         } else {
66                                 $ver = $1;
67                         }
68                 }
69
70                 if ($content =~ /SYSCTL_DID0_CLASS_[^M].+?0x(\S+)/s) {
71                         $class = hex($1) >> 16;
72                 } else {
73                         # attempt another way to get class
74                         if ($content =~ /\s(\S+)-class/) {
75                                 $class = getclass($1);
76                                 if ($class eq 0xFF) {
77                                         print STDERR "$h_file unknown class\n";
78                                         $invalid = 1;
79                                 }
80                         } else {
81                                 print STDERR "$h_file is missing SYSCTL_DID0_CLASS_\n";
82                                 $class = 0;
83                                 $invalid = 1;
84                         }
85                 }
86
87                 if ($content =~ /SYSCTL_DID1_PRTNO_$base.+0x(\S+)/) {
88                         $prtno = hex($1);
89                         $base = "LM3S" . $base;
90                 } else {
91                         # LM4F have a changed header
92                         if ($content =~ /SYSCTL_DID1_PRTNO_LM4F$base.+?0x(\S+)/s) {
93                                 $prtno = hex($1);
94                                 $base = "LM4F" . $base;
95                         } else {
96                                 print STDERR "$h_file is missing SYSCTL_DID1_PRTNO\n";
97                                 $prtno = 0;
98                                 $invalid = 1;
99                         }
100                 }
101                 $new_member = sprintf "{0x%02X, 0x%02X, \"%s\"},", $class, $prtno >> 16, $base;
102                 if ($invalid == 1) {
103                         #$new_struct .= "\t//$new_member\t// Invalid\n";
104                 } else {
105                         $new_struct .= "\t$new_member\n";
106                 }
107         }
108 }
109
110 sub getclass {
111         $class = $_[0];
112         if ($class =~ /Sandstorm/i) {
113                 return 0;
114         } elsif ($class =~ /Fury/i) {
115                 return 1;
116         } elsif ($class =~ /DustDevil/i) {
117                 return 3;
118         } elsif ($class =~ /Tempest/i) {
119                 return 4;
120         } elsif ($class =~ /Blizzard/i) {
121                 return 5;
122         } elsif ($class =~ /Firestorm/i) {
123                 return 6;
124         }
125         return 0xFF;
126 }