-
Notifications
You must be signed in to change notification settings - Fork 55
/
cbptc19696.cpp
370 lines (317 loc) · 10.4 KB
/
cbptc19696.cpp
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Copyright (C) 2012 by Ian Wraith
* Copyright (C) 2015 by Jonathan Naylor G4KLX
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "cbptc19696.h"
#include "chamming.h"
//#include "cutils.h"
#include <cstdio>
#include <cassert>
#include <cstring>
CBPTC19696::CBPTC19696()
{
}
CBPTC19696::~CBPTC19696()
{
}
// The main decode function
void CBPTC19696::decode(const uint8_t* in, uint8_t* out)
{
assert(in != NULL);
assert(out != NULL);
// Get the raw binary
decodeExtractBinary(in);
// Deinterleave
decodeDeInterleave();
// Error check
decodeErrorCheck();
// Extract Data
decodeExtractData(out);
}
// The main encode function
void CBPTC19696::encode(const uint8_t* in, uint8_t* out)
{
assert(in != NULL);
assert(out != NULL);
// Extract Data
encodeExtractData(in);
// Error check
encodeErrorCheck();
// Deinterleave
encodeInterleave();
// Get the raw binary
encodeExtractBinary(out);
}
void CBPTC19696::byteToBitsBE(uint8_t byte, bool* bits)
{
assert(bits != NULL);
bits[0U] = (byte & 0x80U) == 0x80U;
bits[1U] = (byte & 0x40U) == 0x40U;
bits[2U] = (byte & 0x20U) == 0x20U;
bits[3U] = (byte & 0x10U) == 0x10U;
bits[4U] = (byte & 0x08U) == 0x08U;
bits[5U] = (byte & 0x04U) == 0x04U;
bits[6U] = (byte & 0x02U) == 0x02U;
bits[7U] = (byte & 0x01U) == 0x01U;
}
void CBPTC19696::bitsToByteBE(bool* bits, uint8_t& byte)
{
assert(bits != NULL);
byte = bits[0U] ? 0x80U : 0x00U;
byte |= bits[1U] ? 0x40U : 0x00U;
byte |= bits[2U] ? 0x20U : 0x00U;
byte |= bits[3U] ? 0x10U : 0x00U;
byte |= bits[4U] ? 0x08U : 0x00U;
byte |= bits[5U] ? 0x04U : 0x00U;
byte |= bits[6U] ? 0x02U : 0x00U;
byte |= bits[7U] ? 0x01U : 0x00U;
}
void CBPTC19696::decodeExtractBinary(const uint8_t* in)
{
// First block
byteToBitsBE(in[0U], m_rawData + 0U);
byteToBitsBE(in[1U], m_rawData + 8U);
byteToBitsBE(in[2U], m_rawData + 16U);
byteToBitsBE(in[3U], m_rawData + 24U);
byteToBitsBE(in[4U], m_rawData + 32U);
byteToBitsBE(in[5U], m_rawData + 40U);
byteToBitsBE(in[6U], m_rawData + 48U);
byteToBitsBE(in[7U], m_rawData + 56U);
byteToBitsBE(in[8U], m_rawData + 64U);
byteToBitsBE(in[9U], m_rawData + 72U);
byteToBitsBE(in[10U], m_rawData + 80U);
byteToBitsBE(in[11U], m_rawData + 88U);
byteToBitsBE(in[12U], m_rawData + 96U);
// Handle the two bits
bool bits[8U];
byteToBitsBE(in[20U], bits);
m_rawData[98U] = bits[6U];
m_rawData[99U] = bits[7U];
// Second block
byteToBitsBE(in[21U], m_rawData + 100U);
byteToBitsBE(in[22U], m_rawData + 108U);
byteToBitsBE(in[23U], m_rawData + 116U);
byteToBitsBE(in[24U], m_rawData + 124U);
byteToBitsBE(in[25U], m_rawData + 132U);
byteToBitsBE(in[26U], m_rawData + 140U);
byteToBitsBE(in[27U], m_rawData + 148U);
byteToBitsBE(in[28U], m_rawData + 156U);
byteToBitsBE(in[29U], m_rawData + 164U);
byteToBitsBE(in[30U], m_rawData + 172U);
byteToBitsBE(in[31U], m_rawData + 180U);
byteToBitsBE(in[32U], m_rawData + 188U);
}
// Deinterleave the raw data
void CBPTC19696::decodeDeInterleave()
{
for (uint32_t i = 0U; i < 196U; i++)
m_deInterData[i] = false;
// The first bit is R(3) which is not used so can be ignored
for (uint32_t a = 0U; a < 196U; a++) {
// Calculate the interleave sequence
uint32_t interleaveSequence = (a * 181U) % 196U;
// Shuffle the data
m_deInterData[a] = m_rawData[interleaveSequence];
}
}
// Check each row with a Hamming (15,11,3) code and each column with a Hamming (13,9,3) code
void CBPTC19696::decodeErrorCheck()
{
bool fixing;
uint32_t count = 0U;
do {
fixing = false;
// Run through each of the 15 columns
bool col[13U];
for (uint32_t c = 0U; c < 15U; c++) {
uint32_t pos = c + 1U;
for (uint32_t a = 0U; a < 13U; a++) {
col[a] = m_deInterData[pos];
pos = pos + 15U;
}
if (CHamming::decode1393(col)) {
uint32_t pos = c + 1U;
for (uint32_t a = 0U; a < 13U; a++) {
m_deInterData[pos] = col[a];
pos = pos + 15U;
}
fixing = true;
}
}
// Run through each of the 9 rows containing data
for (uint32_t r = 0U; r < 9U; r++) {
uint32_t pos = (r * 15U) + 1U;
if (CHamming::decode15113_2(m_deInterData + pos))
fixing = true;
}
count++;
} while (fixing && count < 5U);
}
// Extract the 96 bits of payload
void CBPTC19696::decodeExtractData(uint8_t* data)
{
bool bData[96U];
uint32_t pos = 0U;
for(uint32_t a = 4U; a <= 11U; a++, pos++){
bData[pos] = m_deInterData[a];
}
for(uint32_t a = 16U; a <= 26U; a++, pos++){
bData[pos] = m_deInterData[a];
}
for(uint32_t a = 31U; a <= 41U; a++, pos++){
bData[pos] = m_deInterData[a];
}
for(uint32_t a = 46U; a <= 56U; a++, pos++){
bData[pos] = m_deInterData[a];
}
for(uint32_t a = 61U; a <= 71U; a++, pos++){
bData[pos] = m_deInterData[a];
}
for(uint32_t a = 76U; a <= 86U; a++, pos++){
bData[pos] = m_deInterData[a];
}
for(uint32_t a = 91U; a <= 101U; a++, pos++){
bData[pos] = m_deInterData[a];
}
for(uint32_t a = 106U; a <= 116U; a++, pos++){
bData[pos] = m_deInterData[a];
}
for(uint32_t a = 121U; a <= 131U; a++, pos++){
bData[pos] = m_deInterData[a];
}
bitsToByteBE(bData + 0U, data[0U]);
bitsToByteBE(bData + 8U, data[1U]);
bitsToByteBE(bData + 16U, data[2U]);
bitsToByteBE(bData + 24U, data[3U]);
bitsToByteBE(bData + 32U, data[4U]);
bitsToByteBE(bData + 40U, data[5U]);
bitsToByteBE(bData + 48U, data[6U]);
bitsToByteBE(bData + 56U, data[7U]);
bitsToByteBE(bData + 64U, data[8U]);
bitsToByteBE(bData + 72U, data[9U]);
bitsToByteBE(bData + 80U, data[10U]);
bitsToByteBE(bData + 88U, data[11U]);
}
// Extract the 96 bits of payload
void CBPTC19696::encodeExtractData(const uint8_t* in)
{
bool bData[96U];
byteToBitsBE(in[0U], bData + 0U);
byteToBitsBE(in[1U], bData + 8U);
byteToBitsBE(in[2U], bData + 16U);
byteToBitsBE(in[3U], bData + 24U);
byteToBitsBE(in[4U], bData + 32U);
byteToBitsBE(in[5U], bData + 40U);
byteToBitsBE(in[6U], bData + 48U);
byteToBitsBE(in[7U], bData + 56U);
byteToBitsBE(in[8U], bData + 64U);
byteToBitsBE(in[9U], bData + 72U);
byteToBitsBE(in[10U], bData + 80U);
byteToBitsBE(in[11U], bData + 88U);
for (uint32_t i = 0U; i < 196U; i++)
m_deInterData[i] = false;
uint32_t pos = 0U;
for (uint32_t a = 4U; a <= 11U; a++, pos++)
m_deInterData[a] = bData[pos];
for (uint32_t a = 16U; a <= 26U; a++, pos++)
m_deInterData[a] = bData[pos];
for (uint32_t a = 31U; a <= 41U; a++, pos++)
m_deInterData[a] = bData[pos];
for (uint32_t a = 46U; a <= 56U; a++, pos++)
m_deInterData[a] = bData[pos];
for (uint32_t a = 61U; a <= 71U; a++, pos++)
m_deInterData[a] = bData[pos];
for (uint32_t a = 76U; a <= 86U; a++, pos++)
m_deInterData[a] = bData[pos];
for (uint32_t a = 91U; a <= 101U; a++, pos++)
m_deInterData[a] = bData[pos];
for (uint32_t a = 106U; a <= 116U; a++, pos++)
m_deInterData[a] = bData[pos];
for (uint32_t a = 121U; a <= 131U; a++, pos++)
m_deInterData[a] = bData[pos];
}
// Check each row with a Hamming (15,11,3) code and each column with a Hamming (13,9,3) code
void CBPTC19696::encodeErrorCheck()
{
// Run through each of the 9 rows containing data
for (uint32_t r = 0U; r < 9U; r++) {
uint32_t pos = (r * 15U) + 1U;
CHamming::encode15113_2(m_deInterData + pos);
}
// Run through each of the 15 columns
bool col[13U];
for (uint32_t c = 0U; c < 15U; c++) {
uint32_t pos = c + 1U;
for (uint32_t a = 0U; a < 13U; a++) {
col[a] = m_deInterData[pos];
pos = pos + 15U;
}
CHamming::encode1393(col);
pos = c + 1U;
for (uint32_t a = 0U; a < 13U; a++) {
m_deInterData[pos] = col[a];
pos = pos + 15U;
}
}
}
// Interleave the raw data
void CBPTC19696::encodeInterleave()
{
for (uint32_t i = 0U; i < 196U; i++)
m_rawData[i] = false;
// The first bit is R(3) which is not used so can be ignored
for (uint32_t a = 0U; a < 196U; a++) {
// Calculate the interleave sequence
uint32_t interleaveSequence = (a * 181U) % 196U;
// Unshuffle the data
m_rawData[interleaveSequence] = m_deInterData[a];
}
}
void CBPTC19696::encodeExtractBinary(uint8_t* data)
{
// First block
bitsToByteBE(m_rawData + 0U, data[0U]);
bitsToByteBE(m_rawData + 8U, data[1U]);
bitsToByteBE(m_rawData + 16U, data[2U]);
bitsToByteBE(m_rawData + 24U, data[3U]);
bitsToByteBE(m_rawData + 32U, data[4U]);
bitsToByteBE(m_rawData + 40U, data[5U]);
bitsToByteBE(m_rawData + 48U, data[6U]);
bitsToByteBE(m_rawData + 56U, data[7U]);
bitsToByteBE(m_rawData + 64U, data[8U]);
bitsToByteBE(m_rawData + 72U, data[9U]);
bitsToByteBE(m_rawData + 80U, data[10U]);
bitsToByteBE(m_rawData + 88U, data[11U]);
// Handle the two bits
uint8_t byte;
bitsToByteBE(m_rawData + 96U, byte);
data[12U] = (data[12U] & 0x3FU) | ((byte >> 0) & 0xC0U);
data[20U] = (data[20U] & 0xFCU) | ((byte >> 4) & 0x03U);
// Second block
bitsToByteBE(m_rawData + 100U, data[21U]);
bitsToByteBE(m_rawData + 108U, data[22U]);
bitsToByteBE(m_rawData + 116U, data[23U]);
bitsToByteBE(m_rawData + 124U, data[24U]);
bitsToByteBE(m_rawData + 132U, data[25U]);
bitsToByteBE(m_rawData + 140U, data[26U]);
bitsToByteBE(m_rawData + 148U, data[27U]);
bitsToByteBE(m_rawData + 156U, data[28U]);
bitsToByteBE(m_rawData + 164U, data[29U]);
bitsToByteBE(m_rawData + 172U, data[30U]);
bitsToByteBE(m_rawData + 180U, data[31U]);
bitsToByteBE(m_rawData + 188U, data[32U]);
}