Backport last part of https://github.com/flyinghead/flycast/commit/00358714227aa2acf6f35899a9829eab7a4ef83b#diff-d8363d6c25c1418dc77ef6545c73c83e878096e308a0b2471b1b86427729ceb7