CPU

  • Tutorial
As far as I can remember, I always dreamed of making a processor. Finally, yesterday I made it. God knows what: 8 bits, RISC, the current operating frequency is 4 kHz, but it works. So far in the program of modeling logical circuits, but we all know: "today - on the model, tomorrow - in practice!".

Under the cat are a few animations, a brief introduction to binary logic for the smallest, a short story about the main processor logic chips and, in fact, the circuit.


Binary logic


Binary number system (for those who are not in the know) is a number system in which there are no digits greater than one. This definition of many is confusing until they remember that in the decimal number system there are no digits greater than nine.
The binary system is used in computers because the numbers in it are easy to encode by voltage: if there is voltage, it means unity; no voltage means zero. In addition, “zero” and “one” can easily be understood as “false” and “true”. Moreover, the majority of devices operating in the binary system of numeration usually refer to numbers as an array of “truths” and “falsities”, that is, they operate with numbers as logical quantities. For the smallest and those who are not in the know, I will tell and show how the simplest elements of binary logic work.

Buffer Element


Imagine that you are sitting in your room, and your friend is in the kitchen. You shout to him: “Friend, tell me, is the light on in the corridor?” A friend replies: “Yes, it burns!” or "No, it’s off." Your friend is a buffer between the signal source (a light in the hallway) and the receiver (you). Moreover, your friend is not a regular buffer, but a managed buffer. It would be an ordinary buffer if it constantly shouted: “The light is on” or “The light is not on”.

The element "NOT" - NOT


Now imagine that your friend is a joker who always tells lies. And if the light in the corridor is lit, then he will tell you “No, it is very, very dark in the corridor”, and if it is not lit, then “Yes, the light is on in the corridor”. If you really have such a friend, it means that he is the embodiment of the “Not” element.

Or Element - OR


Unfortunately, one bulb and one friend will not be enough to explain the essence of the “Or” element. Need two bulbs. So, you have two lights in the corridor - a floor lamp, for example, and a chandelier. You shout: “Friend, tell me, does at least one light in the corridor shine?”, And your friend answers “Yes” or “No”. Obviously, for the answer “No” all the lights must be turned off.

The And Element - AND


The same apartment, you, a friend in the kitchen, a floor lamp and a chandelier in the hallway. To your question, “Are both lights on in the corridor?” you get a yes or no answer. Congratulations, now your friend is an AND element.

Exclusive Or Element - XOR


Let us repeat the experiment again for the “Or” element, but we reformulate our question to a friend: “Friend, tell me, is there only one light in the hallway?”. An honest friend will answer this question “Yes” only if there is really only one light in the corridor.

Adders


Quarter total


The quarter-adder is called the Exclusive Or element. Why? Let's figure it out.
Let's make up the addition table for two numbers in the binary number system:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 10

Now we write down the truth table of the “Exclusive Or” element. To do this, we denote the luminous bulb as 1, extinct - as 0, and the friend's answers “Yes” / “No” as 1 and 0, respectively.
0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0

Very similar, isn't it? The addition table and the truth table of the “Exclusive Or” coincide completely, except for a single case. And this case is called Overflow.

Half adder

When overflowing, the result of addition is no longer placed in the same number of digits as the terms were placed. The terms are two single-digit numbers (one significant digit, understand?), And the sum is already double-digit (two significant digits). Two digits with one light (“On” / “Off”) cannot be transmitted anymore. Need two bulbs. It’s necessary - we’ll do it!


In addition to XOR, for the adder we need the element “AND” (AND).
0 XOR 0 = 0 0 AND 0 = 0
0 XOR 1 = 1 0 AND 1 = 0
1 XOR 0 = 1 1 AND 0 = 0
1 XOR 1 = 0 1 AND 1 = 1

Tadam!
0 + 0 = 00
0 + 1 = 01
1 + 0 = 01
1 + 1 = 10

Our wunderwafflehalf adder is working. It can be considered the simplest specialized processor that adds two numbers. A half-adder is called a half-adder because it cannot be used to take into account the transfer (the result of another adder), that is, you cannot add three single-digit binary numbers. In this regard, one multi-bit cannot be made from several single-bit half-adders.

I will not go into details of the work of full and multi-digit adders, I just hope that you have caught the main idea.

More complex elements


Multiplexer


I propose to turn on the imagination again. So imagine. You live in a private single-family house, near the door of this house is your mailbox. Going for a walk, you notice a strange postman who is standing near this mailbox. And here's what he does: he pulls out a bunch of letters from his bag, reads the number on the mailbox, and depending on the number on the mailbox he throws a letter in it. The postman works as a multiplexer. It in a certain way (the number on the envelope) determines which signal (letter) to send along the signal line (mailbox).

Multiplexers usually consist only of combinations of the elements "And", "Or" and "Not". A single-bit multiplexer has one input called “address selection”, two inputs with the general name “input signal” and one output, which is called “output signal”.
When 0 is applied to the “address selection”, the “output signal” becomes the same as the first “input signal”. Accordingly, when 1 is applied to the “selection”, the “output signal” becomes equal to the second “input signal”.

Demultiplexer


But this thing works exactly the opposite. We give the address to the “address selection”, we give the data to the “data input”, we have the input data at the output with the address number.

Counter


To understand how the meter works, you will need your friend again. Call him from the kitchen (I hope he didn’t get bored there, and, most importantly, didn’t eat all your food), and ask him to do this: let him remember the number 0. Each time you touch him, he must add one to the number that remembers, say the result and remember it. When the result is (say) 3, he should shout “Abracadabra!” and answer at the next touch that he now remembers the number 0. Is it a little complicated? Look:

