Jump to content

CPU in C++

topblast

I was looking at browsing the web and I saw one of those computers on minecraft. 
http://www.minecraftforum.net/topic/1296399-the-redgame-3-redstone-computer-now-finnished-download-now/

On my journey to be a know it all at computers, i tried to think of a simple way to make a simple one of these in c++. My head still hurting. Can someone explain to me how I can create a CPU in c++, say have a special assembly language or something.

Link to comment
Share on other sites

Link to post
Share on other sites

First of all i'm sorry if i miss-understood you question, but you want to code a CPU?

I'm no engineer or something but a CPU is an Hardware part that works like a super fast calculator in your system, well I know there are physical cores and logical cores but ive never heard anyone who coded a CPU from scratch. 

That Minecraft link you had in you post was a guy who made a CPU whit "virtual hardware", so I would say you need to build your own CPU somehow and then code you computer to use the CPU or i dont know?

Well I'm no expert on the subject so I'm sorry that i cant help you.

As conclusion good luck coding a CPU if it is possible! 

CPU: Intel I5 3550 Motherboard: Maximus V Gene RAM: 8Gb Kingston HyperX Genesis GPU: Zotac GTX 670 Case: NZXT Vulcan Storage: 120gb Kintson SSD + 1TB WB blue PSU: Corsair CX600 Display(s): Asus 24" VE247H Full HD Cooling: Coolermaster V8 and Casefans Keyboard: Steelseries 6G V/2 Mouse: Razer Naga Molten Sound: Onboard + Astro a40

Link to comment
Share on other sites

Link to post
Share on other sites

Notch has done something similar with his new game where he emulates an old 16-bit CPU. As it's essentially a collection of logical operators it shouldn't be too hard to form, but again you'll need a good knowledge of the operators and the cpu architecture you wish to implement as they all differ.

Link to comment
Share on other sites

Link to post
Share on other sites

i understand that, i want to emulator of a CPU is what i should of said.

Link to comment
Share on other sites

Link to post
Share on other sites

If you want to write your own CPU emulator you must be familial with Assembly, or if you want to "invent your CPU" and write an emulator for it you should start by writing your own Assembly.

Just C++ won't be enough, you still need to learn a bit of Assembly to have an idea of what is Assembly.

My dream in life is to live of quality internet fiber and junk food.

Link to comment
Share on other sites

Link to post
Share on other sites

You need to have very good knowledge about Virtual Execution Environments, Operating Systems and Computer Architectures (at least, and without counting programming skills in various languages).

 

Basically, what you want to do is an emulator. The complexity of that emulator depends on the architecture of the CPU you are trying to emulate. You should first understand the requirements and the way the architecture works and only then can you have a clear approach on solving the problem. For instance there are Atari emulators built completely in Java whereas a System VM capable of running x86 OS's would need an interface with the host OS (probably in C) and even some assembly for directly accessing the CPU.

 

And after having the CPU emulated, if you really want to use it, you would need to emulate other parts of the system such as memory, input and output, etc. Basically you will need to, at least, emulate memory (and this may require from a simple byte array in your code to a complex set of structures that emulate the behavior of page tables with hardware page table walkers and a TLB), registers (including flags; this may be as simple as a few variables in your code to as complex as a memory allocated structure that is protected from the guest OS's reads and writes for instance) and the CPU itself (this usually is code - a simple switch case if you only have a few instructions - but may require assembly programming if you want to do stuff like trap-and-emulate or binary translation and may require that some code is written as a device driver in order to directly access the underlying CPU).

 

If you never heard about most of the things in the previous paragraph you are probably better off starting with analyzing a simple emulator like an Atari emulator (in C++ or Java). Maybe then you could create a simple CPU instruction set (with a couple of opcodes, for instance the byte 0x00 would be a NOP, 0x01 would be an ADD, 0x02 a STORE, ..., and instructions, for instance an ADD would have opcode 0x00 followed by the address of a memory position and then constant) and emulate the system by going through the instructions and doing certain things depending on the opcode (if it is an ADD you have to go to memory, get the value on that position, add the value with the constant and store the result in the same memory address for example). Notice, though, that there are a lot of instructions needed to perform even some basic things and that creating an emulator for even the most simple made up language may take some time.

 

But in any case learning and making emulators requires knowledge on low level aspects of computing like memory addressing, CPU architectures (ALU, control, how fetch-and execute cycles work ...), input and output, etc. And after knowing how a normal computer operates you have to take those concepts even further in order to tackle the problems of having one computer running on top of another one.

Link to comment
Share on other sites

Link to post
Share on other sites

I had a course at university where we implemented the LC-3 on an FPGA. It was pretty basic, but in the end we could compile some small C programs into binary code that it could understand.

 

http://en.wikipedia.org/wiki/LC-3

Link to comment
Share on other sites

Link to post
Share on other sites

First of all, you have to pick a CPU to emulate.  There is Intel CPU which is based on x86 architecture, and there is CPU used in an iPhone that is based on ARM architecture.  Then you have to study each instruction set, and then implement each of them in your preferred programming language.

 

Give you an example.  Someone wrote a x86 CPU emulator and hard disk emulator in javascript.  It is running a working Linux operating system in your web browser.

 

http://bellard.org/jslinux/

Link to comment
Share on other sites

Link to post
Share on other sites

To read into the question it sounds more to me like to want to try designing a CPU more so than emulating one.

 

If that's the case than there is a whole slew of topics you would have to learn before starting and even then it would be a hefty undertaking, I haven't tried myself yet.

 

First you would need to get an understanding of logic gates and their operations, and, or, nand, nor, and xor. Then you would have to learn Boolean algebra and how to work with it to create the circuits you need. You then would find out how to build the various components of the computer, half-adder, then full-adder, various flip-flops to create memory, then registers/accumulator, Multiplexers and the like to send/recieve data across a bus, and then you need to write circuits to work with the opcodes/binary that makes up the assembly code and knows where in the computer to send the commands.

 

There are lots of very big words that take a bit of studying to fully understand. And there are things I didn't mention above. I would encourage to tackle it and I can site some resources, books, and tools to help out.

 

But I think I would agree with others that an emulator would be much easier to write, though that is not quiet what the minecraft computer builders are doing.

My rig: 2600k(4.2 GHz) w/ Cooler Master hyper 212+, Gigabyte Z68-UD3H-B3, Powercolor 7870 xt(1100/1500) w/AIO mod,

8GB DDR3 1600, 120GB Kingston HyperX 3K SSD, 1TB Seagate, Antec earthwatts 430, NZXT H2

Verified max overclock, just for kicks: http://valid.canardpc.com/show_oc.php?id=2609399

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×