Skip to content

Instantly share code, notes, and snippets.

@michaelcmartin
Created May 10, 2015 22:53
Show Gist options
  • Save michaelcmartin/cc93d75f2b27550ddfd3 to your computer and use it in GitHub Desktop.
Save michaelcmartin/cc93d75f2b27550ddfd3 to your computer and use it in GitHub Desktop.
A simple Python utility to generate MLX listings
#!/usr/bin/python
# mlx.py - convert a .PRG program into an MLX listing.
# By default it uses the MLX 1 format (7 decimals per line), but
# the -2 option will use the MLX 2 format (9 hexadecimals per line).
import sys
def mlx1_encode(code, start_address):
while len(code) % 6 != 0:
code += "\x00"
index = 0
address = start_address
while index < len(code):
vals = [ord(c) for c in code[index:index+6]]
vals.append((sum(vals) + address) % 256)
print "%05d :%s" % (address, ",".join(["%03d" % i for i in vals]))
index += 6
address += 6
def mlx2_encode(code, start_address):
while len(code) % 8 != 0:
code += "\x00"
index = 0
address = start_address
while index < len(code):
vals = [ord(c) for c in code[index:index+8]]
checksum = (address - 254 * int(address / 256)) % 255
for v in vals:
checksum = (checksum * 2 + v) % 255
vals.append(checksum)
print "%04X:%s" % (address, " ".join(["%02X" % i for i in vals]))
index += 8
address += 8
def mlx_prg(prg, mlx_version=1):
if len(prg) < 3:
print "Not a PRG file"
start_address = ord(prg[0]) + 256*ord(prg[1])
if start_address >= 2048 and start_address < 12288:
startpage = (start_address + len(prg) + 254) / 256
print "Before loading MLX to enter this program, enter this line:"
print "POKE 44,%d:POKE %d,0:NEW" % (startpage, startpage*256)
print
print "Start address: %d" % start_address
print "End address: %d" % (start_address + len(prg) - 3)
print
if mlx_version == 2:
mlx2_encode(prg[2:], start_address)
else:
mlx1_encode(prg[2:], start_address)
if __name__=='__main__':
mlx_version = 1
target_file = None
for arg in sys.argv[1:]:
if arg == "-2":
mlx_version = 2
elif arg == "-1":
mlx_version = 1
else:
target_file = arg
if target_file == None:
print "Usage: %s [-2] prgfile"
else:
mlx_prg(file(target_file, "rb").read(), mlx_version)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment