Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

qib_genbits.pl 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License as
  7. # published by the Free Software Foundation; either version 2 of the
  8. # License, or any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. use strict;
  19. use warnings;
  20. my $offsets = {};
  21. my $defaults = {};
  22. my $structures = {};
  23. my $structure = "";
  24. while ( <> ) {
  25. chomp;
  26. if ( /^\#define (\S+)_OFFS (\S+)$/ ) {
  27. $structure = $1;
  28. $offsets->{$structure} = $2;
  29. } elsif ( /^\#define ${structure}_DEF (\S+)$/ ) {
  30. $defaults->{$structure} = $1;
  31. } elsif ( /^\#define ${structure}_(\S+)_LSB (\S+)$/ ) {
  32. $structures->{$structure}->{$1}->{LSB} = $2;
  33. } elsif ( /^\#define ${structure}_(\S+)_MSB (\S+)$/ ) {
  34. $structures->{$structure}->{$1}->{MSB} = $2;
  35. } elsif ( /^\#define ${structure}_(\S+)_RMASK (\S+)$/ ) {
  36. $structures->{$structure}->{$1}->{RMASK} = $2;
  37. } elsif ( /^\s*$/ ) {
  38. # Do nothing
  39. } else {
  40. print "$_\n";
  41. }
  42. }
  43. my $data = [ map { { name => $_, offset => $offsets->{$_},
  44. default => $defaults->{$_} }; }
  45. sort { hex ( $offsets->{$a} ) <=> hex ( $offsets->{$b} ) }
  46. keys %$offsets ];
  47. foreach my $datum ( @$data ) {
  48. next unless exists $structures->{$datum->{name}};
  49. $structure = $structures->{$datum->{name}};
  50. my $fields = [ map { { name => $_, lsb => $structure->{$_}->{LSB},
  51. msb => $structure->{$_}->{MSB},
  52. rmask => $structure->{$_}->{RMASK} }; }
  53. sort { hex ( $structure->{$a}->{LSB} ) <=>
  54. hex ( $structure->{$b}->{LSB} ) }
  55. keys %$structure ];
  56. $datum->{fields} = $fields;
  57. }
  58. print "\n/* This file has been further processed by $0 */\n\n";
  59. print "FILE_LICENCE ( GPL2_ONLY );\n\n";
  60. foreach my $datum ( @$data ) {
  61. printf "#define %s_offset 0x%08xUL\n",
  62. $datum->{name}, hex ( $datum->{offset} );
  63. if ( exists $datum->{fields} ) {
  64. my $lsb = 0;
  65. my $reserved_idx = 0;
  66. printf "struct %s_pb {\n", $datum->{name};
  67. foreach my $field ( @{$datum->{fields}} ) {
  68. my $pad_width = ( hex ( $field->{lsb} ) - $lsb );
  69. die "Inconsistent LSB/RMASK in $datum->{name} before $field->{name}\n"
  70. if $pad_width < 0;
  71. printf "\tpseudo_bit_t _unused_%u[%u];\n", $reserved_idx++, $pad_width
  72. if $pad_width;
  73. $lsb += $pad_width;
  74. # Damn Perl can't cope with 64-bit hex constants
  75. my $width = 0;
  76. die "Missing RMASK in $datum->{name}.$field->{name}\n"
  77. unless defined $field->{rmask};
  78. my $rmask = $field->{rmask};
  79. while ( $rmask =~ /^(0x.+)f$/i ) {
  80. $width += 4;
  81. $rmask = $1;
  82. }
  83. $rmask = hex ( $rmask );
  84. while ( $rmask ) {
  85. $width++;
  86. $rmask >>= 1;
  87. }
  88. if ( defined $field->{msb} ) {
  89. my $msb_width = ( hex ( $field->{msb} ) - $lsb + 1 );
  90. $width ||= $msb_width;
  91. die "Inconsistent LSB/MSB/RMASK in $datum->{name}.$field->{name}\n"
  92. unless $width == $msb_width;
  93. }
  94. printf "\tpseudo_bit_t %s[%u];\n", $field->{name}, $width;
  95. $lsb += $width;
  96. }
  97. my $pad_width = ( 64 - $lsb );
  98. die "Inconsistent LSB/RMASK in $datum->{name} final field\n"
  99. if $pad_width < 0;
  100. printf "\tpseudo_bit_t _unused_%u[%u];\n", $reserved_idx++, $pad_width
  101. if $pad_width;
  102. printf "};\n";
  103. printf "struct %s {\n\tPSEUDO_BIT_STRUCT ( struct %s_pb );\n};\n",
  104. $datum->{name}, $datum->{name};
  105. }
  106. printf "/* Default value: %s */\n", $datum->{default}
  107. if defined $datum->{default};
  108. print "\n";
  109. }