473,756 Members | 4,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CPU simulator written in C

I have to do a homework: make a CPU simulator using C language.

I have a set of asm instructions so I have to write a program
that should:
- load .asm file
- view .asm file
- do a step by step simulation
- display registers contents

I tried to search con google with no success.

Any help (links, tricks/tips...)
would be really appreciated.

Thanks in advance.
Nov 14 '05
61 16283
><OT>
Why not simply use the pc as an index in memory? This
would allow you to write memory[pc]. And doesn't this
This is a great idea for simpler CPUs without memory management
hardware, and especially for CPUs where the maximum addressable
memory of the emulated CPU (e.g. Z80 or 8086) is small (e.g. 64k
bytes or even 1MB) compared to the available RAM of the host CPU
running the emulation.
perfectly match what a PC does, being an index into a
block of data?
Yes, given the absence of memory management. Just Intel segment-register
mappings don't make it THAT hard, but protected-mode memory management
can make it much harder. With memory management, things get
complicated all of a sudden, especially if it's allowed for a
multi-byte integer fetch to straddle memory-management pages, and
where multiple very-different addresses can refer to the same block
of memory.
<\OT>


Gordon L. Burditt
Nov 14 '05 #21
Gordon Burditt wrote:
<OT>
Why not simply use the pc as an index in memory? This
would allow you to write memory[pc]. And doesn't this


This is a great idea for simpler CPUs without memory management
hardware, and especially for CPUs where the maximum addressable
memory of the emulated CPU (e.g. Z80 or 8086) is small (e.g. 64k
bytes or even 1MB) compared to the available RAM of the host CPU
running the emulation.
perfectly match what a PC does, being an index into a
block of data?


Yes, given the absence of memory management. Just Intel segment-register
mappings don't make it THAT hard, but protected-mode memory management
can make it much harder. With memory management, things get
complicated all of a sudden, especially if it's allowed for a
multi-byte integer fetch to straddle memory-management pages, and
where multiple very-different addresses can refer to the same block
of memory.


I didn't think of this in the context of OP's homework
assignment. Thanks for the extra info! CPU (and virtual
machine) design is and remains a very interesting subject.

Case
<\OT>


