From 5be9e44c5b9a835e60048b3ef5403c6e1843972b Mon Sep 17 00:00:00 2001 From: "David E. Tiller" <3858971+dtiller@users.noreply.github.com> Date: Tue, 22 Mar 2022 16:26:15 -0400 Subject: [PATCH] Closer. Lots of ACCA data to chew on. --- src/bch.c | 76 +++++++++++++++++++++++++++++++++++++++++++-------- src/bch.h | 10 ++++++- src/mkpkts.sh | 4 +++ src/pkttest.c | 68 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+), 12 deletions(-) create mode 100644 src/mkpkts.sh create mode 100644 src/pkttest.c diff --git a/src/bch.c b/src/bch.c index 4a74190..d6bef4c 100644 --- a/src/bch.c +++ b/src/bch.c @@ -75,7 +75,9 @@ #include #include "bch.h" -int bch_init(bch_t *bch, int m, int length, int t) { +#undef DEBUG + +int init_bch(bch_t *bch, int m, int length, int t) { int p[21], n; @@ -115,14 +117,20 @@ int bch_init(bch_t *bch, int m, int length, int t) { else if (m == 18) p[7] = 1; else if (m == 19) p[1] = p[5] = p[6] = 1; else if (m == 20) p[3] = 1; - printf("p(x) = "); n = 1; +#ifdef DEBUG + printf("p(x) = "); +#endif for (int i = 0; i <= m; i++) { n *= 2; +#ifdef DEBUG printf("%1d", p[i]); +#endif } +#ifdef DEBUG printf("\n"); +#endif n = n / 2 - 1; bch->n = n; int ninf = (n + 1) / 2 - 1; @@ -184,10 +192,12 @@ int bch_init(bch_t *bch, int m, int length, int t) { cycle[1][0] = 1; size[1] = 1; jj = 1; /* cycle set index */ +#ifdef DEBUG if (bch->m > 9) { printf("Computing cycle sets modulo %d\n", bch->n); printf("(This may take some time)...\n"); } +#endif do { /* Generate the jj-th cycle set */ ii = 0; @@ -247,11 +257,12 @@ int bch_init(bch_t *bch, int m, int length, int t) { if (bch->k<0) { - printf("Parameters invalid!\n"); return -4; } +#ifdef DEBUG printf("This is a (%d, %d, %d) binary BCH code\n", bch->length, bch->k, d); +#endif /* Compute the generator polynomial */ bch->g = malloc((rdncy + 1) * sizeof(int)); @@ -266,12 +277,13 @@ int bch_init(bch_t *bch, int m, int length, int t) { bch->g[jj] = bch->g[jj - 1]; bch->g[0] = bch->alpha_to[(bch->index_of[bch->g[0]] + zeros[ii]) % bch->n]; } +#ifdef DEBUG printf("Generator polynomial:\ng(x) = "); for (ii = 0; ii <= rdncy; ii++) { printf("%d", bch->g[ii]); } printf("\n"); - +#endif return 0; } @@ -303,8 +315,7 @@ void generate_bch(bch_t *bch, int *data, int *bb) { } -int -apply_bch(bch_t *bch, int *recd) +int apply_bch(bch_t *bch, int *recd) /* * Simon Rockliff's implementation of Berlekamp's algorithm. * @@ -335,7 +346,9 @@ apply_bch(bch_t *bch, int *recd) t2 = 2 * bch->t; /* first form the syndromes */ +#ifdef DEBUG printf("S(x) = "); +#endif for (i = 1; i <= t2; i++) { s[i] = 0; for (j = 0; j < bch->length; j++) @@ -349,9 +362,13 @@ apply_bch(bch_t *bch, int *recd) */ /* convert syndrome from polynomial form to index form */ s[i] = bch->index_of[s[i]]; +#ifdef DEBUG printf("%3d ", s[i]); +#endif } +#ifdef DEBUG printf("\n"); +#endif if (syn_error) { /* if there are errors, try to correct them */ /* @@ -451,12 +468,13 @@ apply_bch(bch_t *bch, int *recd) for (i = 0; i <= l[u]; i++) elp[u][i] = bch->index_of[elp[u][i]]; +#ifdef DEBUG printf("sigma(x) = "); for (i = 0; i <= l[u]; i++) printf("%3d ", elp[u][i]); printf("\n"); printf("Roots: "); - +#endif /* Chien search: find roots of the error location polynomial */ for (i = 1; i <= l[u]; i++) reg[i] = elp[u][i]; @@ -473,10 +491,14 @@ apply_bch(bch_t *bch, int *recd) root[count] = i; loc[count] = bch->n - i; count++; +#ifdef DEBUG printf("%3d ", bch->n - i); +#endif } } +#ifdef DEBUG printf("\n"); +#endif if (count == l[u]) { /* no. roots = degree of elp hence <= t errors */ for (i = 0; i < l[u]; i++) @@ -484,7 +506,9 @@ apply_bch(bch_t *bch, int *recd) return l[u]; } else { /* elp has degree >t hence cannot solve */ +#ifdef DEBUG printf("Incomplete decoding: errors detected\n"); +#endif return -1; } } else { @@ -521,15 +545,29 @@ void bits_to_bytes(int *bits, int *byte_dest, int num_bits) { byte_dest[index] <<= 8 - (num_bits % 8); } - + +void swap_format(int *bits, int cutoff, int num_bits) { + // Do it the easy way + int *temp = malloc(num_bits * sizeof(int)); + for (int i = 0; i < num_bits; i++) { + if (i < cutoff) { + temp[num_bits - cutoff + i] = bits[i]; + } else { + temp[i - cutoff] = bits[i]; + } + } + + memcpy(bits, temp, num_bits * sizeof(int)); +} void dump_bch(bch_t *bch) { printf("m: %d length: %d t: %d n: %d k: %d\n", bch->m, bch->length, bch->t, bch->n, bch->k); } +#undef MAIN #undef TEST_BYTES_TO_BITS -#define TEST_APPLY - +#define TEST_SWAP +#ifdef MAIN int main() { int test[][8] = { @@ -555,7 +593,7 @@ int main() int temp[8]; bch_t bch; - bch_init(&bch, 6, 63, 3); + init_bch(&bch, 6, 63, 3); for (int count = 0; count < sizeof(test) / sizeof(*test); count++) { bytes_to_bits(test[count], bits, 63); @@ -593,6 +631,21 @@ int main() printf("\n"); #endif +#ifdef TEST_SWAP + printf("orig: "); + for (int i = 0; i < 63; i++) { + printf("%d ", bits[i]); + } + printf("\n"); + + swap_format(bits, 45, 63); + + printf("rev: "); + for (int i = 0; i < 63; i++) { + printf("%d ", bits[i]); + } + printf("\n"); +#endif #ifdef TEST_APPLY int recv[63]; @@ -635,3 +688,4 @@ int main() #endif } } +#endif diff --git a/src/bch.h b/src/bch.h index 1f90da5..bbd23f1 100644 --- a/src/bch.h +++ b/src/bch.h @@ -15,8 +15,16 @@ struct bch { typedef struct bch bch_t; -int bch_init(bch_t *bch, int m, int length, int t); +int init_bch(bch_t *bch, int m, int length, int t); void generate_bch(bch_t *bch, int *data, int *bb); +int apply_bch(bch_t *bch, int *recd); + +void bytes_to_bits(int *bytes, int *bit_dest, int num_bits); + +void bits_to_bytes(int *bits, int *byte_dest, int num_bits); + +void swap_format(int *bits, int cutoff, int num_bits); + #endif diff --git a/src/mkpkts.sh b/src/mkpkts.sh new file mode 100644 index 0000000..38679b9 --- /dev/null +++ b/src/mkpkts.sh @@ -0,0 +1,4 @@ +DATA_DIR=../build +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 +cut -d' ' -f5- $DATA_DIR/all.cut > $DATA_DIR/all.pkts +./pkttest < $DATA_DIR/all.pkts > $DATA_DIR/all.good diff --git a/src/pkttest.c b/src/pkttest.c new file mode 100644 index 0000000..7ab839b --- /dev/null +++ b/src/pkttest.c @@ -0,0 +1,68 @@ +#include +#include "bch.h" + +int main(int argc, char **argv) { + bch_t bch; + int bytes[8]; + int bits[63]; + + init_bch(&bch, 6, 63, 3); +#ifdef ARGS + if (argc != 9) { + fprintf(stderr, "Expecting 8 arguments.\n"); + return -1; + } + + for (int i = 0; i < 8; i++) { + sscanf(argv[i + 1], "%x", bytes + i); + } +#else + while (1) { + for (int i = 0; i < 8; i++) { + int status = scanf("%x ", bytes + i); + if (status == EOF) { + return 0; + } + + if (status != 1) { + fprintf(stderr, "Error: %d", status); + } + } +#endif + // UNEEDED + // swap_format(bits, 45, 63); + bytes_to_bits(bytes, bits, 63); + int corrected = apply_bch(&bch, bits); + if (corrected >= 0) { +#ifdef DEBUG + printf("%d corrected\n", corrected); + for (int i = 0; i < 8; i++) { + printf("%02x ", bytes[i]); + } + printf("\n"); +#endif + bits_to_bytes(bits, bytes, 63); + for (int i = 0; i < 8; i++) { + printf("%02x ", bytes[i]); + } + // Slice packet + + printf("chain=%1x,",(bytes[0] >> 6) & 0x03); + printf("devst=%1x,",(bytes[0] >> 4) & 0x03); + printf("msgid=%1x,",(bytes[0] >> 1) & 0x07); + printf("uaddr=%03x,",((bytes[0] & 0x01) << 16) | (bytes[1] << 8) | (bytes[2])); + printf("bpres=%d,",(bytes[3] >> 1) & 0x07f); + printf("dbit1=%02x,",((bytes[3] & 0x01) << 7) | ((bytes[4] >> 1) & 0x7f)); + printf("confm=%x,",(bytes[5] >> 7) & 0x01); + printf("dbit2=%x,",(bytes[5] >> 6) & 0x01); + printf("motdt=%x,",(bytes[5] >> 5) & 0x01); + printf("ltbat=%x,",(bytes[5] >> 4) & 0x01); + printf("ltsta=%x",(bytes[5] >> 3) & 0x01); + printf("\n"); + } +#ifndef ARGS + } +#endif + + return 0; +}