#!/usr/bin/perl

$fltname = shift;

$fltname =~ /^([a-z]*)(.*)$/;

$baseflt = $1;
$fltopt = $2;

# set default parameters
$pedestal = 10.0;
$fpnp_value = -1;
$eff_ped = 4.0 * $pedestal;

$tail_weight = 270;
$tail_short  = 449;
$tail_long   = 348;

if      ($baseflt eq 'nf') {
    $enable_ped   = 0;
    $enable_gain  = 0;
    $enable_tail  = 0;
} elsif ($baseflt eq 'p') {
    $enable_ped   = 1;
    $enable_gain  = 0;
    $enable_tail  = 0;
} elsif ($baseflt eq 'pt') {
    $enable_ped   = 1;
    $enable_gain  = 0;
    $enable_tail  = 1;
} elsif ($baseflt eq 'pg') {
    $enable_ped   = 1;
    $enable_gain  = 1;
    $enable_tail  = 0;
} elsif ($baseflt eq 'pgt') {
    $enable_ped   = 1;
    $enable_gain  = 1;
    $enable_tail  = 1;
} else {
    die "ERROR: Unknown filter settings: $baseflt"
}

if ( $fltopt =~ /w(\d*)s(\d*)l(\d*)/) {
    $tail_weight = $1;
    $tail_short  = $2;
    $tail_long   = $3;
}


# This section sets the value for the pedestal filter.


if ( $fltopt =~ /b(\d*)/) {
    
    $pedestal = $1;
	
} 

if ( $fltopt =~ /fpnp([0-9\.]*)/ ) {

    $fpnp_value = $1;
}



if ($fpnp_value < 0) {
    $fpnp_value = 4 * ($pedestal);
}

if ( $enable_gain or $enable_tail) {
# this must take into account the DC-behaviour of g/t-filters, t.b.d.!!!
	$eff_ped = 4.0 * $pedestal;
}



print "p: $enable_ped FPNP=$fpnp_value effective pedestal=$pedestal\n";
print "g: $enable_gain\n";
print "t: $enable_tail w=$tail_weight s=$tail_short l=$tail_long\n";

print "src/filter/".$fltname.".auto.tcs\n";

open outfile, ">src/filter/$fltname.auto.tcs";


print outfile <<EOF;

// ***************************************************************************
// ***************************************************************************
// 
//                  THIS FILE HAS BEEN CREATED AUTOMATICALLY. 
//                  UPON RECREATION ALL CHANGES WILL BE LOST.
//
//                          DO NOT EDIT THIS FILE !!!!!
//
//
// ***************************************************************************
// ***************************************************************************
//
// Filter parameters
//
// ***************************************************************************

include src/common/param_includes.tcs

// Enable/Disable the filter stages
// --------------------------------

// 1/0 enable/disable the corresponding filter or correction
const EnablePedestal         = $enable_ped;
const EnableGainCorrection   = $enable_gain;
const EnableTailCancellation = $enable_tail;

// Preprocessor and filter parameters

const ADCdatapipe = 5;                  // 0 to 5, used in EBD register, 2 are already in the Crosstalk filter
const ADCraw      = 0;                  // 0 or 1, store the filtered(0) or unfiltered(1) ADC data, used in EBSF


// Pedestal filter parameters
// --------------------------

const PedestalTimeConstant = 0; // 0 for fastest, 3 for slowest
const EffectPedestal       = $eff_ped;
const FPNPvalue            = $fpnp_value;

// Tail cancellation filter parameters
// -----------------------------------

const LongDecayWeight     = $tail_weight;
const LongDecayParameter  = $tail_long;
const ShortDecayParameter = $tail_short;


// now come the actual write commands

// Common Timer Delay -- Do not change due to NI LVDS timing
write TPPT0, 0x01;

// Event Buffer Input Delay
write EBD, ADCdatapipe; // 0 to 5

// Event Buffer Storage Mode
write EBSF, ADCraw;

// Event Buffer Acquisition Offset Address
write EBAQA, 0;

// Event Buffer Simulation Mode
write EBSIM, 1; // active low

// Event Buffer Simulation Offset Address
write EBSIA, 0x20;              // start address for ADC read data in simulation mode

// Event Buffer Parity Bit Mask
write EBPP, 1; // 0 for parity bit enable, 1 for mask


// ----- non-linearity filter -----
// Bypass
write FLBY, 0;

// ----- pedestal filter -----
restrict 1;

// Bypass
write FPBY, EnablePedestal;

restrict EnablePedestal;
// Time Constant
write FPTC, PedestalTimeConstant;

// Additive
write FPNP, FPNPvalue;

// Clear
write FPCL, 0;
write FPCL, 1;


// ----- gain filter -----
// actual gain tables written elsewhere
restrict 1

// Bypass
write FGBY, EnableGainCorrection;

restrict EnableGainCorrection;
// Counter Threshold A
write FGTA, 0x014;

// Counter Threshold B
write FGTB, 0x80C;


// ----- tail cancellation -----
restrict 1

// Bypass
write FTBY, EnableTailCancellation;

restrict EnableTailCancellation;
// Long Decay Weight
write FTAL, LongDecayWeight;

// Long Decay Parameter
write FTLL, LongDecayParameter;

// Short Decay Parameter
write FTLS, ShortDecayParameter;


// ----- cross talk filter -----
restrict 1;

const EnableXtalkSuppression = 0;

const XtalkM0=0x1E * EnableXtalkSuppression;    // not expected to be used actually
const XtalkM1=0xD4 * EnableXtalkSuppression;    // not expected to be used actually
const XtalkM2=0xE6 * EnableXtalkSuppression;    // not expected to be used actually
const XtalkM3=0x4A * EnableXtalkSuppression;    // not expected to be used actually
const XtalkM4=0xEF * EnableXtalkSuppression;    // not expected to be used actually
const XtalkF0=1;   // This is correct only when Xtalk disabled!
                   // Otherwise, the DC responce of the filter must be calculated

// Bypass
write FCBY, 1; // Do not panic, it is enabled always to have two more data latency.
               // Instead, XtalkMx is set to zero to deactivate crosstalk suppression.

// Weight 0
write FCWn+0, XtalkM0;
// Weight 1
write FCWn+1, XtalkM1;
// Weight 2
write FCWn+2, XtalkM2;
// Weight 3
write FCWn+3, XtalkM3;
// Weight 4
write FCWn+4, XtalkM4;

EOF

    close outfile;
