summaryrefslogtreecommitdiffstats
path: root/libtoprammer/generic_algorithms.py
blob: 1eca4038072dc1862d7fe33fd7cbb4b041070f57 (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
"""
#    TOP2049 Open Source programming suite
#
#    Generic programming algorithms
#
#    Copyright (c) 2012 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 *


class AddrSetter(object):
	"""Generic address setter"""

	def __init__(self, chip,
		     fpgaCmdByte0,
		     fpgaCmdByte1 = -1,
		     fpgaCmdByte2 = -1,
		     fpgaCmdByte3 = -1,
		     addrSetupFunc = lambda byteNr, data: None,
		     addrFinishFunc = lambda byteNr, data: None):
		"""
		chip => The Chip.
		fpgaCmdByte0 => FPGA command number for setting addr byte 0.
		fpgaCmdByte1 => FPGA command number for setting addr byte 1.
		fpgaCmdByte2 => FPGA command number for setting addr byte 2.
		fpgaCmdByte3 => FPGA command number for setting addr byte 3.
		addrSetupFunc => Optional function called before addr write.
		addrFinishFunc => Optional function called after addr write.
		"""
		self.chip = chip
		self.fpgaCmds = [
			fpgaCmdByte0,
			fpgaCmdByte1,
			fpgaCmdByte2,
			fpgaCmdByte3,
		]
		self.addrSetupFunc = addrSetupFunc
		self.addrFinishFunc = addrFinishFunc
		self.reset()

	def reset(self):
		self.prevAddr = None

	def load(self, addr):
		"""Load an address into the FPGA.
		This loads only the bytes that changed."""
		shifts = (0, 8, 16, 24)
		for byteNr, shift in enumerate(shifts):
			fpgaCmd = self.fpgaCmds[byteNr]
			if fpgaCmd < 0:
				continue
			addrByte = (addr >> shift) & 0xFF
			if self.prevAddr is None or\
			   ((self.prevAddr >> shift) & 0xFF) != addrByte:
				self.addrSetupFunc(byteNr, addrByte)
				self.chip.top.cmdFPGAWrite(fpgaCmd, addrByte)
				self.addrFinishFunc(byteNr, addrByte)
		self.prevAddr = addr

class GenericAlgorithms(object):
	"""Generic programming algorithms."""

	def __init__(self, chip):
		self.chip = chip

	def simpleVoltageSetup(self, vcc=5.0, vpp=5.0, vppEnable=False):
		"""Simple voltage setup."""
		self.chip.top.cmdSetVCCXVoltage(vcc)
		self.chip.top.cmdSetVPPVoltage(vpp)
		self.chip.applyVCCX(True)
		self.chip.applyGND(True)
		self.chip.applyVPP(vppEnable)

	def simpleVoltageShutdown(self):
		"""Turn off voltages."""
		self.chip.applyVCCX(False)
		self.chip.applyVPP(False)
		self.chip.applyGND(False)
		self.chip.top.cmdSetVCCXVoltage(self.chip.top.vccx.minVoltage())
		self.chip.top.cmdSetVPPVoltage(self.chip.top.vpp.minVoltage())

	def simpleRead(self, name, sizeBytes,
		       readData8Func,
		       addrSetter,
		       initFunc = lambda: None,
		       exitFunc = lambda: None):
		"""Simple 8-bit data read algorithm."""
		self.chip.progressMeterInit("Reading %s" % name, sizeBytes)
		image, count = [], 0
		initFunc()
		addrSetter.reset()
		for addr in range(0, sizeBytes):
			self.chip.progressMeter(addr)
			addrSetter.load(addr)
			readData8Func()
			count += 1
			if count == self.chip.top.getBufferRegSize():
				image.append(self.chip.top.cmdReadBufferReg())
				count = 0
		if count:
			image.append(self.chip.top.cmdReadBufferReg()[:count])
		exitFunc()
		self.chip.progressMeterFinish()
		return b"".join(image)

	def simpleReadEPROM(self, sizeBytes,
			    readData8Func,
			    addrSetter,
			    initFunc = lambda: None,
			    exitFunc = lambda: None):
		return self.simpleRead("EPROM", sizeBytes, readData8Func,
				       addrSetter, initFunc, exitFunc)

	def simpleReadEEPROM(self, sizeBytes,
			     readData8Func,
			     addrSetter,
			     initFunc = lambda: None,
			     exitFunc = lambda: None):
		return self.simpleRead("EEPROM", sizeBytes, readData8Func,
				       addrSetter, initFunc, exitFunc)

	def simpleTest(self, readFunc, writeFunc, size):
		"""Simple Unit-test."""
		image = genRandomBlob(size)
		writeFunc(image)
		if readFunc() != image:
			self.chip.throwError("Unit-test failed. "
				"The chip may be physically broken.")
bues.ch cgit interface