-
Notifications
You must be signed in to change notification settings - Fork 0
/
stim2vcd.py
337 lines (233 loc) · 7.97 KB
/
stim2vcd.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/python
import os
import sys
import string
import datetime
from argparse import ArgumentParser
from optparse import OptionParser
### Global variables ###
# Pathname of simulation output file
s_stim_file_path = ''
# Pathname of VCD file
s_vcd_file_path = ''
# Name of simulator
s_version = 'Atmel Studio 6.1'
# List of all VCD variable names
s_vcd_vars = []
# List of all VCD variable objects
vcd_vars = []
# List of all lines to be printed to VCD file
s_vcd_lines = []
# Interconnect System's Wave program does not work
# with timescales other than '1'
# Need to multiply base time unit to get true scale
s_timescale = '1 us'
n_time_multiplier = 1
# Elapsed simulation time
n_cur_time = 0
class VCD_Var:
n_printable_indx = 0
def __init__(
self,
s_ref,
n_width = 8
):
self.s_id = string.printable[ VCD_Var.n_printable_indx ]
self.s_ref = s_ref
self.n_width = n_width
self.sub_vcd_vars = []
self.n_prev_value = 0
VCD_Var.n_printable_indx = VCD_Var.n_printable_indx + 1
if( self.n_width > 1 ):
for n_bit_indx in range( n_width ):
self.sub_vcd_vars.append( VCD_Var(
s_ref = ( '%s%d' % ( self.s_ref, n_bit_indx ) ),
n_width = 1
) )
def Get_Var_Lines(
self
):
s_var_lines = []
s_var_lines
s_var_lines.append( '$var reg %(n_width)d %(s_id)s %(s_ref)s $end\n' % {
"n_width":self.n_width,
"s_id":self.s_id,
"s_ref":self.s_ref
} )
if( self.n_width > 1 ):
s_var_lines.append( '$scope module %sbits $end\n' % self.s_ref )
for sub_vcd_var in self.sub_vcd_vars:
s_var_lines = s_var_lines + sub_vcd_var.Get_Var_Lines()
s_var_lines.append( '$upscope $end\n' )
return s_var_lines
def Get_Scope_Lines(
self
):
s_scope_lines = []
s_scope_lines.append( '$scope begin %s $end\n' % self.s_ref )
if( self.n_width > 1 ):
for sub_vcd_var in self.sub_vcd_vars:
s_scope_lines = s_scope_lines + sub_vcd_var.Get_Scope_Lines()
return s_scope_lines
def Get_Var_Dump_Lines(
self
):
s_dump_lines = []
s_cur_dump_line = ''
s_sub_dump_lines = []
if( self.n_width > 1 ):
s_cur_dump_line = 'b'
for n_bit_indx in range( self.n_width ):
s_cur_dump_line = '%s0' % ( s_cur_dump_line )
s_sub_dump_lines = s_sub_dump_lines + self.sub_vcd_vars[ n_bit_indx ].Get_Var_Dump_Lines()
s_cur_dump_line = s_cur_dump_line + ' '
else:
s_cur_dump_line = '0'
s_cur_dump_line = s_cur_dump_line + self.s_id + '\n'
s_dump_lines.append( s_cur_dump_line )
s_dump_lines = s_dump_lines + s_sub_dump_lines
return s_dump_lines
def Get_Value_Dump_Lines(
self,
n_new_value
):
if( n_new_value == self.n_prev_value ):
return []
self.n_prev_value = n_new_value
s_dump_lines = []
s_cur_dump_line = ''
s_sub_dump_lines = []
n_cur_value_rot = n_new_value
if( self.n_width > 1 ):
for n_bit_indx in range( self.n_width ):
n_cur_bit = n_cur_value_rot & 1
n_cur_value_rot = n_cur_value_rot >> 1
s_cur_dump_line = '%d%s' % (
n_cur_bit,
s_cur_dump_line
)
s_sub_dump_lines = s_sub_dump_lines + self.sub_vcd_vars[ n_bit_indx ].Get_Value_Dump_Lines( n_cur_bit )
s_cur_dump_line = 'b' + s_cur_dump_line + ' '
else:
s_cur_dump_line = '%d' % n_new_value
s_cur_dump_line = s_cur_dump_line + self.s_id + '\n'
s_dump_lines.append( s_cur_dump_line )
s_dump_lines = s_dump_lines + s_sub_dump_lines
return s_dump_lines
print( 'INFO: Starting program...' )
# Parse command-line arguments
s_usage = 'usage: %prog [options] stimulus-file'
arg_parser = ArgumentParser()
#opt_parser = OptionParser(
# usage = s_usage
# )
arg_parser.add_argument(
'stimulus_file_path',
help = 'Pathname of stimulus file to read'
)
#opt_parser.add_option(
# '-o',
# '--output',
# action = 'store',
# type = str,
# dest = 's_vcd_file_path',
# default = '',
# help = 'Pathname of file to print VCD to',
# metavar = 'FILE'
# )
s_args = arg_parser.parse_args()
#( opt_names, opt_values ) = opt_parser.parse_args()
s_stim_file_path = s_args.stimulus_file_path
n_stim_file_base_end_pos = s_stim_file_path.find( '.sim_out' )
if( n_stim_file_base_end_pos == -1 ):
print 'ERROR: Incorrect file type'
sys.exit( -1 )
s_vcd_file_path = s_stim_file_path[ : n_stim_file_base_end_pos ]
s_vcd_file_path = s_vcd_file_path + '.vcd'
# Open simulation output file
print( 'INFO: Opening %s...\n' % s_stim_file_path )
stim_file = open(
s_stim_file_path,
'r'
)
s_stim_lines = stim_file.readlines()
stim_file.close()
# Enumerate all listed variables
for s_stim_line in s_stim_lines:
if( s_stim_line[ 0 ] == '$' ):
print( 'ERROR: AS directive found in stimulus file.' )
sys.exit( -1 )
if( s_stim_line[ 0 ] == '#' ):
continue
s_stim_line_tokens = s_stim_line.split()
s_cur_var = s_stim_line_tokens[ 0 ]
if( s_cur_var not in s_vcd_vars ):
s_vcd_vars.append( s_cur_var )
print( 'INFO: Found %d logged variables.' % len( s_vcd_vars ) )
# Populate VCD object list
for s_vcd_var in s_vcd_vars:
vcd_vars.append( VCD_Var( s_vcd_var ) )
# Compose date specifier
t_now = datetime.datetime.now()
s_vcd_lines.append(
'$date ' +
t_now.strftime( '%b %d %Y %H:%M:%S' ) +
' $end\n'
)
# Compose simulator version specifier
s_vcd_lines.append(
'$version ' +
s_version +
' $end\n'
)
# Compose timescale specifier
s_vcd_lines.append(
'$timescale ' +
s_timescale +
' $end\n'
)
# Compose variable declarations
s_vcd_lines.append( '$scope module top $end\n' )
for vcd_var in vcd_vars:
s_vcd_lines = s_vcd_lines + vcd_var.Get_Var_Lines()
s_vcd_lines.append( '$upscope $end\n' )
s_vcd_lines.append( '$enddefinitions $end\n' )
# Compose scope declarations
#for vcd_var in vcd_vars:
# s_vcd_lines = s_vcd_lines + vcd_var.Get_Scope_Lines()
# Compose variable dump lines
s_vcd_lines.append( '#0\n' )
s_vcd_lines.append( '$dumpvars\n' )
for vcd_var in vcd_vars:
s_vcd_lines = s_vcd_lines + vcd_var.Get_Var_Dump_Lines()
s_vcd_lines.append( '$end\n' )
for vcd_var in vcd_vars:
s_vcd_lines = s_vcd_lines + vcd_var.Get_Value_Dump_Lines( 0 )
# Compose value dump lines
print( 'INFO: Parsing value dump...' )
for s_stim_line in s_stim_lines:
if( s_stim_line[ 0 ] == '#' ):
s_time_delay = s_stim_line[ 1 :].strip()
n_time_delay = int( s_time_delay ) * n_time_multiplier
n_cur_time = n_cur_time + n_time_delay
s_vcd_lines.append( '#%d\n' % n_cur_time )
continue
s_stim_line_tokens = s_stim_line.split()
for vcd_var in vcd_vars:
if( s_stim_line_tokens[ 0 ] == vcd_var.s_ref ):
s_new_value = s_stim_line_tokens[ 2 ]
s_new_value = s_new_value.replace( '0x', '' )
s_vcd_lines = s_vcd_lines + vcd_var.Get_Value_Dump_Lines( int( s_new_value, 16 ) )
# Write all lines to VCD file
print( 'INFO: Writing VCD to %s' % s_vcd_file_path )
vcd_file = open(
s_vcd_file_path,
'wb'
)
vcd_file.writelines( s_vcd_lines )
vcd_file.close()
# Delete simulation output file to avoid concatenation
# with next simulation's output
print( 'INFO: Deleting %s' % s_stim_file_path )
os.remove( s_stim_file_path )
print( 'INFO: End of program' )