#! /usr/bin/perl
#
# Usage:
#   expand_scsnid.pl robtype_A robtype_B filename
#         
# Arguments:
#   robtype_A  : Type of ROB sitting in A side (ex. T1A)
#   robtype_B  : Type of ROB sitting in A side (ex. T1B)
#   filename   : input filename
#
# Example:
# Notice:
#
# By Ken Oyama 2006/02/02

# Get arguments
#
$robtype_a  = $ARGV[0];
$robtype_b  = $ARGV[1];
$file       = $ARGV[2];

# Table for each ROB's used in the class.
#                    0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 BM HM
$SCSN_TBL{'T1A'} = "10 11 12 13  9 16 15 14  8  6  7  1  5  4  3  2 17";
$SCSN_TBL{'T1B'} = "11 12 13 14 10 17 16 15  9  7  8  2  6  5  4  3  1";
$SCSN_TBL{'T2B'} = "11 12 13 14 10 17 16 15  9  7  8  2  6  5  4  3  1";
$SCSN_TBL{'T3A'} = "10 11 12 13  9 16 15 14  8  6  7  1  5  4  3  2 18 17";
$SCSN_TBL{'T3B'} = "12 13 14 15 11 18 17 16 10  8  9  3  7  6  5  4  2  1";
$SCSN_TBL{'T4A'} = "10 11 12 13  9 16 15 14  8  6  7  1  5  4  3  2 17";
$SCSN_TBL{'T4B'} = "11 12 13 14 10 17 16 15  9  7  8  2  6  5  4  3  1";

# Chip group number definition (copy from scsn_ids_alice.tcs)

$CGID{'hcm3a'}  = 0100000100000000b; # Half chamber merger in A side on C0 chamber
$CGID{'hcm3b'}  = 0100000010000000b; # Half chamber merger in B side on C0 chamber
$CGID{'hcm4a'}  = 0100000001000000b; # Half chamber merger in A side on C1 chamber
$CGID{'hcm4b'}  = 0100000000100000b; # Half chamber merger in B side on C1 chamber
$CGID{'bmrg'}   = 0100000000010000b; # All board merger chips (ALICE#16)
$CGID{'am01'}   = 0100000000001000b; # All ADC MCM's ADC ch. 0 & 1 must be masked
$CGID{'am20'}   = 0100000000000100b; # All ADC MCM's ADC ch. 20    must be masked
$CGID{'colmrg'} = 0100000000000010b; # All Column Merger chips (ALICE#2,6,10,14)
$CGID{'normal'} = 0100000000000001b; # All ADC chips except for am01,am20,colmrg

$CGID{'chip_pos0'} = 0110000000000000b; # All chips in position 0
$CGID{'chip_pos1'} = 0110010000000000b; # All chips in position 1
$CGID{'chip_pos2'} = 0110100000000000b; # All chips in position 2
$CGID{'chip_pos3'} = 0110110000000000b; # All chips in position 3
$CGID{'chip_pos4'} = 0111000000000000b; # All chips in position 4
$CGID{'chip_pos5'} = 0111010000000000b; # All chips in position 5
$CGID{'chip_pos6'} = 0111100000000000b; # All chips in position 6
$CGID{'chip_pos7'} = 0111110000000000b; # All chips in position 7

$CGID{'hcm3a'}  = " -1";
$CGID{'hcm3b'}  = " -1";
$CGID{'hcm4a'}  = " -1";
$CGID{'hcm4b'}  = " -1";
$CGID{'bmrg'}   = " -1";
$CGID{'am01'}   = " -1";
$CGID{'am20'}   = " -1";
$CGID{'colmrg'} = " -1";
$CGID{'normal'} = " -1";
$CGID{'chip_pos0'} =  " -1";
$CGID{'chip_pos1'} =  " -1";
$CGID{'chip_pos2'} =  " -1";
$CGID{'chip_pos3'} =  " -1";
$CGID{'chip_pos4'} =  " -1";
$CGID{'chip_pos5'} =  " -1";
$CGID{'chip_pos6'} =  " -1";
$CGID{'chip_pos7'} =  " -1";

# Main from here
#

# Diagnostics printout
#
printf( stderr "I expect input dat is now in Alice numbering and convert to SCSN ID.\n");
printf( stderr "ROB Type in A side : %s  -> Table : %s\n", $robtype_a, $SCSN_TBL{$robtype_a});
printf( stderr "ROB Type in B side : %s  -> Table : %s\n", $robtype_b, $SCSN_TBL{$robtype_b});

# Make an array from selected table
@TBL_A = split( /\s+/, $SCSN_TBL{$robtype_a} );
@TBL_B = split( /\s+/, $SCSN_TBL{$robtype_b} );




$n_slaves = $#TBL + 1;
printf( stderr "n_slaves = %d\n", $n_slaves );
printf( stderr "Shift $shift_id, and expand 127 to $n_slaves slaves.\n");

open( infile, $file ) || die;

while( <infile> ) {
    if( /^(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/ ) {
	$command = $1;
	$address = $2;
	$data    = $3;
	$slave   = $4;
	if( $slave == 127 ) {
	    for( $i = 1 ; $i <= $n_slaves ; $i++ ) {
		$slave = $i + $shift_id;
		printf( "$command $address $data $slave\n");
	    }
	} else {
	    # $slave = $TBL[$slave] + $shift_id;
	    if( $slave < $n_slaves ) {
		$slave = $TBL[$slave] + $shift_id;
		printf( "$command $address $data $slave\n");
	    }
	}
    }
}

close( infile );

# EOF
