Skip to content

Commit 7d333d4

Browse files
author
Leo
committed
New Function to read Verilog EEPROM format added
1 parent 5afa291 commit 7d333d4

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

pylab_ml/common/file_io.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,52 @@ def writeVlogMemFile(fileName, mem, a_dig=4, d_dig=8, adroffset=0, adrinc=1, hea
420420
fi.close()
421421
return True
422422

423+
def readVerilogMemFile(filename):
424+
"""
425+
Parses an EEPROM file with format '@Address Data' in hex
426+
and returns a dictionary with decimal keys and values.
427+
428+
Parameters
429+
@filename : string
430+
Path to the EEPROM file.
431+
432+
Returns
433+
eeprom_dict : dict
434+
Dictionary with address (decimal) as keys and data (decimal) as values.
435+
"""
436+
eeprom_dict = {}
437+
438+
try:
439+
with open(filename, 'r') as file:
440+
for line in file:
441+
line = line.strip()
442+
# Ensure the line starts with '@' and has both address and data
443+
if line.startswith('@'):
444+
parts = line.split()
445+
if len(parts) >= 2:
446+
# Extract and clean hex strings
447+
hex_address = parts[0].replace('@', '')
448+
hex_data = parts[1]
449+
450+
# Convert hex to decimal
451+
address_decimal = int(hex_address, 16)
452+
data_decimal = int(hex_data, 16)
453+
454+
# Store in dictionary
455+
eeprom_dict[address_decimal] = data_decimal
456+
457+
return eeprom_dict
458+
459+
except FileNotFoundError:
460+
print(f"Error: The file '{filename}' was not found.")
461+
return {}
462+
except ValueError:
463+
print(f"Error: Could not convert a value in the file. Check the hex format.")
464+
return {}
465+
except Exception as e:
466+
print(f"An unexpected error occurred: {e}")
467+
return {}
468+
423469

424470
if __name__ == '__main__':
425471
data = [1, 2, 3, 4, 5]

0 commit comments

Comments
 (0)