blob: dff3ede016b878430801a670b9771cad8f9414d5 [file] [log] [blame]
piotrfaacc722014-07-20 23:48:32 +02001/* -*- c++ -*- */
2/*
3 * Copyright 2005 Free Software Foundation, Inc.
4 *
5 * This file is part of GNU Radio
6 *
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23#include "fire_crc.h"
24#include <stdio.h>
25#include <string.h>
26
27#define REM(x, y) (x) % (y)
28
29static int FC_syndrome_shift(FC_CTX *ctx, unsigned int bit);
30
31static int
32outit(int *data, int len)
33{
34 int i;
35
36 for (i = 0; i < len; i++)
37 printf("%d ", data[i]);
38 printf("\n");
ptrkrysik5ad7d632014-12-13 11:39:53 +010039 return 0;
piotrfaacc722014-07-20 23:48:32 +020040}
41
42int
43FC_init(FC_CTX *ctx, unsigned int crc_size, unsigned int data_size)
44{
45 ctx->crc_size = crc_size;
46 ctx->data_size = data_size;
47 ctx->syn_start = 0;
48
49 return 0;
50}
51
52int
53FC_check_crc(FC_CTX *ctx, unsigned char *input_bits, unsigned char *control_data)
54{
55 int j,error_count = 0, error_index = 0, success_flag = 0, syn_index = 0;
56 unsigned int i;
57
58 ctx->syn_start = 0;
59 // reset the syndrome register
60 memset(ctx->syndrome_reg, 0, sizeof ctx->syndrome_reg);
61
62 // shift in the data bits
63 for (i=0; i < ctx->data_size; i++) {
64 error_count = FC_syndrome_shift(ctx, input_bits[i]);
65 control_data[i] = input_bits[i];
66 }
67
68 // shift in the crc bits
69 for (i=0; i < ctx->crc_size; i++) {
70 error_count = FC_syndrome_shift(ctx, 1-input_bits[i+ctx->data_size]);
71 }
72
73 // Find position of error burst
74 if (error_count == 0) {
75 error_index = 0;
76 }
77 else {
78 error_index = 1;
79 error_count = FC_syndrome_shift(ctx, 0);
80 error_index += 1;
81 while (error_index < (ctx->data_size + ctx->crc_size) ) {
82 error_count = FC_syndrome_shift(ctx, 0);
83 error_index += 1;
84 if ( error_count == 0 ) break;
85 }
86 }
87
88 // Test for correctable errors
89 //printf("error_index %d\n",error_index);
90 if (error_index == 224) success_flag = 0;
91 else {
92
93 // correct index depending on the position of the error
94 if (error_index == 0) syn_index = error_index;
95 else syn_index = error_index - 1;
96
97 // error burst lies within data bits
98 if (error_index < 184) {
99 //printf("error < bit 184,%d\n",error_index);
100 j = error_index;
101 while ( j < (error_index+12) ) {
102 if (j < 184) {
103 control_data[j] = control_data[j] ^
104 ctx->syndrome_reg[REM(ctx->syn_start+39-j+syn_index,40)];
105 }
106 else break;
107 j = j + 1;
108 }
109 }
110 else if ( error_index > 212 ) {
111 //printf("error > bit 212,%d\n",error_index);
112 j = 0;
113 while ( j < (error_index - 212) ) {
114 control_data[j] = control_data[j] ^
115 ctx->syndrome_reg[REM(ctx->syn_start+39-j-224+syn_index,40)];
116 j = j + 1;
117 }
118 }
119 // for 183 < error_index < 213 error in parity alone so ignore
120 success_flag = 1;
121 }
122 return success_flag;
123}
124
125static int
126FC_syndrome_shift(FC_CTX *ctx, unsigned int bit)
127{
128 int error_count = 0;
129 unsigned int i;
130
131 if (ctx->syn_start == 0)
132 ctx->syn_start = 39;
133 else ctx->syn_start -= 1;
134
135 int temp_syndrome_reg[sizeof ctx->syndrome_reg];
136
137 memcpy(temp_syndrome_reg, ctx->syndrome_reg, sizeof temp_syndrome_reg);
138
139 temp_syndrome_reg[REM(ctx->syn_start+3,40)] =
140 ctx->syndrome_reg[REM(ctx->syn_start+3,40)] ^
141 ctx->syndrome_reg[ctx->syn_start];
142 temp_syndrome_reg[REM(ctx->syn_start+17,40)] =
143 ctx->syndrome_reg[REM(ctx->syn_start+17,40)] ^
144 ctx->syndrome_reg[ctx->syn_start];
145 temp_syndrome_reg[REM(ctx->syn_start+23,40)] =
146 ctx->syndrome_reg[REM(ctx->syn_start+23,40)] ^
147 ctx->syndrome_reg[ctx->syn_start];
148 temp_syndrome_reg[REM(ctx->syn_start+26,40)] =
149 ctx->syndrome_reg[REM(ctx->syn_start+26,40)] ^
150 ctx->syndrome_reg[ctx->syn_start];
151
152 temp_syndrome_reg[REM(ctx->syn_start+4,40)] =
153 ctx->syndrome_reg[REM(ctx->syn_start+4,40)] ^ bit;
154 temp_syndrome_reg[REM(ctx->syn_start+6,40)] =
155 ctx->syndrome_reg[REM(ctx->syn_start+6,40)] ^ bit;
156 temp_syndrome_reg[REM(ctx->syn_start+10,40)] =
157 ctx->syndrome_reg[REM(ctx->syn_start+10,40)] ^ bit;
158 temp_syndrome_reg[REM(ctx->syn_start+16,40)] =
159 ctx->syndrome_reg[REM(ctx->syn_start+16,40)] ^ bit;
160 temp_syndrome_reg[REM(ctx->syn_start+27,40)] =
161 ctx->syndrome_reg[REM(ctx->syn_start+27,40)] ^ bit;
162 temp_syndrome_reg[REM(ctx->syn_start+29,40)] =
163 ctx->syndrome_reg[REM(ctx->syn_start+29,40)] ^ bit;
164 temp_syndrome_reg[REM(ctx->syn_start+33,40)] =
165 ctx->syndrome_reg[REM(ctx->syn_start+33,40)] ^ bit;
166 temp_syndrome_reg[REM(ctx->syn_start+39,40)] =
167 ctx->syndrome_reg[REM(ctx->syn_start+39,40)] ^ bit;
168
169 temp_syndrome_reg[ctx->syn_start] = ctx->syndrome_reg[ctx->syn_start] ^ bit;
170
171 memcpy(ctx->syndrome_reg, temp_syndrome_reg, sizeof ctx->syndrome_reg);
172
173 for (i = 0; i < 28; i++) {
174 error_count = error_count + ctx->syndrome_reg[REM(ctx->syn_start+i,40)];
175 }
176 return error_count;
177}
178
179