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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 $structures = {};
  22. my $structure = "";
  23. while ( <> ) {
  24. chomp;
  25. if ( /^\#define (\S+)_OFFS (\S+)$/ ) {
  26. $structure = $1;
  27. $offsets->{$structure} = $2;
  28. } elsif ( /^\#define ${structure}_(\S+)_LSB (\S+)$/ ) {
  29. $structures->{$structure}->{$1}->{LSB} = $2;
  30. } elsif ( /^\#define ${structure}_(\S+)_RMASK (\S+)$/ ) {
  31. $structures->{$structure}->{$1}->{RMASK} = $2;
  32. } elsif ( /^\s*$/ ) {
  33. # Do nothing
  34. } else {
  35. print "$_\n";
  36. }
  37. }
  38. my $data = [ map { { name => $_, offset => $offsets->{$_} }; }
  39. sort { hex ( $offsets->{$a} ) <=> hex ( $offsets->{$b} ) }
  40. keys %$offsets ];
  41. foreach my $datum ( @$data ) {
  42. next unless exists $structures->{$datum->{name}};
  43. $structure = $structures->{$datum->{name}};
  44. my $fields = [ map { { name => $_, lsb => $structure->{$_}->{LSB},
  45. rmask => $structure->{$_}->{RMASK} }; }
  46. sort { hex ( $structure->{$a}->{LSB} ) <=>
  47. hex ( $structure->{$b}->{LSB} ) }
  48. keys %$structure ];
  49. $datum->{fields} = $fields;
  50. }
  51. print "\n/* This file has been further processed by $0 */\n\n"
  52. print "FILE_LICENCE ( GPL2_ONLY );\n\n";
  53. foreach my $datum ( @$data ) {
  54. printf "#define %s_offset 0x%08xUL\n",
  55. $datum->{name}, hex ( $datum->{offset} );
  56. if ( exists $datum->{fields} ) {
  57. my $lsb = 0;
  58. my $reserved_idx = 0;
  59. printf "struct %s_pb {\n", $datum->{name};
  60. foreach my $field ( @{$datum->{fields}} ) {
  61. my $pad_width = ( hex ( $field->{lsb} ) - $lsb );
  62. die "Inconsistent LSB/RMASK in $datum->{name}\n" if $pad_width < 0;
  63. printf "\tpseudo_bit_t _unused_%u[%u];\n", $reserved_idx++, $pad_width
  64. if $pad_width;
  65. # Damn Perl can't cope with 64-bit hex constants
  66. my $width = 0;
  67. my $rmask = $field->{rmask};
  68. while ( $rmask =~ /^(0x.+)f$/i ) {
  69. $width += 4;
  70. $rmask = $1;
  71. }
  72. $rmask = hex ( $rmask );
  73. while ( $rmask ) {
  74. $width++;
  75. $rmask >>= 1;
  76. }
  77. printf "\tpseudo_bit_t %s[%u];\n", $field->{name}, $width;
  78. $lsb += $width;
  79. }
  80. my $pad_width = ( 64 - $lsb );
  81. die "Inconsistent LSB/RMASK in $datum->{name}\n" if $pad_width < 0;
  82. printf "\tpseudo_bit_t _unused_%u[%u];\n", $reserved_idx++, $pad_width
  83. if $pad_width;
  84. printf "};\n";
  85. printf "struct %s {\n\tPSEUDO_BIT_STRUCT ( struct %s_pb );\n};\n",
  86. $datum->{name}, $datum->{name};
  87. }
  88. print "\n";
  89. }