conncmp.pl 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/perl
  2. #
  3. # conncmp.pl - Compare signal names on connectors
  4. #
  5. sub usage
  6. {
  7. print STDERR <<"EOF";
  8. usage: $0 list-a.net comp-a list-b.net comp-b pat-a=pat-b ...
  9. patterns can be constant, e.g., GND=GND3
  10. or variable, e.g., %=%_U
  11. Nets connecting to no other component are renamed to NC.
  12. EOF
  13. exit(1);
  14. }
  15. sub get_pins
  16. {
  17. local ($file, $comp) = @_;
  18. my %pins = ();
  19. my $net = undef;
  20. my $single = undef;
  21. my $conns = 0;
  22. open(FILE, $file) || die "$file: $!";
  23. while (<FILE>) {
  24. if (/\(net\s+.*\(name\s+"([^"]+)"\)/ ||
  25. /\(net\s+.*\(name\s+(\S+?)\)/) {
  26. $pins{$single} = "NC" if $conns == 1;
  27. $net = $1;
  28. $net =~ s#^/.*/##;
  29. $single = undef;
  30. $conns = 0;
  31. next;
  32. }
  33. next unless /\(node\s+\(ref\s+(\S+?)\)\s+\(pin\s+(\S+?)\)/;
  34. $conns++;
  35. next unless $1 eq $comp;
  36. die "duplicate pin $1.$2" if defined $pins{$2};
  37. die "undefined net" unless defined $net;
  38. $pins{$2} = $net;
  39. $single = $2;
  40. }
  41. close(FILE);
  42. return \%pins;
  43. }
  44. &usage unless $#ARGV >= 3;
  45. %a = %{ &get_pins($ARGV[0], $ARGV[1]) };
  46. %b = %{ &get_pins($ARGV[2], $ARGV[3]) };
  47. @eq = @ARGV[4 .. $#ARGV];
  48. for (@eq) {
  49. &usage unless $_ =~ /=/;
  50. }
  51. PIN: for $pin (keys %a) {
  52. if (!defined $b{$pin}) {
  53. warn "A.$pin has no matching B.$pin\n";
  54. next;
  55. }
  56. my $a = $a{$pin};
  57. my $b = $b{$pin};
  58. delete $b{$pin};
  59. next if $a eq $b;
  60. for (@eq) {
  61. die unless /=/;
  62. my ($pa, $pb) = ($`, $');
  63. if ($pa =~ /%/) {
  64. my $pat = "^$`(.*)$'\$";
  65. next unless $a =~ $pat;
  66. my $var = $1;
  67. $pb =~ s/%/$var/g;
  68. next PIN if $b eq $pb;
  69. } else {
  70. next PIN if $a eq $pa && $b eq $pb;
  71. }
  72. }
  73. die "A.$pin($a) does not seem to match B.$pin($b)\n";
  74. }
  75. for $pin (keys %b) {
  76. warn "B.$pin has no matching A.$pin\n";
  77. }