summaryrefslogtreecommitdiff
path: root/map65/libm65/cgen65.f90
blob: 6fcf98d63b3805e6ef00eefa7e63d6210019b611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
module cgen65_mod
  implicit none
contains

subroutine cgen65(message,mode65,samfac,nsendingsh,msgsent,cwave,nwave)

  use iso_fortran_env, only: int8, real64
  use packjt
  use chkmsg_mod
  use graycode_mod
  use rs_mod
  use interleave63_mod, only: interleave63
  !--------------------------------------------------------------------
  ! Parameters and arguments
  !--------------------------------------------------------------------
  integer, parameter :: NMAX = 60*96000

  character(len=22),intent(inout)  :: message
  integer,          intent(in)  :: mode65
  real(real64),     intent(in)  :: samfac

  integer,          intent(out) :: nsendingsh
  character(len=22),intent(out) :: msgsent
  complex,          intent(out) :: cwave(:)
  integer,          intent(out) :: nwave

  !--------------------------------------------------------------------
  ! Local variables (explicit typing)
  !--------------------------------------------------------------------
  character(len=3) :: cok
  real(real64) :: t, dt, phi, f, f0, dfgen, dphi, twopi, tsymbol
  real(real64) :: xphi
  real   :: pr(126), flip

  integer :: dgen(12)
  integer :: sent(63)
  integer :: nprc(126)
  integer :: nspecial, itype
  integer :: nsym, ndata
  integer :: i, j, j0, k

  logical :: first

  !--------------------------------------------------------------------
  ! DATA initialization (preserved exactly)
  !--------------------------------------------------------------------
  data nprc/1,0,0,1,1,0,0,0,1,1,1,1,1,1,0,1,0,1,0,0,  &
            0,1,0,1,1,0,0,1,0,0,0,1,1,1,0,0,1,1,1,1,  &
            0,1,1,0,1,1,1,1,0,0,0,1,1,0,1,0,1,0,1,1,  &
            0,0,1,1,0,1,0,1,0,1,0,0,1,0,0,0,0,0,0,1,  &
            1,0,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,0,1,  &
            0,1,0,1,0,0,1,1,0,0,1,0,0,1,0,0,0,0,1,1,  &
            1,1,1,1,1,1/
  data twopi /6.283185307179586476d0/
  data first /.true./
  save

  !--------------------------------------------------------------------
  ! First‑time initialization of pr()
  !--------------------------------------------------------------------
  if (first) then
     do i = 1, 126
        pr(i) = 2*nprc(i) - 1
     enddo
     first = .false.
  endif

  !--------------------------------------------------------------------
  ! Message packing / shorthand detection
  !--------------------------------------------------------------------
  call chkmsg(message, cok, nspecial, flip)

  if (nspecial .eq. 0) then
     call packmsg(message, dgen, itype)
     nsendingsh = 0
     if (iand(dgen(10),8) .ne. 0) nsendingsh = -1   ! Plain text flag

     call rs_encode_(dgen, sent)
     call interleave63(sent, 1)
     call graycode(sent, 63, 1)

     nsym    = 126
     tsymbol = 4096.d0 / 11025.d0
  else
     nsendingsh = 1
     nsym    = 32
     tsymbol = 16384.d0 / 11025.d0
  endif

  !--------------------------------------------------------------------
  ! Waveform generation constants
  !--------------------------------------------------------------------
  dt    = 1.d0 / (samfac * 96000.d0)
  f0    = 118 * 11025.d0 / 1024.d0
  dfgen = mode65 * 11025.d0 / 4096.d0

  t     = 0.d0
  phi   = 0.d0
  k     = 0
  j0    = 0

  ndata = nsym * 96000.d0 * samfac * tsymbol

  !--------------------------------------------------------------------
  ! Main waveform synthesis loop
  !--------------------------------------------------------------------
  do i = 1, ndata
     t = t + dt
     j = min(nsym, int(t / tsymbol) + 1)

     if (j .ne. j0) then
        f = f0

        if (nspecial .ne. 0 .and. mod(j,2) .eq. 0) then
           f = f0 + 10 * nspecial * dfgen
        endif

        if (nspecial .eq. 0 .and. flip * pr(j) .lt. 0.d0) then
           k = k + 1
           f = f0 + (sent(k) + 2) * dfgen
        endif

        dphi = twopi * dt * f
        j0   = j
     endif

     phi = phi + dphi
     if (phi .gt. twopi) phi = phi - twopi

     xphi = phi
     cwave(i) = cmplx(cos(xphi), -sin(xphi))
  enddo

  !--------------------------------------------------------------------
  ! Pad and finalize
  !--------------------------------------------------------------------
  cwave(ndata+1:) = (0.0, 0.0)
  nwave = ndata + 48000

  call unpackmsg(dgen, msgsent)

  if (flip .lt. 0.d0) then
     do i = 22, 1, -1
        if (msgsent(i:i) .ne. ' ') exit
     enddo
     msgsent = msgsent(1:i) // ' OOO'
  endif

  if (nsendingsh .eq. 1) then
     if (nspecial .eq. 2) msgsent = 'RO'
     if (nspecial .eq. 3) msgsent = 'RRR'
     if (nspecial .eq. 4) msgsent = '73'
  endif

end subroutine cgen65

end module cgen65_mod