summaryrefslogtreecommitdiff
path: root/crystal.py
diff options
context:
space:
mode:
authorBryan Bishop <kanzure@gmail.com>2012-04-21 11:33:23 -0500
committerBryan Bishop <kanzure@gmail.com>2012-04-21 11:33:23 -0500
commit61c429040f9f7a297741950ac6b5428ebfd6a2fd (patch)
treeca32b48871214473df9c74132290708a32440855 /crystal.py
parent55280b7f9e6e862834d95868245154472b40cdb8 (diff)
foundation for asm parsing
original-commit-id: 285b3066a443438b30691be7193e7f7135b9a950
Diffstat (limited to 'crystal.py')
-rw-r--r--crystal.py79
1 files changed, 79 insertions, 0 deletions
diff --git a/crystal.py b/crystal.py
index 1e3c728..a2c9fed 100644
--- a/crystal.py
+++ b/crystal.py
@@ -4428,6 +4428,85 @@ def apply_diff(diff, try_fixing=True, do_compile=True):
os.system("mv ../main1.asm ../main.asm")
return False
+class AsmLine:
+ #TODO: parse label lines
+ def __init__(self, line, bank=None):
+ self.line = line
+ self.bank = bank
+ def to_asm(self):
+ return self.line
+
+class Incbin:
+ def __init__(self, line, bank=None):
+ self.line = line
+ self.bank = bank
+ self.parse()
+ def parse(self):
+ incbin = self.line
+ partial_start = incbin[21:]
+ start = partial_start.split(",")[0].replace("$", "0x")
+ start = eval(start)
+ start_hex = hex(start).replace("0x", "$")
+
+ partial_interval = incbin[21:].split(",")[1]
+ partial_interval = partial_interval.replace(";", "#")
+ partial_interval = partial_interval.replace("$", "0x").replace("0xx", "0x")
+ interval = eval(partial_interval)
+ interval_hex = hex(interval).replace("0x", "$").replace("x", "")
+
+ end = start + interval
+ end_hex = hex(end).replace("0x", "$")
+
+ self.address = start
+ self.start_address = start
+ self.end_address = end
+ self.last_address = end
+ self.interval = interval
+ def to_asm(self):
+ if self.interval > 0:
+ return self.line
+ else: return ""
+
+def AsmSection:
+ def __init__(self, line):
+ self.bank_id = None
+ self.line = line
+ self.parse()
+ def parse(self):
+ line = self.line
+ bank_id = int(line.split("\"")[1].split("bank")[1])
+ self.bank_id = bank_id
+ start_address = bank_id * 0x4000
+ end_address = (bank_id * 0x4000) + 0x4000 - 1
+ #this entity doesn't actually take up this space..
+ #although it could be argued that lines should exist under this object
+ #self.address = self.start_address = start_address
+ #self.last_address = self.end_address = end_address
+ def to_asm(self):
+ return self.line
+
+class Asm:
+ """controls the overall asm output"""
+ def __init__(self, filename="../main.asm"):
+ self.parts = []
+ self.filename = filename
+ self.load_and_parse()
+
+ def load_and_parse(self):
+ self.parts = []
+ asm = open(self.filename, "r").read().split("\n")
+ asm_list = AsmList(asm)
+ bank = 0
+ for line in asm_list:
+ if line[0:6] == "INCBIN" or line[1:6] == "INCBIN":
+ thing = Incbin(line, bank=bank)
+ if line[0:7] == "SECTION":
+ thing = AsmSection(line)
+ bank = thing.bank_id
+ else:
+ thing = AsmLine(line, bank=bank)
+ self.parts.append(thing)
+
def index(seq, f):
"""return the index of the first item in seq
where f(item) == True."""