mirror of https://github.com/wb2osz/direwolf.git
Last checkin before trying dev branch.
This commit is contained in:
parent
5be9e44c5b
commit
742dfb486b
86
src/bch.c
86
src/bch.c
|
@ -75,8 +75,6 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "bch.h"
|
#include "bch.h"
|
||||||
|
|
||||||
#undef DEBUG
|
|
||||||
|
|
||||||
int init_bch(bch_t *bch, int m, int length, int t) {
|
int init_bch(bch_t *bch, int m, int length, int t) {
|
||||||
|
|
||||||
int p[21], n;
|
int p[21], n;
|
||||||
|
@ -119,16 +117,16 @@ int init_bch(bch_t *bch, int m, int length, int t) {
|
||||||
else if (m == 20) p[3] = 1;
|
else if (m == 20) p[3] = 1;
|
||||||
|
|
||||||
n = 1;
|
n = 1;
|
||||||
#ifdef DEBUG
|
#ifdef BCH_DEBUG
|
||||||
printf("p(x) = ");
|
printf("p(x) = ");
|
||||||
#endif
|
#endif
|
||||||
for (int i = 0; i <= m; i++) {
|
for (int i = 0; i <= m; i++) {
|
||||||
n *= 2;
|
n *= 2;
|
||||||
#ifdef DEBUG
|
#ifdef BCH_DEBUG
|
||||||
printf("%1d", p[i]);
|
printf("%1d", p[i]);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
#ifdef DEBUG
|
#ifdef BCH_DEBUG
|
||||||
printf("\n");
|
printf("\n");
|
||||||
#endif
|
#endif
|
||||||
n = n / 2 - 1;
|
n = n / 2 - 1;
|
||||||
|
@ -192,7 +190,7 @@ int init_bch(bch_t *bch, int m, int length, int t) {
|
||||||
cycle[1][0] = 1;
|
cycle[1][0] = 1;
|
||||||
size[1] = 1;
|
size[1] = 1;
|
||||||
jj = 1; /* cycle set index */
|
jj = 1; /* cycle set index */
|
||||||
#ifdef DEBUG
|
#ifdef BCH_DEBUG
|
||||||
if (bch->m > 9) {
|
if (bch->m > 9) {
|
||||||
printf("Computing cycle sets modulo %d\n", bch->n);
|
printf("Computing cycle sets modulo %d\n", bch->n);
|
||||||
printf("(This may take some time)...\n");
|
printf("(This may take some time)...\n");
|
||||||
|
@ -260,7 +258,7 @@ int init_bch(bch_t *bch, int m, int length, int t) {
|
||||||
return -4;
|
return -4;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef BCH_DEBUG
|
||||||
printf("This is a (%d, %d, %d) binary BCH code\n", bch->length, bch->k, d);
|
printf("This is a (%d, %d, %d) binary BCH code\n", bch->length, bch->k, d);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -277,7 +275,7 @@ int init_bch(bch_t *bch, int m, int length, int t) {
|
||||||
bch->g[jj] = bch->g[jj - 1];
|
bch->g[jj] = bch->g[jj - 1];
|
||||||
bch->g[0] = bch->alpha_to[(bch->index_of[bch->g[0]] + zeros[ii]) % bch->n];
|
bch->g[0] = bch->alpha_to[(bch->index_of[bch->g[0]] + zeros[ii]) % bch->n];
|
||||||
}
|
}
|
||||||
#ifdef DEBUG
|
#ifdef BCH_DEBUG
|
||||||
printf("Generator polynomial:\ng(x) = ");
|
printf("Generator polynomial:\ng(x) = ");
|
||||||
for (ii = 0; ii <= rdncy; ii++) {
|
for (ii = 0; ii <= rdncy; ii++) {
|
||||||
printf("%d", bch->g[ii]);
|
printf("%d", bch->g[ii]);
|
||||||
|
@ -287,7 +285,7 @@ int init_bch(bch_t *bch, int m, int length, int t) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void generate_bch(bch_t *bch, int *data, int *bb) {
|
void generate_bch(bch_t *bch, const int *data, int *bb) {
|
||||||
/*
|
/*
|
||||||
* Compute redundacy bb[], the coefficients of b(x). The redundancy
|
* Compute redundacy bb[], the coefficients of b(x). The redundancy
|
||||||
* polynomial b(x) is the remainder after dividing x^(length-k)*data(x)
|
* polynomial b(x) is the remainder after dividing x^(length-k)*data(x)
|
||||||
|
@ -315,7 +313,7 @@ void generate_bch(bch_t *bch, int *data, int *bb) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int apply_bch(bch_t *bch, int *recd)
|
int apply_bch(const bch_t *bch, int *recd)
|
||||||
/*
|
/*
|
||||||
* Simon Rockliff's implementation of Berlekamp's algorithm.
|
* Simon Rockliff's implementation of Berlekamp's algorithm.
|
||||||
*
|
*
|
||||||
|
@ -346,7 +344,7 @@ int apply_bch(bch_t *bch, int *recd)
|
||||||
t2 = 2 * bch->t;
|
t2 = 2 * bch->t;
|
||||||
|
|
||||||
/* first form the syndromes */
|
/* first form the syndromes */
|
||||||
#ifdef DEBUG
|
#ifdef BCH_DEBUG
|
||||||
printf("S(x) = ");
|
printf("S(x) = ");
|
||||||
#endif
|
#endif
|
||||||
for (i = 1; i <= t2; i++) {
|
for (i = 1; i <= t2; i++) {
|
||||||
|
@ -362,11 +360,11 @@ int apply_bch(bch_t *bch, int *recd)
|
||||||
*/
|
*/
|
||||||
/* convert syndrome from polynomial form to index form */
|
/* convert syndrome from polynomial form to index form */
|
||||||
s[i] = bch->index_of[s[i]];
|
s[i] = bch->index_of[s[i]];
|
||||||
#ifdef DEBUG
|
#ifdef BCH_DEBUG
|
||||||
printf("%3d ", s[i]);
|
printf("%3d ", s[i]);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
#ifdef DEBUG
|
#ifdef BCH_DEBUG
|
||||||
printf("\n");
|
printf("\n");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -468,7 +466,7 @@ int apply_bch(bch_t *bch, int *recd)
|
||||||
for (i = 0; i <= l[u]; i++)
|
for (i = 0; i <= l[u]; i++)
|
||||||
elp[u][i] = bch->index_of[elp[u][i]];
|
elp[u][i] = bch->index_of[elp[u][i]];
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef BCH_DEBUG
|
||||||
printf("sigma(x) = ");
|
printf("sigma(x) = ");
|
||||||
for (i = 0; i <= l[u]; i++)
|
for (i = 0; i <= l[u]; i++)
|
||||||
printf("%3d ", elp[u][i]);
|
printf("%3d ", elp[u][i]);
|
||||||
|
@ -491,12 +489,12 @@ int apply_bch(bch_t *bch, int *recd)
|
||||||
root[count] = i;
|
root[count] = i;
|
||||||
loc[count] = bch->n - i;
|
loc[count] = bch->n - i;
|
||||||
count++;
|
count++;
|
||||||
#ifdef DEBUG
|
#ifdef BCH_DEBUG
|
||||||
printf("%3d ", bch->n - i);
|
printf("%3d ", bch->n - i);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#ifdef DEBUG
|
#ifdef BCH_DEBUG
|
||||||
printf("\n");
|
printf("\n");
|
||||||
#endif
|
#endif
|
||||||
if (count == l[u]) {
|
if (count == l[u]) {
|
||||||
|
@ -506,7 +504,7 @@ int apply_bch(bch_t *bch, int *recd)
|
||||||
return l[u];
|
return l[u];
|
||||||
}
|
}
|
||||||
else { /* elp has degree >t hence cannot solve */
|
else { /* elp has degree >t hence cannot solve */
|
||||||
#ifdef DEBUG
|
#ifdef BCH_DEBUG
|
||||||
printf("Incomplete decoding: errors detected\n");
|
printf("Incomplete decoding: errors detected\n");
|
||||||
#endif
|
#endif
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -520,7 +518,7 @@ int apply_bch(bch_t *bch, int *recd)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* LEFT justified in hex */
|
/* LEFT justified in hex */
|
||||||
void bytes_to_bits(int *bytes, int *bit_dest, int num_bits) {
|
void bytes_to_bits(const int *bytes, int *bit_dest, int num_bits) {
|
||||||
for (int i = 0; i < num_bits; i++) {
|
for (int i = 0; i < num_bits; i++) {
|
||||||
int index = i / 8;
|
int index = i / 8;
|
||||||
int bit_pos = 7 - (i % 8);
|
int bit_pos = 7 - (i % 8);
|
||||||
|
@ -529,7 +527,7 @@ 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 bits_to_bytes(const int *bits, int *byte_dest, int num_bits) {
|
||||||
|
|
||||||
int index;
|
int index;
|
||||||
|
|
||||||
|
@ -540,30 +538,66 @@ void bits_to_bytes(int *bits, int *byte_dest, int num_bits) {
|
||||||
}
|
}
|
||||||
|
|
||||||
byte_dest[index] <<= 1;
|
byte_dest[index] <<= 1;
|
||||||
byte_dest[index] |= bits[i] & 0x01;
|
byte_dest[index] |= (bits[i] & 0x01);
|
||||||
}
|
}
|
||||||
|
|
||||||
byte_dest[index] <<= 8 - (num_bits % 8);
|
byte_dest[index] <<= 8 - (num_bits % 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
void swap_format(int *bits, int cutoff, int num_bits) {
|
void swap_format(const int *bits, int *dest, int cutoff, int num_bits) {
|
||||||
// Do it the easy way
|
// Do it the easy way
|
||||||
int *temp = malloc(num_bits * sizeof(int));
|
|
||||||
for (int i = 0; i < num_bits; i++) {
|
for (int i = 0; i < num_bits; i++) {
|
||||||
if (i < cutoff) {
|
if (i < cutoff) {
|
||||||
temp[num_bits - cutoff + i] = bits[i];
|
dest[num_bits - cutoff + i] = bits[i];
|
||||||
} else {
|
} else {
|
||||||
temp[i - cutoff] = bits[i];
|
dest[i - cutoff] = bits[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(bits, temp, num_bits * sizeof(int));
|
int rotate_byte(int x) {
|
||||||
|
int y = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
y <<= 1;
|
||||||
|
y |= (x & 0x01);
|
||||||
|
x >>= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dump_bch(bch_t *bch) {
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
void rotate_bits(const int *in, int *out, int num_bits) {
|
||||||
|
for (int i = 0; i < num_bits; i++) {
|
||||||
|
out[i] = in[num_bits - i - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void invert_bits(const int *bits, int *dest, int num_bits) {
|
||||||
|
for (int i = 0; i < num_bits; i++) {
|
||||||
|
dest[i] = (bits[i] == 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void dump_bch(const 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);
|
printf("m: %d length: %d t: %d n: %d k: %d\n", bch->m, bch->length, bch->t, bch->n, bch->k);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void print_array(const char *msg, const char *format, const int *bytes, int num_bytes) {
|
||||||
|
printf("%s", msg);
|
||||||
|
for (int i = 0; i < num_bytes; i++) {
|
||||||
|
printf(format, bytes[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_bytes(const char *msg, const int *bytes, int num_bytes) {
|
||||||
|
print_array(msg, "%02x ", bytes, num_bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_bits(const char *msg, const int *bits, int num_bits) {
|
||||||
|
print_array(msg, "%d ", bits, num_bits);
|
||||||
|
}
|
||||||
|
|
||||||
#undef MAIN
|
#undef MAIN
|
||||||
#undef TEST_BYTES_TO_BITS
|
#undef TEST_BYTES_TO_BITS
|
||||||
#define TEST_SWAP
|
#define TEST_SWAP
|
||||||
|
|
20
src/bch.h
20
src/bch.h
|
@ -17,14 +17,24 @@ typedef struct bch bch_t;
|
||||||
|
|
||||||
int init_bch(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);
|
void generate_bch(bch_t *bch, const int *data, int *bb);
|
||||||
|
|
||||||
int apply_bch(bch_t *bch, int *recd);
|
int apply_bch(const bch_t *bch, int *recd);
|
||||||
|
|
||||||
void bytes_to_bits(int *bytes, int *bit_dest, int num_bits);
|
void bytes_to_bits(const int *bytes, int *bit_dest, int num_bits);
|
||||||
|
|
||||||
void bits_to_bytes(int *bits, int *byte_dest, int num_bits);
|
void bits_to_bytes(const int *bits, int *byte_dest, int num_bits);
|
||||||
|
|
||||||
void swap_format(int *bits, int cutoff, int num_bits);
|
void swap_format(const int *bits, int *dest, int cutoff, int num_bits);
|
||||||
|
|
||||||
|
int rotate_byte(int x);
|
||||||
|
|
||||||
|
void rotate_bits(const int *in, int *out, int num_bits);
|
||||||
|
|
||||||
|
void print_bytes(const char *msg, const int *bytes, int num_bytes);
|
||||||
|
|
||||||
|
void print_bits(const char *msg, const int *bits, int num_bits);
|
||||||
|
|
||||||
|
void invert_bits(const int *bits, int *dest, int num_bits);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -67,7 +67,7 @@ void eotd_to_nmea (unsigned char *eotd, int eotd_len, char *nmea, int nmea_size)
|
||||||
strcat(nmea, ctime(&now));
|
strcat(nmea, ctime(&now));
|
||||||
for (int i = 0; i < eotd_len; i++) {
|
for (int i = 0; i < eotd_len; i++) {
|
||||||
char temp[32];
|
char temp[32];
|
||||||
snprintf(temp, sizeof(temp), "%d=%02x ", i, eotd[i]);
|
snprintf(temp, sizeof(temp), " %02x", eotd[i]);
|
||||||
strlcat(nmea, temp, nmea_size);
|
strlcat(nmea, temp, nmea_size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,6 +52,8 @@
|
||||||
|
|
||||||
//#define DEBUG3 1 /* monitor the data detect signal. */
|
//#define DEBUG3 1 /* monitor the data detect signal. */
|
||||||
|
|
||||||
|
#undef EOTD_DEBUG
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -427,9 +429,10 @@ a good modem here and providing a result when it is received.
|
||||||
*
|
*
|
||||||
***********************************************************************************/
|
***********************************************************************************/
|
||||||
|
|
||||||
#define PREAMBLE_AND_BARKER_CODE 0x55555712
|
#define EOTD_PREAMBLE_AND_BARKER_CODE 0x55555712
|
||||||
|
#define HOTD_PREAMBLE_AND_BARKER_CODE 0x558f1129
|
||||||
#define EOTD_MAX_LEN 8
|
#define EOTD_MAX_LEN 8
|
||||||
#define DUMMY_BIT_HACK
|
#undef DUMMY_BIT_HACK
|
||||||
|
|
||||||
static void eotd_rec_bit (int chan, int subchan, int slice, int raw, int future_use)
|
static void eotd_rec_bit (int chan, int subchan, int slice, int raw, int future_use)
|
||||||
{
|
{
|
||||||
|
@ -441,7 +444,7 @@ static void eotd_rec_bit (int chan, int subchan, int slice, int raw, int future_
|
||||||
H = &hdlc_state[chan][subchan][slice];
|
H = &hdlc_state[chan][subchan][slice];
|
||||||
|
|
||||||
#ifdef EOTD_DEBUG
|
#ifdef EOTD_DEBUG
|
||||||
dw_printf(stderr, "chan=%d subchan=%d slice=%d raw=%d\n", chan, subchan, slice, raw);
|
dw_printf("chan=%d subchan=%d slice=%d raw=%d\n", chan, subchan, slice, raw);
|
||||||
#endif
|
#endif
|
||||||
//dw_printf ("slice %d = %d\n", slice, raw);
|
//dw_printf ("slice %d = %d\n", slice, raw);
|
||||||
|
|
||||||
|
@ -452,9 +455,11 @@ dw_printf(stderr, "chan=%d subchan=%d slice=%d raw=%d\n", chan, subchan, slice,
|
||||||
|
|
||||||
int done = 0;
|
int done = 0;
|
||||||
|
|
||||||
if (!H->eotd_gathering && H->eotd_acc == PREAMBLE_AND_BARKER_CODE) {
|
if (!H->eotd_gathering &&
|
||||||
|
(H->eotd_acc == EOTD_PREAMBLE_AND_BARKER_CODE ||
|
||||||
|
H->eotd_acc == HOTD_PREAMBLE_AND_BARKER_CODE)) {
|
||||||
#ifdef EOTD_DEBUG
|
#ifdef EOTD_DEBUG
|
||||||
dw_printf ("Barker Code Found\n");
|
dw_printf ("Barker Code Found %x\n", H->eotd_acc);
|
||||||
#endif
|
#endif
|
||||||
H->olen = 0;
|
H->olen = 0;
|
||||||
H->eotd_gathering = 1;
|
H->eotd_gathering = 1;
|
||||||
|
|
Loading…
Reference in New Issue