This week, I got back to thinking about “unreal mode” (a.k.a. flat real mode) games, and whether there’s a way to make them work with SBEMU. I’ve been reading the Pentium Pro manual, and it looks like there might be? But I will need some input from someone more knowledgeable than me.
From what I understand, addressing is the only meaningful difference between a task in V86 mode and a task in "normal" ring 3 protected mode. So, just like how the V86 system virtualizes 16-bit real mode with 16-bit ring 3 protected mode, it should be possible to virtualize a 32-bit real mode task with 32-bit ring 3 protected mode.
- When a V86 task tries to enter unreal mode, instead of terminating it with a GPF like normal, switch the task into 32-bit protected mode with the same CPL/IOPL as the V86 task had. This gives it access to 32-bit addressing without breaking the model of 386 protected mode. It’s then handed over from the V86 monitor to the unreal monitor running within the same process. “Unreal monitor” is a term that I just made up, but it should be self-explanatory.
- Just like a V86 monitor, the unreal monitor catches GPFs, and, when appropriate, emulates the instructions that caused them. The range of instructions emulated has to be different (and probably much harder to implement), though, since unreal mode can do things that ordinary 16-bit real mode can’t.
- Interrupt handling and port trapping can be done on the ring 3 protected-mode task just like they would on a V86 task. This means the unreal mode program can work with SBEMU and other PCI sound drivers. (If it couldn’t, this entire exercise would be pointless.)
This all sounds simple enough when explained in a high-level overview like this, but the details add some complications. For one thing, the SMSW instruction is unprivileged [*], so a separate solution is needed to defeat the V86/protected mode detection that most unreal mode games have. Maybe there are some other unprivileged instructions that would also need to be patched or trapped with breakpoints. I don’t know.
So the question is, do any of those complications actually make it impossible to do? I haven’t found any such problem yet, but as someone who only recently started learning about how x86 stuff works, I’m not exactly an expert. So hopefully someone more knowledgeable can tell me either a) that this approach can work, or b) why it can’t.
[*] This is only mostly true. There actually is a CPU flag, CR4.UMIP, that makes SMSW generate a GPF in V86 mode and unprivileged protected mode. Unfortunately, that flag was first introduced in Ice Lake and Zen 2, less than 10 years ago! So most processors will need per-game patches or breakpoint lists for these instructions. However, it should be possible to generate either one automatically by running the game in an emulator and recording the locations of the relevant instructions.