Skip to content

Commit 5be9e44

Browse files
committed
Closer. Lots of ACCA data to chew on.
1 parent a707fc8 commit 5be9e44

File tree

4 files changed

+146
-12
lines changed

4 files changed

+146
-12
lines changed

Diff for: src/bch.c

+65-11
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@
7575
#include <string.h>
7676
#include "bch.h"
7777

78-
int bch_init(bch_t *bch, int m, int length, int t) {
78+
#undef DEBUG
79+
80+
int init_bch(bch_t *bch, int m, int length, int t) {
7981

8082
int p[21], n;
8183

@@ -115,14 +117,20 @@ int bch_init(bch_t *bch, int m, int length, int t) {
115117
else if (m == 18) p[7] = 1;
116118
else if (m == 19) p[1] = p[5] = p[6] = 1;
117119
else if (m == 20) p[3] = 1;
118-
printf("p(x) = ");
119120

120121
n = 1;
122+
#ifdef DEBUG
123+
printf("p(x) = ");
124+
#endif
121125
for (int i = 0; i <= m; i++) {
122126
n *= 2;
127+
#ifdef DEBUG
123128
printf("%1d", p[i]);
129+
#endif
124130
}
131+
#ifdef DEBUG
125132
printf("\n");
133+
#endif
126134
n = n / 2 - 1;
127135
bch->n = n;
128136
int ninf = (n + 1) / 2 - 1;
@@ -184,10 +192,12 @@ int bch_init(bch_t *bch, int m, int length, int t) {
184192
cycle[1][0] = 1;
185193
size[1] = 1;
186194
jj = 1; /* cycle set index */
195+
#ifdef DEBUG
187196
if (bch->m > 9) {
188197
printf("Computing cycle sets modulo %d\n", bch->n);
189198
printf("(This may take some time)...\n");
190199
}
200+
#endif
191201
do {
192202
/* Generate the jj-th cycle set */
193203
ii = 0;
@@ -247,11 +257,12 @@ int bch_init(bch_t *bch, int m, int length, int t) {
247257

248258
if (bch->k<0)
249259
{
250-
printf("Parameters invalid!\n");
251260
return -4;
252261
}
253262

263+
#ifdef DEBUG
254264
printf("This is a (%d, %d, %d) binary BCH code\n", bch->length, bch->k, d);
265+
#endif
255266

256267
/* Compute the generator polynomial */
257268
bch->g = malloc((rdncy + 1) * sizeof(int));
@@ -266,12 +277,13 @@ int bch_init(bch_t *bch, int m, int length, int t) {
266277
bch->g[jj] = bch->g[jj - 1];
267278
bch->g[0] = bch->alpha_to[(bch->index_of[bch->g[0]] + zeros[ii]) % bch->n];
268279
}
280+
#ifdef DEBUG
269281
printf("Generator polynomial:\ng(x) = ");
270282
for (ii = 0; ii <= rdncy; ii++) {
271283
printf("%d", bch->g[ii]);
272284
}
273285
printf("\n");
274-
286+
#endif
275287
return 0;
276288
}
277289

@@ -303,8 +315,7 @@ void generate_bch(bch_t *bch, int *data, int *bb) {
303315
}
304316

305317

306-
int
307-
apply_bch(bch_t *bch, int *recd)
318+
int apply_bch(bch_t *bch, int *recd)
308319
/*
309320
* Simon Rockliff's implementation of Berlekamp's algorithm.
310321
*
@@ -335,7 +346,9 @@ apply_bch(bch_t *bch, int *recd)
335346
t2 = 2 * bch->t;
336347

337348
/* first form the syndromes */
349+
#ifdef DEBUG
338350
printf("S(x) = ");
351+
#endif
339352
for (i = 1; i <= t2; i++) {
340353
s[i] = 0;
341354
for (j = 0; j < bch->length; j++)
@@ -349,9 +362,13 @@ apply_bch(bch_t *bch, int *recd)
349362
*/
350363
/* convert syndrome from polynomial form to index form */
351364
s[i] = bch->index_of[s[i]];
365+
#ifdef DEBUG
352366
printf("%3d ", s[i]);
367+
#endif
353368
}
369+
#ifdef DEBUG
354370
printf("\n");
371+
#endif
355372

356373
if (syn_error) { /* if there are errors, try to correct them */
357374
/*
@@ -451,12 +468,13 @@ apply_bch(bch_t *bch, int *recd)
451468
for (i = 0; i <= l[u]; i++)
452469
elp[u][i] = bch->index_of[elp[u][i]];
453470

471+
#ifdef DEBUG
454472
printf("sigma(x) = ");
455473
for (i = 0; i <= l[u]; i++)
456474
printf("%3d ", elp[u][i]);
457475
printf("\n");
458476
printf("Roots: ");
459-
477+
#endif
460478
/* Chien search: find roots of the error location polynomial */
461479
for (i = 1; i <= l[u]; i++)
462480
reg[i] = elp[u][i];
@@ -473,18 +491,24 @@ apply_bch(bch_t *bch, int *recd)
473491
root[count] = i;
474492
loc[count] = bch->n - i;
475493
count++;
494+
#ifdef DEBUG
476495
printf("%3d ", bch->n - i);
496+
#endif
477497
}
478498
}
499+
#ifdef DEBUG
479500
printf("\n");
501+
#endif
480502
if (count == l[u]) {
481503
/* no. roots = degree of elp hence <= t errors */
482504
for (i = 0; i < l[u]; i++)
483505
recd[loc[i]] ^= 1;
484506
return l[u];
485507
}
486508
else { /* elp has degree >t hence cannot solve */
509+
#ifdef DEBUG
487510
printf("Incomplete decoding: errors detected\n");
511+
#endif
488512
return -1;
489513
}
490514
} else {
@@ -521,15 +545,29 @@ void bits_to_bytes(int *bits, int *byte_dest, int num_bits) {
521545

522546
byte_dest[index] <<= 8 - (num_bits % 8);
523547
}
524-
548+
549+
void swap_format(int *bits, int cutoff, int num_bits) {
550+
// Do it the easy way
551+
int *temp = malloc(num_bits * sizeof(int));
552+
for (int i = 0; i < num_bits; i++) {
553+
if (i < cutoff) {
554+
temp[num_bits - cutoff + i] = bits[i];
555+
} else {
556+
temp[i - cutoff] = bits[i];
557+
}
558+
}
559+
560+
memcpy(bits, temp, num_bits * sizeof(int));
561+
}
525562

526563
void dump_bch(bch_t *bch) {
527564
printf("m: %d length: %d t: %d n: %d k: %d\n", bch->m, bch->length, bch->t, bch->n, bch->k);
528565
}
529566

567+
#undef MAIN
530568
#undef TEST_BYTES_TO_BITS
531-
#define TEST_APPLY
532-
569+
#define TEST_SWAP
570+
#ifdef MAIN
533571
int main()
534572
{
535573
int test[][8] = {
@@ -555,7 +593,7 @@ int main()
555593
int temp[8];
556594
bch_t bch;
557595

558-
bch_init(&bch, 6, 63, 3);
596+
init_bch(&bch, 6, 63, 3);
559597

560598
for (int count = 0; count < sizeof(test) / sizeof(*test); count++) {
561599
bytes_to_bits(test[count], bits, 63);
@@ -593,6 +631,21 @@ int main()
593631
printf("\n");
594632
#endif
595633

634+
#ifdef TEST_SWAP
635+
printf("orig: ");
636+
for (int i = 0; i < 63; i++) {
637+
printf("%d ", bits[i]);
638+
}
639+
printf("\n");
640+
641+
swap_format(bits, 45, 63);
642+
643+
printf("rev: ");
644+
for (int i = 0; i < 63; i++) {
645+
printf("%d ", bits[i]);
646+
}
647+
printf("\n");
648+
#endif
596649
#ifdef TEST_APPLY
597650
int recv[63];
598651

@@ -635,3 +688,4 @@ int main()
635688
#endif
636689
}
637690
}
691+
#endif

Diff for: src/bch.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@ struct bch {
1515

1616
typedef struct bch bch_t;
1717

18-
int bch_init(bch_t *bch, int m, int length, int t);
18+
int init_bch(bch_t *bch, int m, int length, int t);
1919

2020
void generate_bch(bch_t *bch, int *data, int *bb);
2121

22+
int apply_bch(bch_t *bch, int *recd);
23+
24+
void bytes_to_bits(int *bytes, int *bit_dest, int num_bits);
25+
26+
void bits_to_bytes(int *bits, int *byte_dest, int num_bits);
27+
28+
void swap_format(int *bits, int cutoff, int num_bits);
29+
2230
#endif

Diff for: src/mkpkts.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
DATA_DIR=../build
2+
cut -c22-41,54-56,59-61,64-66,69-71,74-76,79-81,84-86,89-90 $DATA_DIR/all.out >$DATA_DIR/all.cut
3+
cut -d' ' -f5- $DATA_DIR/all.cut > $DATA_DIR/all.pkts
4+
./pkttest < $DATA_DIR/all.pkts > $DATA_DIR/all.good

Diff for: src/pkttest.c

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <stdio.h>
2+
#include "bch.h"
3+
4+
int main(int argc, char **argv) {
5+
bch_t bch;
6+
int bytes[8];
7+
int bits[63];
8+
9+
init_bch(&bch, 6, 63, 3);
10+
#ifdef ARGS
11+
if (argc != 9) {
12+
fprintf(stderr, "Expecting 8 arguments.\n");
13+
return -1;
14+
}
15+
16+
for (int i = 0; i < 8; i++) {
17+
sscanf(argv[i + 1], "%x", bytes + i);
18+
}
19+
#else
20+
while (1) {
21+
for (int i = 0; i < 8; i++) {
22+
int status = scanf("%x ", bytes + i);
23+
if (status == EOF) {
24+
return 0;
25+
}
26+
27+
if (status != 1) {
28+
fprintf(stderr, "Error: %d", status);
29+
}
30+
}
31+
#endif
32+
// UNEEDED
33+
// swap_format(bits, 45, 63);
34+
bytes_to_bits(bytes, bits, 63);
35+
int corrected = apply_bch(&bch, bits);
36+
if (corrected >= 0) {
37+
#ifdef DEBUG
38+
printf("%d corrected\n", corrected);
39+
for (int i = 0; i < 8; i++) {
40+
printf("%02x ", bytes[i]);
41+
}
42+
printf("\n");
43+
#endif
44+
bits_to_bytes(bits, bytes, 63);
45+
for (int i = 0; i < 8; i++) {
46+
printf("%02x ", bytes[i]);
47+
}
48+
// Slice packet
49+
50+
printf("chain=%1x,",(bytes[0] >> 6) & 0x03);
51+
printf("devst=%1x,",(bytes[0] >> 4) & 0x03);
52+
printf("msgid=%1x,",(bytes[0] >> 1) & 0x07);
53+
printf("uaddr=%03x,",((bytes[0] & 0x01) << 16) | (bytes[1] << 8) | (bytes[2]));
54+
printf("bpres=%d,",(bytes[3] >> 1) & 0x07f);
55+
printf("dbit1=%02x,",((bytes[3] & 0x01) << 7) | ((bytes[4] >> 1) & 0x7f));
56+
printf("confm=%x,",(bytes[5] >> 7) & 0x01);
57+
printf("dbit2=%x,",(bytes[5] >> 6) & 0x01);
58+
printf("motdt=%x,",(bytes[5] >> 5) & 0x01);
59+
printf("ltbat=%x,",(bytes[5] >> 4) & 0x01);
60+
printf("ltsta=%x",(bytes[5] >> 3) & 0x01);
61+
printf("\n");
62+
}
63+
#ifndef ARGS
64+
}
65+
#endif
66+
67+
return 0;
68+
}

0 commit comments

Comments
 (0)