Converted back to C++, incorporating fixes up to libretro/imame4all-libretro@474ff5d84fa76f54c10bd00e89f78af222df369d to simplify backporting more recent mame code.