aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/8b10b.c5
-rw-r--r--common/8b10b_code_table.c65
-rw-r--r--common/crc32.c34
-rw-r--r--common/crc32.h18
-rw-r--r--common/protocol.h13
-rw-r--r--common/xorshift.c12
-rw-r--r--common/xorshift.h8
7 files changed, 154 insertions, 1 deletions
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 <stdio.h>
+#include <string.h>
+
+#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<len; i++) {
+ int digit = s[i];
+ if ('0' <= digit && digit <= '9')
+ digit -= '0';
+ else if ('a' <= digit && digit <= 'f')
+ digit -= 'a' - 0xa;
+ else if ('A' <= digit && digit <= 'F')
+ digit -= 'A' - 0xA;
+ else return -1;
+ acc = acc << 4 | digit;
+ }
+
+ return acc;
+}
+
+int main(void) {
+ struct state_8b10b_enc st;
+
+ printf("Comma symbols (RD -/+)\n");
+ for (int i=1; i<K_CODES_LAST; i++) {
+ st.rd = -1;
+ int sym_neg = xfr_8b10b_encode(&st, -i);
+ st.rd = +1;
+ int sym_pos = xfr_8b10b_encode(&st, -i);
+ printf("%s %010b %010b\n", rc_names[i], sym_neg, sym_pos);
+ }
+
+ printf("\n");
+ printf("Data symbols (RD -/+)\n");
+ for (int i=0; i<256; i++) {
+ st.rd = -1;
+ int sym_neg = xfr_8b10b_encode(&st, i);
+ st.rd = +1;
+ int sym_pos = xfr_8b10b_encode(&st, i);
+ printf("%02x %010b %010b\n", i, sym_neg, sym_pos);
+ }
+
+ return 0;
+}
+
diff --git a/common/crc32.c b/common/crc32.c
new file mode 100644
index 0000000..3f8d128
--- /dev/null
+++ b/common/crc32.c
@@ -0,0 +1,34 @@
+#include "crc32.h"
+
+/* Polynomial: 0xEDB88320 */
+
+/* python3:
+ * In [26]: apply = lambda st: ((st>>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 <unistd.h>
+
+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 <stdint.h>
+
+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 <stdint.h>
+
+uint32_t xorshift32(uint32_t x);
+
+#endif /* __XORSHIFT_H__ */