From 05aba2e57eec1b6cdaf5bae62fb396402088b0ae Mon Sep 17 00:00:00 2001 From: Michael Buesch Date: Wed, 19 Jan 2011 21:17:46 +0100 Subject: Rewrite byte handling. Signed-off-by: Michael Buesch --- libtoprammer/bitfile.py | 21 ++++++---- libtoprammer/chip_at89c2051dip20.py | 14 +++---- libtoprammer/chip_atmega_common.py | 20 ++++----- libtoprammer/chip_attiny13dip8.py | 34 ++++++++------- libtoprammer/chip_generic_sram.py | 6 +-- libtoprammer/chip_m24cxxdip8.py | 2 +- libtoprammer/chip_m2764a.py | 6 +-- libtoprammer/chip_m8cissp.py | 4 +- libtoprammer/chip_w29ee011dip32.py | 4 +- libtoprammer/toprammer_main.py | 82 +++++++++++++++++++++---------------- libtoprammer/util.py | 62 +++++++++++++++++----------- toprammer-gui | 60 +++++++++++++++------------ 12 files changed, 172 insertions(+), 143 deletions(-) diff --git a/libtoprammer/bitfile.py b/libtoprammer/bitfile.py index e141602..263c24d 100644 --- a/libtoprammer/bitfile.py +++ b/libtoprammer/bitfile.py @@ -1,7 +1,7 @@ """ # *.BIT file parser # -# Copyright (c) 2009 Michael Buesch +# Copyright (c) 2009-2011 Michael Buesch # # 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 @@ -27,6 +27,9 @@ except ImportError: print "On Debian Linux run: apt-get install python-pkg-resources" sys.exit(1) +from util import * + + class BitfileException(Exception): pass class Bitfile: @@ -89,22 +92,22 @@ class Bitfile: raise BitfileException("No payload found") def __parseNextField(self, data, i): - fieldId = ord(data[i + 0]) + fieldId = byte2int(data[i + 0]) if (fieldId == self.FIELD_SRCFILE): data = self.__parse16bitField(data, i + 1) - self.srcfile = data.strip().strip("\x00") + self.srcfile = data.decode("ASCII").strip().strip("\x00") return len(data) + 3 if (fieldId == self.FIELD_FPGA): data = self.__parse16bitField(data, i + 1) - self.fpga = data.strip().strip("\x00") + self.fpga = data.decode("ASCII").strip().strip("\x00") return len(data) + 3 if (fieldId == self.FIELD_DATE): data = self.__parse16bitField(data, i + 1) - self.date = data.strip().strip("\x00") + self.date = data.decode("ASCII").strip().strip("\x00") return len(data) + 3 if (fieldId == self.FIELD_TIME): data = self.__parse16bitField(data, i + 1) - self.time = data.strip().strip("\x00") + self.time = data.decode("ASCII").strip().strip("\x00") return len(data) + 3 if (fieldId == self.FIELD_PAYLOAD): self.payload = self.__parse32bitField(data, i + 1) @@ -112,12 +115,12 @@ class Bitfile: raise BitfileException("Found unknown data field 0x%02X" % fieldId) def __parse16bitField(self, data, i): - fieldLen = (ord(data[i + 0]) << 8) | ord(data[i + 1]) + fieldLen = (byte2int(data[i + 0]) << 8) | byte2int(data[i + 1]) return data[i + 2 : i + 2 + fieldLen] def __parse32bitField(self, data, i): - fieldLen = (ord(data[i + 0]) << 24) | (ord(data[i + 1]) << 16) |\ - (ord(data[i + 2]) << 8) | ord(data[i + 3]) + fieldLen = (byte2int(data[i + 0]) << 24) | (byte2int(data[i + 1]) << 16) |\ + (byte2int(data[i + 2]) << 8) | byte2int(data[i + 3]) return data[i + 4 : i + 4 + fieldLen] def __probeFile(fullpath): diff --git a/libtoprammer/chip_at89c2051dip20.py b/libtoprammer/chip_at89c2051dip20.py index 05afda0..0025366 100644 --- a/libtoprammer/chip_at89c2051dip20.py +++ b/libtoprammer/chip_at89c2051dip20.py @@ -64,7 +64,7 @@ class Chip_AT89C2051dip20(Chip): signature = "" signature += data[0] signature += data[1] - self.top.printInfo("Signature: %X, %X" % (ord(signature[0]), ord(signature[1]))) + self.top.printInfo("Signature: %X, %X" % (byte2int(signature[0]), byte2int(signature[1]))) return signature def erase(self): @@ -136,7 +136,7 @@ class Chip_AT89C2051dip20(Chip): self.progressMeterInit("Writing Flash", len(image)) for addr in range(0, len(image)): self.progressMeter(addr) - data = ord(image[addr]) + data = byte2int(image[addr]) if data != 0xFF: self.__loadData(data) self.__loadCommand(3) @@ -161,7 +161,7 @@ class Chip_AT89C2051dip20(Chip): ok = 0 image = self.readProgmem() for addr in range(0, 0x800): - if ord(image[addr]) != 0xFF: + if byte2int(image[addr]) != 0xFF: ok = 1 return ok @@ -169,7 +169,7 @@ class Chip_AT89C2051dip20(Chip): data = self.readProgmem() ok = 0 for addr in range(0, 0x800): - if ord(image[addr]) != ord(data[addr]): + if byte2int(image[addr]) != byte2int(data[addr]): ok = 1 return ok @@ -198,7 +198,7 @@ class Chip_AT89C2051dip20(Chip): def __getStatusFlags(self): self.top.cmdFPGARead(0x12) stat = self.top.cmdReadBufferReg() - return ord(stat[0]) + return byte2int(stat[0]) def __busy(self): return bool(self.__getStatusFlags() & self.STAT_BUSY) @@ -214,8 +214,8 @@ class Chip_AT89C2051dip20(Chip): for i in range(0,4): self.top.cmdFPGARead(0x12) stat = self.top.cmdReadBufferReg() - if (ord(stat[0]) & self.STAT_BUSY) == 0: - return ord(stat[0]) + if (byte2int(stat[0]) & self.STAT_BUSY) == 0: + return byte2int(stat[0]) self.top.hostDelay(0.001) self.throwError("Timeout in busywait.") diff --git a/libtoprammer/chip_atmega_common.py b/libtoprammer/chip_atmega_common.py index 9af4861..5bb5c6f 100644 --- a/libtoprammer/chip_atmega_common.py +++ b/libtoprammer/chip_atmega_common.py @@ -106,7 +106,7 @@ class Chip_ATMega_common(Chip): self.__loadAddr(addr) addr *= 2 data = image[addr : addr + 2] - self.__loadData(ord(data[0]) | (ord(data[1]) << 8)) + self.__loadData(byte2int(data[0]) | (byte2int(data[1]) << 8)) self.__setBS1(1) self.__pulsePAGEL() self.__setBS1(0) @@ -146,7 +146,7 @@ class Chip_ATMega_common(Chip): addr = (page * self.eepromPageSize) + byte self.__loadAddr(addr) data = image[addr] - self.__loadDataLow(ord(data[0])) + self.__loadDataLow(byte2int(data[0])) self.__pulsePAGEL() self.__setBS1(0) self.__pulseWR() @@ -170,11 +170,11 @@ class Chip_ATMega_common(Chip): self.progressMeterInit("Writing Fuse bits", 0) self.__loadCommand(self.CMD_WRITEFUSE) self.__setBS2(0) - self.__loadDataLow(ord(image[0])) + self.__loadDataLow(byte2int(image[0])) self.__pulseWR() self.__waitForRDY() self.__loadCommand(self.CMD_WRITEFUSE) - self.__loadDataLow(ord(image[1])) + self.__loadDataLow(byte2int(image[1])) self.__setBS1(1) self.__pulseWR() self.__waitForRDY() @@ -197,7 +197,7 @@ class Chip_ATMega_common(Chip): self.progressMeterInit("Writing lock bits", 0) self.__loadCommand(self.CMD_WRITELOCK) - self.__loadDataLow(ord(image[0])) + self.__loadDataLow(byte2int(image[0])) self.__pulseWR() self.__waitForRDY() self.progressMeterFinish() @@ -272,10 +272,10 @@ class Chip_ATMega_common(Chip): if signature != self.signature: msg = "Unexpected device signature. " +\ "Want %02X%02X%02X, but got %02X%02X%02X" % \ - (ord(self.signature[0]), ord(self.signature[1]), - ord(self.signature[2]), - ord(signature[0]), ord(signature[1]), - ord(signature[2])) + (byte2int(self.signature[0]), byte2int(self.signature[1]), + byte2int(self.signature[2]), + byte2int(signature[0]), byte2int(signature[1]), + byte2int(signature[2])) if self.top.getForceLevel() >= 1: self.printWarning(msg) else: @@ -373,7 +373,7 @@ class Chip_ATMega_common(Chip): """Read the programmer status register""" self.top.cmdFPGARead(0x12) stat = self.top.cmdReadBufferReg() - return ord(stat[0]) + return byte2int(stat[0]) def __setOE(self, high): """Set the OE pin of the DUT""" diff --git a/libtoprammer/chip_attiny13dip8.py b/libtoprammer/chip_attiny13dip8.py index 86dba3c..8eaaf90 100644 --- a/libtoprammer/chip_attiny13dip8.py +++ b/libtoprammer/chip_attiny13dip8.py @@ -103,8 +103,8 @@ class Chip_AtTiny13dip8(Chip): low = word & 0xFF high = (word >> 8) & 0xFF self.__sendInstr(SDI=low, SII=0x0C) - self.__sendInstr(SDI=ord(image[word * 2 + 0]), SII=0x2C) - self.__sendInstr(SDI=ord(image[word * 2 + 1]), SII=0x3C) + self.__sendInstr(SDI=byte2int(image[word * 2 + 0]), SII=0x2C) + self.__sendInstr(SDI=byte2int(image[word * 2 + 1]), SII=0x3C) self.__sendInstr(SDI=0x00, SII=0x7D) self.__sendInstr(SDI=0x00, SII=0x7C) if ((word + 1) % self.flashPageSize == 0) or word == len(image) // 2 - 1: @@ -155,7 +155,7 @@ class Chip_AtTiny13dip8(Chip): for i in range(0, len(image)): self.progressMeter(i) self.__sendInstr(SDI=i, SII=0x0C) - self.__sendInstr(SDI=ord(image[i]), SII=0x2C) + self.__sendInstr(SDI=byte2int(image[i]), SII=0x2C) self.__sendInstr(SDI=0x00, SII=0x6D) self.__sendInstr(SDI=0x00, SII=0x6C) if ((i + 1) % self.eepromPageSize == 0) or i == len(image) - 1: @@ -166,22 +166,21 @@ class Chip_AtTiny13dip8(Chip): self.progressMeterFinish() def readFuse(self): - fuses = "" + fuses = [] self.__enterPM() self.progressMeterInit("Reading fuses", 0) self.__sendInstr(SDI=0x04, SII=0x4C) self.__sendInstr(SDI=0x00, SII=0x68) self.__sendInstr(SDI=0x00, SII=0x6C) self.__readSDOBufferHigh() - fuses += self.top.cmdReadBufferReg()[0] + fuses.append(self.top.cmdReadBufferReg(1)) self.__sendInstr(SDI=0x04, SII=0x4C) self.__sendInstr(SDI=0x00, SII=0x7A) self.__sendInstr(SDI=0x00, SII=0x7E) self.__readSDOBufferHigh() - data = ord(self.top.cmdReadBufferReg()[0]) - fuses += chr(data | 0xE0) + fuses.append(int2byte(self.top.cmdReadBufferReg8() | 0xE0)) self.progressMeterFinish() - return fuses + return b"".join(fuses) def writeFuse(self, image): if len(image) != 2: @@ -190,12 +189,12 @@ class Chip_AtTiny13dip8(Chip): self.__enterPM() self.progressMeterInit("Writing fuses", 0) self.__sendInstr(SDI=0x40, SII=0x4C) - self.__sendInstr(SDI=ord(image[0]), SII=0x2C) + self.__sendInstr(SDI=byte2int(image[0]), SII=0x2C) self.__sendInstr(SDI=0x00, SII=0x64) self.__sendInstr(SDI=0x00, SII=0x6C) self.__waitHighSDO() self.__sendInstr(SDI=0x40, SII=0x4C) - self.__sendInstr(SDI=(ord(image[1]) & 0x1F), SII=0x2C) + self.__sendInstr(SDI=(byte2int(image[1]) & 0x1F), SII=0x2C) self.__sendInstr(SDI=0x00, SII=0x74) self.__sendInstr(SDI=0x00, SII=0x7C) self.__waitHighSDO() @@ -208,8 +207,7 @@ class Chip_AtTiny13dip8(Chip): self.__sendInstr(SDI=0x00, SII=0x78) self.__sendInstr(SDI=0x00, SII=0x7C) self.__readSDOBufferHigh() - data = ord(self.top.cmdReadBufferReg()[0]) - lockbits = chr(data | 0xFC) + lockbits = int2byte(self.top.cmdReadBufferReg8() | 0xFC) self.progressMeterFinish() return lockbits @@ -220,7 +218,7 @@ class Chip_AtTiny13dip8(Chip): self.__enterPM() self.progressMeterInit("Writing lockbits", 0) self.__sendInstr(SDI=0x20, SII=0x4C) - self.__sendInstr(SDI=(ord(image[0]) & 3), SII=0x2C) + self.__sendInstr(SDI=(byte2int(image[0]) & 3), SII=0x2C) self.__sendInstr(SDI=0x00, SII=0x64) self.__sendInstr(SDI=0x00, SII=0x6C) self.__waitHighSDO() @@ -262,10 +260,10 @@ class Chip_AtTiny13dip8(Chip): if signature != self.signature: msg = "Unexpected device signature. " +\ "Want %02X%02X%02X, but got %02X%02X%02X" % \ - (ord(self.signature[0]), ord(self.signature[1]), - ord(self.signature[2]), - ord(signature[0]), ord(signature[1]), - ord(signature[2])) + (byte2int(self.signature[0]), byte2int(self.signature[1]), + byte2int(self.signature[2]), + byte2int(signature[0]), byte2int(signature[1]), + byte2int(signature[2])) if self.top.getForceLevel() >= 1: self.printWarning(msg) else: @@ -325,7 +323,7 @@ class Chip_AtTiny13dip8(Chip): def __getStatusFlags(self): self.top.cmdFPGARead(0x12) stat = self.top.cmdReadBufferReg() - return ord(stat[0]) + return byte2int(stat[0]) def __readSDOBufferHigh(self): self.top.cmdFPGARead(0x10) diff --git a/libtoprammer/chip_generic_sram.py b/libtoprammer/chip_generic_sram.py index c682447..2eb0226 100644 --- a/libtoprammer/chip_generic_sram.py +++ b/libtoprammer/chip_generic_sram.py @@ -38,7 +38,7 @@ class Chip_genericSRAM(Chip): assert(nrDataBits == 8) def erase(self): - self.writeRAM(chr(0) * self.__sizeBytes()) + self.writeRAM(int2byte(0) * self.__sizeBytes()) def test(self): self.genericTest(self.readRAM, self.writeRAM, @@ -63,7 +63,7 @@ class Chip_genericSRAM(Chip): self.__setControlPins(CE=1, OE=1, WE=1) self.progressMeterFinish() - return "".join(image) + return b"".join(image) def writeRAM(self, image): if len(image) > self.__sizeBytes(): @@ -104,7 +104,7 @@ class Chip_genericSRAM(Chip): self.top.cmdFPGAWrite(0x11, value) def __writeData(self, data): - data = ord(data) + data = byte2int(data) self.top.cmdFPGAWrite(0x10, data) def __readData(self): diff --git a/libtoprammer/chip_m24cxxdip8.py b/libtoprammer/chip_m24cxxdip8.py index 4e52e10..1e792fd 100644 --- a/libtoprammer/chip_m24cxxdip8.py +++ b/libtoprammer/chip_m24cxxdip8.py @@ -91,7 +91,7 @@ class Chip_m24cXXdip8_common(Chip): prevAddr = None for addr in range(0, len(image)): self.progressMeter(addr) - self.__setData(ord(image[addr])) + self.__setData(byte2int(image[addr])) if prevAddr is None or (prevAddr & 0xFFF0) != (addr & 0xFFF0): self.__setAddress(addr, writeMode=True) self.__runCommand(self.CMD_DEVSEL_WRITE, busyWait=True) diff --git a/libtoprammer/chip_m2764a.py b/libtoprammer/chip_m2764a.py index 1519288..6b5f28c 100644 --- a/libtoprammer/chip_m2764a.py +++ b/libtoprammer/chip_m2764a.py @@ -84,7 +84,7 @@ class Chip_M2764A(Chip): self.__setEG(E=1, G=1) for addr in range(0, len(image)): self.progressMeter(addr) - data = ord(image[addr]) + data = byte2int(image[addr]) if data != 0xFF: self.__writeData(addr, data) self.__setEG(E=1, G=1) @@ -104,7 +104,7 @@ class Chip_M2764A(Chip): for i in range(0, 25): self.__readDataToStatusReg(addr) stat = self.top.cmdReadBufferReg() - r = ord(stat[0]) + r = byte2int(stat[0]) if r == data: break self.__setEG(E=0, G=1) @@ -150,7 +150,7 @@ class Chip_M2764A(Chip): def __getStatusFlags(self): self.top.cmdFPGARead(0x12) stat = self.top.cmdReadBufferReg() - return ord(stat[0]) + return byte2int(stat[0]) def __busy(self): return bool(self.__getStatusFlags() & self.STAT_BUSY) diff --git a/libtoprammer/chip_m8cissp.py b/libtoprammer/chip_m8cissp.py index eeb6002..6f8a740 100644 --- a/libtoprammer/chip_m8cissp.py +++ b/libtoprammer/chip_m8cissp.py @@ -279,7 +279,7 @@ class Chip_M8C_ISSP(Chip): def __getStatusFlags(self): self.top.cmdFPGARead(0x12) stat = self.top.cmdReadBufferReg() - stat = ord(stat[0]) + stat = byte2int(stat[0]) isspState = (stat & self.STAT_ISSPSTATE) >> self.STAT_ISSPSTATE_SHIFT isBusy = bool(stat & self.STAT_BUSY0) != bool(stat & self.STAT_BUSY1) self.printDebug("isspState = 0x%02X, isBusy = %d, busyFlags = 0x%01X" %\ @@ -302,7 +302,7 @@ class Chip_M8C_ISSP(Chip): self.top.cmdFPGARead(0x14) self.top.cmdFPGARead(0x15) stat = self.top.cmdReadBufferReg() - return ord(stat[0]) | (ord(stat[1]) << 8) | (ord(stat[2]) << 16) + return byte2int(stat[0]) | (byte2int(stat[1]) << 8) | (byte2int(stat[2]) << 16) def __stringVectorToBinary(self, vector): binary = 0 diff --git a/libtoprammer/chip_w29ee011dip32.py b/libtoprammer/chip_w29ee011dip32.py index 42ac542..051fe9e 100644 --- a/libtoprammer/chip_w29ee011dip32.py +++ b/libtoprammer/chip_w29ee011dip32.py @@ -149,7 +149,7 @@ class Chip_w29ee011dip32(Chip): self.__swDataProtect(True) assert(len(pageData) <= 128) for byte in pageData: - self.__writeBufAppend(ord(byte)) + self.__writeBufAppend(byte2int(byte)) self.__loadWriteAddr(pageAddress) self.__runCommandSync(self.PROGCMD_WRITEBUF) self.top.hostDelay(0.01) @@ -230,7 +230,7 @@ class Chip_w29ee011dip32(Chip): def __getStatusFlags(self): self.top.cmdFPGARead(0x12) stat = self.top.cmdReadBufferReg() - return ord(stat[0]) + return byte2int(stat[0]) def __busy(self): return bool(self.__getStatusFlags() & self.STAT_BUSY) diff --git a/libtoprammer/toprammer_main.py b/libtoprammer/toprammer_main.py index e0aaae3..3fded08 100644 --- a/libtoprammer/toprammer_main.py +++ b/libtoprammer/toprammer_main.py @@ -18,14 +18,23 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. """ +import sys +__pymajor = sys.version_info[0] +__pyminor = sys.version_info[1] +if __pymajor < 2 or (__pymajor == 2 and __pyminor < 6): + print "FATAL: TOPrammer requires Python version 2.6. Please install Python 2.6" + sys.exit(1) + + +# TOPrammer version stamp VERSION_MAJOR = 0 VERSION_MINOR = 8 VERSION = "%d.%d" % (VERSION_MAJOR, VERSION_MINOR) + from bitfile import * from util import * -import sys import time import re try: @@ -232,14 +241,14 @@ class TOP: else: assert(0) - self.queueCommand("\x0D") + self.queueCommand(b"\x0D") stat = self.cmdReadBufferReg32() if stat != 0x00020C69: self.printWarning("Init: Unexpected status (a): 0x%08X" % stat) self.cmdSetVPPVoltage(0) self.cmdSetVPPVoltage(0) - self.queueCommand("\x0E\x20\x00\x00") + self.queueCommand(b"\x0E\x20\x00\x00") self.cmdDelay(0.01) self.cmdSetVCCXVoltage(0) @@ -247,9 +256,9 @@ class TOP: self.cmdLoadVPPLayout(0) self.cmdLoadVCCXLayout(0) - self.queueCommand("\x0E\x20\x00\x00") + self.queueCommand(b"\x0E\x20\x00\x00") self.cmdDelay(0.01) - self.queueCommand("\x0E\x25\x00\x00") + self.queueCommand(b"\x0E\x25\x00\x00") stat = self.cmdReadBufferReg32() if stat != 0x0000686C: self.printWarning("Init: Unexpected status (b): 0x%08X" % stat) @@ -272,10 +281,10 @@ class TOP: self.cmdFPGARead(0xFE) self.cmdFPGARead(0xFF) data = self.cmdReadBufferReg(3) - gotID = ord(data[0]) | (ord(data[1]) << 8) + gotID = byte2int(data[0]) | (byte2int(data[1]) << 8) if gotID == 0xFEFD or gotID == 0xFFFF: gotID = 0 - gotRev = ord(data[2]) + gotRev = byte2int(data[2]) if gotRev == 0xFF: gotRev = 0 return (gotID, gotRev) @@ -426,10 +435,10 @@ class TOP: self.printDebug("Done writing the image.") def __cmdDelay_4usec(self): - self.queueCommand(chr(0x00)) + self.queueCommand(int2byte(0x00)) def __cmdDelay_10msec(self): - self.queueCommand(chr(0x1B)) + self.queueCommand(int2byte(0x1B)) def cmdDelay(self, seconds): """Send a delay request to the device. Note that this causes the @@ -470,76 +479,77 @@ class TOP: assert(nrBytes <= regSize) if not nrBytes: return "" - self.queueCommand(chr(0x07)) + self.queueCommand(int2byte(0x07)) return self.receive(regSize)[0:nrBytes] def cmdReadBufferReg8(self): """Read a 8bit value from the buffer register.""" stat = self.cmdReadBufferReg(1) - stat = ord(stat[0]) + stat = byte2int(stat[0]) return stat def cmdReadBufferReg16(self): """Read a 16bit value from the buffer register.""" stat = self.cmdReadBufferReg(2) - stat = ord(stat[0]) | (ord(stat[1]) << 8) + stat = byte2int(stat[0]) | (byte2int(stat[1]) << 8) return stat def cmdReadBufferReg24(self): """Read a 24bit value from the buffer register.""" stat = self.cmdReadBufferReg(3) - stat = ord(stat[0]) | (ord(stat[1]) << 8) | (ord(stat[2]) << 16) + stat = byte2int(stat[0]) | (byte2int(stat[1]) << 8) | (byte2int(stat[2]) << 16) return stat def cmdReadBufferReg32(self): """Read a 32bit value from the buffer register.""" stat = self.cmdReadBufferReg(4) - stat = ord(stat[0]) | (ord(stat[1]) << 8) | \ - (ord(stat[2]) << 16) | (ord(stat[3]) << 24) + stat = byte2int(stat[0]) | (byte2int(stat[1]) << 8) | \ + (byte2int(stat[2]) << 16) | (byte2int(stat[3]) << 24) return stat def cmdReadBufferReg48(self): """Read a 48bit value from the buffer register.""" stat = self.cmdReadBufferReg(6) - stat = ord(stat[0]) | (ord(stat[1]) << 8) | \ - (ord(stat[2]) << 16) | (ord(stat[3]) << 24) | \ - (ord(stat[4]) << 32) | (ord(stat[5]) << 40) + stat = byte2int(stat[0]) | (byte2int(stat[1]) << 8) | \ + (byte2int(stat[2]) << 16) | (byte2int(stat[3]) << 24) | \ + (byte2int(stat[4]) << 32) | (byte2int(stat[5]) << 40) return stat def cmdRequestVersion(self): """Returns the device ID and versioning string.""" - self.queueCommand("\x0E\x11\x00\x00") + self.queueCommand(b"\x0E\x11\x00\x00") data = self.cmdReadBufferReg(16) return data.strip() def cmdFPGAInitiateConfig(self): """Initiate a configuration sequence on the FPGA.""" - self.queueCommand("\x0E\x21\x00\x00") + self.queueCommand(b"\x0E\x21\x00\x00") def cmdFPGAUploadConfig(self, data): """Upload configuration data into the FPGA.""" assert(len(data) <= 60) - cmd = "\x0E\x22\x00\x00" + data - cmd += "\x00" * (64 - len(cmd)) # padding + cmd = b"\x0E\x22\x00\x00" + data + cmd += b"\x00" * (64 - len(cmd)) # padding + print len(cmd) self.queueCommand(cmd) def cmdFPGARead(self, address): """Read a byte from the FPGA at address into the buffer register.""" if address == 0x10: # Fast tracked - self.queueCommand(chr(0x01)) + self.queueCommand(int2byte(0x01)) return - self.queueCommand(chr(0x0B) + chr(address)) + self.queueCommand(int2byte(0x0B) + int2byte(address)) def cmdFPGAWrite(self, address, byte): """Write a byte to an FPGA address.""" if address == 0x10: # Fast tracked - self.queueCommand(chr(0x10) + chr(byte)) + self.queueCommand(int2byte(0x10) + int2byte(byte)) return - self.queueCommand(chr(0x0A) + chr(address) + chr(byte)) + self.queueCommand(int2byte(0x0A) + int2byte(address) + int2byte(byte)) def cmdLoadGNDLayout(self, layout): """Load the GND configuration into the H/L shiftregisters.""" - cmd = chr(0x0E) + chr(0x16) + chr(layout) + chr(0) + cmd = int2byte(0x0E) + int2byte(0x16) + int2byte(layout) + int2byte(0) self.queueCommand(cmd) self.cmdDelay(0.01) self.hostDelay(0.15) @@ -547,13 +557,13 @@ class TOP: def cmdSetVPPVoltage(self, voltage): """Set the VPP voltage. voltage is a floating point voltage number.""" centivolt = int(voltage * 10) - cmd = chr(0x0E) + chr(0x12) + chr(centivolt) + chr(0) + cmd = int2byte(0x0E) + int2byte(0x12) + int2byte(centivolt) + int2byte(0) self.queueCommand(cmd) self.cmdDelay(0.01) def cmdLoadVPPLayout(self, layout): """Load the VPP configuration into the shift registers.""" - cmd = chr(0x0E) + chr(0x14) + chr(layout) + chr(0) + cmd = int2byte(0x0E) + int2byte(0x14) + int2byte(layout) + int2byte(0) self.queueCommand(cmd) self.cmdDelay(0.01) self.hostDelay(0.15) @@ -561,13 +571,13 @@ class TOP: def cmdSetVCCXVoltage(self, voltage): """Set the VCCX voltage. voltage is a floating point voltage number.""" centivolt = int(voltage * 10) - cmd = chr(0x0E) + chr(0x13) + chr(centivolt) + chr(0) + cmd = int2byte(0x0E) + int2byte(0x13) + int2byte(centivolt) + int2byte(0) self.queueCommand(cmd) self.cmdDelay(0.01) def cmdLoadVCCXLayout(self, layout): """Load the VCCX configuration into the shift registers.""" - cmd = chr(0x0E) + chr(0x15) + chr(layout) + chr(0) + cmd = int2byte(0x0E) + int2byte(0x15) + int2byte(layout) + int2byte(0) self.queueCommand(cmd) self.cmdDelay(0.01) self.hostDelay(0.15) @@ -577,7 +587,7 @@ class TOP: param = 0 if enable: param = 1 - cmd = chr(0x0E) + chr(0x28) + chr(param) + chr(0) + cmd = int2byte(0x0E) + int2byte(0x28) + int2byte(param) + int2byte(0) self.queueCommand(cmd) def __doSend(self, command): @@ -612,8 +622,8 @@ class TOP: self.flushCommands() try: ep = self.bulkIn.address - data = "".join(map(lambda b: chr(b), - self.usbh.bulkRead(ep, size))) + data = b"".join(map(lambda b: int2byte(b), + self.usbh.bulkRead(ep, size))) if len(data) != size: raise TOPException("USB bulk read error: Could not read the " +\ "requested number of bytes (req %d, got %d)" % (size, len(data))) @@ -626,12 +636,12 @@ class TOP: def flushCommands(self): """Flush the command queue.""" - command = "" + command = b"" for oneCommand in self.commandQueue: assert(len(oneCommand) <= 64) if len(command) + len(oneCommand) > 64: self.__doSend(command) - command = "" + command = b"" command += oneCommand if command: self.__doSend(command) diff --git a/libtoprammer/util.py b/libtoprammer/util.py index 96f4043..9722062 100644 --- a/libtoprammer/util.py +++ b/libtoprammer/util.py @@ -29,36 +29,48 @@ import random class TOPException(Exception): pass +if sys.version_info[0] == 2: # Python 2.x + def byte2int(byte): + return ord(byte) + + def int2byte(integer): + return chr(integer) +else: # Python 3.x + def byte2int(byte): + return int(byte[0]) + + def int2byte(integer): + return bytes( (integer, ) ) + def hex2bin(hexdata): assert(len(hexdata) % 2 == 0) - bindata = [] - for i in range(0, len(hexdata), 2): - bindata.append(chr(int(hexdata[i:i+2], 16))) - return "".join(bindata) + bindata = map(lambda i: int2byte(int(hexdata[i:i+2], 16)), + range(0, len(hexdata), 2)) + return b"".join(bindata) -def chr2hex(c): - return "%02X" % ord(c) +def byte2hex(byte): + return "%02X" % byte2int(byte) -def bin2hex(bindata): +def bytes2hex(bindata): if not bindata: return "" - return "".join(map(chr2hex, bindata)) + return "".join(map(byte2hex, bindata)) -def chr2ascii(c): - if ord(c) >= 32 and ord(c) <= 126: - return c +def byte2ascii(c): + ci = byte2int(c) + if ci >= 32 and ci <= 126: + return c.decode("ASCII") return "." -def bin2ascii(bindata): +def bytes2ascii(bindata): if not bindata: return "" - return "".join(map(chr2ascii, bindata)) + return "".join(map(byte2ascii, bindata)) def genRandomBlob(size): - blob = [] - for i in range(0, size): - blob.append(chr(random.randint(0, 0xFF))) - return "".join(blob) + blob = map(lambda x: int2byte(random.randint(0, 0xFF)), + range(0, size)) + return b"".join(blob) def nrBitsSet(integer): count = 0 @@ -89,8 +101,8 @@ def parseHexdump(dump): raise TOPException("Invalid hexdump format (odd bytestring len)") for i in range(0, len(bytes), 2): byte = int(bytes[i:i+2], 16) - bin.append(chr(byte)) - return "".join(bin) + bin.append(int2byte(byte)) + return b"".join(bin) except (ValueError), e: raise TOPException("Invalid hexdump format (Integer error)") @@ -103,11 +115,11 @@ def generateHexdump(mem): asc = "" if i % 16 == 0: ret += "0x%04X: " % i - c = ord(mem[i]) + c = byte2int(mem[i]) ret += "%02X" % c if (i % 2 != 0): ret += " " - asc += chr2ascii(chr(c)) + asc += byte2ascii(mem[i]) ret += " " + asc + "\n\n" return ret @@ -168,9 +180,9 @@ class IO_ihex: continue if type == self.TYPE_DATA: if len(bin) < addr + count: # Reallocate - bin += ['\xFF'] * (addr + count - len(bin)) + bin += [b'\xFF'] * (addr + count - len(bin)) for i in range(9, 9 + count * 2, 2): - byte = chr(int(line[i:i+2], 16)) + byte = int2byte(int(line[i:i+2], 16)) if bin[(i - 9) / 2 + addr] != '\xFF' and \ not doublewriteWarned: doublewriteWarned = True @@ -180,7 +192,7 @@ class IO_ihex: raise TOPException("Invalid IHEX format (unsup type %d)" % type) except ValueError: raise TOPException("Invalid IHEX format (digit format)") - return "".join(bin) + return b"".join(bin) def fromBinary(self, binData): ihex = [] @@ -201,7 +213,7 @@ class IO_ihex: ihex.append(":%02X%04X%02X" % (size, addr, self.TYPE_DATA)) checksum += size + ((addr >> 8) & 0xFF) + (addr & 0xFF) + self.TYPE_DATA for j in range(0, size): - data = ord(binData[i + j]) + data = byte2int(binData[i + j]) checksum = (checksum + data) & 0xFF ihex.append("%02X" % data) checksum = ((checksum ^ 0xFF) + 1) & 0xFF diff --git a/toprammer-gui b/toprammer-gui index 4e8e49b..4a93e6b 100755 --- a/toprammer-gui +++ b/toprammer-gui @@ -1036,36 +1036,42 @@ class HexEditWidget(QWidget): self.bytesPerLine = 16 - self.setData("") + self.previousData = b"" + self.setData(b"") self.__setCursor(0, False) self.setFocusPolicy(Qt.StrongFocus) def updateText(self): - nrLines = 0 - text = [] - asc = [] + lines = [] if self.data: - for i in range(0, len(self.data)): - if i % 16 == 0: - if i != 0: - text.append(" |%s|\n" % "".join(asc)) - asc = [] - nrLines += 1 - text.append("[%08X]: " % i) - text.append(" " + chr2hex(self.data[i])) - asc.append(bin2ascii(self.data[i])) - if asc: - if len(self.data) % 16: - nrLeft = 16 - (len(self.data) % 16) - text.append(" " * nrLeft) - text.append(" |%s|" % "".join(asc)) - nrLines += 1 + for i in range(0, len(self.data), self.bytesPerLine): + end = min(i + self.bytesPerLine, len(self.data)) + data = self.data[i : end] + try: + prevData = self.previousData[i : end] + except IndexError: + prevData = None + if prevData == data: + lines.append(self.textLines[i // self.bytesPerLine]) + else: + text = [ "[%08X]: " % i ] + for byte in data: + text.append(" " + byte2hex(byte)) + if len(data) % self.bytesPerLine: + # padding + nrLeft = self.bytesPerLine - (len(data) % self.bytesPerLine) + text.append(" " * nrLeft) + text.append(" |") + for byte in data: + text.append(bytes2ascii(byte)) + text.append("|") + lines.append("".join(text)) else: - text = [ "", ] - text = "".join(text) - self.textLines = text.splitlines() - self.nrLines = nrLines + lines = [ "", ] + self.textLines = lines + self.nrLines = len(lines) + self.previousData = self.data width = self.fontMetrics().width(self.textLines[0]) height = self.nrLines * self.charHeight @@ -1091,7 +1097,7 @@ class HexEditWidget(QWidget): data[self.cursorByte] = char self.__setCursor(self.cursorByte + 1, True) else: - dataByte = ord(data[self.cursorByte]) + dataByte = byte2int(data[self.cursorByte]) char = ord(char.upper()) if char >= ord("0") and char <= ord("9"): nibble = char - ord("0") @@ -1360,7 +1366,7 @@ class InfoBufferWidget(BufferWidget): def setChipSignature(self, bindata): if bindata: - self.chipSig.setText(bin2hex(bindata)) + self.chipSig.setText(bytes2hex(bindata)) else: self.chipSig.setText("None") @@ -1471,7 +1477,7 @@ class BitBufferWidget(BufferWidget): if desc: desc = " (%s)" % desc checkbox = QCheckBox("bit %d%s" % (i, desc), self.bitsArea) - if ord(image[i // 8]) & (1 << (i % 8)): + if byte2int(image[i // 8]) & (1 << (i % 8)): checkbox.setCheckState(Qt.Checked) self.bitsArea.layout().addWidget(checkbox, 1 + i, 0, 1, 2) self.connect(checkbox, SIGNAL("stateChanged(int)"), @@ -1480,7 +1486,7 @@ class BitBufferWidget(BufferWidget): self.__updateHexView() def __updateHexView(self): - self.hexView.setText(bin2hex(self.image)) + self.hexView.setText(bytes2hex(self.image)) self.bitsAreaScroll.setWidget(self.bitsArea) def __bitStateChanged(self, unused): -- cgit v1.2.3