Quote:
Originally Posted by
theKbStockpiler
Okay, I write code that mimics a CPU in that it has memory assigned to registers and flags and such to document the entire state of the CPU. I can't find a way around that I believe that for every target CPU instruction my emulator basically looks up a routine in a table and this routine changes the state of my phony CPU that exists in memory.
A table, or a giant switch() statement, or what have you. It just has to make a decision between a lot of instructions at once.
Quote:
Now I would have the phony CPU run the data and this would just be saved in memory. What would the process or application be called that puts this data in a certain form so the O.S would then run it and how would it do it?
[edit] I think I misunderstood your question.
I'm not sure I understand it, for that matter. Put the data in a format so the O.S can run it? Run
data?
Are you asking how would you get useful data in/out of the emulator? Up to you I suppose. If you're expecting it to run programs meant for a different kind of computer, you'd probably need to emulate more hardware too, like DOSbox does.
DOSbox for example does what it says on the tin. It emulates a DOS machine well enough to run a good number of old games and applications. (On any system -- it doesn't depend on a real x86 cpu.) The list of hardware it emulates ends up being a bit daunting, including but probably not limited to:
- VGA controller, which displays the contents of a special hardware memory area on screen. In an emulator, the memory would be ordinary RAM like everything else. Routines independent of the CPU emulator would be responsible for transferring its contents into an ordinary graphical window where you can see them.
- Keyboard controller: DOS programs expect to communicate with the raw, old-fashioned keyboard controller, so DOSbox pretends to have one.
- PC speaker: To do proper PC speaker beeps, DOSbox pretends to have the old-fashioned IBM PC timer chip. This chip is necessary for lots of other things anyway, like the interrupts DOS and many games use to mark time.
- Soundblaster: It pretends to have one of these too. Or a Gravis Ultrasound.
- Adlib card: Mostly an OPL2 hardware synthesizer chip. An earlier soundcard that the Soundblaster cloned and expanded with raw digital sound.
- BIOS routines. In a real computer they're instructions stored in a special read-only area of memory. But since programs don't usually care what's in them as long as they work, DOSbox cheats a little, just triggering higher-level routines outside the emulated environment instead of doing all the work inside the emulated CPU. This includes some things like basic video mode changing, etc.
- Operating system routines. DOSbox gives you an actual DOS-like prompt and most of the DOS routines a real DOS environment would give to programs. This includes things like opening, reading, writing, deleting, closing files. DOSbox cheats. When the emulator causes a software interrupt that'd make a real CPU to jump to a location in memory, the emulator catches it, and does a real, native system call which ends up doing what the program asked. Not keeping all that DOS code in emulated memory also has the happy side effect of a DOS machine which can boot with nearly 640K free
- Comm ports and modems. In the days before widespread internet access most home telecommunication meant modems and COM ports. DOSbox can emulate having a dialup modem convincingly enough for some BBS software to work, but the pretend-modem is actually a TCP socket: When people connect to the socket, the DOS program is told the phone is ringing, and people can connect and talk with it with a terminal. Which is why we still have Tradewars 2001 in 2011
DOSbox even has several different CPU emulators -- a fast simplified one, a slower 32-bit one with protected mode, etc -- and switches between them depending on what CPU features are needed.
So there's a lot more to emulating a whole computer than emulating its CPU.