#!/usr/local/bin/perl -w

@selected = (0, 0, 0, 0);
$selnone = 0;

if($#ARGV == 0)
{
    if($ARGV[0] == 4)
    {
	$selnone = 1;
    }
    for($i=0; $i<$ARGV[0]; $i++)
    {
	$selected[$i] = 1;
    }
}
elsif($#ARGV == 3)
{
    @selected = @ARGV[0..3];
}
else
{
    print STDERR "usage: jtagnet #proc|sel0 sel1 sel2 sel3\n";
    exit(1);
}

open(JTL, "|/home/pram/bin/jtl");
print JTL
    "connect real /dev/pram0/control\n"
    . "include /home/pram/jtag/path/root.jtl\n"
    . "startup root\n";

#@chips = ("net1:UA11 net1:UA12",
#	   "net1:UA21 net1:UA22",
#	   "net2:UB11 net2:UB12",
#	   "net2:UB21 net2:UB22");
if(1)
{
    # untere Netzwerkhlfte
    @chips = ("cnet:U11", "cnet:U12",
	      "cnet:U21", "cnet:U22",
	      "dnet:U11", "dnet:U12",
	      "dnet:U21", "dnet:U22");
}
else
{
    # obere Netzwerkhlfte
    @chips = ("anet:U11", "anet:U12",
	      "anet:U21", "anet:U22",
	      "bnet:U11", "bnet:U12",
	      "bnet:U21", "bnet:U22");
}

$netprefix = "root:root_net:";
$cpuscprefix = "root:bp_cpu0:sc_local:";
$cpusc = $cpuscprefix."tom ".$cpuscprefix."jerry";

if(!$selnone)
{
    print "select";
    for($i=0; $i<4; $i++)
    {
	if($selected[$i]) {next;};
	for($j=0; $j<2; $j++)
	{
	    print JTL " ".$netprefix.$chips[2*$i+$j];
	}
    }

    print JTL " ".$cpusc."\n"
	. "instr";

    for($i=0; $i<4; $i++)
    {
	if($selected[$i]) {next;};
	print JTL " SAMPLE SAMPLE";
    }

    print JTL " bypass bypass\n"
	. "shift ";

    for($i=0; $i<4; $i++)
    {
	if($selected[$i]) {next;};
	print JTL "10000010000000000000000000001111111111111111111100000000000000000000000000000000000000001000001000000000000000000000000110101111111111110000000000000000000000000000000000000000";
    }

    print JTL "00\n"
	. "instr";

    for($i=0; $i<4; $i++)
    {
	if($selected[$i]) {next;};
	print JTL " CLAMP CLAMP";
    }
    print JTL "\n";
}

print JTL "quit\n";

close JTL;
