summaryrefslogtreecommitdiff
path: root/tools/cv_bsp_generator/model.py
blob: c30d6246cc4f811c58d9fede04e535ea597dfc26 (plain)
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
# SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
"""
Data models for XML files required for generating a preloader.

These classes encapsulate the complexities of XML DOM in order to
make retrieving data from XML files easier and more reliable.
By shielding data model deserialization from data consumers,
it'd be easier to switch to other formats such as JSON if required.

There are some assumptions about how these XML files are structured
such as the hierarchy of elements and ordering of attributes, these
are relatively safe assumptions for as long as the XML files are
always generated by HPS megawizard (isw.tcl) and are not hand-edited.

Copyright (C) 2022 Intel Corporation <www.intel.com>

Author: Lee, Kah Jing <kah.jing.lee@intel.com>
"""
import xml.dom.minidom

def getSingletonElementByTagName(parent, tagName):
	"""
	Find tag by name and ensure that there is exactly one match
	"""
	nodes = parent.getElementsByTagName(tagName)

	if len(nodes) == 0:
		raise Exception("Can't find element: " + tagName)
	elif len(nodes) > 1:
		raise Exception("Unexpected multiple matches for singleton element: " + tagName)
	else:
		return nodes[0]

class hps(object):
	"""
	Data model for hps.xml
	"""
	@staticmethod
	def create(file):
		""" hps model """
		return hps(file)

	def __init__(self, file):
		""" hps model initialization """
		self.dom = xml.dom.minidom.parse(file)

		try:
			# Look for <hps> node
			self.hpsNode = getSingletonElementByTagName(self.dom, "hps")
			# Look for <hps><system> node
			self.hpsSystemNode = getSingletonElementByTagName(self.hpsNode, "system")
		except Exception:
			raise Exception("Can't initialize from file: " + file)

	def getSystemConfig(self, param):
		""" parse system configuration tag """
		hpsSystemConfigNode = None

		# Look for <hps><system><config ...> nodes
		for node in self.hpsSystemNode.getElementsByTagName("config"):
			# assume name is the first attribute as in <config name="..." ...>
			nameAttrNode = node.attributes.item(0)
			if nameAttrNode.nodeName == "name" and nameAttrNode.nodeValue == param:
				# assume value is the second attribute as in <config name="..." value="...">
				valueAttrNode = node.attributes.item(1)
				if valueAttrNode.nodeName == "value":
					hpsSystemConfigNode = valueAttrNode
					break

		if hpsSystemConfigNode == None:
			raise ValueError("Can't find <hps><system><config> node: " + param)

		return hpsSystemConfigNode.nodeValue

class emif(object):
	"""
	Data model for emif.xml.
	"""
	@staticmethod
	def create(file):
		""" emif model """
		return emif(file)

	def __init__(self, file):
		""" emif model initialization """
		self.dom = xml.dom.minidom.parse(file)

		try:
			# Look for <emif> node
			self.emifNode = getSingletonElementByTagName(self.dom, "emif")
			# Look for <emif><pll> node
			self.emifPllNode = getSingletonElementByTagName(self.emifNode, "pll")
		except Exception:
			raise Exception("Can't initialize from file: " + file)

	def getPllDefine(self, param):
		""" parse pll define tag """
		emifPllDefineNode = None

		# Look for <emif><pll><define ...> nodes
		for node in self.emifPllNode.getElementsByTagName("define"):
			nameAttrNode = node.attributes.item(0)
			# assume name is the first attribute as in <define name="..." ...>
			if nameAttrNode.nodeName == "name" and nameAttrNode.nodeValue == param:
				# assume value is the second attribute as in <config name="..." value="...">
				valueAttrNode = node.attributes.item(1)
				if valueAttrNode.nodeName == "value":
					emifPllDefineNode = valueAttrNode
					break

		if emifPllDefineNode == None:
			raise Exception("Can't find EMIF PLL define node: " + param)

		return emifPllDefineNode.nodeValue