joeylib2/scripts/lib/mameNullModemCfg.sh

82 lines
3.1 KiB
Bash
Executable file

#!/usr/bin/env bash
# Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License.
# See LICENSE.md for license information.
# SPDX-License-Identifier: MIT
#
# mameNullModemCfg.sh - 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.sh 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.
set -uo pipefail
label=mameNullModemCfg.sh
# MAME's RS232_* enum values (src/devices/bus/rs232/rs232.h), and each
# table's keys in the order a message lists them.
declare -A 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)
BAUD_KEYS="50 75 110 150 200 300 600 1200 1800 2000 2400 3600 4800 7200 9600 14400 19200 28800 38400 57600 115200"
declare -A DATA=([5]=0 [6]=1 [7]=2 [8]=3)
DATA_KEYS="5 6 7 8"
declare -A PARITY=([N]=0 [O]=1 [E]=2)
PARITY_KEYS="N O E"
declare -A STOP=([1]=1 [2]=3)
STOP_KEYS="1 2"
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
if [ $# -lt 3 ]; then
echo "usage: $label SYSTEM TAG BAUD [DATABITS PARITY STOPBITS]" >&2
exit 2
fi
system=$1
tag=$2
shift 2
read -ra line <<< "$*"
if [ "${#line[@]}" -eq 1 ]; then
line+=("${DEFAULT_FORMAT[@]}")
fi
if [ "${#line[@]}" -ne 4 ]; then
echo "$label: the line is BAUD [DATABITS PARITY STOPBITS]" >&2
exit 2
fi
baud=${line[0]}
data=${line[1]}
parity=${line[2]^^}
stop=${line[3]}
# check VALUE TABLE KEYS WHAT -- exit 2 unless TABLE has VALUE.
check() {
local -n table=$2
if [ -z "${table[$1]+x}" ]; then
echo "$label: MAME's null_modem has no $4 $1 (it takes $3)" >&2
exit 2
fi
}
check "$baud" BAUD "$BAUD_KEYS" "baud rate"
check "$data" DATA "$DATA_KEYS" "data bits"
check "$parity" PARITY "$PARITY_KEYS" "parity"
check "$stop" STOP "$STOP_KEYS" "stop bits"
printf '<?xml version="1.0"?>\n<mameconfig version="10">\n <system name="%s">\n <input>\n' "$system"
for port in "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]}"; do
read -r name default value <<< "$port"
printf ' <port tag="%s:%s" type="CONFIG" mask="255" defvalue="%s" value="%s" />\n' "$tag" "$name" "$default" "$value"
done
printf ' </input>\n </system>\n</mameconfig>\n'