Simulation of a CPU

Good Friends
In college I was left a project of the simulation of a CPU, it has to be basic and carry various functions, consult thought that you can lead me to develop it. These functions would be:

Fetch cycle of a CPU for example to show fetch cycle can follow a sequence similar to that seen with paper_processor classes, which was carrying instructions memory cells steps indicated instruction he executed, as increasing 1 a value in a register and repeat these steps to generate a stop condition in this cycle.

An addition operation of 8-digit binary numbers form positive (0 to +255) from two registers to a third register, in case of exceeding the sum should switch on the corresponding flag. The user can enter the values ​​you want to add.

Operations and records landslides rotations, select 2 of all possible operating in conjunction with the CF flag

Show the complement operation 2 a 8-bit register. The user can enter the binary value to support, using the leftmost bit as the value of sign, 0 for positive and 1 for negative. They must show the 2 steps: a. invest digits, b. add 1 to the inverted sequence. If an overflow occurs should turn on the corresponding flag.

Here the operations to be able to show step by step the machine and must be executed by the same graphical or visible to the user are defined. For example, using sequences of lights to display binary data in a record (on = 1, off = 0), engines, parts animations provided by tools, LCDs, LED matrices, 7-segment LED, etc.

The CPU will work to develop a minimum size of 8 bits in their operations.
It must have at least the basic records: PC, IR, 4 general registers as A, B, C and D, basic flags as ZF, SF, CF and OF, all minimum size of 8 bits as indicated and flags a clear bit each. You can include other items for your CPU if required for the instructions you want to deploy. It is expected that the clock rate is slow, 1 Hz (1 cycle = 1 second) or slower, so that the user can observe the step by step or operation, the clock would be manually, each user manual key (a button for example) will be the next step. The machine can operate in ways it is not necessary that all operations are visible at once, enough to show an operation at a time, you must decide how to do it and how the user will move from one operation to another, etc.

The user must indicate which instruction or operation is being displayed at all times and should indicate when you start and when it ends for the user to understand the order of the sequence of steps.

Thank you.

Wouldn't it be much easier to do that in a program on a PC - especially if you need visual output. After all, the whole point of a simulation is that it is not real.

An Arduino will be fully occupied doing its own work without also being asked to explain iteself as it goes along.


There's lots of "Breadboard CPU" projects out there. Just google it.

An Arduino may be involved as part of the input or output but using a CPU to 'simulate' a CPU is probably not what you want to see.

Thank you friends for replying
Then you could do to develop ?, you recommend?

I think the Arduino in not the right platform for your project.

Simulations in combination with graphical outputs require some performance.

The new Raspberry Pi gives you 4 cores running at much higher frequencies than the Arduino and you have a lot more memory for your code.

I prefer the Arduino when it comes to controlling digital and analog hardware.

If all you need is a grafical output, Arduino is not an optimal solution.

The job always defines the tools you need to get the job done.

Digame, donde vives - en cual pais?

I am from Guatemala
And how could elaborate on that tool you say? As performed the functions I need?

Saludos de Alemania

First of all we need a LOT more information of what you are trying to do.

This is just a guess or an idea what you might want to do:

Your input is two integer numbers and you want to do an addition.
On the screen you want to show how the CPU registers are loaded and
how the result is stored in another register.
And all of this has to be visualized.

OK, this was just an idea of what your project might look like.


Fui en America Central hace muchos anos. Fui tambien en Panajachel al lago Atitlan.
Fui en Guatemala por dos semanas, solo yo y mi mochila.
Me gustaba mucho.

I like speaking spanish, however, my spanish is not good enough to have a technical discussion.

Your english is more than sufficient to tell us what you want.

It will not be the words and grammar - it will be the structure of your thoughts that
will help us to understand your problem a litte bit better.

See this:

It's basically an on-paper simulation of a 2-bit CPU; it implements a very small set of instructions, and contains a total of three registers - a program counter (PC), a general purpose register, and a status register (for overflow).

If you can work this and understand it on paper - then you'll be well on your way to understanding how a larger CPU works - at least at the register and memory level (basically an "instruction set simulator" - see Instruction set simulator - Wikipedia).

You might also be interested in this modern day implementation of a "mechanical computer":

Which was inspired by this bit of educational kit from the 1960s:

...whose predecessor was this other kit:

An earlier simulated education "computer" was the Geniac:

Note - on these three machines from the 1950s and 60s - don't even bother trying to acquire one, unless you plan on being very patient, and spending more than a bit of money on vintage collectible computer stuff. Seriously, these one-time toys are very difficult and expensive to obtain today - you sometimes (rarely) see them go on Ebay - and when they do, they fetch quite a top dollar.

