Created
July 9, 2014 20:30
-
-
Save jedie/356d51afc2a84bef4f19 to your computer and use it in GitHub Desktop.
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 python | |
# encoding:utf-8 | |
""" | |
Hacked script to reformat | |
:created: 2014 by Jens Diemer - www.jensdiemer.de | |
:license: GNU GPL v3 or above, see LICENSE for more details. | |
""" | |
SKIP_LINES = ( | |
"Disassembly of D64 ROM - 32K mode", | |
"($8000 - $9FFF)" | |
) | |
def nice_hex(v): | |
if v < 0x100: | |
return "$%02x" % v | |
if v < 0x10000: | |
return "$%04x" % v | |
return "$%x" % v | |
def format_comment(start, end, comment): | |
if start == end or end is None: | |
addr = nice_hex(start) | |
else: | |
addr = "%s-%s" % (nice_hex(start), nice_hex(end)) | |
return "%-11s ; %s" % (addr, comment) | |
def reformat(filename): | |
result = [] | |
text=[] | |
addr_info=[] | |
start_addr = None | |
last_addr = None | |
in_code_block=False | |
with open(filename, "r") as f: | |
lines = f.readlines() | |
lines.append("--END--") # Hack that the last Block will be also consume ;) | |
for line in lines: | |
line = line.strip() | |
if not line or line in SKIP_LINES: | |
continue | |
#~ print line | |
addr = line[:4] | |
try: | |
addr = int(addr, 16) | |
except: | |
addr = None | |
try: | |
comment = line.split(";",1)[1] | |
except IndexError: | |
comment = None | |
if addr: | |
if comment: | |
info = "%-11s ; %s" % ( | |
nice_hex(addr), comment | |
) | |
#~ print info | |
addr_info.append(info) | |
if start_addr is None: | |
#~ print "+++ START %x +++" % addr | |
start_addr=addr | |
last_addr=addr | |
else: | |
if start_addr is not None: | |
# a block comments ends | |
#~ print "+++ END %x-%x +++" % (start_addr, last_addr) | |
result.append("") | |
if text: | |
for block_line in text: | |
block_line = "* %s" % block_line | |
block_line = format_comment( | |
start_addr, last_addr, block_line | |
) | |
result.append(block_line) | |
text=[] | |
start_addr = None | |
result += addr_info | |
addr_info = [] | |
text.append(line.strip("* ")) | |
return result | |
lines = """ | |
############################################################################### | |
# Information about Dragon 64 ROM - 32K mode | |
# | |
# Collected from: | |
# http://archive.worldofdragon.org/phpBB3/viewtopic.php?f=5&t=4370&start=10#p11378 | |
# AUTHOR: sorchard alias Stew | |
# | |
# copyleft: 2014 by the 6809 team, see AUTHORS for more details. | |
# license: GNU GPL v3 or above, see LICENSE for more details. | |
# | |
############################################################################### | |
""".strip().splitlines() | |
lines += reformat("D64ROM.TXT") | |
lines += reformat("D64ROM2.TXT") | |
for line in lines: | |
print line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment