     1	#!/usr/local/bin/perl5 -w
     2	
     3	$input = 'chaintest.scan';
     4	$output= "OUT.out";
     5	
     6	open (INFILE,"$input")||die "cannot open $input";
     7	open(OUTFILE,">$output")||die "canoot\n";
     8	
     9	#######################################################
    10	$/=";";
    11	$ct_scanout = 0;
    12	
    13	while (<INFILE>) {
    14	  chomp;
    15	  last if /^SCAN_CELLS/;
    16	  $seen_CHAIN_TEST=1  if /CHAIN_TEST/;
    17	  next unless /apply/ && $seen_CHAIN_TEST;
    18	  die "Missing ';' in 'apply' line" if /apply[^;]*\n/; 
    19	
    20	  $ct_scanout = 1 if /apply\s*"grp[0-9]_unload"/;
    21	  next unless /\t*chain\s+"chain([0-9])"/;
    22	  $chain[$1] = cleanup($_);
    23	  &printout if $1 == 7;
    24	}
    25	
    26	sub printout {
    27	  if ($ct_scanout ) {
    28	    for ($i=0;$i<@{$chain[3]};$i++){
    29	      for (@chain) { 
    30	        next unless defined;
    31	        $_->[$i] =~ s/0/L/g; 
    32	        $_->[$i] =~ s/1/H/g;
    33	      }
    34	      my @chars = map $_->[$i], @chain;
    35	      print OUTFILE "\n(ct_so\n", join("", @chars), "\n )";
    36	    }
    37	    $ct_scanout=0;
    38	  } elsif ($ct_scanout==0) {
    39	    for ($i=0;$i<@{$chain[3]};$i++) {
    40	      for (@chain) {
    41	        next unless defined;
    42	        $_->[$i] =~ s/X/0/g;
    43	      }
    44	      my @chars = map $_->[$i], @chain;
    45	      print OUTFILE "\n(ct_si  $ct_si{tdi}\n", join("", @chars), "\n )";
    46	    }
    47	  }
    48	}
    49	
    50	sub cleanup {
    51	  local $_ = shift;
    52	  s/.*=//s;
    53	  tr/01X//cd;
    54	  return [ split // ]
    55	}
    56	
