use XML::Simple;

$parameters = XMLin("parameters.xml");

sub getAttributeVal {
    my $paramName = shift;
    my $attrName = shift;
    foreach my $Parameter (@{$parameters->{Parameter}})
    {
        if ($paramName eq $Parameter->{Name})
        {
            foreach my $Attr (@{$Parameter->{Attributes}->{Attribute}})
            {
                if ($attrName eq $Attr->{Name} )
                {
                    return $Attr->{Value};
                }
            }
        }
    }
}

# Added by Armand C.
print "// index for 10 ms in dynCoefTab[]\n";
print "#define tenMsIndex      67\n";
print "// index for 20 ms in dynCoefTab[]\n";
print "#define twentyMsIndex   74\n";

$functions = XMLin("functions.xml", forcearray => ['Parameter']);

foreach my $Function (@{$functions->{Function}})
{
    print "\n//---------- ", $Function->{Name} , " ----------\n";
    my $wordnum = 0;
    my $offset = 0;
    foreach my $Par (@{$Function->{Parameters}->{Parameter}})
    {
        my $res = getAttributeVal($Par->{Type} , "Resolution");
        if ( ($offset + $res) > 32 )
        {
            print "// NEXT WORD, offset + res = ", $offset+$res, "\n";
            $offset = 0;
            $wordnum++;
        }
        my $uniqueParName = "$Function->{Name}_$Par->{Name}";
        $uniqueParName =~ tr/ \//_/;     #change blank and '/' to '_'
        my $mask = 0x00000001;
        for ($count=1; $count<$res; $count++){
            $mask = ($mask << 1) + 1;
        }
        printf ("#define %s_MASK 0x%08lX\n",$uniqueParName,$mask);
        print "#define ", $uniqueParName,"_OFFSET $offset\n";
        print "#define ", $uniqueParName,"_WORD $wordnum\n";
        my $max = getAttributeVal($Par->{Type} , "Maximum");
        print "#define ", $uniqueParName,"_MAX $max\n";
        print "\n";
        $offset += $res;
    }
}
