use strict;
my $deltatime;
my $deltatimeunits;
my $unit;
my $operation="unknown";
my $addresshigh;
my $addresslow;
my $address;
my $data;
my $handled=0;
my $remaining;
my @remaining;
my %cx25840registers;
while (<>) {
   
   $handled=0;
   if ($operation eq "Read") {
      if (/^\W*([\d.]*)([um]S): ([\dABCDEF]*) r (.*)/) {
         ($remaining,undef)=split("  ",$4);
         printf "Read unit 0x%x address 0x%x - Result $remaining\n",$unit,$address;
         $handled=1;
      }
      $operation="unknown";
   }
   if (/^\W*([\d.]*)([um]S): ([\dABCDEF]*) ([Wr]) (.*)/ and $handled==0) {
      $deltatime=$1;
      $deltatimeunits=$2;
      $unit=hex($3);
      $operation=$4;
      if (defined $5) {
         ($remaining,undef)=split("  ",$5);
         @remaining=split(" ",$remaining);
         if ($operation eq "W" and @remaining==2) {
            ($addresshigh,$addresslow)=@remaining;
            $address=(hex($addresshigh)<<8)+hex($addresslow);
            $operation="Read";
            $handled=1;
         } elsif ($operation eq "W" and @remaining==3) {
            ($addresshigh,$addresslow,$data)=@remaining;
            $address=(hex($addresshigh)<<8)+hex($addresslow);
            $data=hex($data);
            printf "Write unit 0x%x address 0x%x - Value 0x%x\n",$unit,$address,$data;
            if ($unit==hex("0x44")) {
               $cx25840registers{$address}=$data;
            }
            $handled=1;
         } elsif ($operation eq "W" and @remaining >3) {
            ($addresshigh,$addresslow,@remaining)=@remaining;
            $address=(hex($addresshigh)<<8)+hex($addresslow);
            if ($address!=hex("0x802")) {
               foreach $data (@remaining) {
                  $data=hex($data);
                  printf "Write unit (combined) 0x%x address 0x%x - Value 0x%x\n",$unit,$address,$data;
                  $cx25840registers{$address}=$data;
                  $address++;
               }
            } else {
               print "<audio firmware upload>\n"
            }
            $handled=1;
         }
      }
   }
   if ($handled==0) {
      print $_;
   } else {
     # print "HANDLED: $_\n";
   }
}; 
foreach $address (sort {$a <=> $b} (keys (%cx25840registers))) {
   printf "Address: 0x%x Value: 0x%02x %08b\n",$address,$cx25840registers{$address},$cx25840registers{$address};
}