summaryrefslogtreecommitdiff
path: root/source/notes/pic-mcu/pickit2/pk2cmd-stuff/readdat.py
diff options
context:
space:
mode:
Diffstat (limited to 'source/notes/pic-mcu/pickit2/pk2cmd-stuff/readdat.py')
-rw-r--r--source/notes/pic-mcu/pickit2/pk2cmd-stuff/readdat.py280
1 files changed, 280 insertions, 0 deletions
diff --git a/source/notes/pic-mcu/pickit2/pk2cmd-stuff/readdat.py b/source/notes/pic-mcu/pickit2/pk2cmd-stuff/readdat.py
new file mode 100644
index 0000000..6fc8919
--- /dev/null
+++ b/source/notes/pic-mcu/pickit2/pk2cmd-stuff/readdat.py
@@ -0,0 +1,280 @@
+import struct
+import sys
+
+import pic18
+import pic24
+import pk2cmd_script
+
+datfile = open("../pk2cmd/pk2cmd/PK2DeviceFile.dat").read()
+
+def readstr(datfile, offset):
+ length = ord(datfile[offset]) & 0xff
+ offset += 1
+ if length > 0x7f:
+ length &= 0x7f
+ length += 0x80 * (ord(datfile[offset]) & 0xff)
+ offset += 1
+ s = ""
+ for i in range(length):
+ s += datfile[offset]
+ offset += 1
+# if s[-1] == '\x0d':
+# s += datfile[offset]
+# offset += 1
+ return (offset, s)
+
+def readDeviceFileParams(datfile, offset):
+ result = {}
+ (result['versionMajor'], result['versionMinor'], result['versionDot']) = struct.unpack_from('=iii', datfile, offset)
+ offset += 12
+ (offset, result['versionNote']) = readstr(datfile, offset)
+ (result['numFamilies'], result['numParts'], result['numScripts']) = struct.unpack_from('=iii', datfile, offset)
+ offset += 12
+ result['compatibility'] = ord(datfile[offset])
+ offset += 1
+ offset += 1 + 2 + 4
+ return (offset, result)
+
+(offset, params) = readDeviceFileParams(datfile, offset=0)
+
+print(params)
+
+families = {}
+parts = {}
+scripts = {}
+
+def readDevicePart(datfile, offset):
+ result = {}
+ (offset, result['name']) = readstr(datfile, offset)
+ (
+ result['family'],
+ result['id'],
+ result['programMem'],
+ result['EEMem'],
+ result['EEAddr'],
+ result['configWords'],
+ result['configAddr'],
+ result['userIDWords'],
+ result['userIDAddr'],
+ result['bandGapMask']
+ ) = struct.unpack_from('=HIIHIBIBII', datfile, offset)
+ offset += 2 + 4 + 4 + 2 + 4 + 1 + 4 + 1 + 4 + 4
+ result['configMasks'] = [0] * 9
+ # entry 8 will come later. ~backwards compat~
+ (
+ result['configMasks'][0],
+ result['configMasks'][0],
+ result['configMasks'][0],
+ result['configMasks'][0],
+ result['configMasks'][0],
+ result['configMasks'][0],
+ result['configMasks'][0],
+ result['configMasks'][0]
+ ) = struct.unpack_from('=HHHHHHHH', datfile, offset)
+ offset += 2 * 8
+
+ result['configBlank'] = [0] * 9
+ # entry 8 will come later. ~backwards compat~
+ (
+ result['configBlank'][0],
+ result['configBlank'][0],
+ result['configBlank'][0],
+ result['configBlank'][0],
+ result['configBlank'][0],
+ result['configBlank'][0],
+ result['configBlank'][0],
+ result['configBlank'][0]
+ ) = struct.unpack_from('=HHHHHHHH', datfile, offset)
+ offset += 2 * 8
+ (
+ result['CPMask'],
+ result['CPConfig'],
+ result['OSSCALSave'],
+ result['ignoreAddress'],
+ result['vddMin'],
+ result['vddMax'],
+ result['vddErase'],
+ result['calibrationWords'],
+ result['chipEraseScript'],
+ result['progMemAddrSetScript'],
+ result['progMemAddrBytes'],
+ result['progMemRdScript'],
+ result['progMemRdWords'],
+ result['EERdPrepScript'],
+ result['EERdScript'],
+ result['EERdLocations'],
+ result['userIDRdPrepScript'],
+ result['userIDRdScript'],
+ result['configRdPrepScript'],
+ result['configRdScript'],
+ result['progMemWrPrepScript'],
+ result['progMemWrScript'],
+ result['progMemWrWords'],
+ result['progMemPanelBufs'],
+ result['progMemPanelOffset'],
+ result['EEWrPrepScript'],
+ result['EEWrScript'],
+ result['EEWrLocations'],
+ result['userIDWrPrepScript'],
+ result['userIDWrScript'],
+ result['configWrPrepScript'],
+ result['configWrScript'],
+ result['OSCCALRdScript'],
+ result['OSCCALWrScript'],
+ result['DPMask'],
+ result['writeCfgOnErase'],
+ result['blankCheckSkipUsrIDs'],
+ result['ignoreBytes'],
+ result['chipErasePrepScript'],
+ result['bootFlash']
+ ) = struct.unpack_from('=HB?IfffBHHBHHHHHHHHHHHHBIIIIIIIIIII??HHI', datfile, offset)
+ # f I B ? H
+ offset += 4 * 3 + 4 * 13 + 1 * 4 + 1 * 3 + 2 * 17
+ (
+ result['configMasks'][8], # and these are the last entries above
+ result['configBlank'][8],
+ result['progMemEraseScript'],
+ result['EEMemEraseScript'],
+ result['configMemEraseScript'],
+ result['Reserved1EraseScript'],
+ result['Reserved2EraseScript'],
+ result['testMemoryRdScript'],
+ result['testMemoryRdWords'],
+ result['EERowEraseScript'],
+ result['EERowEraseWords'],
+ result['exportToMPLAB'],
+ result['debugHaltScript'],
+ result['debugRunScript'],
+ result['debugStatusScript'],
+ result['debugReadExecVerScript'],
+ result['debugSingleStepScript'],
+ result['debugBulkWrDataScript'],
+ result['debugBulkRdDataScript'],
+ result['debugWriteVectorScript'],
+ result['debugReadVectorScript'],
+ result['debugRowEraseScript'],
+ result['debugRowEraseSize'],
+ result['debugReserved5Script'],
+ result['debugReserved6Script'],
+ result['debugReserved7Script'],
+ result['debugReserved8Script'],
+ result['LVPScript']
+ ) = struct.unpack_from('=HHHHHHHHHHH?HHHHHHHHHHHHHHHH', datfile, offset)
+ offset += 2 * 27 + 1 * 1
+# uhhhhh
+ offset -= 20
+ return (offset, result)
+
+def readDeviceFamily(datfile, offset):
+ result = {}
+ (result['id'], result['type'], result['searchPriority']) = struct.unpack_from('=HHH', datfile, offset)
+ offset += 6
+ (offset, result['name']) = readstr(datfile, offset)
+ (result['progEntryScript'], result['progExitScript'], result['readDevIDScript'], result['deviceIDMask'], result['blankValue'], result['bytesPerLocation'], result['addressIncrement']) = struct.unpack_from('=HHHiiBB', datfile, offset)
+ offset += 2 * 3 + 4 * 2 + 1 * 2
+ result['partDetect'] = ord(datfile[offset])
+ offset += 1
+ (
+ result['progEntryVPPScript'],
+ result['unused1'],
+ result['EEMemBytesPerWord'],
+ result['EEMemAddressIncrement'],
+ result['userIDHexBytes'],
+ result['userIDBytes'],
+ result['progMemHexBytes'],
+ result['EEMemHexBytes'],
+ result['progMemShift'],
+ result['testMemoryStart'],
+ result['testMemoryLength'],
+ result['vpp']
+ ) = struct.unpack_from('=HHBBBBBBBiHf', datfile, offset)
+ offset += 2 * 2 + 1 * 7 + 4 + 2 + 4
+ return (offset, result)
+
+def readScript(datfile, offset):
+ result = {}
+ result['number'] = struct.unpack_from('=H', datfile, offset)[0]
+ offset += 2
+ (offset, result['name']) = readstr(datfile, offset)
+ (result['version'], result['unused1'], result['length']) = struct.unpack_from('=HIH', datfile, offset)
+ offset += 8
+ script = []
+ print("Reading script " + result['name'] + " at " + hex(offset) + " len=" + str(result['length']))
+ for i in range(result['length']):
+ script.append(struct.unpack_from('=B', datfile, offset)[0])
+# script.append(struct.unpack_from('=B', datfile, offset + 1)[0])
+ offset += 2
+ result['script'] = script
+ (offset, result['comment']) = readstr(datfile, offset)
+ return (offset, result)
+
+print("Reading families...")
+
+for i in range(params['numFamilies']):
+ (offset, family) = readDeviceFamily(datfile, offset)
+ families[family['id']] = family
+
+print("Reading parts...")
+
+for i in range(params['numParts']):
+ (offset, part) = readDevicePart(datfile, offset)
+ parts[part['id']] = part
+
+print("Reading scripts...")
+
+for i in range(params['numScripts']):
+ (offset, script) = readScript(datfile, offset)
+ scripts[script['number']] = script
+
+database = { "parts": parts, "families": families, "scripts": scripts }
+
+print("Ready!")
+
+def findPart(model, database):
+ for p in database['parts']:
+ if database['parts'][p]['name'] == model:
+ return database['parts'][p]
+ return None
+
+part = findPart("PIC24FJ256DA206", database)
+
+if part is None:
+ print("Could not find part.")
+ sys.exit(1)
+
+def disassemble_script(content):
+ offset = 0
+ while offset < len(content):
+ (offset, instr) = pk2cmd_script.disassemble(content, offset)
+ print(instr)
+
+def printScript(script):
+ print(" Name: " + script['name'])
+ print(" Script length: " + str(script['length']))
+ if True:
+ disassemble_script(script['script'])
+# print("Comment: " + script['comment'])
+
+def printPart(part, database):
+ print("Name: " + part['name'])
+ family = database["families"][part["family"]]
+ print("Family: " + family['name'])
+ print("Program memory: " + str(part['programMem']))
+ print("Vdd_min: " + str(part['vddMin']))
+ print("Vdd_max: " + str(part['vddMax']))
+ print("Vdd_erase: " + str(part['vddErase']))
+ print("ProgMemAddrBytes: " + str(part['progMemAddrBytes']))
+ print("ChipEraseScript: ")
+ printScript(database['scripts'][part['chipEraseScript']])
+ print("ProgEntryScript: ")
+ printScript(database['scripts'][family['progEntryScript']])
+ print("ProgExitScript: ")
+ printScript(database['scripts'][family['progExitScript']])
+ print("ReadDevIDScript: ")
+ printScript(database['scripts'][family['readDevIDScript']])
+ print("ProgMemRdScript (id " + str(part['progMemRdScript']) + "): ")
+ printScript(database['scripts'][part['progMemRdScript']])
+ print("ProgMemAddrSetScript (id " + str(part['progMemAddrSetScript']) + "): ")
+ printScript(database['scripts'][part['progMemAddrSetScript']])
+
+printPart(part, database)