Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Advanced info on CPU Emulators Post 302533401 by Corona688 on Thursday 23rd of June 2011 01:53:42 PM
Old 06-23-2011
Quote:
Originally Posted by theKbStockpiler
I think I have figured out that part of the Emulator application is an Interpreter (if we choose this route) , that the Emulator programmer writes that uses the native target binary as source code (basically) and this Emulators output creates a phony CPU that exists by the means of the storage of it's states. I'm interested to know if I have to translate machine code by hand to assembly or how that works?
It doesn't use it as source code as much as byte code. It doesn't have to compile or even translate it into local machine code -- all the programmer has to know is what bytes mean what instructions.

How about an imaginary processor with three registers and three instructions?

Code:
#include <stdio.h>

int main(void)
{
        int running=1;
        // The memory the emulated program is read from
        unsigned char program[]={0x01, 13, 0x02, 12, 0x03, 0x00 };
        unsigned char a=0, b=0, ip=0; // A reg, B reg, instruction pointer

        while(running)
        {
                printf("A=0x%02x B=0x%02x IP=0x%02x\n", a, b, ip);
                switch(program[ip++])
                {
                case 0x01:  // Load byte into a
                        printf("LODA 0x%02x\n", program[ip]);
                        a=program[ip++];
                        break;
                case 0x02: // load byte into b
                        printf("LODB 0x%02x\n", program[ip]);
                        b=program[ip++];
                        break;
                case 0x03:
                        printf("ADD B,A\n");
                        b += a;
                        break;
                case 0x00:
                        printf("HALT\n");
                        running=0;
                        break;
                default:
                        printf("ERROR invalid instruction 0x%02x\n", program[ip-1]);
                        return(1);
                        break;
                }
        }

        printf("A=0x%02x B=0x%02x IP=0x%02x\n", a, b, ip);
        return(0);
}

A real processor would be much more complicated of course. x86 and x86_64 for instance have instructions of different sizes, some as few as 1 byte and some more than 12.

Quote:
The part right now I can't grasp is where does the Emulation stop and when can the host use it?
I'm still not sure what you mean.
Quote:
If I Emulate the hardware it is still in the form that the host can not use.
emulation doesn't turn a foreign program into a local program. Usually you emulate more of the system and interact with the system itself.

How to get the data out depends on what's being emulated how. DOSbox for example supports files, you could write the data you wanted to file to make it available in the host OS. It can also pretend to have a fake modem or serial port, letting things outside the emulator connect to programs inside the emulator over TCP.

Quote:
Emulation seems like it is a painfully in-direct. Why not just translate it to begin with?
How to do that isn't always obvious. How do you make 16-bit real-mode base/offset style memory access work in 64-bit protected mode with all the same side-effects? How do you translate instructions your processor has no direct equivalent for -- some architectures have weird ones, like "skip next instruction if bit N is set in register W". You could do it in two instructions but then you'd have to worry about whether the flags register got altered by instruction 2 in ways that change how the program will branch later. How do you keep track of what your instruction pointer is supposed to be when the instructions aren't the same size they used to be? What about instructions like REP STOSB which are entire little self-contained loops?

And once you translate it, then what? The program's still going to expect to be talking to its native operating system and not yours. You'd need to write your own substitues. (Sort of what WINE does. WINE doesn't need to emulate anything, since it's running x86 programs on an x86 machine, but it does need to provide the Windows libraries that Windows programs expect.)

Not saying it's impossible, but it wouldn't be easy, would be difficult to debug, couldn't be ported anywhere, and could end up being as much overhead as just emulating it.

Last edited by Corona688; 06-23-2011 at 03:13 PM..
This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

any emulators

iam new to unix , i would like to practice unix commands at home, can anyone help me to know if there are any emulators that i can download to practice or any trial versions that can be installed to practice. Regards dep (1 Reply)
Discussion started by: dep
1 Replies

2. UNIX for Dummies Questions & Answers

bus speed and CPU info

Hi, Is there a command I can use to find out how many CPU's and what type are on my server? (I was told to use cat /proc/cpuinfo) Also, how do I know what kind of bus speeds are on my server? Thanks in advance:) (3 Replies)
Discussion started by: ihot
3 Replies

3. HP-UX

cpu info

is there a single command or location from which one can get information like cpu Mhz,cpu cache...etc in HP UX:) (1 Reply)
Discussion started by: vijayca
1 Replies

4. Gentoo

top in batch mode, cpu info is wrong

well. the title says it all. im runing top in batch mode like this top -b -n1 > somefile but the cpu usage info is not correct. if i run top normally, the first second, i see the same wrong info, and then it corrects itself. i found only one small mention of it on this forum. with this link... (7 Replies)
Discussion started by: broli
7 Replies

5. AIX

How to access process and cpu info on AIX?

Hi, may be this is an AIX noob question: my current C++ application runs on Linux and is quite memory consuming. Therefore, the application writes a logfile after it has finished containing memory information, CPU information, information on the running other processes besides my application... (5 Replies)
Discussion started by: DarthVader77
5 Replies

6. HP-UX

CPU Info

Hi, I am going to buy a software that is licenced per CORE. I have a HPUX B1123 64 bit with 8 cpus. How can i know how many cores are in my machine ? Thanks (3 Replies)
Discussion started by: yoavbe
3 Replies

7. Shell Programming and Scripting

Generic command for CPU info

Dear all, Is there any generic command working on all Unix listing the CPU of a server? I found different command line per OS but I am looking for a more generic one. Thanks for your answer. (5 Replies)
Discussion started by: sgoiffon
5 Replies

8. Shell Programming and Scripting

Perl agent which calculates CPU info and more

Hello to everyone. This is my first post. I want to make one perl agent which calculate following things from Linux Server. Actually I want to pull all this information from 2 linux client and wants to display on web interface. First I want to calculate below details 1) CPU 2) MEMORY 3)... (6 Replies)
Discussion started by: sania.mirza
6 Replies

9. AIX

To get only the cpu info from the topas command terminal

To get only the cpu info from the topas command terminal. CPU User% Kern% Wait% Idle% Physc Entc ALL 2.3 4.4 0.0 93.3 0.07 7.7 I tried some thing like this but did not work topas << done grep "ALL" q done Can someone help me in this. (5 Replies)
Discussion started by: rpm120
5 Replies

10. UNIX for Dummies Questions & Answers

Best ways to get clear info about CPU and Memory

Hello all i did search the web and found allot of answers but im confused what are the best ways to get this info via Linux default commands 1. current Cpu Usage in Percent 2. current Memory Usage In Bytes 3. current Memory Available In Bytes Thanks! (2 Replies)
Discussion started by: umen
2 Replies
All times are GMT -4. The time now is 12:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy