Page 1 of 3

Real Mode Emulator v2

Posted: Wed Mar 10, 2010 8:08 am
by thepowersgang
Well, nearly two years ago I posted one of my projects on this forum, the Real Mode Emulator (Link)

Recently, someone brought the code back up in another topic and this spurred me to complete it. I ended up essentially doing a complete rewrite but now I am truly confident that it is ready for release.

The current version has some opcodes still unimplemented, and there seems to be a bug somewhere but I can't seem to find it, but the codebase is stable and should be usable.

So without further ado, here is Real Mode Emulator 2.

The emulator is licensed under the BSD licence and id designed to be included in almost any OS.
There is a small amount of changes needed to the start of rme.c to account for different debugging function names and IO port access, but this should be all.
I have included with it a testing kernel that sets up an emulator context and calls different BIOS functions (atm these are selected by using #if statements to disable unused ones)

If people could try this out and post bug reports (unimplemented opcodes and badly implemented opcodes) it would be appreciated.
I will be pushing changes to my tree to my git repository as I go along, so if you have posted a bug report, expect it to be fixed in this version rather quickly.

NOTE: This emulator does not, and I doubt will ever, support size overrides. Including support for these would make the code that much more ugly to work with, so I am planning not to support them. If someone can convince me why I might need them, I may reconsider, if it can be done cleanly.

Re: Real Mode Emulator v2

Posted: Wed Mar 10, 2010 8:46 am
by quanganht
It was me who brought your code back from 6 feet under :)
I have a couple of (dumb) questions:
1. Can it work in Long mode?
2. Can I call Vesa interrupts using this emulator?

Sadly, my 64-bit kernel is on a long way to go, so I can't test this emulator yet. :(

Re: Real Mode Emulator v2

Posted: Wed Mar 10, 2010 8:48 am
by Combuster
thepowersgang wrote:NOTE: This emulator does not, and I doubt will ever, support size overrides. Including support for these would make the code that much more ugly to work with, so I am planning not to support them. If someone can convince me why I might need them, I may reconsider, if it can be done cleanly.
...because half of the modern bios call require 32 bits registers (=operand size prefix)

Re: Real Mode Emulator v2

Posted: Wed Mar 10, 2010 9:25 am
by quanganht
Just compiled the test kernel:

Code: Select all

lib.c: Assembler messages:
lib.c:18: Error: suffix or operands invalid for `mov'
lib.c:31: Error: suffix or operands invalid for `mov'

Re: Real Mode Emulator v2

Posted: Wed Mar 10, 2010 11:27 am
by earlz
So by "Real-Mode Emulator" do you mean you've built an 8086 emulator? Or is it more than that? Are all the opcodes interpreted?

Re: Real Mode Emulator v2

Posted: Wed Mar 10, 2010 5:24 pm
by thepowersgang
@earlz: It's currently an 8086 emulator.

@quanganht: Well, it's designed to be able to work in long mode, but I haven't explicitly tested it in long mode. As for Vesa interrupts, it should be able to call them.
Try changing the mov instructions to movl. On my version (GCC 4.4.1 / Binutils 2.20) it doesn't seem to error.

@Combuster: Hmm, I didn't know that (I rarely use BIOS functions). I'll look into how to implement size overrides without making the codebase messy.

Re: Real Mode Emulator v2

Posted: Thu Mar 11, 2010 5:00 am
by quanganht
movl doesn't help. I'm using GCC targeted at x86_64

Re: Real Mode Emulator v2

Posted: Thu Mar 11, 2010 6:12 am
by thepowersgang
I forgot to mention that the test kernel is for PMode.
The emulator (in rme/) should work x86_64, but the test kernel won't

Re: Real Mode Emulator v2

Posted: Thu Mar 11, 2010 1:00 pm
by earlz
thepowersgang wrote:@earlz: It's currently an 8086 emulator.
And is it interpreted or JITed or what? What is magic about it compared to porting an emulator to your OS?

Re: Real Mode Emulator v2

Posted: Thu Mar 11, 2010 2:43 pm
by ~
From what I saw in the code, it looks like it doesn't use JIT, but instead does manually all of the CPU operations (parity checking, etc.) using C macros, but I may be wrong.

I think it is intended to be used as a kernel-mode interface which would replace V86 mode in the same way as software multitasking replaces x86 built-in multitasking capabilities.