Nov 14 '05 #22
In article <40************ **@sun.com>,
Eric Sosman <Er*********@Su n.COM> wrote:
SNIP....`

I once worked on an actual machine whose program counter
registers (there were two) were signed -- the explanation
given was that the same circuitry implemented all the machine's
registers, so the signedness of the PCs was just a by-product
of parsimonious hardware design. However, only the low-order
bits of the active PC participated in address generation; the
sign bits and high-order bits were simply ignored.

So far, just an amusing peculiarity. But the really odd
thing was that the operation of "increment the program counter"
was implemented as the arithmetic operation "add one to the
program counter" -- so if the PC contained a negative value,
the effect was that the program ran backwards! I got some
diversion out of dreaming up a code sequence that executed
in the usual fashion for a while and then negated the PC and
"backed out" by re-executing the preceding instructions in
reverse order ...


Huh?

0x8000 = -32768
0x8000 + 1 = 0x8001 = -32767
0x8001 + 1 = 0x8002 = -32766
....

Show me where adding 1 to a negative binary number will cause the low
order bits to "run backwards" compared to adding 1 to an unsigned binary
number.
Nov 14 '05 #23
Dan Pop

OP:> >> I tried to search con google with no success.

Osmium:> >You are searching with the wrong target word. The proper word is
emulate,
not simulate.

POP:> You're splitting hairs. Try googling for "8051 simulator".

If you think you will shock me by revealing that people sometimes use the
wrong word, you have failed miserably.

When using something such as google, where the basic commodity is the right
word spelled properly, it is best to use the right word if you know it. If
you consider that hair splitting, feel free to continue to think so.
Nov 14 '05 #24
jd*@smof.fiawol .org (John Cochran) wrote in message news:<cb******* ***@smof.fiawol .org>...
0x8000 = -32768
0x8000 + 1 = 0x8001 = -32767
0x8001 + 1 = 0x8002 = -32766
...

Show me where adding 1 to a negative binary number will cause the low
order bits to "run backwards" compared to adding 1 to an unsigned binary
number.


Repeat your demonstration using sign and magnitude representation in
place of two's complement.
Nov 14 '05 #25
On Wed, 23 Jun 2004 19:10:38 GMT, RoSsIaCrIiLoIA <n@esiste.ee> wrote:
for me it is difficult write a portable and *fast* x86 cpu in C
(it has to execute an OS)
I'm a beginner but I would 'solve' the problem in this way:

______________ _______________ ______
#include <stdio.h>
#include <stdint.h> /* or stddef don't remember for uintxx_t */

struct r16{
uint8_t rl; /* uintXX_t would be in the standard c c89 */
uint8_t rh; /* so it is portable: it is ok in every cpu */
}; /* but in the x86 cpu a register is good for
signed
and unsigned calculation */
struct r32{
struct r16 ac;
uint16_t sn;
};
/* all global */
struct r32 eax_={0}, ebx_={0}, ecx_={0}, edx_={0};

/* they are static so until I don't write ={0} they are ={0} ie all 0
at the start of prog */

struct r32 esi_, edi_, ebp_, esp_, eip_;
uint16_t cs_, ds_, es_, ss_, fs_, gs_, flags_;

struct r32 *eax= &eax_, *ebx= &ebx_, *ecx= &ecx_, *edx= &edx_;
struct r32 *esi= &esi_, *edi= &edi_, *ebp= &ebp_, *esp= &esp_,
*eip = &eip_;
uint16_t *cs=&cs_, *ds=&ds_, *es=&es_, *ss=&ss_, *fs=&fs_,
*gs=&gs_, *falgs=&flags_;


But I prefer this:

#include <stdio.h>
#include <stdint.h> /* or stddef don't remember for uintxx_t */

struct r32{
uint32_t e;
uint16_t x;
uint8_t h;
uint8_t l;
};
/* all global */
struct r32 eax_={0}, ebx_={0}, ecx_={0}, edx_={0};
struct r32 esi_, edi_, ebp_, esp_, eip_;
uint16_t cs_, ds_, es_, ss_, fs_, gs_, flags_;

struct r32 *eax= &eax_, *ebx= &ebx_, *ecx= &ecx_, *edx= &edx_;
struct r32 *esi= &esi_, *edi= &edi_, *ebp= &ebp_, *esp= &esp_, *eip
= &eip_;
uint16_t *cs=&cs_, *ds=&ds_, *es=&es_, *ss=&ss_, *fs=&fs_,
*gs=&gs_, *falgs=&flags_;
#define ax eax->x
#define al eax->l
#define ah eax->h

#define bx ebx->x
#define bl ebx->l
#define bh ebx->h

#define cx ecx->x
#define cl ecx->l
#define ch ecx->h

#define dx edx->x
#define dl edx->l
#define dh edx->h

#define sp esp->x
#define bp ebp->x
#define si esi->x
#define di edi->x
#define ip eip->x
#define U unsigned
#define P printf

void assign(struct r32* a, uint32_t b)
{uint8_t l, h;
/*--------------*/
a->e = b;
a->x = (b>>16) & 0xFFFF;
a->h = (b>>8) & 0xFF;
a->l = b & 0xFF;
}
void Pr(struct r32* a)
{printf("%x", (int) a->e);
fflush(stdout);
}

void somma(struct r32* a,struct r32* b)
{assign(a, a->e + b->e);}

int main(void)
{
assign( eax , 0xFEFEFEFE); assign( ebx , 0xFAFAFAFA);
P("eax="); Pr(eax); P(" ebx="); Pr(ebx); P("\n");
assign(ecx, 50000); assign(edx, 512341);
somma(ecx, edx);
P("ecx="); Pr(ecx); P(" edx="); Pr(edx); P("\n");
printf("somma=% x", (int)(50000 + 512341) );
return 0;
}
Nov 14 '05 #26
On Thu, 24 Jun 2004 07:28:14 GMT, RoSsIaCrIiLoIA <n@esiste.ee> wrote:
But I prefer this:

#include <stdio.h>
#include <stdint.h> /* or stddef don't remember for uintxx_t */

struct r32{
uint32_t e;
uint16_t x;
uint8_t h;
uint8_t l;
};
/* all global */
struct r32 eax_={0}, ebx_={0}, ecx_={0}, edx_={0};
struct r32 esi_, edi_, ebp_, esp_, eip_;
uint16_t cs_, ds_, es_, ss_, fs_, gs_, flags_;


better this:

#include <stdio.h>
#include <stdint.h> /* or stddef don't remember for uintxx_t */
/* all global */
uint32_t eax, ebx, ecx, edx;
uint32_t esi, edi, ebp, esp, eip;
uint16_t cs, ds, es, ss, fs, gs, flags;

union u{
uint32_t e;
uint16_t x;
uint8_t l;
};

#define ax (eax & 0xFFFF)
#define al (eax & 0xFF )
#define ah ( (eax >>8) & 0xFF )

#define bx (ebx & 0xFFFF)
#define bl (ebx & 0xFF )
#define bh ( (ebx >>8) & 0xFF )

#define cx (ecx & 0xFFFF)
#define cl (ecx & 0xFF )
#define ch ( (ecx >>8) & 0xFF )

#define dx (edx & 0xFFFF)
#define dl (edx & 0xFF )
#define dh ( (edx >>8) & 0xFF )

#define sp esp & 0xFFFF
#define bp ebp & 0xFFFF
#define si esi & 0xFFFF
#define di edi & 0xFFFF
#define ip eip & 0xFFFF
#define U unsigned
#define P printf

void inc_x(uint32_t* a)
{uint16_t r;
/*-------------*/
r = *a & 0xFFFF; ++r;
// P(" r=%x ", (int) r );
*a = *a & ((uint32_t) 0xFFFF0000 | r);
}

void inc_l(uint32_t* a)
{uint8_t r;
/*-------------*/
r = *a & 0xFF; ++r;
*a = *a & ((uint32_t)0xFF FFFF00 | r);
}

/* don't know if it is ok */
void inc_l1(uint32_t * a)
{++( (*(union u*)a).l );}

/* don't know if it is ok */
void inc_x1(uint32_t * a)
{++( (*(union u*)a).x );}

void inc_h(uint32_t* a)
{uint8_t r;
/*-------------*/
r = (*a>>8) & 0xFF; ++r;
*a = *a & ( 0xFFFF00FF | (uint32_t) r << 8 );
}

int main(void)
{
eax = 0xFEFEFEFE; ebx = 0xFAFAFAFA;
P("eax=0x%x ebx=Ox%x\n", (int) eax, (int) ebx);
ecx=50000; edx=512341;
ecx += edx;
printf("somma=0 x%x\n", (int)(50000 + 512341) );
eax=0xFFFFFFFF;
printf("eax=0x% x ", (int) eax );
inc_x(&eax); /* inc ax */
P("inc ax -> eax=0x%x ax=0x%x\n", (int) eax, (int) ax );

eax=0xFFFFFFFF;
printf("eax=0x% x ", (int) eax );
inc_x1(&eax); /* inc ax [& union] */
P("inc ax -> eax=0x%x ax=0x%x\n", (int) eax, (int) ax );

eax=0xFFFFFFFF;
printf("eax=0x% x ", (int) eax );
inc_l(&eax); /* inc al */
P("inc ax -> eax=0x%x al=0x%x\n", (int) eax, (int) al );

eax=0xFFFFFFFF;
printf("eax=0x% x ", (int) eax );
inc_h(&eax); /* inc ah */
P("inc ah -> eax=0x%x ah=0x%x\n", (int) eax, (int) ah );

return 0;
}

Nov 14 '05 #27
"Case -" <no@no.no> wrote in message
news:40******** **************@ dreader2.news.t iscali.nl...
Dan Pop wrote:
An usigned integer type would be a much better choice for the program
counter. Ditto for the other integer registers.
I didn't say what kind of int, so technically what you propose
is covered by my statement ;-)


;-)
Yes, you're right, the PC is best typed as unsigned. I'm not sure
about the registers. Values in registers are seen as 2-s complement
by instruction in at least some CPU's (e.g., MIPS has pairs of
similar instructions for singed and unsigned register operand).


It's straightforward to simulate a 2's complement CPU using unsigned
integers, with the semantics C guarantees. This is for the same reason that
2's complement is popular.

Alex
Nov 14 '05 #28
In <2j************ *@uni-berlin.de> "osmium" <r1********@com cast.net> writes:
Dan Pop

OP:> >> I tried to search con google with no success.

Osmium:> >You are searching with the wrong target word. The proper word is
emulate,
>not simulate.

POP:> You're splitting hairs. Try googling for "8051 simulator".

If you think you will shock me by revealing that people sometimes use the
wrong word, you have failed miserably.


Many of these people were vendors of such products. But, of course,
you know better than them...

If you were capable to conceive the notion that you might be wrong, the
google exercise would have been quite useful to you.

Contrary to your narrow ideas, there are many levels of software
CPU simulation, serving different purposes, and none of them is less
entitled to the name "simulation " than the others:

- sub-transistor level simulation: used for checking the correctness of
the chip design.

- transistor level simulation: used for checking the correctness of the
CPU implementation.

- gate level simulation: used for checking the correctness of the CPU
design.

- register to register transfer level simulation: used to evaluate the
CPU design performance.

- functional simulation, kernel mode: used for developing operating
system kernels and freestanding applications.

- functional simulation, user mode: used for developing hosted
applications (much faster than kernel mode simulation, as the OS kernel
functionality is provided by the simulator, instead of having the
simulated processor execute kernel code).

The last two simulation levels are *also* called emulation. In the case
of some very popular embedded control CPUs, hardware emulation is often
used for software development purposes instead of the real processor,
because it has much better debugging capabilities.

The Linux to Itanium port was done using "ski", a functional simulator
developed by HP and gcc as a cross-compiler on x86 hardware.
The kernel people used ski in kernel mode, the glibc people used it in
user mode. Everything was ready by the time Intel produced the first
silicon prototypes of Itanium. I don't remember anyone calling ski
"emulator", although it wouldn't have been technically incorrect.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #29
Eric Sosman wrote:
Case - wrote:
Dan Pop wrote:
In <40************ ***********@new s.xs4all.nl> Case <no@no.no> writes:
The key idea is to map all parts of a CPU on C structures
and routines. For example: the program counter (PC) can
be simply mapped to an int variable.


An usigned integer type would be a much better choice for the program
counter. Ditto for the other integer registers.


I didn't say what kind of int, so technically what you propose
is covered by my statement ;-)

Yes, you're right, the PC is best typed as unsigned. I'm not sure
about the registers. Values in registers are seen as 2-s complement
by instruction in at least some CPU's (e.g., MIPS has pairs of
similar instructions for singed and unsigned register operand).

Note that you must issue the occasional no-op when
using instructions of the first type, to give the singed
registers time to cool.


Is this some kind of joke?

Case

Nov 14 '05 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2310
by: Alex Vinokur | last post by:
C++ Simulator of a Universal Turing Machine can be downloaded at : * http://alexvn.freeservers.com/s1/utm.html * http://sourceforge.net/projects/turing-machine/ The program simulates a Universal Turing Machine (UTM). The UTM used in the Simulator is three-tape Turing Machine: * Tape#0 contains transition table and initial instantaneous description of a Particular Turing Machine (TM);
9
2634
by: Milk | last post by:
Hi all, Can anyone help me to do this Question. Coz this is my first time study C++ language and my lecture want me to do this kind of program, i really don't have any ideal pls help me here is the Question:: Improve the following (badly written) matrix multiplicationprogram and translate it into MIPs assembly language then assemble into machine language (hexadecimal representation). You may use a compiler to produce an assembly
26
5583
by: Wouter van Teijlingen | last post by:
Dear Readers, This is my first post to this group. I was pointed to this group in a other vb group, so i have better luck here! For my learning curve of VB .NET i want to make a traffic simulation program. Before i start programming, i need to know if there even is a possibility to make a algorithm in VB .NET that is able to let traffic drive and making the lights go green, orange or red.
0
1606
by: SatishPasala | last post by:
Hi I am developing a Windows Mobile application. I want Visual Studio to use Palm Simulator to display the application. Now it is pointing to Default Simulator. I downloaded the Palm Simulator. I need to add it to Visual Studio. Can some one help me to add the simulator?
0
1408
by: François | last post by:
Hi, My current job is to develop VoiceXML dialogs. The voice platform that we use provides neither a debugger nor a simulator, and each time I want to test a new dialog, I have to upload it on the remote production server, and to call the voice platform. So I would like to know where I can find a freeware VoiceXML simulator able to run a VoiceXML dialog. As my dialogs only use DTMF as an input, and text-to-speech synthesis for the...
1
4482
by: Ciko | last post by:
Was wondering how I could write a simple simulator (assembler CPU Z80). Thanks for any advice, link.
0
4080
by: Killingkids | last post by:
hello, everyone...im facing a big problem in my final year project, hope that u all can help me solve the problem ... i was doing a mobile web application which enable student to check the college information through mobile, however i just use simulator to test my system... one of modules or function was enable the student to downloads some notes which in txt file format through mobile.Hence, i tested using IE,it does not appear any problem, i...
0
1707
by: Killingkids | last post by:
hello, everyone...im facing a big problem in my final year project, hope that u all can help me solve the problem ... i was doing a mobile web application which enable student to check the college information through mobile, however i just use simulator to test my system... one of modules or function was enable the student to downloads some notes which in txt file format through mobile.Hence, i tested using IE,it does not appear any problem, i...
3
4743
by: DanielJohnson | last post by:
I was wondering if anyblody can suggest me a network simulator written in python in which I can add on my own code and extend its functionality. I am looking for a simulator which will simualte TCP, UDP, RTP and most networking protocol. The learning curve for ns2 and other simulator is too high for me at this time and thats why I am considering Python for it. Every help will be appreciated.
0
9455
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9271
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9869
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7242
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5140
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.