Backport some stuff in this commit - https://github.com/libretro/fceu-next/commit/aea20914a6734f82a0962c8b3204f54c1d06f671