It is also more portable, which means that it could run 16-bit x86 code even if there's no V86 mode available, just what happens in 64-bit Long Mode.

Now BIOS interrupts could be executed (with care) as long as they don't contain instructions beyond 8086 or 32-bit register usage, without having to go back to Real Mode under any circumstances other than, maybe, really shuting down the PC.

Re: Real Mode Emulator v2

Posted: Thu Mar 11, 2010 5:40 pm
by thepowersgang
@~
Exactly. That is entirely the point of writing it.
Actually, the first version was written because I couldn't be bothered dropping to real mode or setting up VM8086 (I didn't have stable multitasking)

I'm currently working on getting size overrides working, but it seems that the Qemu BIOS doesn't use them.
Also, I've checked VESA and it seems to work correctly with AX=0x4F00, ..01 and ..02 in Qemu (I'm going to check bochs sometime today)

Re: Real Mode Emulator v2

Posted: Thu Mar 11, 2010 8:08 pm
by pcmattman
Sounds a lot like x86emu to me...

Re: Real Mode Emulator v2

Posted: Fri Mar 12, 2010 3:40 am
by thepowersgang
Possibly, I haven't had much experience with x86emu so I can't really say.
Mostly I'm doing this for fun, it may have already been done, but what are we all doing?

Re: Real Mode Emulator v2

Posted: Fri Mar 12, 2010 6:19 am
by Combuster
thepowersgang wrote:Also, I've checked VESA and it seems to work correctly with AX=0x4F00, ..01 and ..02 in Qemu (I'm going to check bochs sometime today)
I was mostly referring to real hardware which often uses 32-bit registers, and thus 32-bit outs (= o32 outw)

Some random function in my test box' video bios (copyright ATI/AMD, ~1996, posted under fair use) so you'll know what to expect :wink::

Code: Select all

00000572  6650              push eax
00000574  6653              push ebx
00000576  660FB7DB          movzx ebx,bx
0000057A  668BC3            mov eax,ebx
0000057D  66C1E303          shl ebx,0x3
00000581  B103              mov cl,0x3
00000583  E8C800            call 0x64e
00000586  7632              jna 0x5ba
00000588  662BD8            sub ebx,eax
0000058B  662BD8            sub ebx,eax
0000058E  B106              mov cl,0x6
00000590  E8BB00            call 0x64e
00000593  7625              jna 0x5ba
00000595  662BD8            sub ebx,eax
00000598  662BD8            sub ebx,eax
0000059B  B102              mov cl,0x2
0000059D  E8AE00            call 0x64e
000005A0  7618              jna 0x5ba
000005A2  662BD8            sub ebx,eax
000005A5  B104              mov cl,0x4
000005A7  E8A400            call 0x64e
000005AA  760E              jna 0x5ba
000005AC  662BD8            sub ebx,eax
000005AF  B101              mov cl,0x1
000005B1  E89A00            call 0x64e
000005B4  7604              jna 0x5ba
000005B6  D1EB              shr bx,1
000005B8  32C9              xor cl,cl
000005BA  8BC3              mov ax,bx
000005BC  665B              pop ebx
000005BE  8BD8              mov bx,ax
000005C0  6658              pop eax
000005C2  B81F00            mov ax,0x1f
000005C5  F7E3              mul bx
000005C7  BB9805            mov bx,0x598
000005CA  F7F3              div bx
000005CC  3DFF01            cmp ax,0x1ff
000005CF  7309              jnc 0x5da
000005D1  D1EB              shr bx,1
000005D3  D3EB              shr bx,cl
000005D5  3BDA              cmp bx,dx
000005D7  7701              ja 0x5da
000005D9  40                inc ax
000005DA  D1E8              shr ax,1
000005DC  8AD8              mov bl,al
000005DE  8AF9              mov bh,cl
000005E0  C3                ret

Re: Real Mode Emulator v2

Posted: Fri Mar 12, 2010 8:42 am
by earlz
thepowersgang wrote: I'm currently working on getting size overrides working, but it seems that the Qemu BIOS doesn't use them.
Also, I've checked VESA and it seems to work correctly with AX=0x4F00, ..01 and ..02 in Qemu (I'm going to check bochs sometime today)
Size overrides? I thought it was just emulating the 8086 right now? Have I forgotten some opcodes?

Also, I've made something similar to your project then. x86lib, it is a very lightweight library with it's only dependencies being exceptions and (optionally) cout/cin.