That said - they do show how it is possible to implement a computer using simple means. Do your research - look up "cpu simulation" and "cpu simulators" - you'll find tons of papers and other information about such. Keep in mind that you'll want to likely limit the scope of things to 8 bit machines and under, and likely at the beginning only implement 2 and 4 bit machines - at least as "instruction set simulations" (which are typically more than good enough to simulate a CPU - in fact, if you peel back the code underneath most classic computer emulators - you'll find at heart they are really nothing more than "instruction set simulations", with their "memory" being a very large single dimension array, or some other similar memory structure).

Once you understand how to create and operate on paper a simple CPU - see if you can expand it. Add extra registers, more instructions, maybe more memory (though note that as your address space increases, so will the amount of work you'll have to do by hand to make it "compute"). You'll hit a hard and fast limit of what you can really do by hand at around the 5-6 bit mark; you can do 7 bits if you are very patient.

At that point, though - you should have enough knowledge to begin implementing that CPU in actual code; then it become easy to expand.

At a certain point - you'll begin to wonder how all of this translates to actual circuitry. I can't explain it to you here (I don't have the space or time) - but just realize that those binary digits are represented by electrical signals. You can think of them as +V for HIGH (1) and 0V for LOW (0). There is something called a clock - it is a circuit that generates a series of high (1) and low (0) pulses. You have things called "flip-flops" - which can form memory (aka - registers) - they can also act as counters if hooked up right; they usually count in binary - so you feed a pulse in, you get binary 0001 - another pulse, you get 0010, another and you get 0011 - etc. Again - these represent electrical values. That counter is your "program counter" - and the number it spits out can represent an address in memory...

Technically - even Babbages Analytical Engine worked in the same manner (had it been realized) - just mechanically, and using base-10 instead of base-2.

Ok - even at this basic level I would go on for pages - but honestly, CPUs in their basic form are not really that complicated to understand. Seriously - you'll go "duh" when it clicks in your brain. One thing that I realized when it clicked for me was that a CPU was just a really fast player piano - RAM was the piano roll - the address was where this "piano" was on the roll, and the information for processing was the holes in the roll. Only this "roll" could be changed on the fly, and it was possible to "back up" the roll (branch) based on the state of a register or other value. Indeed, a player piano is a very apt concept - if you don't believe me, do some research on "reproducer pianos" - they were probably the closest thing to a CPU that a player piano came to.

I seriously could bore you to tears here - but I won't. I hope my links and information help you (or anyone else) in some manner to understand how a CPU works.

Good luck. :slight_smile:

cr0sh, the first link, with the paper cpu.
Waayyyyyy bck when in Uni and had first micro classes (Z80), our lecturer (well versed in how to teach students) did a similar thing, but he had a large board made up with hooks and you placed plastic or cardboard labels on them.
Areas representing address and data registers and flags.
You would write a simple pseudo code then step it out on the board.
Only had one class session with it then onto the real electronic thing.
But it stayed in the room for each class to help any programming probs.
It must have saved him many many hours of instruction, and help sooo many of us with visualization of a somewhat new area of engineering.

Tom.... :slight_smile:
ex Z80, VIC20, Commodore64, user..

Anyhow to get back on track.

Your I/O device to the real world is probably a big concern, you could use a PC running Processing to purely scan registers etc that you construct in the arduino controller. (like SCADA does in the industrial world)
A button to let you control the clock of your virtual CPU, eg. step, run(slow) so you can see your program running.

Tom.... :slight_smile:
An interesting project, I like projects like this where they are producing an educational resource about the basics of electronics and computing.

Thanks to all

Change the description of the more explanatory as possible

Any help will be welcome

Thanks for the very interesting information links for the project

like me you are REALLY trying to help people.

a lot of persons in this forum are not able to write more than one or two sentences.

Often times it is easier to read just one or two sentences - if they are the right sentences.


Often times it is easier to read just one or two sentences - if they are the right sentences.


TL,DR. :smiley:



Full marks for brevity :slight_smile:

But, assuming you have read all the advice that has already been given, I have no idea what you want help with.


What kind of cpu/processor adds one register to another and puts the results in a third register?

Just about all of them? That's a pretty basic function.

What kind of cpu/processor adds one register to another and puts the results in a third register?

Three address opcodes are typical for RISC architectures (ARM...).

Just about all of them? That’s a pretty basic function.

Not to hijack the thread, but… could you give me a reference to one?
Everything I’ve ever worked with would add ‘B’ to ‘A’ with the results being in ‘A’.

As in: ADD Rd,Rs <=== Rd<–Rd+Rs

Yes, in Assembler language you have commands that give you back the
the result of an addition of register A and register B in either register A or register B.

The core of the microprocesser needs a THIRD register to do the calculation and send the result to the desired register.

The guy has to simulate the workings of a CPU and not assembler code.

So two registers will not be enough in his simulation.

Microprocessors use hidden registers that you will never ever see in any documentation, because you cannot access them.

For a simulation these registers are relavant.