startmacro/quiet/np=20/nw=120 a:device[PIC1] &
  a:clksrc[N] l:nfft[1k] l:ndec[256] l:ntun[-1] l:samplerate[10e6] d:freq[620e3]
local flags nn item

res flags "MUXCLK=^clksrc|AOVSR|CPC=2|ITDEC"
picd/flags=^flags reset ^device 
if ntun lt 0 picd/flags=^flags get ^device "CHNS" ntun
res aa 2
res flags

calc psdr 10
calc buflen 0.5
calc l:ntun ntun 8 min
calc nyquist samplerate/2
calc osamplerate samplerate/ndec/2
calc pdec samplerate nfft*psdr / round 1 max
calc qdec osamplerate nfft*psdr / round 1 max

res plotmin -50
res plotmax  50

! create test output files
calc fsize buflen*samplerate nfft*2 max 64k max round
pic create testoutA SI fsize samplerate
pic create testoutB SI fsize samplerate
calc fsize buflen*osamplerate nfft*2 max 64k max round
loop ntun nn
  pic create testout^nn CI fsize osamplerate
endloop

xpipe/setup on 

  sourcepic/wb=11/port=Module1/flags=^flags/skip=pdec/tl=1 &
		testoutA(fs=nfft) _cbA ^device 1 freq
  mfft _cbA _cbfA nfft HANN 0
  xrtplot/xs=2/dbrange=80/xn="Module-A" _cbfA ,,, lo

  sourcepic/wb=12/port=Module2/flags=^flags/skip=pdec/tl=1 &
		testoutB(fs=nfft) _cbB ^device 1 freq
  mfft _cbB _cbfB nfft HANN 0
  xrtplot/xs=3/dbrange=80/xn="Module-B" _cbfB  ,,, lo

 loop ntun nn 
  sourcepic/wb=^nn/port=tuner^nn/flags=^flags/skip=qdec/tl=1 &
		testout^nn(fs=nfft) _cb^nn ^device ndec freq
  mfft _cb^nn _cbf^nn nfft HANN 0
  xrtplot/xs=3+^nn/dbrange=80/xn="Tuner-^nn" _cbf^nn  ,,, lo
 endloop

xpipe off

picd reset ^device 

! remove test output files
erase testoutA testoutB
loop ntun nn
  erase testout^nn 
endloop

endmacro

startcontrols

label initialentry
xcontrol/lab=m1 menu item ,, "TEST;Exit,ReStart,Test1,Test2"
return

label m1
if item eq 1 
  stop

elseif item eq 2 then
  loop ntun nn
    res w_^{nn}001 4
  endloop
  pause 1
  loop ntun nn
    res w_^{nn}001 2
  endloop

elseif item eq 3 then
!  loop ntun nn
!    res w_^{nn}001 4
!  endloop
  pause 1
  loop ntun nn
    res w_^{nn}001 11
  endloop
  pause 1
  loop ntun nn
    res w_^{nn}001 7
  endloop

elseif item eq 4 then
!  loop ntun nn
!    res w_^{nn}001 4
!  endloop
!  pause 1
  loop ntun nn
    while w_^{nn}001 neq 0
      res w_^{nn}001 4
      pause 0.1
    endwhile
    res w_^{nn}003 w_^{nn}003/aa
    res w_^{nn}004 nn*8*aa+200
    res w_^{nn}005 w_^{nn}005/aa
    pause 0.2
!    res w_^{nn}001 11
    while w_^{nn}001 neq 0
      pause 0.1
    endwhile
    res w_^{nn}001 2
  endloop
  pause 1
!  loop ntun nn
!    res w_^{nn}001 7
!  endloop
  calc aa 1/aa

endif
return

label f1
res w_1005 freq
return

label t1
return

endcontrols
