Generate flat binary
This commit is contained in:
parent
69bb5a9dab
commit
a853e8eaad
14
asm.py
14
asm.py
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
from types import SimpleNamespace
|
from types import SimpleNamespace
|
||||||
from rply import ParserGenerator, LexerGenerator
|
from rply import ParserGenerator, LexerGenerator
|
||||||
|
import struct
|
||||||
|
|
||||||
OPS = {
|
OPS = {
|
||||||
'ADD': lambda a, b: a + b,
|
'ADD': lambda a, b: a + b,
|
||||||
|
@ -205,13 +206,14 @@ def none(p):
|
||||||
@pg.production("line : EOL")
|
@pg.production("line : EOL")
|
||||||
def line(p):
|
def line(p):
|
||||||
label = None
|
label = None
|
||||||
|
instr = Directive(label=None, directive=None, args=None)
|
||||||
for t in p:
|
for t in p:
|
||||||
if type(t) in (ASM, Directive):
|
if type(t) in (ASM, Directive):
|
||||||
t.label = label
|
instr = t
|
||||||
return t
|
|
||||||
elif t.gettokentype() == 'SYMBOL':
|
elif t.gettokentype() == 'SYMBOL':
|
||||||
label = t.getstr()
|
label = t.getstr()
|
||||||
return None
|
instr.label = label
|
||||||
|
return instr
|
||||||
|
|
||||||
@pg.production("op : OP2 arg_b COMMA arg_a")
|
@pg.production("op : OP2 arg_b COMMA arg_a")
|
||||||
def op_op2(p):
|
def op_op2(p):
|
||||||
|
@ -393,7 +395,13 @@ if __name__ == '__main__':
|
||||||
assemble(sym, inst)
|
assemble(sym, inst)
|
||||||
print(sym)
|
print(sym)
|
||||||
sym['.addr'] = 0
|
sym['.addr'] = 0
|
||||||
|
binimage = b''
|
||||||
for inst in code: # pylint: disable=E1133
|
for inst in code: # pylint: disable=E1133
|
||||||
a = assemble(sym, inst, 2)
|
a = assemble(sym, inst, 2)
|
||||||
if a is not None:
|
if a is not None:
|
||||||
print(["%04x" % x for x in a])
|
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)
|
||||||
|
|
Loading…
Reference in New Issue