#!/usr/bin/perl

open (INPUT,$ARGV[0]);

$out_mask = "0056FFFF";

while ($line = <INPUT>) {
  if (substr($line,0,5) eq "Break") {
    ($d,$d,$d,$d,$address,$d) = split(" ",$line);
    $subaddr = substr($address,-3,4);
    $line = <INPUT>;
    $line = <INPUT>;
    $line = <INPUT>;
    ($value,$d,$d) = split(" ",$line);
    if ($value eq "MSR") {
      $line = <INPUT>;
      ($value,$d) = split(" ",$line);
    }
    if ($subaddr eq "118") {
      $out_mask = $value;
      printf("118: $out_mask\n");
      next;
    }
    if ($subaddr eq "200") {
      $line = <INPUT>;
      ($esi,$d) = split(" ",$line);
      $line = <INPUT>;
      ($eip,$d) = split(" ",$line);
    }  
    $offset = hex($eip) - hex($aud_address);
    $offstr = sprintf("%04X",$offset); 
    if ($offstr eq "2AAD" ||
        $offstr eq "2AEC" ||
        $offstr eq "2C14") {
      $masked_write = hex($value) & hex($out_mask);
      printf("200: $value $esi WRITE   %06X\n",$masked_write);
    } elsif ($offstr eq "2A1C" ||
             $offstr eq "2C7C" ||
             $offstr eq "2B8C" ||
             $offstr eq "2B19") {
      $masked_read = hex($value) & ~hex($out_mask);
      printf("200: $value $esi READ    %06X\n",$masked_read);
    } else {
      printf("unknown offset: $offstr\n");
    }
  }
  if (substr($line,0,8) eq "BTPCIVID") {
    ($d,$vid_address,$d) = split(" ",$line);
    printf("Found BTPCIVID address: %s\n",$vid_address);
    $line = <INPUT>;
    $line = <INPUT>;
  }
  if (substr($line,0,8) eq "BTPCIAUD") {
    ($d,$aud_address,$d) = split(" ",$line);
    printf("Found BTPCIAUD address: %s\n",$aud_address);
    $line = <INPUT>;
    $line = <INPUT>;
  }
};
