8086 PC emulator written entirely in QuickBASIC!

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Mike Chambers
Newbie
Posts: 5
Joined: Sun Jul 27, 2008 5:39 pm
Location: St. Louis, MO

8086 PC emulator written entirely in QuickBASIC!

Post by Mike Chambers »

This was an absolutely terrible (in a good way) idea. I wrote an 8086 CPU emulator entirely in QuickBASIC, then added enough PC hardware support to allow it to boot DOS and run programs. It's called QB8086. I just wanted to see how well it could work. :)

If you're not familiar with how emulation works, here's a quick overview. QB8086 runs compiled bytecode (i.e. machine language) designed to run on an 8086 CPU. It emulates the address (memory) space of an 8086 by using arrays of bytes, loads a real PC BIOS ROM binary into it, and steps through each instruction in RAM. It decodes each instruction the same way the real CPU does, but performs each operation using equivalent QuickBASIC code. It has variables to simulate the 8086's internal registers (AX, BX, ES, etc), and manipulates them as each CPU instruction is supposed to on the real processor.

In this way it can run DOS, PC games, etc. that a real 8086 PC can.

It is EXTREMELY slow, but it really works. There is one very short piece of ASM I wrote that I embedded in a string too. All that does is act as a new keyboard interrupt handler that toggles a flag in a QB variable that lets the emulator know a key was pressed, so it can then read the scancode from the KB port and pass it on to the emulation engine. But yes, a functional 8086 PC emulator... all in good ol' QuickBASIC.

It can emulate CGA text modes, CGA graphics modes, and 320x200 8-bit MCGA graphics mode. It also forwards emulated output to the Adlib ports through to the host system's ports, so if a game being emulated uses Adlib you will actually hear the sound in DOSBox. (or a real host PC if it has an SB/Adlib)

There do seem to be a few CPU bugs, but for the most part it works fairly well. It can even emulate Windows 3.0! See screenshots below.

You can run QB8086 in DOSBox, but you really need to configure DOSBox with "cpu=dynamic" and "cycles=max" for any sort of useable speed... and even then it's still slow. If you can set up a VM in VirtualBox to run it instead, if you have a good host CPU (like a core i7) then it actually does reach the about speed of the original PC. A Pentium 4 will do it around half the speed in a VBox VM. I even ran it on an old 286 computer, where it worked... but it took a good 10 minutes just to finish emulating the BIOS POST. :P

Download source code and binary, plus a bootable hard drive image with DOS 6.22 and several very old CGA games:
http://rubbermallet.org/qb8086/qb8086-0.14.3.28.zip" target="_blank

I know some of that code isn't pretty, and some of the indentation is screwed up... sorry. I only tested this with QuickBASIC 7.1 PDS. QB45 will probably work. QBASIC and QB64 won't work.

You can edit the qb8086.ini file to change configuration options. It can emulate between 368 KB and 640 KB of RAM, but any memory beyond 368 KB is simulated using disk swap... which is slow as hell, avoid it if you can. When you're running the emulator, you can press F11 to toggle on-screen update of the emulated 8086's current CS:IP registers and how many instructions have been emulated total. Press F12 to exit QB8086.

And now for some neato screenshots...

Image
QB8086 emulator running a BIOS

Image
QB8086 emulator running Ms. Pacman

Image
QB8086 emulator running Stargate

Image
QB8086 emulator starting Windows 3.0

Image
QB8086 emulator actually running Windows 3.0 desktop

Image
QB8086 emulator running Zaxxon

DOSBox is not directly emulating those games/programs... DOSBox is running QB8086 which in turn is emulating what you see. QB8086 is a rough port of a PC emu I wrote in C called Fake86.
-Mike
User avatar
bongomeno
Veteran
Posts: 266
Joined: Wed Dec 10, 2008 9:08 am
Location: Arizona
Contact:

Re: 8086 PC emulator written entirely in QuickBASIC!

Post by bongomeno »

I am not sure why I haven't seen this or why on one has replied, but I must say thank you for this extremely educational work of art!
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: 8086 PC emulator written entirely in QuickBASIC!

Post by burger2227 »

He has also managed to get it working with QB64.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Erik
Veteran
Posts: 72
Joined: Wed Jun 20, 2007 12:31 pm
Location: LI, NY
Contact:

Re: 8086 PC emulator written entirely in QuickBASIC!

Post by Erik »

Nice work! This is really awesome!
iamdenteddisk
Veteran
Posts: 185
Joined: Mon Jun 30, 2008 4:10 pm

Re: 8086 PC emulator written entirely in QuickBASIC!

Post by iamdenteddisk »

this looks really awesome I wish I could find some implementation info for VM or on a HDD. I didn't see any readme.txt file or anything other than source ,executables and what i think is a bootstrap IDK,

this could possibly be something i have been looking for, for a few years in fact..

please reply again and let us know if we can use it??
Mike Chambers
Newbie
Posts: 5
Joined: Sun Jul 27, 2008 5:39 pm
Location: St. Louis, MO

Re: 8086 PC emulator written entirely in QuickBASIC!

Post by Mike Chambers »

iamdenteddisk wrote:this looks really awesome I wish I could find some implementation info for VM or on a HDD. I didn't see any readme.txt file or anything other than source ,executables and what i think is a bootstrap IDK,

this could possibly be something i have been looking for, for a few years in fact..

please reply again and let us know if we can use it??
Hi sorry for the late reply, yeah you can use it for whatever you want.

The easiest way it just running it under DOSBox. Put all the files in the ZIP somewhere, navigate to the path in DOSBox and run qb8086.exe

You really should set the DOSBox config file so that cpu=dynamic and cycles=max for remotely decent performance. The best performance by far will be in a real VM like VirtualBox or on bare metal. To make a VIrtualBox system, create a new VM with MS-DOS as the OS type. Use a VMDK format blank disk. Install MS-DOS from floppy images. Then use something like WinImage to inject all the files from the ZIP into the VMDK file. Run the VM again and start QB8086.
-Mike
iamdenteddisk
Veteran
Posts: 185
Joined: Mon Jun 30, 2008 4:10 pm

Re: 8086 PC emulator written entirely in QuickBASIC!

Post by iamdenteddisk »

thanks so much..
Post Reply