melhorias.
[vutg.git] / src / vut_parser.py
blob260cb9f21292e849ee11c82e90b96552385f7849
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 """
4 =======================================
5 module_name: vut_analyser
6 ---------------------------------------
7 Author: Rodrigo Peixoto
8 Data: 10/02/2008
9 ---------------------------------------
10 Description:
11 - This moludes fetchs the data from
12 the waveform to generate the Module
13 Skeleton and the UnitTest Module
14 =======================================
15 """
17 from elementtree.ElementTree import *
18 from vut_generator import *
19 from utils import *
20 import re
21 import sys
23 class VUTParser:
25 def __init__(self, xml_source):
26 self.vut = ElementTree(file=xml_source)
27 self.root = self.vut.getroot()
28 self.vmodule = VerilogModule(self.root.get("module_name"))
29 #print self.vmodule
30 #self.ran_range = {}
31 print "Initing parser to %s..." % xml_source
33 def clear_comments(self, dlist):
34 for i in dlist:# removing commets
35 if "#" in i:
36 dlist[dlist.index(i)] = i[:i.index("#")]
37 return map(str.strip, dlist)
40 def parse_waveform(self, *args):
41 print "--> parsing waveform..."
42 waves = map(str.strip, args[0].text.splitlines())
43 waves = [x for x in waves if((x!= "") and ("-" not in x) and ("=" not in x))]
45 for signal in waves:
46 t = re.findall(r'(^[a-zA-z]\w*( )*\[)|(^[a-zA-z]\w*( )*[io]\@)', signal)[0]
47 sig_name = [x for x in t if x!=""][0].split()[0].replace('[', '') #Cleaning all once!
48 slice = re.findall(r'\[\d+\:\d+\]', signal)
49 port = None
50 if slice:
51 port = (sig_name, tuple(map(int, re.findall(r'\d+', slice[0]))))
52 else: port = (sig_name,)
54 if re.match(r'.*i@.*', signal):
55 self.vmodule.in_ports.append(port)
56 else:
57 self.vmodule.out_ports.append(port)
58 #print self.vmodule
60 waves = map(lambda a : a.replace(" ", ""), waves)
61 self.parse_signal_values(waves)
63 def parse_gen_with(self, *args):
64 '''
66 @param *args:
67 '''
69 print " parsing gen_with..."
70 ran = map(str.strip, args[0].text.splitlines())
71 ran = [x for x in ran if((x!= "") and (x[0]!="#"))]
72 #ran = self.clear_comments(ran)
73 for i in ran:
74 self.vmodule.ran_range[re.findall(r'R\d+', i)[0]] = (int(re.findall(r'[0-9A-Fa-f]+\,', i)[0][:-1],16), int(re.findall(r'[0-9A-Fa-f]+\s*\)', i)[0][:-1],16))
75 print self.vmodule.ran_range
76 for code in args[0].getchildren():
77 var = code.get("var")
78 assert var, """Var not defined in python_code tag.\nUsage <python_code var="var_name">...code...</python_code>!"""
79 assert re.match(r'[a-zA-Z][a-zA-Z0-9_]*$', var)
80 self.python_code_parser(code.text)
81 #print ran
83 def parse_time_scale(self, *args):
84 '''
85 @param *args:
86 '''
88 print " parsing time_scale..."
89 t_div = args[0].get("t_div")
90 unit = args[0].get("unit")
91 assert t_div, """Time division not defined in time_scale tag.\n
92 Usage <time_scale t_div='val_integer' unit="unit{(ump)s}"/>!"""
93 assert unit, "Unit not defined in time_scale tag!"
95 def parse_signal_values(self, pwaves):
96 print "--> creating memories..."
97 list_imem = []
98 list_omem = []
99 for wav in pwaves:
100 sig_name = re.sub("\[\d+:\d+\]", '', wav[:wav.index('@')-1])
101 values = wav[wav.index('@')+1:].split('|')[1:-1]
102 if wav[wav.index('@')-1] == 'i':
103 mem = MemoryIO("i_%s"% sig_name, values)
104 #print mem
105 #exec ("imem_%s = %s" % (sig_name, values)) #wav[wav.index('@')+1:].split('|')[1:-1]))
106 list_imem.append(mem)
107 else:
108 mem = MemoryIO("o_%s"% sig_name, values)
109 #exec ("omem_%s = %s"% (sig_name, values)) #wav[wav.index('@')+1:].split('|')[1:-1]))
110 #exec("list_omem.append(omem_%s)"% sig_name)
111 list_omem.append(mem)
112 #print list_imem
113 #print list_omem
114 self.vmodule.set_in_ports_behavior(list_imem)
115 self.vmodule.set_out_ports_behavior(list_omem)
118 def parse_all(self):
121 root_children = self.root.getchildren()
122 switch = {"waveform": self.parse_waveform,
123 "gen_with": self.parse_gen_with,
124 "time_scale": self.parse_time_scale}
126 for item in root_children:
127 try:
128 switch[item.tag](item)
129 except KeyError, e:
130 raise InvalidTagException(e)
131 print "Parse complete successful!!!"
132 return self.vmodule
134 def python_code_parser(self, pycode):
135 #TODO: Ajsutar isso aqui!!!
136 print "checking python code..."
138 code = self.clear_comments(pycode.splitlines())
139 code = [x for x in code if(x!= "")]
141 try:
142 if re.match(r"\s*include_python_file\(\"(((\/\w+)+|\.)\/)?\w+.py[oc]?\"\)\s*$",pycode):
143 print pycode.strip()
144 else:
145 exec("\n".join(code))
146 print "Fetching reference model: ", "\n".join(code)
147 except:
148 print "Python reference model code error: ", sys.exc_info()
149 if __name__ == '__main__':
150 test = VUTParser("teste.vut")
151 vm = test.parse_all()
152 VUTGenerator(vm).gen()