!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!  Production Test macro for ICE cards
!
!  Switches:	/NOTUN - no tuner test
!		/NOINT - no internal transfer tests
!		/NOEXT - no external transfer tests
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
startmacro/quiet u:device[pic] n:npass[3] u:deviceo[]

local answer pstat tuners tgold nn chip tdec flags
local chan size dmax dmin imin imax pass rate ctype

if /fab gt 0
ask answer "Make sure extender slot power is OFF [Y]/N :"
if answer neqss "Y" goto abort
ask answer "Insert card to be tested into extender slot [Y]/N :"
if answer neqss "Y" goto abort
ask answer "Switch extender slot power to ON [Y]/N :"
if answer neqss "Y" goto abort
picd/flags=test test ^device 91
endif

say "Erasing old test files ..."
erase/nowarn testin testout

! default to single card test
if deviceo eqs " " res deviceo device

picd get ^device active pstat
if pstat lt 0 then
say "Halting card ..."
picd/stat=pstat halt ^device
endif

timer set

if /nosig le 0
say "Looking for card signature ..."
picd/stat=pstat sniff ^device 
res stat
if pstat lt 0 goto problem_cardsig

say "Testing PCI slave interface ..."
picd/stat=pstat test ^device 2
if pstat gt 0 goto problem_pci

say "Testing PRC interface ..."
picd/stat=pstat/flags=iom=test test ^device 1
if pstat gt 0 goto problem_sharc
picd/stat=pstat test ^device 3
if pstat gt 0 goto problem_sharc
picd/stat=pstat test ^device 4
if pstat gt 0 goto problem_sharc
picd/stat=pstat test ^device 6
if pstat gt 0 goto problem_sharc

say "Testing IOC interface ..."
picd/stat=pstat test ^device 5
if pstat gt 0 goto problem_ioc

say "Testing PCI master interface ..."
picd/stat=pstat test ^device 7
if pstat gt 0 goto problem_pci
endif

picd get ^device "TYPE" ctype
picd get ^device "PMTYPE1" pmtype1
picd get ^device "PMTYPE2" pmtype2
picd get ^device "CHNS" tuners
picd get ^device "PRCCLK" rate
picd get ^device "GCHIP" gchip

if ctype eq 5  or ctype eq 6 then
say "Testing Processor Module RAM ..."
picd/stat=pstat test ^device 99
if pstat gt 0 goto problem_pmload
endif

if pmtype1 gt 0 then
say "Testing Processor Module #1 RAM ..."
picd/stat=pstat/flags=tp=11 test ^device 99
if pstat gt 0 goto problem_pmload
endif

if pmtype2 gt 0 then
say "Testing Processor Module #2 RAM ..."
picd/stat=pstat/flags=tp=12 test ^device 99
if pstat gt 0 goto problem_pmload
endif

if /speed gt 0
  res rate /speed
else
  res rate 10e6
endif

if /ntun gt 0
  res l:tuners /ntun
endif

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

if /b gt 0
  header testin f=sb
  header testout f=sb
endif

if /noint le 0

say "Testing 1->2 internal transfer ..."
picd reset ^device t2
callp testxfer module1 module2 testin testout testin npass
if pstat lt 0 goto problem_xferi

say "Testing 2->1 internal transfer ..."
picd reset ^device t1
callp testxfer module2 module1 testin testout testin npass
if pstat lt 0 goto problem_xferi

endif
if /noext le 0

say "Testing 1->2 external transfer ..."
picd reset ^deviceo oi
picd reset ^device oi
callp testxfer module1 module2 testin testout testin npass
if pstat lt 0 goto problem_xferx

say "Testing 2->1 external transfer ..."
picd reset ^deviceo io
picd reset ^device io
callp testxfer module2 module1 testin testout testin npass
if pstat lt 0 goto problem_xferx

endif

res tdec 44
if /withovsr gt 0
 res rate 10e6
 res tflags "MUXCLK=I"
elseif /hs ne 0
 picd get ^device "CCLK" rate
 res tflags "MUXCLK=C"
else
 res rate 40e6
 res tflags "MUXCLK=I"
endif

picd reset ^device iir

if /notbank le 0 then

 calc tbs 0
 loop 2 pm
   if pmtype^pm le 0 continue
   if pmtype^pm ge 6 then
     fname tbgold dat gtbank4_32 prm ice
   elseif pmtype^pm le 2 then
     if ctype eq 10 then
       fname tbgold dat gtbank1_32 prm ice
     else
       fname tbgold dat gtbank2_32 prm ice
     endif
   else
     continue
   endif
   calc tbs tbs+1
   pic create testout ci 16k*32/2 rate/(tdec*2)
   loop 2 nn
     say "Testing tuner bank ^{pm}^{nn} transfer ..."
     callp testtbank tbank^{pm}^{nn} testout tbgold npass nn
     if pstat lt 0 goto problem_xfert
   endloop
 endloop
 if tbs gt 0 then ! now address the main board tuners
   res tflags "^tflags|PMI=0"
   picd/flags=^tflags get ^device "CHNS" tuners
   picd/flags=^tflags get ^device "GCHIP" gchip
 endif

endif

picd/ascii get ^device "FPGASIG" sig 

