-
Notifications
You must be signed in to change notification settings - Fork 9
/
245_decode.py
282 lines (203 loc) · 7.94 KB
/
245_decode.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
279
280
281
282
#------------------------------------------------------------------------------
#
# 245_decode.py
#
# Decode captured FT245 data
#
# Version 0.0.1 - 4/25/14
#------------------------------------------------------------------------------
import csv
#------------------------------------------------------------------------------
#
# Globals
signal_names = [
'step',
'tck', # D0
'tms', # D1
'nCE', # D2
'nCS', # D3
'tdi', # D4
'LED', # D5
'read', # D6
'mode', # D7
'SDR',
'UDR',
'WR'
]
cpld_tck = 0
cpld_tdi = 0
cpld_tms = 0
# Named CPLD mode constants
CPLD_START = 0
CPLD_BIT = 1
CPLD_BYTE = 2
RISING = 1
FALLING = 0
#------------------------------------------------------------------------------
#
# A generator of captured data
def cvs(file_name):
csvfile = open(file_name, 'rb')
csv_reader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in csv_reader:
# Get this into a more usable format
dc = DataContainer(row)
yield dc
csvfile.close()
#------------------------------------------------------------------------------
#
# A container class for each row of captured data
class DataContainer(object):
def __init__(self, cvs_row):
# Fields of cvs_row become attributes
d = dict((key, value.strip()) for (key, value) in zip(signal_names, cvs_row))
self.__dict__.update(d)
self.cvs_row = cvs_row
#----
@property
def byte_count(self):
# Getter property method for self.value
bits = [s.strip() for s in self.cvs_row[1:7]]
bits.reverse()
return int(''.join(bits), 2)
#----
@property
def bits(self):
# Getter property method for self.value
return [s.strip() for s in self.cvs_row[1:9]]
#----
@property
def now(self):
# Getter property method for self.now
return float(self.step) - 0.5
#------------------------------------------------------------------------------
#
# CPLD Logic
#
# Process FT245 parallel output for bit & byte mode
class CPLD(object):
def __init__(self, byte_stream, mode):
self.byte_stream = byte_stream
self.mode = mode
#----
def run(self):
try:
new_byte = self.byte_stream.next()
byte_count = 0
byte_read = False
while True:
old_byte = new_byte
new_byte = self.byte_stream.next()
t = new_byte.now
if self.mode == CPLD_START:
# Wait for tck == 0
if new_byte.tck == '0':
self.mode = CPLD_BIT
print 'EXIT START at ', new_byte.now
continue
if self.mode == CPLD_BIT:
if new_byte.mode == '1':
byte_count = new_byte.byte_count
if new_byte.read == '1':
byte_read = True
print 'byte shift read mode %d' % byte_count
else:
byte_read = False
print 'byte shift mode %d' % byte_count
self.mode = CPLD_BYTE
continue
if (new_byte.tck, old_byte.tck) == ('1', '0'):
yield (RISING, new_byte.tms, new_byte.tdi, new_byte.read, new_byte.now)
elif (new_byte.tck, old_byte.tck) == ('0', '1'):
yield (FALLING, new_byte.tms, new_byte.tdi, new_byte.read, new_byte.now)
elif self.mode == CPLD_BYTE:
# Byte mode
if byte_count > 0:
shift_bits = new_byte.bits
for bit in shift_bits:
yield (RISING, '0', bit, byte_read, new_byte.now)
yield (FALLING, '0', bit, byte_read, new_byte.now)
byte_count -= 1
else:
self.mode = CPLD_BIT
continue
t = new_byte.now
if t > 600.5:
break
except StopIteration:
print
print 'Done'
#------------------------------------------------------------------------------
#
# SLD controller
#
# State
class SLD_Controller(object):
def __init__(self, node_count, start='reset'):
# A dict describing JTAG state transitions
self.jtag_states = {
'capture_dr': ['shift_dr', 'exit1_dr'],
'capture_ir': ['shift_ir', 'exit1_ir'],
'exit1_dr': ['pause_dr', 'update_dr'],
'exit1_ir': ['pause_ir', 'update_ir'],
'exit2_dr': ['shift_dr', 'update_dr'],
'exit2_ir': ['shift_ir', 'update_ir'],
'idle': ['idle', 'select_dr'],
'pause_dr': ['pause_dr', 'exit2_dr'],
'pause_ir': ['pause_ir', 'exit2_ir'],
'reset': ['idle', 'reset'],
'select_dr': ['capture_dr', 'select_ir'],
'select_ir': ['capture_ir', 'reset'],
'shift_dr': ['shift_dr', 'exit1_dr'],
'shift_ir': ['shift_ir', 'exit1_ir'],
'update_dr': ['idle', 'select_dr'],
'update_ir': ['idle', 'select_dr']
}
self.state = start
self.ir_shift = ''
self.dr_shift = ''
self.ir = ''
self.dr = ''
self.node_count = node_count
#----
def TCK_rise(self, event):
direction, tms, tdi, rd, t = event
self.new_state = self.jtag_states[self.state][int(tms)]
if self.state == 'shift_ir':
self.ir_shift = tdi + self.ir_shift
# print ' ir_shift: %s' % self.ir_shift, int(tms)
elif (self.state == 'shift_dr'):
self.dr_shift = tdi + self.dr_shift
# print ' dr_shift: %s %s at %.1f' % (self.dr_shift, tms, t)
#----
def TCK_fall(self, event):
direction, tms, tdi, rd, t = event
if self.new_state != self.state:
self.state = self.new_state
print ' TAP transition to: %s at %.1f' % (self.state, t)
if self.state == 'capture_dr':
self.dr_shift = ''
elif self.state == 'update_dr':
self.dr = self.dr_shift
print 'UPDATE %d dr bits to %s' % (len(self.dr), self.dr)
elif self.state == 'capture_ir':
self.ir_shift = ''
elif self.state == 'update_ir':
self.ir = self.ir_shift
if len(self.ir):
print 'UPDATE %d ir bits to %s' % (len(self.ir), self.ir)
else:
print
#------------------------------------------------------------------------------
#
# Main
# This .csv file is broken!!
#pld = CPLD(cvs('ft_245_data.csv'), CPLD_START)
pld = CPLD(cvs('test2.csv'), CPLD_BIT)
sld = SLD_Controller(1)
for event in pld.run():
direction, tms, tdi, rd, t = event
if direction == RISING:
sld.TCK_rise(event)
else:
sld.TCK_fall(event)