-
Notifications
You must be signed in to change notification settings - Fork 0
/
verilog_format.py
599 lines (551 loc) · 19.3 KB
/
verilog_format.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#!/usr/bin/python
# company: nudt-wdzs-671
# filename: verilog_format.py
# description: format verilog file
# author: liusheng
# revision List:
# (rn: date : modifier: description)
# r1: 2021.09.08 liusheng create it
# r2: 2021.09.10 liusheng add the declaration align role.
# r3: 2021.10.03 lizhuoqian add the config format
# r4: 2021.10.03 litianhao add the conf/prim/spcfy/table judge code
# r5: 2021.10.10 litianhao add the table formatter, but need to fix
# r6: 2021.10.17 litianhao add the blank format to keep two blank lines only
# and debug for last code
# r7: 2021.11.08 litianhao add the assign align and format of assign's formula
# and debug for code of table module
# work to be done:
# 0) add the config/primitive/specify/table judge code
# 1) multi blank lines only keep two blank lines
# 2) assign align
# 3) inst align
# 4) translate chinese into english
import sys
import re
import os
POS0_DEFAULT = 7
POS1_DEFAULT = 28
POS2_DEFAULT = 50
POS0_OUT_DEFAULT = 12
POS1_OUT_DEFAULT = 28
# RTL modole Judging
def rtl_has(string, key):
if (string.find(" " + key + " ") != -1): # 0: ##_key_##
return True
if (string.find(" " + key + "(") != -1): # 1: ##_key(##
return True
if (string.find(" " + key + ":") != -1): # 2: ##_key:##
return True
elif (string.find(")" + key + " ") != -1): # 3: ##)key_##
return True
elif (string.find(")" + key + ":") != -1): # 4: ##)key:##
return True
elif (string.find(" " + key + "(") != -1): # 5: ##_key(##
return True
# elif (string.find(":" + key + ' ') != -1): # new: ##:key_##
# return True
elif (string.startswith(key + ' ')): # 6: key_###
return True
elif (string.startswith(key + '(')): # 7: key(###
return True
elif (string.startswith(key + ':')): # 8: key:###
return True
elif (string.endswith(' ' + key)): # 9: ###_key
return True
elif (string.endswith(')' + key)): # 10: ###)key
return True
elif (string.endswith(':' + key)): # new: ###:key
return True
elif (string.startswith(key) and string.endswith(key)): # 11: key###key
return True
else:
return False
def rtl_real_has(string, key1, key2):
if (rtl_has(string, key1)):
if (not rtl_has(string, key2)):
return True
else:
return False
return False
def rtl_just_has(string, key):
if (string.find(key) != -1):
return True
else:
return False
# To find the mapping of {} or if/endif or [] or so on....
def rtl_real_just_has(string, key1, key2):
if (rtl_just_has(string, key1)):
if (not rtl_just_has(string, key2)):
return True
else:
return False
return False
# To get the comment and delete it
def del_comment(type, string):
if (type == 0): # 0:no comment
return string + " "
elif (type == 1): # 1:all comment
return '' + " "
elif (type == 2): # 2:#####//
num = string.find('//')
temp1 = string[0:num]
return temp1 + " "
elif (type == 3): # 3:#####/*
num = string.find('/*')
temp1 = string[0:num]
return temp1 + " "
elif (type == 4): # 4:*/#####
num = string.rfind('*/')
temp1 = string[num + 2:]
return temp1 + " "
elif (type == 5): # 5:####/*...*/#####
num1 = string.find('/*')
num2 = string.rfind('*/')
temp1 = string[0:num1] + " " + string[num2 + 2:]
return temp1 + " "
else:
return " "
# To change the format to the Standard Form
def line_format(type, string):
if (type != 1 and (type != 2)):
string = re.sub(': ', ':', string)
string = re.sub(' :', ':', string)
string = re.sub(' ;', ';', string)
string = re.sub(' ,', ',', string)
if (type == 0): # 0:no comment
temp1 = string.replace('\t', ' ')
temp2 = ' '.join(temp1.split())
temp2.lstrip()
return temp2
elif (type == 1): # 1:all comment
temp1 = string[0:-1]
temp2 = ' '.join(temp1.split())
temp2.lstrip()
temp2 = " " + temp2
return temp2
elif (type == 2): # 2:#####//
num = string.find('//')
temp1 = string[0:num]
temp2 = temp1.replace('\t', ' ')
temp2 = ' '.join(temp1.split())
temp2.lstrip()
return temp2 + string[num:-1]
elif (type == 3): # 3:#####/*
num = string.find('/*')
temp1 = string[0:num]
temp2 = temp1.replace('\t', ' ')
temp2 = ' '.join(temp1.split())
temp2.lstrip()
return temp2 + string[num:-1]
elif (type == 4): # 4:*/#####
num = string.rfind('/*')
temp1 = string[num + 2:]
temp2 = temp1.replace('\t', ' ')
temp2 = ' '.join(temp1.split())
temp2.lstrip()
temp3 = string[0:num + 2]
temp3.lstrip()
return temp3 + temp2
elif (type == 5): # 5:####/*...*/#####
num1 = string.find('/*')
num2 = string.rfind('*/')
temp1 = string[0:num1]
temp2 = string[num2 + 2:]
temp3 = temp1.replace('\t', ' ')
temp3 = ' '.join(temp3.split())
temp3.lstrip()
temp4 = temp2.replace('\t', ' ')
temp4 = ' '.join(temp4.split())
temp4.lstrip()
return temp3 + string[num1:num2 + 2] + temp4
else:
return ""
# To get same align for input / inout / ...
def declare_align(string):
can_align = 0
pos1 = 0
pos2 = 0
list0 = ['input[', 'output[', 'inout[', 'wire[', 'reg[', 'logic[', 'bit[']
list1 = ['input', 'output', 'inout', 'rand']
list2 = ['wire', 'reg', 'logic', 'bit']
for x in list0 + list1 + list2:
if (string.startswith(x)):
string = re.sub('\[', ' [', string)
string = re.sub(' ;', ';', string)
string = re.sub(' ,', ',', string)
temp1 = string.split()
if (len(temp1) > 0):
# eg: reg [35:0] araddr
if (temp1[0] in list2):
pos0 = POS0_DEFAULT
pos1 = POS1_DEFAULT
pos2 = POS2_DEFAULT
can_align = 1
if (len(temp1) > 1):
# eg: input [35:0] addr;
if ((temp1[0] in list1) and (not (temp1[1] in list2))):
pos0 = POS0_DEFAULT
pos1 = POS1_DEFAULT
pos2 = POS2_DEFAULT
can_align = 2
# eg: output reg [5:0] id;
if ((temp1[0] in list1) and ((temp1[1] in list2))):
pos0 = POS0_OUT_DEFAULT
pos1 = POS1_OUT_DEFAULT
pos2 = POS2_DEFAULT
can_align = 3
# print pos1
# print pos2
# print can_align
# print temp1
if (can_align == 1 or can_align == 2 or can_align == 3):
string = re.sub(']', '] ', string)
string = re.sub(' ]', ']', string)
string = re.sub('\[ ', '[', string)
temp1 = string.split()
if (can_align == 1):
string = ''
else:
string = '' + (temp1[0])
string = string.ljust(7, ' ')
temp1 = temp1[1:]
if (temp1[0] in list2):
string = string + (temp1[0])
temp1 = temp1[1:]
if (str(temp1[0]).startswith('[')):
string = string.ljust(pos0, ' ')
string = string + (temp1[0])
temp1 = temp1[1:]
if (len(string)) < pos1:
string = string.ljust(pos1, ' ')
temp1 = ' '.join(temp1)
temp1 = temp1.split('//')
if (len(temp1) == 1):
string = string + ''.join(temp1)
return string
else:
string = string + temp1[0]
if (len(string)) < pos2:
string = string.ljust(pos2, ' ')
temp1 = temp1[1:]
string = string + '//' + '//'.join(temp1)
return string
else:
return string
# Assign align for signal
def is_signal(string):
signal = -1
list2 = ["<=", "=="]
list1 = ["=", "+", "-", "*", "/", "&", "|", "!", "?", ":"]
list0 = ["(", ")", "[", "]", "{", "}"]
if string[0:2] in list2:
signal = string[0:2]
elif string[0] in list1:
signal = string[0]
elif string[0] in list0:
signal = string[0]
else:
if string[0].isalnum() or string[0] == "_":
signal = -1
return signal
# Assign align
def assign_align(string):
can_align = 0
pos1, pos2 = 0, 0
list0 = ["assign"]
bracket = ["(", ")", "{", "}", "[", "]"]
temp = string.split()
if len(temp) == 0:
return string
if not (temp[0] in list0):
return string
NoBlankString = "".join(temp[1:])
# print(NoBlankString)
putpos = []
BlankString = ""
range_list = list(range(len(NoBlankString)))
InMiddleBracket = False
for i in range_list:
signal = is_signal(NoBlankString[i:])
if signal != -1:
if not (signal in bracket):
if not InMiddleBracket:
BlankString += " " + signal + " "
else:
BlankString += signal
if signal == "[":
InMiddleBracket = True
elif signal == "]":
InMiddleBracket = False
if len(signal) > 1:
for j in range(len(signal) - 1):
range_list.remove(i + j)
else:
BlankString += NoBlankString[i]
return temp[0] + " " + BlankString
# return string
# config's formatter
# just for small portion of all codes...
def config_format(string):
pos0 = 0;
pos1 = 0;
list0 = ['design', 'default', 'instance']
for x in list0:
if(string.startswith(x)):
string=re.sub(' ;',';',string)
string=re.sub(' ,',',',string)
temp = string.split()
if(len(temp) > 0):
if(temp[0] == list0[0]):
string = temp[0].ljust(7, ' ')
string = string + temp[1]
elif(temp[0] == list0[1]):
string = temp[0].ljust(8, ' ')
string = string + temp[1].ljust(8, ' ')
temp = ' '.join(temp)
string = string + temp
elif(temp[0] == list0[2]):
string = temp[0].ljust(9, ' ')
string = string + temp[1].ljust(15, ' ')
string = string + temp[2].ljust(8, ' ')
temp = ' '.join(temp)
string = string + temp
# need to fix...
def get_data_length(line):
temp1 = line.split()
input_number = len(temp1) - 1
data_length = []
pos1 = 0
pos2 = 0
pos_colon = 0
line = re.sub('\\*\\', '', line)
for i in range(len(line)):
if line[i] == ':':
pos_colon = i
if i != 0:
if line[i - 1] == ' ' and line[i] != ' ':
pos1 = i
if line[i - 1] != ' ' and line[i] == ' ':
data_length.append(pos1 - pos2 + (i - pos1) / 2)
pos2 = i - 1
return input_number, data_length
# make table keep one ' ' between 0/1/:
def table_formatter(line):
temp1 = line.split(' ')
temp2 = []
for temp in temp1:
if temp != ":" and temp.find(":") != -1:
poscolon = temp.find(":")
if poscolon != 0:
temp2.append(temp[0: poscolon])
temp2.append(":")
if poscolon != len(temp) - 1:
temp2.append(temp[poscolon + 1: ])
else:
temp2.append(temp)
line = ' '.join(temp2)
# print(line)
return line
# To judge if all the line only has blank words
def Judge_Blank(line):
for word in line:
if word != ' ' and word != '\t' and word != '\n':
# print(word, file=fileout)
return 0
return 1
#### main ###########################################################################################
if __name__ == "__main__":
if (sys.argv[1] == '-h'):
print('This tool can format verilog file.')
print('please type "verilog_format.py name.v", it will geneate the formatted name.v')
print('please type "verilog_format.py old_name.v new_name.v", it will geneate the formatted new_name.v')
print('The author of this sofware is liusheng from NUDTWDZS, China.')
sys.exit()
filepath = sys.argv[1]
os.system("dos2unix -q %s" % filepath)
filein = open(filepath, "r")
if (len(sys.argv) == 2):
output_file = sys.argv[1]
elif (len(sys.argv) == 3):
output_file = sys.argv[2]
else:
print("not support!")
fileout = open(".temp.v", "w")
line = filein.readline()
grade = 0 # grade: Times for cycle or judge
delt1 = 0 # delt1: number for \t(1)
delt2 = 0 # delt2: number for \t(2)
line1 = "" # line1: info which has comment
line2 = "" # line2: info wiich not has comment
comment_keep = 0 # comment_keep: if comment is keeping
comment_type = 0 # comment_type: comment type
table_keep = 0 # table_keep: if table is keeping
blank_keep = 0 # blank_keep: if blank line is keeping
while line:
if (line.find("//") != -1):
comment_type = 2
if ((line.find("/*") != -1) and (line.find("*/") == -1)):
comment_keep = 1
comment_type = 3
elif ((line.find("*/") != -1) and (line.find("/*") == -1)):
comment_keep = 0
comment_type = 4
elif ((line.find("*/") != -1) and (line.find("/*") != -1)):
comment_keep = 0
comment_type = 5
elif (comment_keep == 1):
comment_type = 1
# print comment_type
line1 = line_format(comment_type, line)
# print ('line1 :', line1, file=fileout)
line2 = del_comment(comment_type, line)
# print ('line2 - 1:', line2, file=fileout)
line2 = line_format(comment_type, line2)
# print ('line2 - 2:', line2, file=fileout)
# print(line2 + " --line2", file = fileout)
if table_keep:
# if line1.find('//'):
# Input_number, Data_length = get_data_length(line1) # Get from the comment
# else:
# line1 = table_formatter(line1, Input_number, Data_length) # the table format
# print ('line1 ', line1, " --after modify", file=fileout)
line1 = table_formatter(line1) # the table format
# print ('line1 ', line1, " --after modify")
if (rtl_real_has(line2, 'end', 'begin')):
grade -= 1
delt1 = 0
delt2 = 0
elif (rtl_real_has(line2, 'endmodule', 'module')):
grade -= 1
delt1 = 0
delt2 = 0
elif (rtl_real_has(line2, 'endfunction', 'function')):
grade -= 1
delt1 = 0
delt2 = 0
elif (rtl_real_has(line2, 'endtask', 'task')):
grade -= 1
delt1 = 0
delt2 = 0
elif (rtl_real_has(line2, 'endgenerate', 'generate')):
grade -= 1
delt1 = 0
delt2 = 0
elif (rtl_real_has(line2, 'endspecify', 'specify')): # new
grade -= 1
delt1 = 0
delt2 = 0
elif (rtl_real_has(line2, 'endconfig', 'config')): # new
grade -= 1
delt1 = 0
delt2 = 0
elif (rtl_real_has(line2, 'endtable', 'table')): # new
grade -= 1
delt1 = 0
delt2 = 0
table_keep = 0
elif (rtl_real_has(line2, 'endprimitive', 'primitive')): # new
grade -= 1
delt1 = 0
delt2 = 0
elif (rtl_real_has(line2, 'join', 'fork')):
grade -= 1
delt1 = 0
delt2 = 0
elif (rtl_real_has(line2, '`else', '`endif')):
grade -= 1
delt1 = 0
delt2 = 0
elif ((rtl_real_has(line2, '`endif', '`ifdef'))
or rtl_real_has(line2, '`endif', '`ifndef')):
grade -= 1
delt1 = 0
delt2 = 0
elif ((rtl_real_has(line2, 'endcase', 'case'))
or rtl_real_has(line2, 'endcase', 'casez')
or rtl_real_has(line2, 'endcase', 'casex')):
grade -= 1
delt1 = 0
delt2 = 0
elif (rtl_real_has(line2, 'endclass', 'class')):
grade -= 1
delt1 = 0
delt2 = 0
elif (rtl_real_just_has(line2, '}', '{') and line2.startswith("}")):
grade -= 1
delt1 = 0
delt2 = 0
elif (rtl_has(line2, 'if')):
delt2 = 0
elif (rtl_has(line2, 'else')):
delt1 = 0
else:
line1 = declare_align(line1)
line1 = assign_align(line1)
# new
Can_Be_Print = 1
if Judge_Blank(line1):
if blank_keep >= 2:
Can_Be_Print = 0
blank_keep += 1
else:
Can_Be_Print = 1
blank_keep = 0
# print ('line1: ', line1, file=fileout)
if Can_Be_Print: # new
print(((" " * (grade + delt1 + delt2) * 4) + line1), file=fileout)
# print >> fileout, " " * (grade + delt1 + delt2) * 4 + line1
# print >> fileout, str(grade + delt1 + delt2) + " " * (grade + delt1 + delt2) * 4 + line1
delt1 = 0
delt2 = 0
if (rtl_real_has(line2, 'begin', 'end')):
# print('lalala', file = fileout)
grade += 1
elif (rtl_real_has(line2, 'module', 'endmodule')):
grade += 1
elif (rtl_real_has(line2, 'function', 'endfunction')):
grade += 1
elif (rtl_real_has(line2, 'task', 'endtask')):
grade += 1
elif (rtl_real_has(line2, 'generate', 'endgenerate')):
grade += 1
elif (rtl_real_has(line2, 'specify', 'endspecify')): # new
grade += 1
elif (rtl_real_has(line2, 'config', 'endconfig')): # new
grade += 1
elif (rtl_real_has(line2, 'table', 'endtable')): # new
grade += 1
table_keep = 1
elif (rtl_real_has(line2, 'primitive', 'endprimitive')): # new
grade += 1
elif (rtl_real_has(line2, 'fork', 'join')):
grade += 1
elif (rtl_real_has(line2, '`ifdef', '`endif')):
grade += 1
elif (rtl_real_has(line2, '`ifndef', '`endif')):
grade += 1
elif (rtl_real_has(line2, '`else', '`endif')):
grade += 1
elif (rtl_real_has(line2, 'case', 'endcase')):
grade += 1
elif (rtl_real_has(line2, 'casez', 'endcase')):
grade += 1
elif (rtl_real_has(line2, 'casex', 'endcase')):
grade += 1
elif (rtl_real_has(line2, 'class', 'endclass')):
grade += 1
elif (rtl_real_just_has(line2, '{', '}')):
grade += 1
elif (rtl_real_just_has(line2, '}', '{') and (not line2.startswith('}'))):
grade -= 1
elif (rtl_has(line2, 'if') and (not line2.endswith(';'))):
delt1 = 1
elif (line2.endswith('else') and not (line2.endswith(';'))):
delt2 = 1
line = filein.readline()
comment_type = 0
fileout.close()
filein.close()
os.system("cp -rf .temp.v %s" % str(output_file))
os.system("rm -rf .temp.v ")
#### main ###########################################################################################