#! /usr/bin/perl
#
# Generate Header TCS

if( $#ARGV != 0 ) {
    printf("Too many or too few arguments. Read the code.\n");
    exit(2);
}

$POS = $ARGV[0];

if( $POS =~ /(\d)_(\d)_(\d)/ ) {
    $SM    = $1;
    $Layer = $3;
    $Stack = $2;
    $DCS_NAME = `./modules/getdcsbypos.pl $POS`; chop $DCS_NAME;
} else {
    printf("// Not OK\n");
}

if( $DCS_NAME =~ /dcs(\d\d\d\d)/ ) {
    $DCS = $1;
} elsif ( $DCS_NAME =~ /alitrddcb(\d\d\d\d)/ ) {
    $DCS = $1;
} elsif ( $DCS_NAME =~ /alidcsdcb(\d\d\d\d)/ ) {
    $DCS = $1;
} else {
    $DCS = 4097;
}

printf("//\n\n");
printf("// This is auto generated tcs file.\n");
printf("// Write special header for the following chamber.\n");
printf("//\n");
printf("//   DCS   $DCS\n",  $DCS);
printf("//   SM    $SM\n",   $SM);
printf("//   Layer $Layer\n", $Layer);
printf("//   Stack $Stack\n", $Stack);
printf("//\n");
printf("\n");
printf("const CPUGCONST=0x0C04; // all cpus\n");
printf("const C15CPUA=CPUGCONST+3; // c15\n");
printf("const ChamberID_A = (%d << 20) | ($SM << 15) | ($Layer << 12) | ($Stack << 9) | (0 << 8)\n", $DCS);;
printf("const ChamberID_B = (%d << 20) | ($SM << 15) | ($Layer << 12) | ($Stack << 9) | (1 << 8)\n", $DCS);;
printf("write chip_hm | chip_a_side, C15CPUA, 100b | ChamberID_A;\n");
printf("write chip_hm | chip_b_side, C15CPUA, 100b | ChamberID_B;\n");
#printf("read  chip_hm, C15CPUA, 0\n");
printf("\n");
printf("// EOF\n");

# EOF
