Generate flat binary

This commit is contained in:
Maurizio Porrato 2020-01-21 23:31:47 +00:00
parent 69bb5a9dab
commit a853e8eaad
1 changed files with 11 additions and 3 deletions

14
asm.py
View File

@ -2,6 +2,7 @@
from types import SimpleNamespace
from rply import ParserGenerator, LexerGenerator
import struct
OPS = {
'ADD': lambda a, b: a + b,
@ -205,13 +206,14 @@ def none(p):
@pg.production("line : EOL")
def line(p):
label = None
instr = Directive(label=None, directive=None, args=None)
for t in p:
if type(t) in (ASM, Directive):
t.label = label
return t
instr = t
elif t.gettokentype() == 'SYMBOL':
label = t.getstr()
return None
instr.label = label
return instr
@pg.production("op : OP2 arg_b COMMA arg_a")
def op_op2(p):
@ -393,7 +395,13 @@ if __name__ == '__main__':
assemble(sym, inst)
print(sym)
sym['.addr'] = 0
binimage = b''
for inst in code: # pylint: disable=E1133
a = assemble(sym, inst, 2)
if a is not None:
print(["%04x" % x for x in a])
for w in a:
binimage += struct.pack("<H", w)
outfilename = filename[:filename.rfind('.')]+'.bin'
with open(outfilename, 'wb') as binfile:
binfile.write(binimage)