8b10b issues
This commit is contained in:
parent
67e30fa91e
commit
c8623eb4c6
24 changed files with 1105 additions and 968 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
65
common/8b10b_code_table.c
Normal file
65
common/8b10b_code_table.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
|
||||
34
common/crc32.c
Normal file
34
common/crc32.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
18
common/crc32.h
Normal file
18
common/crc32.h
Normal file
|
|
@ -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__ */
|
||||
13
common/protocol.h
Normal file
13
common/protocol.h
Normal file
|
|
@ -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__ */
|
||||
12
common/xorshift.c
Normal file
12
common/xorshift.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
|
||||
8
common/xorshift.h
Normal file
8
common/xorshift.h
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef __XORSHIFT_H__
|
||||
#define __XORSHIFT_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
uint32_t xorshift32(uint32_t x);
|
||||
|
||||
#endif /* __XORSHIFT_H__ */
|
||||
Loading…
Add table
Add a link
Reference in a new issue