473,836 Members | 1,582 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 16316
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.


I've worked on a MIPS assembler for a short while, and know
all about NOPs in delay slots; but I can't remember to have
seen any code in that assembler generating cooling NOPs.

So, is this somekind of joke, or a cool feature? :-)

Case

Nov 14 '05 #31
In <40************ **********@drea der2.news.tisca li.nl> Case - <no@no.no> writes:
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 ;-)


Nope, in C int is a synonym for signed int:

2 At least one type specifier shall be given in the declaration
specifiers in each declaration, and in the specifier-qualifier
list in each struct declaration and type name. Each list of
type specifiers shall be one of the following sets (delimited
by commas, when there is more than one set on a line); the type
specifiers may occur in any order, possibly intermixed with the
other declaration specifiers.
....
- int, signed, or signed int
....

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #32
In <40************ **********@drea der2.news.tisca li.nl> Case - <no@no.no> writes:
Yes, you're right, the PC is best typed as unsigned. I'm not sure
about the registers.
I am. Think hard about the properties of two's complement arithmetic and
the semantics of unsigned arithmetic in C.
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).


That's why you want unsigned, which nicely simulates two's complement
behaviour.

CISC CPUs using two's complement have only one ADD instruction and set
their flags according to the result (if the Carry flag is set, unsigned
overflow occured, if the Overflow flag is set, signed overflow occured).
If no flag is set, the result is correct if interpreted as both signed
and unsigned. Implementing this in C, using unsigned arithmetic, is left
as an exercise for the reader.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #33
Case wrote:
Eric Sosman wrote:
Case - wrote:
Dan Pop wrote:
.... snip ...
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.


I've worked on a MIPS assembler for a short while, and know
all about NOPs in delay slots; but I can't remember to have
seen any code in that assembler generating cooling NOPs.

So, is this somekind of joke, or a cool feature? :-)


It's a cool feature, else those singed registers are liable to
char, which would make them incapable of holding anything
passively unsinged.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #34
Dan Pop wrote:
.... snip ...
Many of these people were vendors of such products. But, of
course, you know better than them...

^^^^
Aha! Finally caught in an English inaccuracy. They. :-)

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #35
John Cochran wrote:
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.


It seems I neglected to mention that the machine used
signed magnitude representation for negative integers:

-32768 = 0x800000008000
-32768 + 1 = -32767 = 0x800000007FFF
-32767 + 1 = -32766 = 0x800000007FFE
...

Sorry for the omission. Honeywell 8200, IIRC, and I think
the year was 1968.

--
Er*********@sun .com

Nov 14 '05 #36
In <40************ ***@yahoo.com> CBFalconer <cb********@yah oo.com> writes:
Dan Pop wrote:

... snip ...

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

^^^^
Aha! Finally caught in an English inaccuracy. They. :-)


If this is my first English inaccuracy you've caught in one of my posts,
you must be *really* reading them with your brain firmly set into Neutral.

Given that I've never even tried to learn the proper English grammar and
that I'm posting quite a lot, there *must* be several English mistakes in
my daily output... (without even mentioning typos and omitted words)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #37
On 23 Jun 2004 19:57:26 GMT, (Gordon Burditt) wrote:
This is not a Unix item but strictly a PC item as follows:

union REGS {
struct WORDREGS x;
struct BYTEREGS h;
};

struct WORDREGS {
unsigned int ax, bx, cx, dx;
unsigned int si, di, cflag, flags;
};

struct BYTEREGS {
unsigned char al, ah, bl, bh;
unsigned char cl, ch, dl, dh;
};

These structures allow C programs on PCs to access the CPU registers.

AX,BX,CX,DX,S I,DI,CF(carry flag),and FLAGS are all Intel 80x86
registers
AL/AH,BL/BH,CL/CH,DL/DH are pseudo 8 bit registers which are derived
from the
AL = Low half of AX, AH = High half of AX, etc.


I have to really wonder about this design for a CPU emulator. Some
of the registers listed above share storage (for example, ax consists
of ah and al concatenated, and eax contains ax). Every time you
change one register (e.g. eax, ax, al, or ah), you have to change
all of them, or keep track of which one is more up to date. This
tends to make things slow. and bug-prone, if you're not careful.
Assembly code WILL occasionally make use of this fact, for example,
loading ah with 0 to do an unsigned-extension of al into ax may be
the fastest way to accomplish this.


The "union r32" in my PC seems like a x86 general pourpose register
Are you agree?

