-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathline_coding.py
278 lines (238 loc) · 7.18 KB
/
line_coding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# only K.28.y control symbols are supported
from migen import *
def disparity(word, nbits):
n0 = 0
n1 = 0
for i in range(nbits):
if word & (1 << i):
n1 += 1
else:
n0 += 1
return n1 - n0
def reverse_table_flip(inputs, flips, nbits):
outputs = [None]*2**nbits
for i, (word, flip) in enumerate(zip(inputs, flips)):
if outputs[word] is not None:
raise ValueError
outputs[word] = i
if flip:
word_n = ~word & (2**nbits-1)
if outputs[word_n] is not None:
raise ValueError
outputs[word_n] = i
for i in range(len(outputs)):
if outputs[i] is None:
outputs[i] = 0
return outputs
def reverse_table(inputs, nbits):
outputs = [None]*2**nbits
for i, word in enumerate(inputs):
if outputs[word] is not None:
raise ValueError
outputs[word] = i
for i in range(len(outputs)):
if outputs[i] is None:
outputs[i] = 0
return outputs
# 5b6b
table_5b6b = [
0b011000,
0b100010,
0b010010,
0b110001,
0b001010,
0b101001,
0b011001,
0b000111,
0b000110,
0b100101,
0b010101,
0b110100,
0b001101,
0b101100,
0b011100,
0b101000,
0b100100,
0b100011,
0b010011,
0b110010,
0b001011,
0b101010,
0b011010,
0b000101,
0b001100,
0b100110,
0b010110,
0b001001,
0b001110,
0b010001,
0b100001,
0b010100,
]
table_5b6b_unbalanced = [bool(disparity(c, 6)) for c in table_5b6b]
table_5b6b_flip = list(table_5b6b_unbalanced)
table_5b6b_flip[7] = True
table_6b5b = reverse_table_flip(table_5b6b, table_5b6b_flip, 6)
# K.28
table_6b5b[0b001111] = 0b11100
table_6b5b[0b110000] = 0b11100
# 3b4b
table_3b4b = [
0b0100,
0b1001,
0b0101,
0b0011,
0b0010,
0b1010,
0b0110,
0b0001, # primary D.x.7
]
table_3b4b_unbalanced = [bool(disparity(c, 4)) for c in table_3b4b]
table_3b4b_flip = list(table_3b4b_unbalanced)
table_3b4b_flip[3] = True
table_4b3b = reverse_table_flip(table_3b4b, table_3b4b_flip, 4)
# alternative D.x.7
table_4b3b[0b0111] = 0b0111
table_4b3b[0b1000] = 0b0111
table_4b3b_kn = reverse_table(table_3b4b, 4)
table_4b3b_kp = reverse_table([~x & 0b1111 for x in table_3b4b], 4)
# primary D.x.7 is not used
table_4b3b_kn[0b0001] = 0b000
table_4b3b_kn[0b1000] = 0b111
table_4b3b_kp[0b1110] = 0b000
table_4b3b_kp[0b0111] = 0b111
class SingleEncoder(Module):
def __init__(self, lsb_first=False):
self.d = Signal(8)
self.k = Signal()
self.disp_in = Signal()
self.output = Signal(10)
self.disp_out = Signal()
# # #
# stage 1: 5b/6b and 3b/4b encoding
code5b = self.d[:5]
code6b = Signal(6)
code6b_unbalanced = Signal()
code6b_flip = Signal()
self.sync += [
If(self.k,
code6b.eq(0b110000),
code6b_unbalanced.eq(1),
code6b_flip.eq(1)
).Else(
code6b.eq(Array(table_5b6b)[code5b]),
code6b_unbalanced.eq(Array(table_5b6b_unbalanced)[code5b]),
code6b_flip.eq(Array(table_5b6b_flip)[code5b])
)
]
code3b = self.d[5:]
code4b = Signal(4)
code4b_unbalanced = Signal()
code4b_flip = Signal()
self.sync += [
code4b.eq(Array(table_3b4b)[code3b]),
code4b_unbalanced.eq(Array(table_3b4b_unbalanced)[code3b]),
If(self.k,
code4b_flip.eq(1)
).Else(
code4b_flip.eq(Array(table_3b4b_flip)[code3b])
)
]
alt7_rd0 = Signal() # if disparity is -1, use alternative D.x.7
alt7_rd1 = Signal() # if disparity is +1, use alternative D.x.7
self.sync += [
alt7_rd0.eq(0),
alt7_rd1.eq(0),
If(code3b == 7,
If((code5b == 17) | (code5b == 18) | (code5b == 20),
alt7_rd0.eq(1)),
If((code5b == 11) | (code5b == 13) | (code5b == 14),
alt7_rd1.eq(1)),
If(self.k,
alt7_rd0.eq(1),
alt7_rd1.eq(1)
)
)
]
# stage 2 (combinatorial): disparity control
output_6b = Signal(6)
disp_inter = Signal()
self.comb += [
disp_inter.eq(self.disp_in ^ code6b_unbalanced),
If(~self.disp_in & code6b_flip,
output_6b.eq(~code6b)
).Else(
output_6b.eq(code6b)
)
]
output_4b = Signal(4)
self.comb += [
If(~disp_inter & alt7_rd0,
self.disp_out.eq(~disp_inter),
output_4b.eq(0b0111)
).Elif(disp_inter & alt7_rd1,
self.disp_out.eq(~disp_inter),
output_4b.eq(0b1000)
).Else(
self.disp_out.eq(disp_inter ^ code4b_unbalanced),
If(~disp_inter & code4b_flip,
output_4b.eq(~code4b)
).Else(
output_4b.eq(code4b)
)
)
]
output_msb_first = Signal(10)
self.comb += output_msb_first.eq(Cat(output_4b, output_6b))
if lsb_first:
for i in range(10):
self.comb += self.output[i].eq(output_msb_first[9-i])
else:
self.comb += self.output.eq(output_msb_first)
class Encoder(Module):
def __init__(self, nwords=1, lsb_first=False):
self.d = [Signal(8) for _ in range(nwords)]
self.k = [Signal() for _ in range(nwords)]
self.output = [Signal(10) for _ in range(nwords)]
# # #
encoders = [SingleEncoder(lsb_first) for _ in range(nwords)]
self.submodules += encoders
self.sync += encoders[0].disp_in.eq(encoders[-1].disp_out)
for e1, e2 in zip(encoders, encoders[1:]):
self.comb += e2.disp_in.eq(e1.disp_out)
for d, k, output, e in zip(self.d, self.k, self.output, encoders):
self.comb += [
e.d.eq(d),
e.k.eq(k)
]
self.sync += output.eq(e.output)
class Decoder(Module):
def __init__(self, lsb_first=False):
self.input = Signal(10)
self.d = Signal(8)
self.k = Signal()
# # #
input_msb_first = Signal(10)
if lsb_first:
for i in range(10):
self.comb += input_msb_first[i].eq(self.input[9-i])
else:
self.comb += input_msb_first.eq(self.input)
code6b = input_msb_first[4:]
code5b = Signal(5)
code4b = input_msb_first[:4]
code3b = Signal(3)
self.sync += [
self.k.eq(0),
If(code6b == 0b001111,
self.k.eq(1),
code3b.eq(Array(table_4b3b_kn)[code4b])
).Elif(code6b == 0b110000,
self.k.eq(1),
code3b.eq(Array(table_4b3b_kp)[code4b])
).Else(
code3b.eq(Array(table_4b3b)[code4b])
),
code5b.eq(Array(table_6b5b)[code6b])
]
self.comb += self.d.eq(Cat(code5b, code3b))