It's a bit complicated but this method does work. You need to have MAME 0.178 or newer to use it, if I recall correctly.
https://docs.mamedev.org/advanced/devicemap.html