-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_link.py
166 lines (145 loc) · 4.7 KB
/
test_link.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
import numpy as np
import unittest
from migen import *
import link
class TestSlip(unittest.TestCase):
def setUp(self):
self.dut = link.Slipper(width=3)
def test_init(self):
self.assertEqual(len(self.dut.data), 3)
def do(self, data):
yield self.dut.data.eq(data)
yield
valid = yield self.dut.valid
yield
bitslip = yield self.dut.bitslip
return valid, bitslip
def test_seq(self):
def gen():
v, b = yield from self.do(0b000)
self.assertEqual(v, 1)
self.assertEqual(b, 0)
v, b = yield from self.do(0b111)
self.assertEqual(v, 1)
self.assertEqual(b, 0)
v, b = yield from self.do(0b001)
self.assertEqual(v, 0)
self.assertEqual(b, 1)
for i in range(3):
yield
self.assertEqual((yield self.dut.bitslip), 0)
yield
self.assertEqual((yield self.dut.bitslip), 1)
run_simulation(self.dut, gen())
def pack(data):
n_frame = 10
n_data = 7
t_clk = 8
n_marker = n_frame // 2 + 1
n_crc = 6
assert len(data) == n_frame * (n_data - 1) * t_clk - n_marker - n_crc
frame = []
for i in range(n_frame):
for j in range(t_clk):
b = 0
if j >= t_clk // 2:
b |= 1
if j == t_clk - 1:
if i == n_frame - 1:
# CRC-6-GSM
# b |= CRC(0x3f, data_width=6)(frame) << 1
b |= 0x3F << 1 # TODO
frame.append(b)
continue
elif i == n_frame - 2:
b |= 2 # marker
elif i < n_frame - 1 - n_marker:
b |= data.pop(0) << 1
else:
b |= data.pop(0) << 1
for k in range(2, n_data):
b |= data.pop(0) << k
frame.append(b)
assert len(data) == 0, data
return frame
def bytes_to_bits(byt):
bit = []
for b in byt:
for i in reversed(range(8)):
bit.append((b >> i) & 1)
return bit
class TestUnframe(unittest.TestCase):
def setUp(self):
self.dut = link.Unframer(n_data=7, t_clk=8, n_frame=10)
self.check = link.Checker(n_data=6, t_clk=8, n_frame=10)
self.dut.submodules += self.check
self.dut.comb += [
self.check.data.eq(self.dut.data_out),
self.check.data_stb.eq(self.dut.data_out_stb),
self.check.end_of_frame.eq(self.dut.end_of_frame),
]
def test_init(self):
self.assertEqual(len(self.dut.data_in), 7)
self.assertEqual(len(self.dut.data_out), 6)
def feed_bits(self, bits):
for b in bits:
yield self.dut.data_in.eq(b)
yield self.dut.data_in_stb.eq(1)
yield
def record_frame(self, bits, n_max=100):
i = 0
while True:
if (yield self.dut.data_out_stb):
bits.append((yield self.dut.data_out))
if (yield self.dut.end_of_frame):
break
yield
i += 1
if n_max and i > n_max:
break
def record_check(self, bits, n_max=100):
i = 0
while True:
if (yield self.check.frame_stb):
bits.append((yield self.check.frame))
break
yield
i += 1
if n_max and i > n_max:
break
def test_mini(self):
bits = []
run_simulation(self.dut, [self.feed_bits([]), self.record_frame(bits)])
self.assertEqual(bits, [])
def test_zeros(self):
frame = pack([0] * (10 * 8 * 6 - 6 - 6))
frame[-1] = 1 | (0x13 << 1) # crc
bits = []
run_simulation(self.dut, [self.feed_bits(frame), self.record_frame(bits)])
self.assertEqual(len(bits), 10 * 8)
self.assertEqual(bits[-8 - 1], 1)
self.assertTrue(
all(
bits[i] == 0
for i in range(len(bits))
if i not in (len(bits) - 8 - 1, len(bits) - 1)
),
bits,
)
def test_ones(self):
frame = pack([1] * (10 * 8 * 6 - 6 - 6))
frame[-1] = 1 | (0x0B << 1) # crc
bits = []
rec_frame = []
run_simulation(
self.dut,
[
self.feed_bits(frame),
self.record_frame(bits),
self.record_check(rec_frame),
],
)
self.assertEqual(len(bits), 10 * 8)
# self.assertEqual(bits[-8 - 1], 0x3f)
self.assertEqual(len(rec_frame), 1)
# self.assertEqual(rec_frame[0], (1 << 10*8*6 - 6 - 6) - 1)