joeylib2/scripts/lib/mameNullModemCfg.py
2026-10-13 14:01:46 -05:00

70 lines
2.9 KiB
Python

#!/usr/bin/env python3
# Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License.
# See LICENSE.md for license information.
# SPDX-License-Identifier: MIT
"""mameNullModemCfg.py - a MAME cfg file that sets a null_modem's line.
MAME's null_modem clocks the wire itself, at 9600 8 N 1 unless its
configuration says otherwise, so a guest whose UART runs at any other rate
sees garbage. The line is a CONFIG input port on the device, which MAME
reads back from cfg/SYSTEM.cfg in its -cfg_directory.
mameNullModemCfg.py SYSTEM TAG BAUD [DATABITS PARITY STOPBITS]
SYSTEM is the driver (x68000, apple2gs, apple2gsr1), TAG the null_modem's
device tag (":rs232c:null_modem", ":modem:null_modem"). The line may also
come as one quoted argument ("4800 7 E 1"). DATABITS PARITY STOPBITS
default to 8 N 1. Prints the cfg; exit 2 for a line MAME cannot set.
"""
import sys
# MAME's RS232_* enum values (src/devices/bus/rs232/rs232.h).
BAUD = {"50": 14, "75": 15, "110": 0, "150": 1, "200": 17, "300": 2, "600": 3,
"1200": 4, "1800": 18, "2000": 19, "2400": 5, "3600": 20, "4800": 6,
"7200": 21, "9600": 7, "14400": 8, "19200": 9, "28800": 10,
"38400": 11, "57600": 12, "115200": 13}
DATA = {"5": 0, "6": 1, "7": 2, "8": 3}
PARITY = {"N": 0, "O": 1, "E": 2}
STOP = {"1": 1, "2": 3}
DEFAULT_FORMAT = ["8", "N", "1"]
# The null_modem's own defaults (src/devices/bus/rs232/null_modem.cpp).
BAUD_DEFAULT = 7
DATA_DEFAULT = 3
PARITY_DEFAULT = 0
STOP_DEFAULT = 1
def main(argv):
if len(argv) < 4:
sys.stderr.write(__doc__)
return 2
system = argv[1]
tag = argv[2]
line = " ".join(argv[3:]).split()
if len(line) == 1:
line += DEFAULT_FORMAT
if len(line) != 4:
sys.stderr.write("mameNullModemCfg.py: the line is BAUD [DATABITS PARITY STOPBITS]\n")
return 2
baud, data, parity, stop = line
parity = parity.upper()
for value, table, what in ((baud, BAUD, "baud rate"), (data, DATA, "data bits"),
(parity, PARITY, "parity"), (stop, STOP, "stop bits")):
if value not in table:
sys.stderr.write("mameNullModemCfg.py: MAME's null_modem has no %s %s (it takes %s)\n"
% (what, value, " ".join(table)))
return 2
ports = [("RS232_TXBAUD", BAUD_DEFAULT, BAUD[baud]), ("RS232_RXBAUD", BAUD_DEFAULT, BAUD[baud]),
("RS232_DATABITS", DATA_DEFAULT, DATA[data]), ("RS232_PARITY", PARITY_DEFAULT, PARITY[parity]),
("RS232_STOPBITS", STOP_DEFAULT, STOP[stop])]
print('<?xml version="1.0"?>\n<mameconfig version="10">\n <system name="%s">\n <input>' % system)
for name, default, value in ports:
print(f' <port tag="{tag}:{name}" type="CONFIG" mask="255" defvalue="{default}" value="{value}" />')
print(' </input>\n </system>\n</mameconfig>')
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))