You are touching a friend. A friend says "One."
You are touching a friend. A friend says "Two."
You are touching a friend. A friend says "Three." A friend shouts out “ Habrahabr! ". Critical attack! You are temporarily paralyzed and cannot move.
You are touching a friend. A friend says "Zero."

Well, and so on. Very simple, right?
You, of course, understood that your friend is now a counter. Touching a friend can be considered a “timing signal” or, quite simply, a signal to continue counting. The cry “Abracadabra” indicates that the stored value in the counter is maximum, and that with the next clock signal the counter will be set to zero. There are two differences between a binary counter and your friend. First: a real binary counter produces a stored value in binary form. Second: he always does only what you tell him, and never falls for stupid jokes that can disrupt the operation of the entire processor system.

Memory


Trigger


Let's continue to mock your unhappy (perhaps even imaginary) friend. Now let him remember the number zero. When you touch his left hand, he should memorize the number zero, and when his right hand - the number one. When asked, "What number do you remember?" a friend should always answer the number that he remembered - zero or one.
The simplest memory cell is an RS trigger (“trigger” means “switch”). The RS-trigger can store one bit of data (“zero” / “one”), and has two inputs. The Set input (just like your friend’s left hand) writes to the “one” trigger, and the Reset / Reset input (respectively, the right hand) - “zero”.

Register


The register is a little more complicated. Your friend turns into a register when you ask him to remember something, and then say “Hey, remind me what I told you to remember?”, And the friend answers correctly.

A register can usually store a little more than one bit. It must have a data input, data output and write permission input. From the output of the data, you can at any time read what is written in this register. At the data input you can submit the data that you want to write to this register. You can submit data until you get bored. Anyway, nothing will be written to the register until you enter one, that is, a “logical unit”, at the input of the write permission.

Shift register


Have you ever stood in lines? Surely stood. So you imagine what it would be like to be data in a shift register. People come and stand at the end of the line. The first person in line enters the office for a big shot. The one who was second in line becomes the first, and the one who was third is now second, and so on. A queue is such a tricky shift register from which “data” (well, that is, people) can run away on business, previously warning neighbors in turn. In a real shift register, of course, “data” cannot escape from the queue.

So, the shift register has a data input (through which data falls into the "queue") and data output (from which you can read the very first record in the "queue"). The shift register also has an input “shift register”. As soon as a “logical unit” arrives at this input, the entire queue shifts.

There is one important difference between a queue and a shift register. If the shift register is designed for four entries (for example, four bytes), then the first entry in the queue will reach the exit from the register only after four signals to the input “shift the register”.

RAM

If many, many triggers are combined into registers, and many, many registers are combined in one chip, then we get a RAM chip. A memory chip usually has an address input, a bi-directional data input (that is, you can write to this input, and you can read from it) and a write permission input. At the input of the address we give some number, and this number will select a specific memory cell. After that, at the input / output of the data, we can read what is written in this very cell.
Now we simultaneously submit to the input / output of the data what we want to write to this cell, and to the input of the write permission - “logical unit”. The result is a bit predictable, isn't it?

CPU


Bitbitjump

Processors are sometimes divided into CISC - those that know how to execute many different commands, and RISC - those that know how to execute a few different commands, but execute them well. One fine evening I thought: it would be great if you could make a full-fledged processor that can execute only one command. I soon learned that there is a whole class of single-processor processors - OISC, most often they use the Subleq (subtract, and if less than or equal to zero, go) or Subeq (subtract, and if zero, then go). Studying various options of OISC-processors, I found on the network the site of Oleg Mazonka, who developed the simplest single-command language BitBitJump. The only command of this language is called BitBitJump (copy the bit and go to the address). This one is definitely esoteric,

A detailed description of BitBitJump and assembler for this language can be found on the developer's website . To describe the algorithm of the processor, it is enough to know the following:
1. When the processor is turned on, 0, are written in the PC, A, and B registers
. 2. We read the memory location with the PC address and save what we read into register A
3. Increase the PC
4. Read the memory location with the PC address and save the read in register B
5. Increase PC
6. Write to the cell with the address written in register B, the contents of the bit with address A.
7. Read the memory cell with the address of PC and save the read in register B
8. Write the contents of the register in the PC register B
9. Go to item 2 of our plan
10. P ROFIT !!!

Unfortunately, the algorithm is endless, and therefore PROFIT will not be achieved.

Actually, the scheme

The scheme was built spontaneously, so fear, horror and confusion reign in it. However, it works, and works decently. To turn on the processor, you need:
1. Enter the program in RAM
2. Press the switch
3. Set the counter to position 4 (this can be done also in hardware, but the circuit would become even more cumbersome)
4. Turn on the clock generator

As you can see, one register was used , one shift register, one RAM chip, two binary counters, one demultiplexer (represented by comparators), two multiplexers and a little clean logic.



You can download the circuit in circ format for Logisim , and play.

What's next?

Firstly, you can increase the capacity of the processor - replacing 8-bit elements with 16-bit ones.
Secondly, you can remove the RAM from the processor, and add a simple circuit that will pause the processor, change the RAM and turn on the processor again. Such a circuit will serve as a simple I / O controller. Then it will be possible to make on the basis of this processor a calculator, a controller or some other funny useless thing.
Thirdly, it is possible to implement this whole scheme in hardware. What am I going to do. As soon as I do, I will definitely tell and show.

Thank you all for your attention!

PS Links (for those who are too lazy to read):
1. URISC processors - ru.wikipedia.org/wiki/Urisc
2. BitBitJump language site - mazonka.com/bbj/index.html
3. Logisim logic circuit simulation program - http://ozark.hendrix.edu/~burch/logisim/
4. Home-made URISC (ORISC) processor for Logisim - narod.ru/disk/31367690001/oo.circ.html

Also popular now: