#!/usr/bin/env python3 import argparse import datetime import re import sys RAM8_EC_TYPES = ['ADDRESS', 'DATA', 'MARCH', 'OUTPUT', 'WRITE'] RAM16_EC_TYPES = ['ADDRESS', 'DATA_LOWER', 'DATA_UPPER', 'DATA_BOTH', 'MARCH_LOWER', 'MARCH_UPPER', 'MARCH_BOTH', 'OUTPUT_LOWER', 'OUTPUT_UPPER', 'OUTPUT_BOTH', 'WRITE_LOWER', 'WRITE_UPPER', 'WRITE_BOTH'] RAM16_LOWER_EC_TYPES = ['ADDRESS', 'DATA', 'SKIP', 'SKIP', 'MARCH', 'SKIP', 'SKIP', 'OUTPUT', 'SKIP', 'SKIP', 'WRITE', 'SKIP', 'SKIP'] def main() -> int: def write_ram_ec(name: str, ec_types: list) -> None: nonlocal error_code ec_base = f'EC_{name}_BASE' if name == "RAM" else f'EC_{name}_RC_BASE' inc_fd.write(f'{ec_base: <30} equ ${error_code:02x}\n') for ec_type in ec_types: if ec_type != 'SKIP': ec_name = f'EC_{name}_{ec_type}' if name == "RAM" else f'EC_{name}_RC_{ec_type}' inc_fd.write(f'{ec_name: <30} equ ${error_code:02x} ; {error_code:0{args.bits}b}\n') error_code = error_code + 1 parser = argparse.ArgumentParser(description='generate error_codes.inc from error_codes.cfg') parser.add_argument('cfg_file', metavar='', type=str, help='input error_codes.cfg file') parser.add_argument('inc_file', metavar='', type=str, help='output error_codes.inc file') parser.add_argument('-b', dest="bits", metavar='', type=int, default=8, help='number of bits to display for error codes [default: 8]') args = parser.parse_args() cfg_lines = [] error_code = 1 try: with open(args.cfg_file, 'r') as f: cfg_lines = [line.rstrip() for line in f] except IOError as e: print(f'Unable to open \'{args.cfg_file}\' for reading: {e.strerror}') sys.exit(1) try: inc_fd = open(args.inc_file, 'w') except IOError as e: print(f'Unable to open \'{args.inc_file}\' for writing: {e.strerror}') sys.exit(1) inc_fd.write(f'; last updated by gen-error-codes on {datetime.datetime.now(datetime.UTC):%Y-%m-%d @ %H:%M UTC}\n') for line in cfg_lines: if line.startswith('#'): continue match = re.search('^RAM8 (.*)$', line) if match: write_ram_ec(match.group(1), RAM8_EC_TYPES) continue match = re.search('^RAM16 (.*)$', line) if match: write_ram_ec(match.group(1), RAM16_EC_TYPES) continue match = re.search('^RAM16_LOWER (.*)$', line) if match: write_ram_ec(match.group(1), RAM16_LOWER_EC_TYPES) continue match = re.search('^(EC_.*)$', line) if match: inc_fd.write(f'{match.group(1): <30} equ ${error_code:02x} ; {error_code:0{args.bits}b}\n') error_code = error_code + 1 continue inc_fd.write(f'{line}\n') inc_fd.close() return 0 if __name__ == '__main__': sys.exit(main())