summaryrefslogtreecommitdiff
path: root/map65/libm65/decode0.f90
diff options
context:
space:
mode:
Diffstat (limited to 'map65/libm65/decode0.f90')
-rw-r--r--map65/libm65/decode0.f90269
1 files changed, 199 insertions, 70 deletions
diff --git a/map65/libm65/decode0.f90 b/map65/libm65/decode0.f90
index 438079f1..26a90366 100644
--- a/map65/libm65/decode0.f90
+++ b/map65/libm65/decode0.f90
@@ -1,71 +1,200 @@
-subroutine decode0(dd,ss,savg,nstandalone)
-
- use timer_module, only: timer
- parameter (NSMAX=60*96000)
-
- real*4 dd(4,NSMAX),ss(4,322,NFFT),savg(4,NFFT)
- real*8 fcenter
- integer hist(0:32768)
- logical ldecoded
- character mycall*12,hiscall*12,mygrid*6,hisgrid*6,datetime*20
- character mycall0*12,hiscall0*12,hisgrid0*6
- common/npar/fcenter,nutc,idphi,mousedf,mousefqso,nagain, &
- ndepth,ndiskdat,neme,newdat,nfa,nfb,nfcal,nfshift, &
- mcall3,nkeep,ntol,nxant,nrxlog,nfsample,nxpol,nmode, &
- ndop00,nsave,max_drift,nhsym,mycall,mygrid,hiscall,hisgrid,datetime
- common/early/nhsym1,nhsym2,ldecoded(32768)
- common/decodes/ndecodes
- data neme0/-99/,mcall3b/1/
- save
-
- call sec0(0,tquick)
- call timer('decode0 ',0)
- if(newdat.ne.0) then
- nz=96000*nhsym/5.3833
- hist=0
- do i=1,nz
- j1=min(abs(dd(1,i)),32768.0)
- hist(j1)=hist(j1)+1
- j2=min(abs(dd(2,i)),32768.0)
- hist(j2)=hist(j2)+1
- j3=min(abs(dd(3,i)),32768.0)
- hist(j3)=hist(j3)+1
- j4=min(abs(dd(4,i)),32768.0)
- hist(j4)=hist(j4)+1
- enddo
- m=0
- do i=0,32768
- m=m+hist(i)
- if(m.ge.2*nz) go to 10
- enddo
-10 rmsdd=1.5*i
- endif
- ndphi=0
- if(iand(nrxlog,8).ne.0) ndphi=1
-
- if(mycall.ne.mycall0 .or. hiscall.ne.hiscall0 .or. &
- hisgrid.ne.hisgrid0 .or. mcall3.ne.0 .or. neme.ne.neme0) mcall3b=1
+module decode0_mod
+ implicit none
+contains
+
+ subroutine decode0(nstandalone) bind(C, name='decode0_')
+
+ use iso_c_binding, only: c_int
+ use timer_module, only: timer
+ use npar_ptrs_mod
+ use datcom_ptrs_mod, only: NFFT, dd, dd_old, dd_use, ss, ss_old, savg, savg_old, ss_use, savg_use
+ use debug_log, only: dbg, itoa, rtoa
+ use sec_midn_mod, only: sec_midn
+ use map65a_mod
+ use stdout_channel_mod, only: write_stdout
+ use decodes_mod, only: ndecodes, nhsym1, nhsym2
+ use sec0_mod, only:sec0
+
+ implicit none
+
+ integer(c_int), intent(in) :: nstandalone
+ integer hist(0:32768)
+ integer i, j1, j2, j3, j4, m, mcall3b, ndphi, neme0, nsum
+ integer :: ndecdone
+ real :: tdec, tquick, rmsdd
+ integer nz
+ integer :: nhsym0
+ integer :: newdat0
+ character(len=128) :: line
+ character mycall0*12, hiscall0*12, hisgrid0*6
+ logical :: first_mcall3b_check
+
+ data neme0/-99/, mcall3b/0/, mycall0/' '/, hiscall0/' '/, hisgrid0/' '/
+ data first_mcall3b_check/.true./
+
+ save
+
+ ! The decoder owns the claimed parameters until this call returns.
+ nhsym0 = nhsym
+ newdat0 = newdat
+
+ ! TEMP diagnostic 2026-09-10 for the missing-final-pass / incomplete-
+ ! data-set investigation.
+ call dbg('decode0: ENTRY at t=' // rtoa(sec_midn()) // &
+ ' nhsym0=' // itoa(nhsym0) // ' newdat0=' // itoa(newdat0) // &
+ ' manualDecodeFlag=' // itoa(manualDecodeFlag) // &
+ ' nagain=' // itoa(nagain) // ' ndiskdat=' // itoa(ndiskdat))
+
+ ! newdat is forced to 1 by decode() for every call (manual or
+ ! automatic); it does not by itself mean "the live buffers just
+ ! finished a fresh accumulation." nagain=1 marks a manual repeat
+ ! (Decode button / Find Delta Phi), which must not refresh dd_old
+ ! from a live buffer that may still be mid-fill.
+ if (newdat0 /= 0 .and. manualDecodeFlag == 0 .and. nagain == 0) then
+ dd_old = dd
+ ss_old = ss
+ savg_old = savg
+ call dbg('decode0: dd_old REFRESHED from live dd at t=' // rtoa(sec_midn()) // ' nhsym0=' // itoa(nhsym0))
+ else
+ call dbg('decode0: dd_old NOT refreshed (reusing prior snapshot) at t=' // rtoa(sec_midn()) // &
+ ' nhsym0=' // itoa(nhsym0) // ' nagain=' // itoa(nagain))
+ endif
+
+ nkeep = 20
+
+ call sec0(0, tquick)
+ call timer('decode0 ', 0)
- mycall0=mycall
- hiscall0=hiscall
- hisgrid0=hisgrid
- neme0=neme
-
- call timer('map65a ',0)
- call map65a(dd,ss,savg,newdat,nutc,fcenter,ntol,idphi,nfa,nfb, &
- mousedf,mousefqso,nagain,ndecdone,nfshift,ndphi,max_drift, &
- nfcal,nkeep,mcall3b,nsum,nsave,nxant,mycall,mygrid, &
- neme,ndepth,nstandalone,hiscall,hisgrid,nhsym,nfsample, &
- ndiskdat,nxpol,nmode,ndop00)
- call timer('map65a ',1)
- call timer('decode0 ',1)
-
- call sec0(1,tdec)
- if(nhsym.eq.nhsym1) write(*,1010) nsum,nsave,nstandalone,nhsym,tdec
-1010 format('<EarlyFinished>',3i4,i6,f6.2)
- if(nhsym.eq.nhsym2) write(*,1012) nsum,nsave,nstandalone,nhsym,tdec,ndecodes
-1012 format('<DecodeFinished>',3i4,i6,f6.2,i5)
- flush(6)
-
- return
-end subroutine decode0
+ ! Always decode from the snapshot (dd_old/ss_old/savg_old) taken above,
+ ! never the live dd/ss/savg. The nhsym2 pass routinely runs past the
+ ! minute boundary (several seconds), and symspec_() on the GUI thread
+ ! resets/overwrites the live buffers for the next cycle unconditionally
+ ! as soon as new UDP data arrives -- with no lock between the two
+ ! threads. Without this, a still-running decode reads a mix of this
+ ! cycle's and next cycle's data for candidates it hasn't reached yet
+ ! in its frequency sweep.
+ dd_use => dd_old
+ ss_use => ss_old
+ savg_use => savg_old
+
+ if (newdat0 .ne. 0 .or. manualDecodeFlag /= 0) then
+ nz = int( real(nrate_active, kind=real64) * nhsym0 / 5.3833_real64 )
+ hist = 0
+ do i = 1, nz
+ j1 = int(min(abs(dd_use(1, i)), 32768.0))
+ hist(j1) = hist(j1) + 1
+ j2 = int(min(abs(dd_use(2, i)), 32768.0))
+ hist(j2) = hist(j2) + 1
+ j3 = int(min(abs(dd_use(3, i)), 32768.0))
+ hist(j3) = hist(j3) + 1
+ j4 = int(min(abs(dd_use(4, i)), 32768.0))
+ hist(j4) = hist(j4) + 1
+ enddo
+ m = 0
+ do i = 0, 32768
+ m = m + hist(i)
+ if (m .ge. 2*nz) go to 10
+ enddo
+10 rmsdd = 1.5*i
+ endif
+ ndphi = 0
+ if (iand(nrxlog, 8) .ne. 0) ndphi = 1
+
+ ! Seed mycall0/hiscall0/hisgrid0/neme0 from the real values on the
+ ! first call instead of blank sentinels, so the mismatch check below
+ ! only fires on a genuine later change, not on startup.
+ if (first_mcall3b_check) then
+ mycall0 = mycall
+ hiscall0 = hiscall
+ hisgrid0 = hisgrid
+ neme0 = neme
+ first_mcall3b_check = .false.
+ endif
+
+ if (mycall .ne. mycall0 .or. hiscall .ne. hiscall0 .or. &
+ hisgrid .ne. hisgrid0 .or. mcall3 .ne. 0 .or. neme .ne. neme0) mcall3b = 1
+
+ mycall0 = mycall
+ hiscall0 = hiscall
+ hisgrid0 = hisgrid
+ neme0 = neme
+
+ ! Manual decode: force a single nhsym2-style cycle and always emit DecodeFinished
+ if (manualDecodeFlag /= 0) then
+ nhsym0 = nhsym2
+ call announce_decode_pass('manual')
+
+ call timer('map65a ', 0)
+ call map65a(dd_use, newdat0, nutc, fcenter, ntol, idphi, nfa, nfb, &
+ mousedf, mousefqso, nagain, ndecdone, nfshift, ndphi, max_drift, &
+ nfcal, nkeep, mcall3b, nsum, nsave, nxant, mycall, mygrid, &
+ neme, ndepth, nstandalone, hiscall, hisgrid, nhsym0, nfsample, &
+ ndiskdat, nxpol, nmode, ndop00)
+ call timer('map65a ', 1)
+ call timer('decode0 ', 1)
+
+ call sec0(1, tdec)
+
+ write (line, '("<DecodeFinished>",3I4,I6,F6.2,I5,1X,I0)') &
+ nsum, nsave, nstandalone, nhsym0, tdec, ndecodes, active_request_id
+ call write_stdout(trim(line)//new_line('a'))
+
+ call dbg('decode0: MANUAL path RETURNING at t=' // rtoa(sec_midn()))
+
+ return
+
+ else
+
+ ! Normal wideband path: nhsym1 (EarlyFinished) + nhsym2 (DecodeFinished)
+ if (ndiskdat /= 0) then
+ call announce_decode_pass('disk')
+ else if (nhsym0 == nhsym1) then
+ call announce_decode_pass('early')
+ else if (nhsym0 == nhsym2) then
+ call announce_decode_pass('final')
+ else
+ call announce_decode_pass('unknown')
+ endif
+ call timer('map65a ', 0)
+ call map65a(dd_use, newdat0, nutc, fcenter, ntol, idphi, nfa, nfb, &
+ mousedf, mousefqso, nagain, ndecdone, nfshift, ndphi, max_drift, &
+ nfcal, nkeep, mcall3b, nsum, nsave, nxant, mycall, mygrid, &
+ neme, ndepth, nstandalone, hiscall, hisgrid, nhsym0, nfsample, &
+ ndiskdat, nxpol, nmode, ndop00)
+ call timer('map65a ', 1)
+ call timer('decode0 ', 1)
+
+ call sec0(1, tdec)
+
+ if (nhsym0 == nhsym1) then
+ write (line, '("<EarlyFinished>",3I4,I6,F6.2)') &
+ nsum, nsave, nstandalone, nhsym0, tdec
+ call write_stdout(trim(line)//new_line('a'))
+ end if
+
+ ! A manual repeat decode (Decode button / Find Delta Phi) is always a
+ ! single, complete request regardless of nhsym, which only reflects
+ ! the automatic per-minute cycle and may never equal nhsym2 if no
+ ! automatic decode has run yet this session.
+ if (nhsym0 == nhsym2 .or. nagain /= 0) then
+ write (line, '("<DecodeFinished>",3I4,I6,F6.2,I5,1X,I0)') &
+ nsum, nsave, nstandalone, nhsym0, tdec, ndecodes, active_request_id
+ call write_stdout(trim(line)//new_line('a'))
+ end if
+
+ call dbg('decode0: WIDEBAND path RETURNING at t=' // rtoa(sec_midn()))
+
+ return
+ end if
+ end subroutine decode0
+
+ subroutine announce_decode_pass(pass_name)
+ use stdout_channel_mod, only: write_stdout
+ implicit none
+
+ character(len=*), intent(in) :: pass_name
+
+ call write_stdout('<Map65DecodePass> '//trim(pass_name)//new_line('a'))
+ end subroutine announce_decode_pass
+
+end module decode0_mod