altos: Move FEC code to core
[fw/altos] / src / core / ao_fec_tx.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao_fec.h>
19 #include <stdio.h>
20
21 void
22 ao_fec_dump_bytes(uint8_t *bytes, uint8_t len, char *name)
23 {
24         uint8_t i;
25
26         printf ("%s (%d):", name, len);
27         for (i = 0; i < len; i++) {
28                 if ((i & 7) == 0)
29                         printf ("\n\t%02x:", i);
30                 printf(" %02x", bytes[i]);
31         }
32         printf ("\n");
33 }
34
35 static uint16_t inline
36 crc_byte(uint8_t byte, uint16_t crc)
37 {
38         uint8_t bit;
39
40         for (bit = 0; bit < 8; bit++) {
41                 if (((crc & 0x8000) >> 8) ^ (byte & 0x80))
42                         crc = (crc << 1) ^ 0x8005;
43                 else
44                         crc = (crc << 1);
45                 byte <<= 1;
46         }
47         return crc;
48 }
49
50 uint16_t
51 ao_fec_crc(uint8_t *bytes, uint8_t len)
52 {
53         uint16_t        crc = AO_FEC_CRC_INIT;
54
55         while (len--)
56                 crc = crc_byte(*bytes++, crc);
57         return crc;
58 }
59
60 uint8_t
61 ao_fec_prepare(uint8_t *in, uint8_t len, uint8_t *out)
62 {
63         uint16_t        crc = ao_fec_crc (in, len);
64         uint8_t         i;
65         uint8_t         num_fec;
66
67         /* Copy data */
68         for (i = 0; i < len; i++)
69                 out[i] = in[i];
70
71         /* Append CRC */
72         out[i++] = crc >> 8;
73         out[i++] = crc;
74
75         /* Append FEC -- 1 byte if odd, two bytes if even */
76         num_fec = 2 - (i & 1);
77         while (num_fec--)
78                 out[i++] = AO_FEC_TRELLIS_TERMINATOR;
79         return i;
80 }
81
82 static const uint8_t whiten[] = {
83 #include "ao_whiten.h"
84 };
85
86 uint8_t
87 ao_fec_whiten(uint8_t *in, uint8_t len, uint8_t *out)
88 {
89         const uint8_t   *w = whiten;
90
91         while (len--)
92                 *out++ = *in++ ^ *w++;
93 }
94
95 static const uint8_t ao_fec_encode_table[16] = {
96 /* next 0  1      state */
97         0, 3,   /* 000 */
98         1, 2,   /* 001 */
99         3, 0,   /* 010 */
100         2, 1,   /* 011 */
101         3, 0,   /* 100 */
102         2, 1,   /* 101 */
103         0, 3,   /* 110 */
104         1, 2    /* 111 */
105 };
106
107 uint8_t
108 ao_fec_encode(uint8_t *in, uint8_t len, uint8_t *out)
109 {
110         uint16_t        fec = 0, output;
111         uint8_t         byte, bit;
112
113         for (byte = 0; byte < len; byte++) {
114                 fec = (fec & 0x700) | in[byte];
115                 output = 0;
116                 for (bit = 0; bit < 8; bit++) {
117                         output = output << 2 | ao_fec_encode_table[fec >> 7];
118                         fec = (fec << 1) & 0x7ff;
119                 }
120                 out[byte * 2] = output >> 8;
121                 out[byte * 2 + 1] = output;
122         }
123         return len * 2;
124 }
125
126 uint8_t
127 ao_fec_interleave(uint8_t *in, uint8_t len, uint8_t *out)
128 {
129         uint8_t i, j;
130
131         for (i = 0; i < len; i += 4) {
132                 uint32_t        interleaved = 0;
133
134                 for (j = 0; j < 4 * 4; j++) {
135                         interleaved <<= 2;
136                         interleaved |= (in[i + (~j & 0x3)] >> (2 * ((j & 0xc) >> 2))) & 0x03;
137                 }
138                 out[i+0] = interleaved >> 24;
139                 out[i+1] = interleaved >> 16;
140                 out[i+2] = interleaved >> 8;
141                 out[i+3] = interleaved;
142         }
143         return len;
144 }