#include <stdio.h>
#include <stdint.h> /* or stddef don't remember for uintxx_t */
union r32
{
union
{
struct
{
uint8_t l;
uint8_t h;
}b;
uint16_t val;
}w;
uint32_t val;
};
/* all global */
union r32 eax_, ebx_, ecx_, edx_;
union r32 esi_, edi_, ebp_, esp_, eip_;
uint16_t cs, ds, es, ss, fs, gs, flags;

#define eax (eax_.val)
#define ax (eax_.w.val)
#define al (eax_.w.b.l)
#define ah (eax_.w.b.h)

#define ebx (ebx_.val)
#define bx (ebx_.w.val)
#define bl (ebx_.w.b.l)
#define bh (ebx_.w.b.h)

#define ecx (ecx_.val)
#define cx (ecx_.w.val)
#define cl (ecx_.w.b.l)
#define ch (ecx_.w.b.h)

#define edx (edx_.val)
#define dx (edx_.w.val)
#define dl (edx_.w.b.l)
#define dh (edx_.w.b.h)

#define sp (esp_.w)
#define bp (ebp_.w)
#define si (esi_.w)
#define di (edi_.w)
#define ip (eip_.w)
#define U unsigned
#define P printf
void inc_w(union r32* a)
{++ a->w.val;}

void inc_l(union r32* a)
{++ a->w.b.l;}

void inc_h(union r32* a)
{++ a->w.b.h;}
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 ecx=0x%x\n", (int)(50000 + 512341), (int) ecx );
eax=0xFFFFFFFF;
printf("eax=0x% x ", (int) eax );
inc_w(&eax); /* inc ax ; or I would have to write inc_x(&eax_) ? */
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;
}

/*
eax=0xfefefefe ebx=Oxfafafafa
somma=0x894a5 ecx=0x894a5
eax=0xffffffff inc ax -> eax=0xffff0000 ax=0x0
eax=0xffffffff inc ax -> eax=0xffffff00 al=0x0
eax=0xffffffff inc ah -> eax=0xffff00ff ah=0x0
*/

Nov 14 '05 #38
On Thu, 24 Jun 2004 09:31:24 GMT, RoSsIaCrIiLoIA <n@esiste.ee> wrote:

This would be portable.

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

union r32{
uint8_t l;
uint16_t x;
uint32_t val;
};
/* all global */
union r32 eax_, ebx_, ecx_, edx_;
union r32 esi_, edi_, ebp_, esp_, eip_;
uint16_t cs, ds, es, ss, fs, gs, flags;

#define eax (eax_.val)
#define ax (eax_.x)
#define al (eax_.l)
#define ah ( (eax_.x>>8) & 0xFF ) /* not lvalue :-( */

#define ebx (ebx_.val)
#define bx (ebx_.x)
#define bl (ebx_.l)
#define bh ( (ebx_.x>>8) & 0xFF ) /* not lvalue */

#define ecx (ecx_.val)
#define cx (ecx_.x)
#define cl (ecx_.l)
#define ch ( (ecx_.x>>8) & 0xFF ) /* not lvalue */
#define edx (edx_.val)
#define dx (edx_.x)
#define dl (edx_.l)
#define dh ( (edx_.x>>8) & 0xFF ) /* not lvalue */

#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 inc_x(union r32* a)
{++ a->x;}

void inc_l(union r32* a)
{++ a->l;}

void inc_h(union r32* a)
{uint8_t r;
/*-------------*/
r = ( a->x >>8) & 0xFF; ++r;
a->val = a->val & ( 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 ecx=0x%x\n", (int)(50000 + 512341), (int) ecx );
eax=0xFFFFFFFF;
printf("eax=0x% x ", (int) eax );
inc_x(&eax); /* inc ax ; or I would have to write inc_x(&eax_) ? */
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 #39
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.


You are talking about signed registers. I was talking about the
way a CPU may 'see' the value contained in a register. And we
are talking in the context of mapping CPU to C. Evidently,
reading other posts, you have much much more experience in CPU
design issues. I don't get your point, could you please elaborate?

Case

Nov 14 '05 #40

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

Similar topics

0
2312
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
2638
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
5591
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
1609
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
4489
by: Ciko | last post by:
Was wondering how I could write a simple simulator (assembler CPU Z80). Thanks for any advice, link.
0
4082
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
4745
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
9668
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
10843
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10546
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
10589
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
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9371
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...
0
6978
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();...
1
4448
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4015
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.