#!/usr/bin/perl -w
#############################################################################
# Generates the values for the bit exclude registers NPn and NED.
# Argument is the position of the spare bit and optional of the parity bit.
# Default output port configuration is: no optical link mode, no root of
# readout, ctrl delay 2.
#
# Example usage:  ./ni-excl.pl 4
# will produce the output:
# NPn,    0x00000424; // spare=bit4, parity=bit9
# NED,    0x00002110; // spare=bit4, parity=bit9
#
# 15.10.2004  Rolf Schneider
#############################################################################
use strict;

my $NPn = 0x0000044C;
my $NED = 0x00002250;

my $ppos = 8;
my $spos = 9;

# one or two arguments are allowed
if (!@ARGV || @ARGV>2) {
  print "usage: ni-excl spare-position [parity-position]\n";
  exit;
}

# set spare position
$NPn = $NPn & ~(15 << 3) | ($ARGV[0] << 3);
$NED = $NED & ~(15 << 6) | ($ARGV[0] << 6);
$spos = $ARGV[0];

# set parity position optionally
if (@ARGV==2) {
  $NPn = $NPn & ~(15 << 7) | ($ARGV[1] << 7);
  $NED = $NED & ~(15 << 10) | ($ARGV[1] << 10);
  $ppos = $ARGV[1];
}

# is the parity position shifted by the spare position?
if ($spos <= $ppos) {
  $ppos++;
}

printf "NPn,    0x%08x; // spare=bit%d, parity=bit%d\n", $NPn, $spos, $ppos;
printf "NED,    0x%08x; // spare=bit%d, parity=bit%d\n", $NED, $spos, $ppos;
