''''
Script to convert EDTASM TRS-80 assembler files to ASCII text

fjkraan@electrickery.nl, 2025-05-03

This script is for EDTASM assembly files,
as they use for format specified below:

ASM format:
per line:
	five bytes with the line number, one decimal digit per byte.
	  the upper nibble has the pattern 1011b
	.... characters
	0x0D or carriage return
	
end of file:
	0x1F or Ctrl-Z

'''

import sys


if len(sys.argv) > 1:
    binFile = sys.argv[1]
else:
    print("Usage: python edtasmDecode.py <asmFile>")
    sys.exit()

f = sys.argv[1]

def printHex(bin):
    print("0x%02x " % ord(bin), end='')
    
def printChar(bin):
    print(bin.decode("utf-8"), end='')

with open(f, "rb") as f:
    while (bin := f.read(1)):
        if ord(bin) == 0x00:
            break
        if ord(bin) >= 0xB0:
            print(str(ord(bin) - 0xB0), end='')
        else:
            printChar(bin)
f.close()
