startmacro/ps=2m/tc=sdds/tcps=10/replay=0 u:device[pic3iistv6] d:freq[-1] d:ovsr[8]

picd reset ^device

res dec   192
res ddec  192/ovsr
res irate 100e6/ovsr
res rrate 104e6/ovsr
res ratio rrate/irate
res nchan 4
res nfft  2k
res psz   32k
res crate rrate/ddec/2
res orate rrate/ddec

if /altd gt 0 then
 res rrate rrate*.9
 res rratio ratio*.9
else
 res rratio ratio
endif

if freq lt 0 then calc/quiet freq irate/4

res flgw "MUXCLK=N|TCLK=110e6"
res flgr "MUXCLK=N|OVSR=2|RESAMP"
res flgn "MUXCLK=N|OVSR=^ovsr|POVSR|FTTM=2|CPC=2|PRESAMP"

res mport "MODULE1"
res rport "PM0TUNER1"
res tport "TBANK1"

if /tuner gt 0 then
  res tport "TUNER1"
  res nchan 1
endif
if /core gt 0 then
  res tport "CORE11"
  res nchan 1
endif
if /both gt 0 then
  sedit tport tport subs "1" "3"
endif
if /side eq 2 then
  sedit mport mport subs "1" "2"
  sedit rport rport subs "1" "2"
  sedit tport tport subs "1" "2"
endif
if /side eq 2 and /both gt 0 then
  res flgn "INP=2|^flgn"
endif

if /back gt 0 then
  res flgn "RGO|^flgn"
else
  res flgw "RGO|^flgw"
endif
if /alt gt 0
  sedit rport rport subs "2" "1"
  res flgr "INP=2|^flgr"
endif
if /nwb lt 0 or /back lt 0 then
  res flgr "RGO|^flgr"
endif

pic/round=psz create wbfile si irate irate
pic/round=psz create rbfile si rrate rrate
pic/round=psz/multi=nchan create nbfile ci crate crate

switch "AFLAGS" u:aflags get
res flgw "^aflags|^flgw"
res flgr "^aflags|^flgr"
res flgn "^aflags|^flgn"
picd/flags=^flgw reset ^device

xpipe/setup/nw=128/timeout=100 on

! wideband data source
if /nwb le 0
sourcepic/wb=1/port=^mport/flags=^flgw/packet=_wbp wbfile(fs=nfft) _wb ^device 1 irate/4 
mfft _wb _wbf nfft ,, -4 ,, 16
xrtplot/xs=3/dbrange=140/xn=WB _wbf ,,, l2
if /dump gt 0 then
  keyword/scope=main _wb put packet "ICE/DET/FS=^nfft"
  noop _wb(fs=0) testwb
  noop _wbp testwb.pkt
endif
endif

! resampled data source
if /wbr gt 0 then
sourcepic/wb=2/port=^rport/ratio=rratio/flags=^flgr/packet=_rbp rbfile(fs=nfft) _rb ^device 1 irate/4 -3
mfft _rb _rbf nfft ,, -4 ,, 16
xrtplot/xs=4/dbrange=140/xn=RB _rbf ,,, l2
if /dump gt 0 then
  keyword/scope=main _rb put packet "ICE/DET/FS=^nfft"
  noop _rb(fs=0) testrb
  noop _rbp testrb.pkt
endif
endif

! narrowband data source
if /nnb le 0
sourcepic/wb=3/port=^tport/ratio=ratio/nchn=nchan/tl=1/flags=^flgn/dfreq=1e4/packet=_nbp nbfile(fs=nfft) _nb ^device dec freq 
thin _nb _nbt 1 -1 nchan
thin _nbp _nbpt 1 -1 nchan
mfft _nbt _nbf nfft
xrtplot/xs=5/dbrange=140/xn=NB _nbf ,,, l2
if /dump gt 0 then
  keyword/scope=main _nbt put packet "ICE/DET/FS=^nfft"
  noop _nbt(fs=0) testnb
  noop _nbpt testnb.pkt
endif
endif

xpipe off

endmacro

startcontrols

label initialentry
xcontrol prompt "MODE" "Mode" mode
xcontrol/lab=xxx menu item 0 "Do;Run,Stop,Exit" 
return

label xxx
if item eq 1 then
 if /back gt 0 then
  if w_3001 rexists res w_3001 2
 else
  if w_1001 rexists res w_1001 2
 endif
  pause .5
  if w_2001 rexists res w_2001 2
  pause .5
 if /back gt 0 then
  if w_1001 rexists res w_1001 2
 else
  if w_3001 rexists res w_3001 2
 endif
elseif item eq 2 then
  if w_1001 rexists res w_1001 4
  if w_2001 rexists res w_2001 4
  if w_3001 rexists res w_3001 4
elseif item eq 3 then
  stop
endif
return

endcontrols
