From c8623eb4c6c1464ffd49e83126e66d71ba5bf862 Mon Sep 17 00:00:00 2001 From: jaseg Date: Mon, 2 Oct 2023 01:23:31 +0200 Subject: 8b10b issues --- common/8b10b.c | 5 +++- common/8b10b_code_table.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++ common/crc32.c | 34 +++++++++++++++++++++++++ common/crc32.h | 18 +++++++++++++ common/protocol.h | 13 ++++++++++ common/xorshift.c | 12 +++++++++ common/xorshift.h | 8 ++++++ 7 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 common/8b10b_code_table.c create mode 100644 common/crc32.c create mode 100644 common/crc32.h create mode 100644 common/protocol.h create mode 100644 common/xorshift.c create mode 100644 common/xorshift.h (limited to 'common') diff --git a/common/8b10b.c b/common/8b10b.c index fd89e6f..4908e1f 100644 --- a/common/8b10b.c +++ b/common/8b10b.c @@ -181,8 +181,10 @@ int xfr_8b10b_feed_bit(struct state_8b10b_dec *st, int bit) { int p3b = map_4b3b[pattern & 0xf]; int p5b = map_6b5b[pattern >> 4]; - if (p3b == 0 || p5b == 0) + if (p3b == 0 || p5b == 0) { + st->bit_ctr = 0; return -DECODING_ERROR; + } p3b = (p3b == -1) ? 0 : p3b; p5b = (p5b == -1) ? 0 : p5b; @@ -191,6 +193,7 @@ int xfr_8b10b_feed_bit(struct state_8b10b_dec *st, int bit) { } else if (st->bit_ctr > 0) { st->bit_ctr++; } /* else we do not have sync yet */ + return -DECODING_IN_PROGRESS; } diff --git a/common/8b10b_code_table.c b/common/8b10b_code_table.c new file mode 100644 index 0000000..df2623a --- /dev/null +++ b/common/8b10b_code_table.c @@ -0,0 +1,65 @@ +#include +#include + +#include "8b10b.h" + +static const char * const rc_names[] = { + [K28_0] = "K.28.0", + [K28_1] = "K.28.1", + [K28_2] = "K.28.2", + [K28_3] = "K.28.3", + [K28_4] = "K.28.4", + [K28_5] = "K.28.5", + [K28_6] = "K.28.6", + [K28_7] = "K.28.7", + [K23_7] = "K.23.7", + [K27_7] = "K.27.7", + [K29_7] = "K.29.7", + [K30_7] = "K.30.7", +}; + +int hex_to_uint(const char *s, size_t len) { + if (len > 7) + return -2; + + int acc = 0; + for (int i=0; i>1) ^ 0xEDB88320) if (st & 1) else (st>>1) + * In [27]: print(',\n'.join([ '0x{:08x}'.format(apply(apply(apply(apply(x))))) for x in range(16) ])) + */ +static uint32_t crc32_table[16] = { + 0x00000000, + 0x1db71064, + 0x3b6e20c8, + 0x26d930ac, + 0x76dc4190, + 0x6b6b51f4, + 0x4db26158, + 0x5005713c, + 0xedb88320, + 0xf00f9344, + 0xd6d6a3e8, + 0xcb61b38c, + 0x9b64c2b0, + 0x86d3d2d4, + 0xa00ae278, + 0xbdbdf21c +}; + +uint32_t crc32_update(uint32_t state, uint8_t c) +{ + state ^= c; + state = (state>>4) ^ crc32_table[state&0xf]; + state = (state>>4) ^ crc32_table[state&0xf]; + return state; +} diff --git a/common/crc32.h b/common/crc32.h new file mode 100644 index 0000000..badf467 --- /dev/null +++ b/common/crc32.h @@ -0,0 +1,18 @@ +#ifndef __CRC32_H__ +#define __CRC32_H__ + +#include + +uint32_t crc32_update(uint32_t old_state, uint8_t c); + +inline static uint32_t crc32_reset(void); +uint32_t crc32_reset() { + return ~0; +} + +inline static uint32_t crc32_finalize(uint32_t state); +uint32_t crc32_finalize(uint32_t state) { + return ~state; +} + +#endif /* __CRC32_H__ */ diff --git a/common/protocol.h b/common/protocol.h new file mode 100644 index 0000000..30f3e0c --- /dev/null +++ b/common/protocol.h @@ -0,0 +1,13 @@ +#ifndef __PROTOCOL_H__ +#define __PROTOCOL_H__ + +#include + +struct __attribute__((__packed__)) data_packet { + uint8_t channels[16]; + uint8_t brightness[8]; + uint8_t flags; + uint32_t crc; +}; + +#endif /* __PROTOCOL_H__ */ diff --git a/common/xorshift.c b/common/xorshift.c new file mode 100644 index 0000000..51f9c80 --- /dev/null +++ b/common/xorshift.c @@ -0,0 +1,12 @@ + +#include "xorshift.h" + +uint32_t xorshift32(uint32_t x) +{ + /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */ + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + return x; +} + diff --git a/common/xorshift.h b/common/xorshift.h new file mode 100644 index 0000000..8d9f448 --- /dev/null +++ b/common/xorshift.h @@ -0,0 +1,8 @@ +#ifndef __XORSHIFT_H__ +#define __XORSHIFT_H__ + +#include + +uint32_t xorshift32(uint32_t x); + +#endif /* __XORSHIFT_H__ */ -- cgit