Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 742dfb4

Browse files
committedMar 29, 2022
Last checkin before trying dev branch.
1 parent 5be9e44 commit 742dfb4

File tree

4 files changed

+86
-37
lines changed

4 files changed

+86
-37
lines changed
 

‎src/bch.c

+60-26
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@
7575
#include <string.h>
7676
#include "bch.h"
7777

78-
#undef DEBUG
79-
8078
int init_bch(bch_t *bch, int m, int length, int t) {
8179

8280
int p[21], n;
@@ -119,16 +117,16 @@ int init_bch(bch_t *bch, int m, int length, int t) {
119117
else if (m == 20) p[3] = 1;
120118

121119
n = 1;
122-
#ifdef DEBUG
120+
#ifdef BCH_DEBUG
123121
printf("p(x) = ");
124122
#endif
125123
for (int i = 0; i <= m; i++) {
126124
n *= 2;
127-
#ifdef DEBUG
125+
#ifdef BCH_DEBUG
128126
printf("%1d", p[i]);
129127
#endif
130128
}
131-
#ifdef DEBUG
129+
#ifdef BCH_DEBUG
132130
printf("\n");
133131
#endif
134132
n = n / 2 - 1;
@@ -192,7 +190,7 @@ int init_bch(bch_t *bch, int m, int length, int t) {
192190
cycle[1][0] = 1;
193191
size[1] = 1;
194192
jj = 1; /* cycle set index */
195-
#ifdef DEBUG
193+
#ifdef BCH_DEBUG
196194
if (bch->m > 9) {
197195
printf("Computing cycle sets modulo %d\n", bch->n);
198196
printf("(This may take some time)...\n");
@@ -260,7 +258,7 @@ int init_bch(bch_t *bch, int m, int length, int t) {
260258
return -4;
261259
}
262260

263-
#ifdef DEBUG
261+
#ifdef BCH_DEBUG
264262
printf("This is a (%d, %d, %d) binary BCH code\n", bch->length, bch->k, d);
265263
#endif
266264

@@ -277,7 +275,7 @@ int init_bch(bch_t *bch, int m, int length, int t) {
277275
bch->g[jj] = bch->g[jj - 1];
278276
bch->g[0] = bch->alpha_to[(bch->index_of[bch->g[0]] + zeros[ii]) % bch->n];
279277
}
280-
#ifdef DEBUG
278+
#ifdef BCH_DEBUG
281279
printf("Generator polynomial:\ng(x) = ");
282280
for (ii = 0; ii <= rdncy; ii++) {
283281
printf("%d", bch->g[ii]);
@@ -287,7 +285,7 @@ int init_bch(bch_t *bch, int m, int length, int t) {
287285
return 0;
288286
}
289287

290-
void generate_bch(bch_t *bch, int *data, int *bb) {
288+
void generate_bch(bch_t *bch, const int *data, int *bb) {
291289
/*
292290
* Compute redundacy bb[], the coefficients of b(x). The redundancy
293291
* 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) {
315313
}
316314

317315

318-
int apply_bch(bch_t *bch, int *recd)
316+
int apply_bch(const bch_t *bch, int *recd)
319317
/*
320318
* Simon Rockliff's implementation of Berlekamp's algorithm.
321319
*
@@ -346,7 +344,7 @@ int apply_bch(bch_t *bch, int *recd)
346344
t2 = 2 * bch->t;
347345

348346
/* first form the syndromes */
349-
#ifdef DEBUG
347+
#ifdef BCH_DEBUG
350348
printf("S(x) = ");
351349
#endif
352350
for (i = 1; i <= t2; i++) {
@@ -362,11 +360,11 @@ int apply_bch(bch_t *bch, int *recd)
362360
*/
363361
/* convert syndrome from polynomial form to index form */
364362
s[i] = bch->index_of[s[i]];
365-
#ifdef DEBUG
363+
#ifdef BCH_DEBUG
366364
printf("%3d ", s[i]);
367365
#endif
368366
}
369-
#ifdef DEBUG
367+
#ifdef BCH_DEBUG
370368
printf("\n");
371369
#endif
372370

@@ -468,7 +466,7 @@ int apply_bch(bch_t *bch, int *recd)
468466
for (i = 0; i <= l[u]; i++)
469467
elp[u][i] = bch->index_of[elp[u][i]];
470468

471-
#ifdef DEBUG
469+
#ifdef BCH_DEBUG
472470
printf("sigma(x) = ");
473471
for (i = 0; i <= l[u]; i++)
474472
printf("%3d ", elp[u][i]);
@@ -491,12 +489,12 @@ int apply_bch(bch_t *bch, int *recd)
491489
root[count] = i;
492490
loc[count] = bch->n - i;
493491
count++;
494-
#ifdef DEBUG
492+
#ifdef BCH_DEBUG
495493
printf("%3d ", bch->n - i);
496494
#endif
497495
}
498496
}
499-
#ifdef DEBUG
497+
#ifdef BCH_DEBUG
500498
printf("\n");
501499
#endif
502500
if (count == l[u]) {
@@ -506,7 +504,7 @@ int apply_bch(bch_t *bch, int *recd)
506504
return l[u];
507505
}
508506
else { /* elp has degree >t hence cannot solve */
509-
#ifdef DEBUG
507+
#ifdef BCH_DEBUG
510508
printf("Incomplete decoding: errors detected\n");
511509
#endif
512510
return -1;
@@ -520,7 +518,7 @@ int apply_bch(bch_t *bch, int *recd)
520518
}
521519

522520
/* LEFT justified in hex */
523-
void bytes_to_bits(int *bytes, int *bit_dest, int num_bits) {
521+
void bytes_to_bits(const int *bytes, int *bit_dest, int num_bits) {
524522
for (int i = 0; i < num_bits; i++) {
525523
int index = i / 8;
526524
int bit_pos = 7 - (i % 8);
@@ -529,7 +527,7 @@ void bytes_to_bits(int *bytes, int *bit_dest, int num_bits) {
529527
}
530528
}
531529

532-
void bits_to_bytes(int *bits, int *byte_dest, int num_bits) {
530+
void bits_to_bytes(const int *bits, int *byte_dest, int num_bits) {
533531

534532
int index;
535533

@@ -540,30 +538,66 @@ void bits_to_bytes(int *bits, int *byte_dest, int num_bits) {
540538
}
541539

542540
byte_dest[index] <<= 1;
543-
byte_dest[index] |= bits[i] & 0x01;
541+
byte_dest[index] |= (bits[i] & 0x01);
544542
}
545543

546544
byte_dest[index] <<= 8 - (num_bits % 8);
547545
}
548546

549-
void swap_format(int *bits, int cutoff, int num_bits) {
547+
void swap_format(const int *bits, int *dest, int cutoff, int num_bits) {
550548
// Do it the easy way
551-
int *temp = malloc(num_bits * sizeof(int));
552549
for (int i = 0; i < num_bits; i++) {
553550
if (i < cutoff) {
554-
temp[num_bits - cutoff + i] = bits[i];
551+
dest[num_bits - cutoff + i] = bits[i];
555552
} else {
556-
temp[i - cutoff] = bits[i];
553+
dest[i - cutoff] = bits[i];
557554
}
558555
}
556+
}
557+
558+
int rotate_byte(int x) {
559+
int y = 0;
560+
561+
for (int i = 0; i < 8; i++) {
562+
y <<= 1;
563+
y |= (x & 0x01);
564+
x >>= 1;
565+
}
559566

560-
memcpy(bits, temp, num_bits * sizeof(int));
567+
return y;
561568
}
562569

563-
void dump_bch(bch_t *bch) {
570+
void rotate_bits(const int *in, int *out, int num_bits) {
571+
for (int i = 0; i < num_bits; i++) {
572+
out[i] = in[num_bits - i - 1];
573+
}
574+
}
575+
576+
void invert_bits(const int *bits, int *dest, int num_bits) {
577+
for (int i = 0; i < num_bits; i++) {
578+
dest[i] = (bits[i] == 0);
579+
}
580+
}
581+
582+
void dump_bch(const bch_t *bch) {
564583
printf("m: %d length: %d t: %d n: %d k: %d\n", bch->m, bch->length, bch->t, bch->n, bch->k);
565584
}
566585

586+
void print_array(const char *msg, const char *format, const int *bytes, int num_bytes) {
587+
printf("%s", msg);
588+
for (int i = 0; i < num_bytes; i++) {
589+
printf(format, bytes[i]);
590+
}
591+
}
592+
593+
void print_bytes(const char *msg, const int *bytes, int num_bytes) {
594+
print_array(msg, "%02x ", bytes, num_bytes);
595+
}
596+
597+
void print_bits(const char *msg, const int *bits, int num_bits) {
598+
print_array(msg, "%d ", bits, num_bits);
599+
}
600+
567601
#undef MAIN
568602
#undef TEST_BYTES_TO_BITS
569603
#define TEST_SWAP

‎src/bch.h

+15-5
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,24 @@ typedef struct bch bch_t;
1717

1818
int init_bch(bch_t *bch, int m, int length, int t);
1919

20-
void generate_bch(bch_t *bch, int *data, int *bb);
20+
void generate_bch(bch_t *bch, const int *data, int *bb);
2121

22-
int apply_bch(bch_t *bch, int *recd);
22+
int apply_bch(const bch_t *bch, int *recd);
2323

24-
void bytes_to_bits(int *bytes, int *bit_dest, int num_bits);
24+
void bytes_to_bits(const int *bytes, int *bit_dest, int num_bits);
2525

26-
void bits_to_bytes(int *bits, int *byte_dest, int num_bits);
26+
void bits_to_bytes(const int *bits, int *byte_dest, int num_bits);
2727

28-
void swap_format(int *bits, int cutoff, int num_bits);
28+
void swap_format(const int *bits, int *dest, int cutoff, int num_bits);
29+
30+
int rotate_byte(int x);
31+
32+
void rotate_bits(const int *in, int *out, int num_bits);
33+
34+
void print_bytes(const char *msg, const int *bytes, int num_bytes);
35+
36+
void print_bits(const char *msg, const int *bits, int num_bits);
37+
38+
void invert_bits(const int *bits, int *dest, int num_bits);
2939

3040
#endif

‎src/eotd.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void eotd_to_nmea (unsigned char *eotd, int eotd_len, char *nmea, int nmea_size)
6767
strcat(nmea, ctime(&now));
6868
for (int i = 0; i < eotd_len; i++) {
6969
char temp[32];
70-
snprintf(temp, sizeof(temp), "%d=%02x ", i, eotd[i]);
70+
snprintf(temp, sizeof(temp), " %02x", eotd[i]);
7171
strlcat(nmea, temp, nmea_size);
7272
}
7373
}

‎src/hdlc_rec.c

+10-5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252

5353
//#define DEBUG3 1 /* monitor the data detect signal. */
5454

55+
#undef EOTD_DEBUG
56+
5557

5658

5759
/*
@@ -427,9 +429,10 @@ a good modem here and providing a result when it is received.
427429
*
428430
***********************************************************************************/
429431

430-
#define PREAMBLE_AND_BARKER_CODE 0x55555712
432+
#define EOTD_PREAMBLE_AND_BARKER_CODE 0x55555712
433+
#define HOTD_PREAMBLE_AND_BARKER_CODE 0x558f1129
431434
#define EOTD_MAX_LEN 8
432-
#define DUMMY_BIT_HACK
435+
#undef DUMMY_BIT_HACK
433436

434437
static void eotd_rec_bit (int chan, int subchan, int slice, int raw, int future_use)
435438
{
@@ -441,7 +444,7 @@ static void eotd_rec_bit (int chan, int subchan, int slice, int raw, int future_
441444
H = &hdlc_state[chan][subchan][slice];
442445

443446
#ifdef EOTD_DEBUG
444-
dw_printf(stderr, "chan=%d subchan=%d slice=%d raw=%d\n", chan, subchan, slice, raw);
447+
dw_printf("chan=%d subchan=%d slice=%d raw=%d\n", chan, subchan, slice, raw);
445448
#endif
446449
//dw_printf ("slice %d = %d\n", slice, raw);
447450

@@ -452,9 +455,11 @@ dw_printf(stderr, "chan=%d subchan=%d slice=%d raw=%d\n", chan, subchan, slice,
452455

453456
int done = 0;
454457

455-
if (!H->eotd_gathering && H->eotd_acc == PREAMBLE_AND_BARKER_CODE) {
458+
if (!H->eotd_gathering &&
459+
(H->eotd_acc == EOTD_PREAMBLE_AND_BARKER_CODE ||
460+
H->eotd_acc == HOTD_PREAMBLE_AND_BARKER_CODE)) {
456461
#ifdef EOTD_DEBUG
457-
dw_printf ("Barker Code Found\n");
462+
dw_printf ("Barker Code Found %x\n", H->eotd_acc);
458463
#endif
459464
H->olen = 0;
460465
H->eotd_gathering = 1;

0 commit comments

Comments
 (0)
Please sign in to comment.