Created
May 16, 2017 01:10
-
-
Save drx/bb47566da766407f36b888084d7fbefe to your computer and use it in GitHub Desktop.
Detect EPROM errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import math | |
from collections import defaultdict | |
def inner_addresses(pin): | |
for i in range(2**pin): | |
yield i | |
def outer_addresses(pin, rom_size): | |
gap = 2**(pin+1) | |
addr = 0 | |
while addr < rom_size: | |
yield addr | |
addr += gap | |
for fn in sys.argv[1:]: | |
print(fn) | |
with open(fn, 'rb') as f: | |
rom = f.read() | |
rom_size = len(rom) | |
max_pin = int(math.log(rom_size)/math.log(2))-1 | |
ratios = [] | |
for pin in range(max_pin+1): | |
diff_count = 0 | |
match_count = 0 | |
for block in outer_addresses(pin, rom_size): | |
for inner in inner_addresses(pin): | |
if rom[block + inner] != rom[block + inner + 2**pin]: | |
diff_count += 1 | |
else: | |
match_count += 1 | |
ratio = float(match_count)/(diff_count+match_count) | |
ratios.append((ratio, pin)) | |
for ratio, pin in sorted(ratios, reverse=True): | |
broken_str = '' | |
if ratio > 0.50: | |
broken_str = ' (possibly broken)' | |
print('A{} - {:.2f}% repeats{}'.format(pin, ratio*100, broken_str)) | |
print('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment