summaryrefslogtreecommitdiffstats
path: root/libtoprammer/layout_generator.py
blob: 9da68fd36f0c330924a0919a1fce2473a681e11c (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
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
"""
#    TOP2049 Open Source programming suite
#
#    ZIF socket layout generator
#
#    Copyright (c) 2010 Michael Buesch <m@bues.ch>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""

from .util import *
from .top_devices import *


class LayoutGenerator:
	"Layout generator baseclass."

	class MapError(Exception): pass

	def __init__(self):
		pass

	def setProgrammerType(self, programmer="TOP2049"):
		supportedDevices = {
			# Map  deviceName : layoutModules, ZIF-pin-count
			"TOP2049"	: (top2049_vcc_layouts, top2049_vpp_layouts,
					   top2049_gnd_layouts, 48)
		}
		try:
			(vcc_layouts, vpp_layouts, gnd_layouts, zifPins) = \
				supportedDevices[programmer.upper()]
		except (KeyError) as e:
			raise TOPException("Programmer " + programmer + " not supported")
		self.vccLayout = vcc_layouts.VCCLayout()
		self.vppLayout = vpp_layouts.VPPLayout()
		self.gndLayout = gnd_layouts.GNDLayout()
		self.zifPins = zifPins

	def setPins(self, vccPin, vppPins, gndPin):
		"""Load the supply pin locations.
		vppPins may either be one pin number or a list of pin numbers or None."""
		self.vccPin = vccPin
		if vppPins is None:
			self.vppPins = [ ]
		else:
			try:
				self.vppPins = list(vppPins)
			except TypeError:
				self.vppPins = [ vppPins, ]
		self.gndPin = gndPin
		self.verifyPins()

	def getZifPinForPackagePin(self, packagePin):
		"Get the ZIF pin number corresponding to the package pin number"
		return self.mapPin2zif(packagePin, self.result_offset,
				       self.result_upsideDown)

	def verifyPins(self):
		pass

	def maxOffset(self, upsideDown):
		"Returns the max possible chip offset (in the ZIF socket)"
		raise TOPException("Reimplement me")

	def mapToZIF(self, offset, upsideDown):
		"Tries to map the chip into the ZIF socket"
		if offset < 0 or offset > self.maxOffset(upsideDown):
			raise self.MapError()

		# Find a GND layout
		zifGndPin = self.mapPin2zif(self.gndPin, offset, upsideDown)
		self.result_GND = self.__findSingleLayout(
					self.gndLayout,
					(1 << (zifGndPin - 1)))

		# Find a VCC layout
		zifVccPin = self.mapPin2zif(self.vccPin, offset, upsideDown)
		self.result_VCC = self.__findSingleLayout(
					self.vccLayout,
					(1 << (zifVccPin - 1)))

		# Find a (possibly cumulative) VPP layout
		if not self.vppPins:
			self.result_VPP = None
		else:
			zifVppMask = 0
			for vppPin in self.vppPins:
				pin = self.mapPin2zif(vppPin, offset, upsideDown)
				zifVppMask |= (1 << (pin - 1))
			self.result_VPP = self.__findCumulativeLayout(
						self.vppLayout,
						zifVppMask)

		# Also store the chip orientation for later use.
		self.result_upsideDown = upsideDown
		self.result_offset = offset

	def mapPin2zif(self, packagePin, offset, upsideDown):
		"Map a package pin to a ZIF pin. Returns the ZIF pin number."
		raise TOPException("Reimplement me")

	def zifLayoutAsciiArt(self):
		"Returns nice ascii ART of the mapped ZIF socket"
		raise TOPException("Reimplement me")

	def zifPinAssignments(self):
		"Returns a string describing the pin assignments"
		vcc = str(self.__bitmask2pinList(self.result_VCC[1])).strip("[]")
		ret =  "VCC ZIF pins: " + vcc + "\n"
		if self.result_VPP:
			vppBitmask = 0
			for (id, mask) in self.result_VPP:
				vppBitmask |= mask
			vpp = str(self.__bitmask2pinList(vppBitmask)).strip("[]")
			ret += "VPP ZIF pins:  " + vpp + "\n"
		gnd = str(self.__bitmask2pinList(self.result_GND[1])).strip("[]")
		ret += "GND ZIF pins:  " + gnd + "\n"
		return ret

	def __bitmask2pinList(self, bitmask):
		ret = []
		bit = 0
		while bitmask:
			if bitmask & (1 << bit):
				ret.append(bit + 1)
			bitmask &= ~(1 << bit)
			bit += 1
		return ret

	def __pinList2Bitmask(self, pinList):
		bitmask = 0
		for pin in pinList:
			assert(pin >= 1)
			bitmask |= (1 << (pin - 1))
		return bitmask

	def recalculate(self):
		"Redo the mapping calculation"
		for upsideDown in (False, True):
			offset = self.maxOffset(upsideDown)
			while offset >= 0:
				try:
					self.mapToZIF(offset, upsideDown)
				except (LayoutGenerator.MapError) as e:
					offset -= 1
					continue
				return
		raise TOPException("Did not find a possible valid layout for the setup")

	def getGNDLayout(self):
		"Get the calculated GND layout ID and mask. Returns a tuple (ID, mask)."
		return self.result_GND

	def applyGNDLayout(self, top):
		"Send the GND layout to hardware"
		(layoutID, layoutMask) = self.getGNDLayout()
		top.gnd.setLayoutID(layoutID)

	def getVCCLayout(self):
		"Get the calculated VCC layout ID and mask. Returns a tuple (ID, mask)."
		return self.result_VCC

	def applyVCCLayout(self, top):
		"Send the VCC layout to hardware"
		(layoutID, layoutMask) = self.getVCCLayout()
		top.vcc.setLayoutID(layoutID)

	def getVPPLayouts(self):
		"""Get the calculated VPP layout IDs and masks.
		Returns a list of tuples ((ID, mask), (ID, mask), ...)"""
		return self.result_VPP

	def applyVPPLayout(self, top, packagePins=[]):
		"""Send the VPP layout to hardware.
		packagePins is a list of pins (on the chip package) to activate.
		If packagePins is not passed, all VPP pins are driven to VPP."""
		if packagePins:
			pins = []
			for pin in packagePins:
				pins.append(self.mapPin2zif(pin, self.result_offset,
							    self.result_upsideDown))
			packagePinsMask = self.__pinList2Bitmask(pins)
		top.vpp.setLayoutMask(0) # Reset
		layouts = self.getVPPLayouts()
		if layouts:
			for (layoutID, mask) in layouts:
				if packagePins:
					if mask & packagePinsMask == 0:
						continue
					if mask & packagePinsMask != mask:
						raise TOPException(
							"Unable to apply partial VPP layout")
				top.vpp.setLayoutID(layoutID)

	def __findSingleLayout(self, layoutDefs, zifBitmask):
		# Returns an (id, mask) tuple
		for (id, mask) in layoutDefs.supportedLayouts():
			if zifBitmask == mask:
				break
		else:
			raise self.MapError()
		return (id, mask)

	def __findCumulativeLayout(self, layoutDefs, zifBitmask):
		# Returns a list of (id, mask) tuples
		result = []
		for (id, mask) in layoutDefs.supportedLayouts():
			if zifBitmask == 0:
				break
			if mask == 0:
				continue
			if mask & zifBitmask == mask:
				result.append( (id, mask) )
				zifBitmask &= ~mask
		if zifBitmask:
			raise self.MapError()
		return result

class LayoutGeneratorDIP(LayoutGenerator):
	"Layout generator for DIP packages."

	def __init__(self, nrPins):
		LayoutGenerator.__init__(self)
		self.nrPins = nrPins

	def verifyPins(self):
		LayoutGenerator.verifyPins(self)
		if self.nrPins < 2 or self.nrPins > self.zifPins or self.nrPins % 2 != 0:
			raise TOPException("Invalid DIP package")
		if self.vccPin < 1 or self.vccPin > self.nrPins:
			raise TOPException("Invalid VCC pin number for the selected package")
		for vppPin in self.vppPins:
			if vppPin < 1 or vppPin > self.nrPins:
				raise TOPException("Invalid VPP pin number for the selected package")
		if self.gndPin < 1 or self.gndPin > self.nrPins:
			raise TOPException("Invalid GND pin number for the selected package")

	def maxOffset(self, upsideDown):
		return self.zifPins // 2 - self.nrPins // 2

	def mapPin2zif(self, dipPin, offset, upsideDown):
		assert(dipPin >= 1 and dipPin <= self.nrPins)
		if upsideDown:
			if dipPin > self.nrPins // 2:
				# Right side of DIP
				dipPin -= self.nrPins // 2
				return dipPin + offset
			else:
				# Left side of DIP
				return self.zifPins - self.nrPins // 2 + dipPin - offset
		else:
			if dipPin > self.nrPins // 2:
				# Right side of DIP
				dipPin -= self.nrPins // 2
				return self.zifPins - self.nrPins // 2 + dipPin - offset
			else:
				# Left side of DIP
				return dipPin + offset

	def zifLayoutAsciiArt(self):
		def line(prefix, content, postfix):
			return "%3s  %s  %3s\n" % (prefix, content, postfix)

		zifGnd = self.getZifPinForPackagePin(self.gndPin)
		zifVcc = self.getZifPinForPackagePin(self.vccPin)
		zifVpp = [ self.getZifPinForPackagePin(p) \
			   for p in self.vppPins ]

		ret =  line("", "T     ZIF socket", "")
		ret += line("", "^--o==============o", "")
		for zp in range(1, self.zifPins // 2 + 1):
			prefix, postfix = "", ""
			if zp == zifGnd:
				prefix = "GND"
			if self.zifPins - zp + 1 == zifGnd:
				postfix = "GND"
			if zp == zifVcc:
				prefix = "VCC"
			if self.zifPins - zp + 1 == zifVcc:
				postfix = "VCC"
			if zp in zifVpp:
				prefix = "VPP"
			if self.zifPins - zp + 1 in zifVpp:
				postfix = "VPP"

			if zp < self.result_offset + 1 or \
			   zp > self.result_offset + self.nrPins // 2:
				ret += line(prefix,
					"%2d |----- || -----| %2d" %\
					(zp, self.zifPins + 1 - zp),
					postfix)
			else:
				if zp == self.result_offset + 1 and \
				   not self.result_upsideDown:
					ret += line(prefix,
						"%2d |-- 1##..##o --| %2d" %\
						(zp, self.zifPins + 1 - zp),
						postfix)
				elif zp == self.result_offset + self.nrPins // 2 and \
				     self.result_upsideDown:
					ret += line(prefix,
						"%2d |-- o##''##1 --| %2d" %\
						(zp, self.zifPins + 1 - zp),
						postfix)
				else:
					ret += line(prefix,
						"%2d |-- o######o --| %2d" %\
						(zp, self.zifPins + 1 - zp),
						postfix)
		ret += line("", "   o==============o", "")
		return ret

def createLayoutGenerator(package):
	try:
		for regex in (r"DIP(\d+)", r"PDIP(\d+)", r"SO(\d+)", r"TSSOP(\d+)", ):
			m = re.match(regex, package, re.IGNORECASE)
			if m:
				nrPins = int(m.group(1))
				return LayoutGeneratorDIP(nrPins)
		if package.upper() == "PLCC32": # 1:1 adapter
			return LayoutGeneratorDIP(32)
		if package.upper() == "PLCC44": # 1:1 adapter
			return LayoutGeneratorDIP(44)
		raise ValueError()
	except (ValueError) as e:
		raise TOPException("Unknown package type " + package)
bues.ch cgit interface