Created
April 1, 2021 07:47
-
-
Save tako2/8dd34cd81fe8dceb7dfd6ee284477fdb to your computer and use it in GitHub Desktop.
Dump binary file with check sums like pages published on retro pc magazines.
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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import sys | |
from optparse import OptionParser | |
#============================================================================== | |
def dump_block(addr, data): | |
print('Add +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F :Sum') | |
idx = 0 | |
asum = 0 | |
for y in range(16): | |
print('{:04X} '.format(addr), end='') | |
xsum = 0 | |
for x in range(16): | |
xsum += data[idx] | |
print('{:02X} '.format(data[idx]), end='') | |
idx += 1 | |
xsum &= 0xff | |
print(':{:02X}'.format(xsum)) | |
asum += xsum | |
addr += 0x10 | |
print('--------------------------------------------------------') | |
print('Sum ', end='') | |
for x in range(16): | |
ysum = 0 | |
for y in range(16): | |
ysum += data[y * 16 + x] | |
ysum &= 0xff | |
print('{:02X} '.format(ysum), end='') | |
asum &= 0xff | |
print(':{:02X}'.format(asum)) | |
print() | |
#============================================================================== | |
def dump_file(in_file, start): | |
with open(in_file, 'rb') as in_fp: | |
data = in_fp.read() | |
if (len(data) % 256) > 0: | |
data += b'\x00' * (256 - (len(data) % 256)) | |
addr = start | |
for idx in range(0, len(data), 256): | |
dump_block(addr, data[idx:idx+256]) | |
addr += 256 | |
#============================================================================== | |
def main(options, args): | |
try: | |
if options.start.startswith('0x'): | |
start = int(options.start[2:], 16) | |
else: | |
start = int(options.start) | |
except: | |
start = 0 | |
dump_file(args[0], start) | |
#============================================================================== | |
if __name__ == '__main__': | |
parser = OptionParser(usage='Usage: %prog [options] binary_file') | |
parser.add_option('-s', '--start', | |
type='str', dest='start', default='0x0000', | |
help='set START address', metavar='START') | |
options, args = parser.parse_args() | |
if len(args) != 1: | |
parser.print_help() | |
exit() | |
main(options, args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment