1 /*****************************************************************************/
2 
3 /*
4  *	sm_fsk9600.c  --  soundcard radio modem driver,
5  *                        9600 baud G3RUH compatible FSK modem
6  *
7  *	Copyright (C) 1996  Thomas Sailer (sailer@ife.ee.ethz.ch)
8  *
9  *	This program is free software; you can redistribute it and/or modify
10  *	it under the terms of the GNU General Public License as published by
11  *	the Free Software Foundation; either version 2 of the License, or
12  *	(at your option) any later version.
13  *
14  *	This program is distributed in the hope that it will be useful,
15  *	but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *	GNU General Public License for more details.
18  *
19  *	You should have received a copy of the GNU General Public License
20  *	along with this program; if not, write to the Free Software
21  *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  *  Please note that the GPL allows you to use the driver, NOT the radio.
24  *  In order to use the radio, you need a license from the communications
25  *  authority of your country.
26  *
27  */
28 
29 #include "sm.h"
30 #include "sm_tbl_fsk9600.h"
31 
32 /* --------------------------------------------------------------------- */
33 
34 struct demod_state_fsk96 {
35 	unsigned int shreg;
36 	unsigned long descram;
37 	unsigned int bit_pll;
38 	unsigned char last_sample;
39 	unsigned int dcd_shreg;
40 	int dcd_sum0, dcd_sum1, dcd_sum2;
41 	unsigned int dcd_time;
42 };
43 
44 struct mod_state_fsk96 {
45 	unsigned int shreg;
46 	unsigned long scram;
47 	unsigned char tx_bit;
48 	unsigned char *txtbl;
49 	unsigned int txphase;
50 };
51 
52 /* --------------------------------------------------------------------- */
53 
54 #define DESCRAM_TAP1 0x20000
55 #define DESCRAM_TAP2 0x01000
56 #define DESCRAM_TAP3 0x00001
57 
58 #define DESCRAM_TAPSH1 17
59 #define DESCRAM_TAPSH2 12
60 #define DESCRAM_TAPSH3 0
61 
62 #define SCRAM_TAP1 0x20000 /* X^17 */
63 #define SCRAM_TAPN 0x00021 /* X^0+X^5 */
64 
65 /* --------------------------------------------------------------------- */
66 
modulator_9600_4_u8(struct sm_state * sm,unsigned char * buf,unsigned int buflen)67 static void modulator_9600_4_u8(struct sm_state *sm, unsigned char *buf, unsigned int buflen)
68 {
69 	struct mod_state_fsk96 *st = (struct mod_state_fsk96 *)(&sm->m);
70 
71 	for (; buflen > 0; buflen--) {
72 		if (!st->txphase++) {
73 			if (st->shreg <= 1)
74 				st->shreg = hdlcdrv_getbits(&sm->hdrv) | 0x10000;
75 			st->scram = (st->scram << 1) | (st->scram & 1);
76 			st->scram ^= !(st->shreg & 1);
77 			st->shreg >>= 1;
78 			if (st->scram & (SCRAM_TAP1 << 1))
79 				st->scram ^= SCRAM_TAPN << 1;
80 			st->tx_bit = (st->tx_bit << 1) | (!!(st->scram & (SCRAM_TAP1 << 2)));
81 			st->txtbl = fsk96_txfilt_4 + (st->tx_bit & 0xff);
82 		}
83 		if (st->txphase >= 4)
84 			st->txphase = 0;
85 		*buf++ = *st->txtbl;
86 		st->txtbl += 0x100;
87 	}
88 }
89 
90 /* --------------------------------------------------------------------- */
91 
modulator_9600_4_s16(struct sm_state * sm,short * buf,unsigned int buflen)92 static void modulator_9600_4_s16(struct sm_state *sm, short *buf, unsigned int buflen)
93 {
94 	struct mod_state_fsk96 *st = (struct mod_state_fsk96 *)(&sm->m);
95 
96 	for (; buflen > 0; buflen--) {
97 		if (!st->txphase++) {
98 			if (st->shreg <= 1)
99 				st->shreg = hdlcdrv_getbits(&sm->hdrv) | 0x10000;
100 			st->scram = (st->scram << 1) | (st->scram & 1);
101 			st->scram ^= !(st->shreg & 1);
102 			st->shreg >>= 1;
103 			if (st->scram & (SCRAM_TAP1 << 1))
104 				st->scram ^= SCRAM_TAPN << 1;
105 			st->tx_bit = (st->tx_bit << 1) | (!!(st->scram & (SCRAM_TAP1 << 2)));
106 			st->txtbl = fsk96_txfilt_4 + (st->tx_bit & 0xff);
107 		}
108 		if (st->txphase >= 4)
109 			st->txphase = 0;
110 		*buf++ = ((*st->txtbl)-0x80) << 8;
111 		st->txtbl += 0x100;
112 	}
113 }
114 
115 /* --------------------------------------------------------------------- */
116 
demodulator_9600_4_u8(struct sm_state * sm,const unsigned char * buf,unsigned int buflen)117 static void demodulator_9600_4_u8(struct sm_state *sm, const unsigned char *buf, unsigned int buflen)
118 {
119 	struct demod_state_fsk96 *st = (struct demod_state_fsk96 *)(&sm->d);
120 	static const int pll_corr[2] = { -0x1000, 0x1000 };
121 	unsigned char curbit;
122 	unsigned int descx;
123 
124 	for (; buflen > 0; buflen--, buf++) {
125 		st->dcd_shreg <<= 1;
126 		st->bit_pll += 0x4000;
127 		curbit = (*buf >= 0x80);
128 		if (st->last_sample ^ curbit) {
129 			st->dcd_shreg |= 1;
130 			st->bit_pll += pll_corr[st->bit_pll < 0xa000];
131 			st->dcd_sum0 += 8 * hweight8(st->dcd_shreg & 0x0c) -
132 				!!(st->dcd_shreg & 0x10);
133 		}
134 		st->last_sample = curbit;
135 		hdlcdrv_channelbit(&sm->hdrv, st->last_sample);
136 		if ((--st->dcd_time) <= 0) {
137 			hdlcdrv_setdcd(&sm->hdrv, (st->dcd_sum0 +
138 						   st->dcd_sum1 +
139 						   st->dcd_sum2) < 0);
140 			st->dcd_sum2 = st->dcd_sum1;
141 			st->dcd_sum1 = st->dcd_sum0;
142 			st->dcd_sum0 = 2; /* slight bias */
143 			st->dcd_time = 240;
144 		}
145 		if (st->bit_pll >= 0x10000) {
146 			st->bit_pll &= 0xffff;
147 			st->descram = (st->descram << 1) | curbit;
148 			descx = st->descram ^ (st->descram >> 1);
149 			descx ^= ((descx >> DESCRAM_TAPSH1) ^
150 				  (descx >> DESCRAM_TAPSH2));
151 			st->shreg >>= 1;
152 			st->shreg |= (!(descx & 1)) << 16;
153 			if (st->shreg & 1) {
154 				hdlcdrv_putbits(&sm->hdrv, st->shreg >> 1);
155 				st->shreg = 0x10000;
156 			}
157 			diag_trigger(sm);
158 		}
159 		diag_add_one(sm, ((short)(*buf - 0x80)) << 8);
160 	}
161 }
162 
163 /* --------------------------------------------------------------------- */
164 
demodulator_9600_4_s16(struct sm_state * sm,const short * buf,unsigned int buflen)165 static void demodulator_9600_4_s16(struct sm_state *sm, const short *buf, unsigned int buflen)
166 {
167 	struct demod_state_fsk96 *st = (struct demod_state_fsk96 *)(&sm->d);
168 	static const int pll_corr[2] = { -0x1000, 0x1000 };
169 	unsigned char curbit;
170 	unsigned int descx;
171 
172 	for (; buflen > 0; buflen--, buf++) {
173 		st->dcd_shreg <<= 1;
174 		st->bit_pll += 0x4000;
175 		curbit = (*buf >= 0);
176 		if (st->last_sample ^ curbit) {
177 			st->dcd_shreg |= 1;
178 			st->bit_pll += pll_corr[st->bit_pll < 0xa000];
179 			st->dcd_sum0 += 8 * hweight8(st->dcd_shreg & 0x0c) -
180 				!!(st->dcd_shreg & 0x10);
181 		}
182 		st->last_sample = curbit;
183 		hdlcdrv_channelbit(&sm->hdrv, st->last_sample);
184 		if ((--st->dcd_time) <= 0) {
185 			hdlcdrv_setdcd(&sm->hdrv, (st->dcd_sum0 +
186 						   st->dcd_sum1 +
187 						   st->dcd_sum2) < 0);
188 			st->dcd_sum2 = st->dcd_sum1;
189 			st->dcd_sum1 = st->dcd_sum0;
190 			st->dcd_sum0 = 2; /* slight bias */
191 			st->dcd_time = 240;
192 		}
193 		if (st->bit_pll >= 0x10000) {
194 			st->bit_pll &= 0xffff;
195 			st->descram = (st->descram << 1) | curbit;
196 			descx = st->descram ^ (st->descram >> 1);
197 			descx ^= ((descx >> DESCRAM_TAPSH1) ^
198 				  (descx >> DESCRAM_TAPSH2));
199 			st->shreg >>= 1;
200 			st->shreg |= (!(descx & 1)) << 16;
201 			if (st->shreg & 1) {
202 				hdlcdrv_putbits(&sm->hdrv, st->shreg >> 1);
203 				st->shreg = 0x10000;
204 			}
205 			diag_trigger(sm);
206 		}
207 		diag_add_one(sm, *buf);
208 	}
209 }
210 
211 /* --------------------------------------------------------------------- */
212 
modulator_9600_5_u8(struct sm_state * sm,unsigned char * buf,unsigned int buflen)213 static void modulator_9600_5_u8(struct sm_state *sm, unsigned char *buf, unsigned int buflen)
214 {
215 	struct mod_state_fsk96 *st = (struct mod_state_fsk96 *)(&sm->m);
216 
217 	for (; buflen > 0; buflen--) {
218 		if (!st->txphase++) {
219 			if (st->shreg <= 1)
220 				st->shreg = hdlcdrv_getbits(&sm->hdrv) | 0x10000;
221 			st->scram = (st->scram << 1) | (st->scram & 1);
222 			st->scram ^= !(st->shreg & 1);
223 			st->shreg >>= 1;
224 			if (st->scram & (SCRAM_TAP1 << 1))
225 				st->scram ^= SCRAM_TAPN << 1;
226 			st->tx_bit = (st->tx_bit << 1) | (!!(st->scram & (SCRAM_TAP1 << 2)));
227 			st->txtbl = fsk96_txfilt_5 + (st->tx_bit & 0xff);
228 		}
229 		if (st->txphase >= 5)
230 			st->txphase = 0;
231 		*buf++ = *st->txtbl;
232 		st->txtbl += 0x100;
233 	}
234 }
235 
236 /* --------------------------------------------------------------------- */
237 
modulator_9600_5_s16(struct sm_state * sm,short * buf,unsigned int buflen)238 static void modulator_9600_5_s16(struct sm_state *sm, short *buf, unsigned int buflen)
239 {
240 	struct mod_state_fsk96 *st = (struct mod_state_fsk96 *)(&sm->m);
241 
242 	for (; buflen > 0; buflen--) {
243 		if (!st->txphase++) {
244 			if (st->shreg <= 1)
245 				st->shreg = hdlcdrv_getbits(&sm->hdrv) | 0x10000;
246 			st->scram = (st->scram << 1) | (st->scram & 1);
247 			st->scram ^= !(st->shreg & 1);
248 			st->shreg >>= 1;
249 			if (st->scram & (SCRAM_TAP1 << 1))
250 				st->scram ^= SCRAM_TAPN << 1;
251 			st->tx_bit = (st->tx_bit << 1) | (!!(st->scram & (SCRAM_TAP1 << 2)));
252 			st->txtbl = fsk96_txfilt_5 + (st->tx_bit & 0xff);
253 		}
254 		if (st->txphase >= 5)
255 			st->txphase = 0;
256 		*buf++ = ((*st->txtbl)-0x80)<<8;
257 		st->txtbl += 0x100;
258 	}
259 }
260 
261 /* --------------------------------------------------------------------- */
262 
demodulator_9600_5_u8(struct sm_state * sm,const unsigned char * buf,unsigned int buflen)263 static void demodulator_9600_5_u8(struct sm_state *sm, const unsigned char *buf, unsigned int buflen)
264 {
265 	struct demod_state_fsk96 *st = (struct demod_state_fsk96 *)(&sm->d);
266 	static const int pll_corr[2] = { -0x1000, 0x1000 };
267 	unsigned char curbit;
268 	unsigned int descx;
269 
270 	for (; buflen > 0; buflen--, buf++) {
271 		st->dcd_shreg <<= 1;
272 		st->bit_pll += 0x3333;
273 		curbit = (*buf >= 0x80);
274 		if (st->last_sample ^ curbit) {
275 			st->dcd_shreg |= 1;
276 			st->bit_pll += pll_corr[st->bit_pll < 0x9999];
277 			st->dcd_sum0 += 16 * hweight8(st->dcd_shreg & 0x0c) -
278 				hweight8(st->dcd_shreg & 0x70);
279 		}
280 		st->last_sample = curbit;
281 		hdlcdrv_channelbit(&sm->hdrv, st->last_sample);
282 		if ((--st->dcd_time) <= 0) {
283 			hdlcdrv_setdcd(&sm->hdrv, (st->dcd_sum0 +
284 						   st->dcd_sum1 +
285 						   st->dcd_sum2) < 0);
286 			st->dcd_sum2 = st->dcd_sum1;
287 			st->dcd_sum1 = st->dcd_sum0;
288 			st->dcd_sum0 = 2; /* slight bias */
289 			st->dcd_time = 240;
290 		}
291 		if (st->bit_pll >= 0x10000) {
292 			st->bit_pll &= 0xffff;
293 			st->descram = (st->descram << 1) | curbit;
294 			descx = st->descram ^ (st->descram >> 1);
295 			descx ^= ((descx >> DESCRAM_TAPSH1) ^
296 				  (descx >> DESCRAM_TAPSH2));
297 			st->shreg >>= 1;
298 			st->shreg |= (!(descx & 1)) << 16;
299 			if (st->shreg & 1) {
300 				hdlcdrv_putbits(&sm->hdrv, st->shreg >> 1);
301 				st->shreg = 0x10000;
302 			}
303 			diag_trigger(sm);
304 		}
305 		diag_add_one(sm, ((short)(*buf - 0x80)) << 8);
306 	}
307 }
308 
309 /* --------------------------------------------------------------------- */
310 
demodulator_9600_5_s16(struct sm_state * sm,const short * buf,unsigned int buflen)311 static void demodulator_9600_5_s16(struct sm_state *sm, const short *buf, unsigned int buflen)
312 {
313 	struct demod_state_fsk96 *st = (struct demod_state_fsk96 *)(&sm->d);
314 	static const int pll_corr[2] = { -0x1000, 0x1000 };
315 	unsigned char curbit;
316 	unsigned int descx;
317 
318 	for (; buflen > 0; buflen--, buf++) {
319 		st->dcd_shreg <<= 1;
320 		st->bit_pll += 0x3333;
321 		curbit = (*buf >= 0);
322 		if (st->last_sample ^ curbit) {
323 			st->dcd_shreg |= 1;
324 			st->bit_pll += pll_corr[st->bit_pll < 0x9999];
325 			st->dcd_sum0 += 16 * hweight8(st->dcd_shreg & 0x0c) -
326 				hweight8(st->dcd_shreg & 0x70);
327 		}
328 		st->last_sample = curbit;
329 		hdlcdrv_channelbit(&sm->hdrv, st->last_sample);
330 		if ((--st->dcd_time) <= 0) {
331 			hdlcdrv_setdcd(&sm->hdrv, (st->dcd_sum0 +
332 						   st->dcd_sum1 +
333 						   st->dcd_sum2) < 0);
334 			st->dcd_sum2 = st->dcd_sum1;
335 			st->dcd_sum1 = st->dcd_sum0;
336 			st->dcd_sum0 = 2; /* slight bias */
337 			st->dcd_time = 240;
338 		}
339 		if (st->bit_pll >= 0x10000) {
340 			st->bit_pll &= 0xffff;
341 			st->descram = (st->descram << 1) | curbit;
342 			descx = st->descram ^ (st->descram >> 1);
343 			descx ^= ((descx >> DESCRAM_TAPSH1) ^
344 				  (descx >> DESCRAM_TAPSH2));
345 			st->shreg >>= 1;
346 			st->shreg |= (!(descx & 1)) << 16;
347 			if (st->shreg & 1) {
348 				hdlcdrv_putbits(&sm->hdrv, st->shreg >> 1);
349 				st->shreg = 0x10000;
350 			}
351 			diag_trigger(sm);
352 		}
353 		diag_add_one(sm, *buf);
354 	}
355 }
356 
357 /* --------------------------------------------------------------------- */
358 
demod_init_9600(struct sm_state * sm)359 static void demod_init_9600(struct sm_state *sm)
360 {
361 	struct demod_state_fsk96 *st = (struct demod_state_fsk96 *)(&sm->d);
362 
363 	st->dcd_time = 240;
364 	st->dcd_sum0 = 2;
365 }
366 
367 /* --------------------------------------------------------------------- */
368 
369 const struct modem_tx_info sm_fsk9600_4_tx = {
370 	"fsk9600", sizeof(struct mod_state_fsk96), 38400, 9600,
371 	modulator_9600_4_u8, modulator_9600_4_s16, NULL
372 };
373 
374 const struct modem_rx_info sm_fsk9600_4_rx = {
375 	"fsk9600", sizeof(struct demod_state_fsk96), 38400, 9600, 1, 4,
376 	demodulator_9600_4_u8, demodulator_9600_4_s16, demod_init_9600
377 };
378 
379 /* --------------------------------------------------------------------- */
380 
381 const struct modem_tx_info sm_fsk9600_5_tx = {
382 	"fsk9600", sizeof(struct mod_state_fsk96), 48000, 9600,
383 	modulator_9600_5_u8, modulator_9600_5_s16, NULL
384 };
385 
386 const struct modem_rx_info sm_fsk9600_5_rx = {
387 	"fsk9600", sizeof(struct demod_state_fsk96), 48000, 9600, 1, 5,
388 	demodulator_9600_5_u8, demodulator_9600_5_s16, demod_init_9600
389 };
390 
391 /* --------------------------------------------------------------------- */
392