summaryrefslogtreecommitdiff
path: root/lib/qra/q65/q65_encoding_modules.f90
blob: 3f749d5bbe3568f9f332d818b2ad1a2ff8c550be (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
module gf64math
! add and subtract in GF(2^6) based on primitive polynomial x^6+x+1

   implicit none
   integer, private ::  gf64log(0:63)
   integer, private ::  gf64antilog(0:62)

! table of the logarithms of the elements of GF(M) (log(0) never used)
   data gf64log/    &
      -1,   0,   1,   6,   2,  12,   7,  26,   3,  32,  &
      13,  35,   8,  48,  27,  18,   4,  24,  33,  16,  &
      14,  52,  36,  54,   9,  45,  49,  38,  28,  41,  &
      19,  56,   5,  62,  25,  11,  34,  31,  17,  47,  &
      15,  23,  53,  51,  37,  44,  55,  40,  10,  61,  &
      46,  30,  50,  22,  39,  43,  29,  60,  42,  21,  &
      20,  59,  57,  58/

! table of GF(M) elements given their logarithm
   data gf64antilog/   &
      1,   2,   4,   8,  16,  32,   3,   6,  12,  24, &
      48,  35,   5,  10,  20,  40,  19,  38,  15,  30, &
      60,  59,  53,  41,  17,  34,   7,  14,  28,  56, &
      51,  37,   9,  18,  36,  11,  22,  44,  27,  54, &
      47,  29,  58,  55,  45,  25,  50,  39,  13,  26, &
      52,  43,  21,  42,  23,  46,  31,  62,  63,  61, &
      57,  49,  33/

contains

   integer function gf64_add(i1,i2)
      implicit none
      integer::i1
      integer::i2
      gf64_add=iand(ieor(i1,i2),63)
   end function gf64_add

   integer function gf64_mult(i1,i2)
      implicit none
      integer::i1
      integer::i2
      integer::j

      if(i1.eq.0 .or. i2.eq.0) then
         gf64_mult=0
      elseif(i1.eq.1) then
         gf64_mult=i2
      elseif(i2.eq.1) then
         gf64_mult=i1
      else
         j=mod(gf64log(i1)+gf64log(i2),63)
         gf64_mult=gf64antilog(j)
      endif
   end function gf64_mult

end module gf64math

module q65_generator

   integer generator(15,50)
   data generator/  &
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, &
      0,20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, &
      0,20, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, &
      0,20, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, &
      0,20, 0, 1, 1, 0, 0, 0,10, 0, 0, 0, 0, 1, 0, &
      0,20, 0, 1, 1, 0, 0, 0,10, 0, 0, 0,44, 1, 0, &
      0,20, 0, 1, 1, 0, 0, 0,10, 1, 0, 0,44, 1, 0, &
      0,20, 0, 1, 1, 0, 0, 0,10, 1, 0, 0,44, 1,14, &
      0,20, 0, 1, 1, 0, 0, 0,10, 1,31, 0,44, 1,14, &
      0,20, 0, 1, 1,33, 0, 0,10, 1,31, 0,44, 1,14, &
      56,20, 0, 1, 1,33, 0, 0,10, 1,31, 0,44, 1,14, &
      56,20, 0, 1, 1,33, 0, 1,10, 1,31, 0,44, 1,14, &
      56, 1, 0, 1, 1,33, 0, 1,10, 1,31, 0,44, 1,14, &
      56, 1, 0, 1, 1,33, 0, 1,10, 1,31,36,44, 1,14, &
      56, 1, 0, 1, 1,33, 0, 1,43, 1,31,36,44, 1,14, &
      56, 1, 0, 1, 1,33, 0, 1,43,17,31,36,44, 1,14, &
      56, 1, 0, 1, 1,33, 0, 1,43,17,31,36,36, 1,14, &
      56, 1, 0, 1, 1,33,53, 1,43,17,31,36,36, 1,14, &
      56, 1, 0,35, 1,33,53, 1,43,17,31,36,36, 1,14, &
      56, 1, 0,35, 1,33,53, 1,43,17,30,36,36, 1,14, &
      56, 1, 0,35, 1,33,53,52,43,17,30,36,36, 1,14, &
      56, 1, 0,35, 1,32,53,52,43,17,30,36,36, 1,14, &
      56, 1,60,35, 1,32,53,52,43,17,30,36,36, 1,14, &
      56, 1,60,35, 1,32,53,52,43,17,30,36,36,49,14, &
      56, 1,60,35, 1,32,53,52,43,17,30,36,37,49,14, &
      56, 1,60,35,54,32,53,52,43,17,30,36,37,49,14, &
      56, 1,60,35,54,32,53,52, 1,17,30,36,37,49,14, &
      1, 1,60,35,54,32,53,52, 1,17,30,36,37,49,14, &
      1, 0,60,35,54,32,53,52, 1,17,30,36,37,49,14, &
      1, 0,60,35,54,32,53,52, 1,17,30,37,37,49,14, &
      1, 0,61,35,54,32,53,52, 1,17,30,37,37,49,14, &
      1, 0,61,35,54,32,53,52, 1,48,30,37,37,49,14, &
      1, 0,61,35,54,32,53,52, 1,48,30,37,37,49,15, &
      1, 0,61,35,54, 0,53,52, 1,48,30,37,37,49,15, &
      1, 0,61,35,54, 0,52,52, 1,48,30,37,37,49,15, &
      1, 0,61,35,54, 0,52,52, 1,48,30,37,37, 0,15, &
      1, 0,61,35,54, 0,52,34, 1,48,30,37,37, 0,15, &
      1, 0,61,35,54, 0,52,34, 1,48,30,37, 0, 0,15, &
      1, 0,61,35,54, 0,52,34, 1,48,30,20, 0, 0,15, &
      1, 0, 0,35,54, 0,52,34, 1,48,30,20, 0, 0,15, &
      1, 0, 0,35,54, 0,52,34, 1, 0,30,20, 0, 0,15, &
      0, 0, 0,35,54, 0,52,34, 1, 0,30,20, 0, 0,15, &
      0, 0, 0,35,54, 0,52,34, 1, 0,38,20, 0, 0,15, &
      0, 0, 0,35, 0, 0,52,34, 1, 0,38,20, 0, 0,15, &
      0, 0, 0,35, 0, 0,52, 0, 1, 0,38,20, 0, 0,15, &
      0, 0, 0,35, 0, 0,52, 0, 1, 0,38,20, 0, 0, 0, &
      0, 0, 0,35, 0, 0,52, 0, 0, 0,38,20, 0, 0, 0, &
      0, 0, 0,35, 0, 0,52, 0, 0, 0,38, 0, 0, 0, 0, &
      0, 0, 0, 0, 0, 0,52, 0, 0, 0,38, 0, 0, 0, 0, &
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,38, 0, 0, 0, 0/

end module q65_generator

module q65_encoding

contains

subroutine q65_encode(message,codeword)
   use gf64math
   use q65_generator
   integer message(15)
   integer codeword(65)
   integer i,j

   codeword=0
   codeword(1:15)=message
   do i=1,15
      do j=16,65
         codeword(j)=gf64_add(codeword(j),gf64_mult(message(i),generator(i,j-15)))
      enddo
   enddo

   return
end

subroutine get_q65crc12(mc2,ncrc1,ncrc2)
!
   character c6*6
   integer*1 mc(90),mc2(90),tmp(6)
   integer*1 r(13),p(13)
! polynomial for 12-bit CRC 0xF01
   data p/1,1,0,0,0,0,0,0,0,1,1,1,1/

! flip bit order of each 6-bit symbol for consistency with Nico's calculation
   do i=0,14
      tmp=mc2(i*6+1:i*6+6)
      mc(i*6+1:i*6+6)=tmp(6:1:-1)
   enddo

! divide by polynomial
   r=mc(1:13)
   do i=0,77
      r(13)=mc(i+13)
      r=mod(r+r(1)*p,2)
      r=cshift(r,1)
   enddo

   write(c6,'(6b1)') r(6:1:-1)
   read(c6,'(b6.6)') ncrc1
   read(c6,'(6b1)') mc2(79:84)
   write(c6,'(6b1)') r(12:7:-1)
   read(c6,'(b6.6)') ncrc2
   read(c6,'(6b1)') mc2(85:90)

end subroutine get_q65crc12

subroutine get_q65_tones(msg37,codeword,itone,msgsent,success)
   use packjt77
   implicit none
   character*37 msg37,msgsent
   character*77 c77
   character*6  c6
   integer codeword(65)
   integer sync(22)
   integer message(15)
   integer shortcodeword(63)
   integer itone(85)
   integer i,j,k,ios,pack_status,ng15
   integer*1 mbits(90)
   integer i3,n3,ncrc1,ncrc2
   logical success,unpk77_success
   data sync/1,9,12,13,15,22,23,26,27,33,35,38,46,50,55,60,62,66,69,74,76,85/

   i3=-1
   n3=-1
   c77=' '
   codeword=0
   itone=0
   msgsent='*** bad message ***                  '
   success=.false.
   call pack77_legacy_truncating_fallback(msg37,i3,n3,c77, &
        status=pack_status)
   if(pack_status.ne.PACK77_STATUS_ENCODED) return
   read(c77(60:74),'(b15)',iostat=ios) ng15
   if(ios.ne.0) return
   if(ng15.eq.32373) c77(60:74)='111111010010011'    !Message is RR73
   call unpack77(c77,0,msgsent,unpk77_success)
   if(.not.unpk77_success) then
      msgsent='*** bad message ***                  '
      return
   endif
   mbits=0
   read(c77,'(77i1)',iostat=ios) mbits(1:77)
   if(ios.ne.0) then
      msgsent='*** bad message ***                  '
      return
   endif

! Message is 77 bits long. Add a 0 bit to create a 78-bit message and pad with 
! 12 zeros to create 90-bit mbit array for CRC calculation. 
   call get_q65crc12(mbits,ncrc1,ncrc2)

! Now have message in bits 1:78 and CRC in bits 79:90.
! Group message bits into 15 6-bit symbols:
   do i=0,14
      write(c6,'(6i1)') mbits( (i*6+1):(i*6+6) )
      read(c6,'(b6.6)') message(i+1)
   enddo

! Encode to create a 65-symbol codeword
   call q65_encode(message,codeword)

!Shorten the codeword by omitting the CRC symbols (symbols 14 and 15)
   shortcodeword(1:13)=codeword(1:13)
   shortcodeword(14:63)=codeword(16:65)

!Insert sync symbols to create array of channel symbols
   j=1
   k=0
   do i=1,85
      if(i.eq.sync(j)) then
         j=j+1
         itone(i)=0
      else
         k=k+1
         itone(i)=shortcodeword(k)+1
      endif
   enddo
   success=.true.
end subroutine get_q65_tones

end module q65_encoding