#!/usr/bin/perl
# A perl script to generate real and binary ramp data.
# Accurate to 31 bits.
# R. Winters - 2/2/2002

$bits=$ARGV[0];
$min=$ARGV[1];
$max=$ARGV[2];

if ($bits eq "" || $min eq "" || $max eq "") {
    print "Usage: sinegen.pl bits min max\n";
    exit -1;
}

$scale=(2**$bits)-1.0;             # real minus 1 bit
$samples=(2**$bits);
$index=0;
print "index\tsample\ttheta\tvolts\tval\tbinary\t2sComp\n";

for ($j=0;$j<$samples;$j++) # ramp up
{
    $val=$scale*($j/($samples-1));
    $volts=($max-$min)*($j/($samples-1))+$min;
    printf ("%.0f\t%.0f\t%f\t%.15f\t%f\t%s\t%s\n",$index,$j,$j/($samples-1),$volts,$val,&dec2bin ($val),&dec2bin_2sComp ($val));
    $index++;
}
for ($j=$samples-1;$j>=0;$j--) #ramp down
{
    $val=$scale*($j/($samples-1));
    $volts=($max-$min)*($j/($samples-1))+$min;
    printf ("%.0f\t%.0f\t%f\t%.15f\t%f\t%s\t%s\n",$index,$j,$j/($samples-1),$volts,$val,&dec2bin ($val),&dec2bin_2sComp ($val));
    $index++;
}

exit 0;


sub dec2bin 
{
    my $binary="";
    $decimal=sprintf("%.0f",@_[0]); #printf "decimal=$decimal\n";
    for ($i=$bits-1;$i>=0;$i--)
    {
	  $resolv=$decimal/(2**$i);
          if ($resolv>=1.0) {$binary = $binary."1";} else {$binary=$binary."0";}
          $decimal=$decimal%(2**$i);
#         print "i=$i, resolv=$resolv, decimal=$decimal, binary=$binary\n";
    }
    return $binary;
}

sub dec2bin_2sComp
{
    my $twosComp="";
    $decimal=sprintf("%.0f",@_[0]); #printf "decimal=$decimal\n";
    if($decimal >= 2**($bits-1)){$decimal=$decimal-2**($bits-1);}else{$decimal=$decimal+2**($bits-1);}
    for ($i=$bits-1;$i>=0;$i--)
    {
	  $resolv=$decimal/(2**$i);
          if ($resolv>=1.0) {$twosComp = $twosComp."1";} else {$twosComp=$twosComp."0";}
          $decimal=$decimal%(2**$i);
#         print "i=$i, resolv=$resolv, decimal=$decimal, twosComp=$twosComp\n";
    }
    return $twosComp;
}

# end program.