if /notun le 0 and tuners gt 0

 if "MT" subs sig or "TD" subs sig then
  fname tgold dat gtun2x prm ice
  res tflags "^tflags|MBITS=-16"
  res l:tuners 0
 elseif gchip eq 4 then
  fname tgold dat gtun5 prm ice
 elseif gchip eq 2 and ctype neq 4 and ctype neq 10
  fname tgold dat gtun11 prm ice
 else
  fname tgold dat gtun^ctype prm ice
 endif

 pic create testout ci 16k rate/(tdec*2)

 loop tuners nn
  say "Testing tuner ^nn transfer ..."
  callp testtuner tuner^nn testout tgold npass nn
  if pstat lt 0 goto problem_xfert
 endloop

endif

timer elapse

say "********************************"
say "* All tests completed normally *"
say "********************************"
if /fab gt 0
picd halt ^device 
!ask answer "Switch extender slot power to OFF [Y]/N :"
!if answer neqss "Y" goto abort
say "It is now safe to remove the card ..."
else 
picd reset ^device
endif

stop

label abort
say "Test aborted ..."
stop

label problem_cardsig
say "Problem getting the card signature."
say "Try  > Cycle power on card extender."
say "     > Power OFF card extender and reseat card in extender slot."
say "     > Check that EPROM U10 is seated correctly."
say "     > Check connections on U1."
stop

label problem_pci
say "Problem with PCI interface U1."
say "     > Power OFF card extender and reseat card in extender slot."
say "     > Check connections on U1."
stop

label problem_pci64
say "Problem with PCI interface U1."
say "     > Power OFF card extender and reseat card in extender slot."
say "     > Check connections on U1 pins 61 through 120."
stop

label problem_sharc
say "Problem with SHARC interface U2 <-> U1."
say "     > Check connections on U2."
stop

label problem_ioc
say "Problem with IOC interface U3 <-> U1."
say "     > Check connections on U3."
stop

label problem_xferl
say "Problem with IOC linkport transfer U3 <-> U2."
say "     > Check connections between U2 && U3."
stop

label problem_xferi
say "Problem with IOC internal transfer U3 <-> U2."
say "     > Check connections between U2 && U3."
stop

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

label problem_xfers
say "Problem with serial port external transfer U2 <-> H5."
say "     > Check ribbon cable connection on H5."
say "     > Check connections between U2 and H5."
stop

label problem_pmload
say "Problem with processor module load."
say "     > Check PWR2,PM1 on PIC4X.
say "     > Check H1,U1 on DTDM.
stop

label problem_xfert
say "Problem with tuner chip transfer."
if ctype eq 2 and nn eq 1
 say "     > Check connections between U3 and U4."
elseif ctype eq 2 and nn eq 2
 say "     > Check connections between U3 and U5."
elseif ctype eq 3 and nn eq 1
 say "     > Check connections between U3 and T1."
elseif ctype eq 3 and nn eq 2
 say "     > Check connections between U3 and T2."
elseif ctype eq 9 or ctype eq 10 or ctype eq 4
 calc/type=l chip nn-1 8 / 2 * nn-1 1 band + 1 + 
 if "|^nn|" subs "|1|2|9|10|17|18|25|26"
  if ctype eq 10
   say "     > Check connections on X1 or between U1 and G^chip on DTDM."
   say "     > Check connections on X1 or tuner chips in same row as G^chip."
  else
   say "     > Check connections between U3 and G^chip."
   say "     > Check connections on tuner chips in same row as G^chip."
  endif
 else
  say "     > Probably a bad tuner chip G^chip - check first."
 endif
endif
!!xplot testout|tgold

stop

endmacro


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

res pstat 0
if /noa gt 0 and porti eqs "MODULE1" return
if /nob gt 0 and porti eqs "MODULE2" return

res flags "TO=3|IOM=TEST"
if ctype eq 3 or ctype eq 4 concat flags "|DUAL" flags
if ctype eq 2 or ctype eq 8 concat flags "|XGO" flags

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

if deviceo eqs device
  picd/replay=4/stat=chan/port=^porto/flags=muxclk=i|^flags &
	play ^deviceo testin
  picd/replay=-3/stat=pstat/kick=chan/port=^porti/flags=^flags/goldtest=^fileg &
	acquire ^device testout
  picd/port=^porto/dir=1 stop ^deviceo 
else
  calc iper fileo(hcb.sz) fileg(hcb.sz) /
  calc nper 3
  pipe on
  picd/replay=-iper*nper-1/stat=chan/port=^porto/flags=muxclk=i|^flags/wait=.5 &
        play ^deviceo filei
  picd/replay=-nper/stat=pstat/port=^porti/flags=^flags/goldtest=^fileg &
        acquire ^device fileo
  pipe off
endif
if pstat lt 0 return

endloop

return


procedure testtuner u:porti a:fileo a:fileg n:count n:index

res pstat 0
calc ii index 2 mod
if /noa gt 0 and ii eq 1 return
if /nob gt 0 and ii eq 0 return

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

 picd/replay=1/stat=pstat/port=^porti/flags=^tflags|ioc=iir|to=3/goldtest="^fileg" &
	acquire ^device testout ,,, tdec rate*.015

if pstat lt 0 return

endloop

return


procedure testtbank u:porti a:fileo a:fileg n:count n:index

res pstat 0
calc ii index 2 mod
if /noa gt 0 and ii eq 1 return
if /nob gt 0 and ii eq 0 return

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

 picd/replay=1/stat=pstat/port=^porti/flags=^tflags|ioc=iir|to=3|pmi=^pm/goldtest="^fileg" &
	acquire ^device testout ,,, tdec rate*.015

if pstat lt 0 return

endloop

return

