You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qib_genbits.pl 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  18. # 02110-1301, USA.
  19. use strict;
  20. use warnings;
  21. my $offsets = {};
  22. my $defaults = {};
  23. my $structures = {};
  24. my $structure = "";
  25. while ( <> ) {
  26. chomp;
  27. if ( /^\#define (\S+)_OFFS (\S+)$/ ) {
  28. $structure = $1;
  29. $offsets->{$structure} = $2;
  30. } elsif ( /^\#define ${structure}_DEF (\S+)$/ ) {
  31. $defaults->{$structure} = $1;
  32. } elsif ( /^\#define ${structure}_(\S+)_LSB (\S+)$/ ) {
  33. $structures->{$structure}->{$1}->{LSB} = $2;
  34. } elsif ( /^\#define ${structure}_(\S+)_MSB (\S+)$/ ) {
  35. $structures->{$structure}->{$1}->{MSB} = $2;
  36. } elsif ( /^\#define ${structure}_(\S+)_RMASK (\S+)$/ ) {
  37. $structures->{$structure}->{$1}->{RMASK} = $2;
  38. } elsif ( /^\s*$/ ) {
  39. # Do nothing
  40. } else {
  41. print "$_\n";
  42. }
  43. }
  44. my $data = [ map { { name => $_, offset => $offsets->{$_},
  45. default => $defaults->{$_} }; }
  46. sort { hex ( $offsets->{$a} ) <=> hex ( $offsets->{$b} ) }
  47. keys %$offsets ];
  48. foreach my $datum ( @$data ) {
  49. next unless exists $structures->{$datum->{name}};
  50. $structure = $structures->{$datum->{name}};
  51. my $fields = [ map { { name => $_, lsb => $structure->{$_}->{LSB},
  52. msb => $structure->{$_}->{MSB},
  53. rmask => $structure->{$_}->{RMASK} }; }
  54. sort { hex ( $structure->{$a}->{LSB} ) <=>
  55. hex ( $structure->{$b}->{LSB} ) }
  56. keys %$structure ];
  57. $datum->{fields} = $fields;
  58. }
  59. print "\n/* This file has been further processed by $0 */\n\n";
  60. print "FILE_LICENCE ( GPL2_ONLY );\n\n";
  61. foreach my $datum ( @$data ) {
  62. printf "#define %s_offset 0x%08xUL\n",
  63. $datum->{name}, hex ( $datum->{offset} );
  64. if ( exists $datum->{fields} ) {
  65. my $lsb = 0;
  66. my $reserved_idx = 0;
  67. printf "struct %s_pb {\n", $datum->{name};
  68. foreach my $field ( @{$datum->{fields}} ) {
  69. my $pad_width = ( hex ( $field->{lsb} ) - $lsb );
  70. die "Inconsistent LSB/RMASK in $datum->{name} before $field->{name}\n"
  71. if $pad_width < 0;
  72. printf "\tpseudo_bit_t _unused_%u[%u];\n", $reserved_idx++, $pad_width
  73. if $pad_width;
  74. $lsb += $pad_width;
  75. # Damn Perl can't cope with 64-bit hex constants
  76. my $width = 0;
  77. die "Missing RMASK in $datum->{name}.$field->{name}\n"
  78. unless defined $field->{rmask};
  79. my $rmask = $field->{rmask};
  80. while ( $rmask =~ /^(0x.+)f$/i ) {
  81. $width += 4;
  82. $rmask = $1;
  83. }
  84. $rmask = hex ( $rmask );
  85. while ( $rmask ) {
  86. $width++;
  87. $rmask >>= 1;
  88. }
  89. if ( defined $field->{msb} ) {
  90. my $msb_width = ( hex ( $field->{msb} ) - $lsb + 1 );
  91. $width ||= $msb_width;
  92. die "Inconsistent LSB/MSB/RMASK in $datum->{name}.$field->{name}\n"
  93. unless $width == $msb_width;
  94. }
  95. printf "\tpseudo_bit_t %s[%u];\n", $field->{name}, $width;
  96. $lsb += $width;
  97. }
  98. my $pad_width = ( 64 - $lsb );
  99. die "Inconsistent LSB/RMASK in $datum->{name} final field\n"
  100. if $pad_width < 0;
  101. printf "\tpseudo_bit_t _unused_%u[%u];\n", $reserved_idx++, $pad_width
  102. if $pad_width;
  103. printf "};\n";
  104. printf "struct %s {\n\tPSEUDO_BIT_STRUCT ( struct %s_pb );\n};\n",
  105. $datum->{name}, $datum->{name};
  106. }
  107. printf "/* Default value: %s */\n", $datum->{default}
  108. if defined $datum->{default};
  109. print "\n";
  110. }