!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!  Production Test macro for ICE-PIC-2 cards
!
!  Switches:	/NOTUN - no tuner test
!		/NOINT - no internal transfer tests
!		/NOEXT - no external transfer tests
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
startmacro u:device1[pictpic] u:device2[pictpic2] n:npass[20] 

local answer pstat tuners tgold
local chan size dmax dmin imin imax pass rate

res rate 1e6
res pstat 0

say "Creating test files ..."
pic create testin si 16k rate ramp
pic create testout si 1m rate 

say "Testing 1->2 external transfer ..."
picd reset ^device1 icepic2oo
picd reset ^device2 icepic2ii
callp testxfer ^device1 ^device2 module1 module1 testin testout testin npass
if pstat lt 0 goto problem_xferx
callp testxfer ^device1 ^device2 module2 module2 testin testout testin npass
if pstat lt 0 goto problem_xferx

say "Creating complex test files ..."
pic create testin si 32k rate ramp
header testin f=ci
pic create testout si 1m rate 

say "Testing 1->2 complex output transfer ..."
demux testin testtmpr testtmpi
callp testxfer ^device1 ^device2 module3 module1 testin testout testtmpr npass
if pstat lt 0 goto problem_xferxr
callp testxfer ^device1 ^device2 module3 module2 testin testout testtmpi npass
if pstat lt 0 goto problem_xferxi

say "Testing complex word 1->2 external transfer ..."
header testout f=ci
picd reset ^device2 icepic2iix
callp testxfer ^device1 ^device2 module3 module3 testin testout testin npass
if pstat lt 0 goto problem_xferxc

say "Testing complex byte 1->2 external transfer ..."
header testin f=cb
header testout f=cb
callp testxfer ^device1 ^device2 module3 module3 testin testout testin npass
if pstat lt 0 goto problem_xferxc

say "********************************"
say "* All tests completed normally *"
say "********************************"

stop

label abort
say "Test aborted ..."
stop

label problem_xferxr
say "Problem with IOC external real output transfer U3 <-> U2."
say "     > Check ribbon cable connection/polarity from H7 to H8."
say "     > Check solder connections between U3 and H7/H8."
stop

label problem_xferxi
say "Problem with IOC external imag output transfer U3 <-> U2."
say "     > Check ribbon cable connection/polarity from H7 to H8."
say "     > Check solder connections between U3 and H7/H8."
stop

label problem_xferx
say "Problem with IOC external transfer U3 <-> U2."
say "     > Check ribbon cable connection/polarity from H7 to H8."
say "     > Check solder connections between U3 and H7/H8."
stop

label problem_xferxc
say "Problem with IOC external complex transfer U3 <-> U2."
say "     > Check ribbon cable connection/polarity from H7 to H8."
say "     > Check solder connections between U3 and H7/H8."
stop

label problem_xfert1
say "Problem with tuner1 transfer U3 -> U4."
say "     > Check solder connections on U4."
say "     > Check solder connections between U3 and U4."
stop

label problem_xfert2
say "Problem with tuner2 transfer U3 -> U5."
say "     > Check solder connections on U5."
say "     > Check solder connections between U3 and U5."
stop

endmacro

procedure testxfer u:devo u:devi u:porto u:porti a:filei a:fileo a:fileg n:count

res size filei(hcb.sz)

loop count pass
say " -> running transfer pass ^pass"

picd/replay=5/port=^porto/flags=intclk/quiet/bg/pause=1 &
	play ^devo testin
picd/replay=1/stat=pstat/port=^porti/flags=to=3/quiet &
	acquire ^devi testout
picd/port=^porto stop ^devo 

if pstat lt 0 return

sub fileo fileg diff index
maxmin/quiet diff dmax dmin imax imin
if dmax ne 0 or dmin ne 0
  if porto eqs module3
    calc/quiet ii imax 
  else
    calc/quiet ii imin imax max
  endif
  if ii gt 12 and fileo(ii) eq fileo(ii-12)
    say "Caught PCI FIFO problem - OK
    continue
  elseif ii gt 6 and fileo(ii) eq fileo(ii-6)
    say "Caught PCI FIFO problem - OK
    continue
  else
    say "Bad bits in 1st block"
    if dmin neq 0 data/hex diff imin
    if dmax neq 0 data/hex diff imax
    res pstat -3
    return
  endif
endif

sub fileo(size+1:) fileo diff index
maxmin/quiet diff dmax dmin imax imin
if dmax ne 0 or dmin ne 0
  if porto eqs module3
    calc/quiet ii imax size +
  else
    calc/quiet ii imin imax min size +
  endif
  if ii gt 12 and fileo(ii) eq fileo(ii-12)
    say "Caught PCI FIFO problem - OK
    continue
  elseif ii gt 6 and fileo(ii) eq fileo(ii-6)
    say "Caught PCI FIFO problem - OK
    continue
  else
    say "Bad bits in extended blocks"
    if dmin neq 0 data/hex diff imin
    if dmax neq 0 data/hex diff imax
    res pstat -4
    return
  endif
endif

endloop

return


