Menu

Skip to content
0xdemo's low level stuff

0xdemo's

Low-level stuff

Month: August 2017

An introduction to modern computers, Part 4 – Clocks and Flip-Flops

Posted on August 25, 2017 by demo

note: This post relies heavily on the basics explained in Part 1 , Part 2 and Part 3.

In the previous post we’ve seen a 2 bit adder that works by manipulating the flow of current through the circuit by using an array of transistors. The adder was controlled manually by flicking switches.

But flicking switches to change the value of single bits wont get us far if we want to have a functional computer. The next step we will need to take is to make a routine that will run on its own. The routine should be basic, like a routine that’ll blink a LED on regular intervals.

First of all, lets see how much we can squeeze out from pushing buttons and connecting wires in a funky way. One of the interesting things we could do is to use feedback. What would happen if you connect the output of an OR gate to one of its inputs? Think about all the truth table of an OR gate (which is very basic), and now consider all the possible scenarios in a dynamic situation were you control the other input.

Once you are done thinking, watch this video about latches. With circuits containing logical gates and feedback, we have a way to introduce switching logic to the circuit. A combination of latches allows us to do some very interesting things as you’ll see later in the post.

However, we are still flicking switches. To make our circuit run a routine we need a way to make it change autonomously. The solution to our problem is oscillation of current in circuits, which can be created by a combination of a capacitor and an inductor. This was discovered during the 19th century, and the physics behind it are too complex to explain in this post (maybe I’ll dedicate a few posts going more into physics in the future). This is actually the first circuit you’ll encounter if you’ll visit the site that was mentioned in the previous post.  By combining an oscillating current with a diode bridge, you can blink a LED a few times (before energy is lost in the form of heat and light emitted from the LED).

A few decades later, circa 1920 a crystal was first used as a way to generate oscillations in a circuit. Again the physics behind how it works will hopefully be explained in a future post. But meantime, here are some nice videos about crystals and oscillations.

What we have now is a mechanism that gives us pulses, ticks and tocks – a clock.  There’s even a way to create a clock with a latch and a combination of basic circuit parts. For an example of this, we’ll jump forward in time all the way to 1971,  when the 555 timer integrated circuit (IC/chip) was invented. This is a great video that explains how the 555 timer works.

Now that we have a clock, we can combine it with latches to create flip-flops. There are several types of flip-flops, check out these videos explaining how the D, T and JK flip-flops work.

With the clock and the switching circuitry, we can create little routines. This circuit will count from 0 to 0xf in binary (result displayed in binary using LEDs):

It’s based on a 555 timer chip (on the right), and 4 T-flip-flops.

With the right combination of switches, we can execute complex routines. All you need to think about the logic behind what you’re trying to achieve, translate this logic to transistors and hook them up to a source of voltage and a clock.

There are a few things still missing though. One of them is memory. We often need a place to store the output from a routine for later use. In the next post, we’ll explain how RAM (and memory in general) works.

Hope you found this post informative. Feel free to leave comments, and ask questions.

 

demo

Posted in An introduction to modern computers | Leave a comment

An introduction to modern computers, Part 3 – Transistors, Boolean algebra, binary, hex – and a tiny calculator!

Posted on August 21, 2017 by demo

note: This post relies heavily on the basics explained in Part 1 and Part 2.

This video does great work in explaining how diodes and transistors work. It should be easy to understand after reading the above-mentioned part 1 and 2. The part you should take note of is near the end of the video, where there is talk about the transistor acting as an electronic switch.

Now lets assume the collector is always connected to a voltage source and that the emitter is connected to a small LED (and a proper resistor to keep the LED from burning). You can now blink the LED by passing current through the base intermittently. When current passes through the base, the LED lights up. When it doesn’t the LED turns off. On, Off. Hold that thought.

Boolean algebra was introduced by George Boole around a century before transistors (transistors circa 1930, Boolean algebra circa 1840). The name “Boolean algebra” was suggested half a century after Boole’s death by the way (in 1913). As far as Boole was probably concerned, he was talking about “The mathematical analysis of logic” (which was the name of his first book).

All the variables and results of Boolean algebra’s calculations were either True or False, which were usually represented by 1 and 0 respectively. The basic operators sound like logical expressions: OR, AND, NOT. To this list of basic operators I’ll add another operator, which is basic as far as everyone who deals with computers is concerned, the operator XOR (eXclusive OR).

The simplest Boolean calculations, which are the most relevant for this post, take place between two variables. Lets look at variable A and B and the results of applying different operators on their values.

The result of “A OR B” is True (or 1) if at least one of them is true, otherwise the result is False(or 0). lets mark the OR operator as one perpendicular horizontal line |. Now lets do some calculations:

0 | 0 == 0 (False OR False equals False)
1  | 0 ==  1 (True OR False equals True)
0 |  1 ==  1 (False OR True equals True)
1  |  1 ==  1 (True OR True equals True)

Moving on to the next operator. The result of “A AND B” is True only if both of them are True, otherwise the result is False. Lets mark the AND operator by the ampersand symbol &.

0 & 0 == 0
1  & 0 == 0
0 &  1 == 0
1  &  1 ==  1

Next. The result of “NOT X” True if X is False, and False if X is True. Lets mark the NOT operator by the tilde symbol ~.

~1  == 0
~0 ==  1

And the last one: The result of “A XOR B” is True if A is NOT B, otherwise the result is False. Lets mark the NOT operator by the caret symbol ^.

0 ^ 0 == 0
1  ^ 0 ==  1
0 ^  1 ==  1
1  ^  1 == 0

All the above calculations form the “truth tables” for each operator (when the result of an operator is true and when it’s false).

Now lets go back to our LED. As far as we’re concerned the LED can be in only one of two states – On or Off – 1 or 0. And we have a transistor acting as an electric switch of our LED. Great, now what?

Well, what if I told you that I can make Boolean calculations just by using two voltage sources, two transistors, two NPN transistors, and get the output result using the LED? Sounds like a lot of trouble for something that one could easily calculate in his head, but bear with me on this one. Here’s a diagram of a circuit to calculate value of the OR operator:

The circuit is simple to understand. Applying voltage to points A or B will result in the LED (which is connected to the OUTPUT) to light up. A or B. A OR B. A|B. Simple. Lets look at the AND operator:

The LED will light up only if you apply voltage to A AND B. A&B.
I’ll leave it up to you to figure out how to build a XOR gate with an NPN transistor combination. It’s not as trivial as it sounds.

The above diagrams represent what is commonly known as Logical Gates, which are represented by the following drawings:

You might be thinking to yourself that there’s a huge chasm between computing as we know it and logical gates which can calculate the Boolean result after we manually mess with their inputs. We only have two pieces of the puzzle missing: The binary numeral system, and a clock. We’ll discuss the clock in the next post. Lets go over the binary numeral system first.

The binary numeral system (or binary) is simple. Each binary digit can be either 0 or 1. Lets count to 10 in binary starting from 0:

0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010

As you can see, since the maximum value of a single digit is 1, once we add 1 and 1 we get 0 and carry 1 over to the left. Lets add two binary numbers and see what we get. For example 10 (decimal 2) and 1 (decimal 1). Lets re-arrange the numbers:

10
01

One can easily see that the result is 11 (decimal 3). Now one with a carry (the little b means it’s a binary number): 111b + 10b ==  1001b (7+2 == 9 decimal). Simple. Subtraction works on the same principle as with decimal numbers. We’ll discuss negative binary numbers in future posts.

Now there’s one thing you might have noticed, back when we counted to 10 decimal in binary, we got an “incomplete” looking number, as apposed to 10 which looks “nice and round” as a decimal number. Lets keep counting:

0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111

Which brings us to 15 decimal. The next number is 16 decimal, or 2**4, or 10000b. Notice that 2**4 is represented in binary as 1 with 4 trailing zeroes. Same thing goes for 8 == 2**3  == 1000b, 4 == 2**2 ==  == 100b, 2 == 2**1 == 10b, 1 == 2**0 == 1.

Base 2 (binary) and Base 16 (Hexadecimal) numeral systems are good friends. 10h == 10000b (h stands for hexadecimal). How does hexadecimal work? Lets count to 10h:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F , 10h

The little ‘h’ is important because 10h equals 16 decimal. Hexadecimal (hex) digits can also be marked by the ‘0x’ prefix: 0x13 (19 decimal), 0xab (171 decimal). If you are working with computers, you should be fluid in binary and hex. If you are running Windows, the calculator that comes with the operating system has a “programmer” mode, and works great for calculations in all bases.

Now lets combine binary with Boolean calculations to create a circuit that can calculate the addition of two binary numbers which are 2 digits wide, and display the result in binary using 3 led lights (which is enough to represent the maximum possible value for the addition, which is 6).

We’ll do this by using two approaches, lets call the first “the pure truth table approach”, and the second “the right way to do things”. Guess which one’s better. Nevertheless, for learning purposes, it’s important to take a look at both.

First lets figure out the truth table for the results. We have a total of 4 inputs (2 numbers, two binary digits in each number), which could yield 7 possible results (0-6) with 3 outputs:

A is the first number, B is the second, and L represents the 3 LEDs. Now since we only care about when each one of the LEDs will light up, we re-arrange the tables for each LED:

If we look close we can see an interesting pattern that lights up the LEDs. I’ve colored this pattern with the following colors:

Green – Only a single digits  is 1.
Red – A XOR B.
Yellow – B equals A.
Orange – Only a single digit is 0.
Blue – All digits are 1.

Using some of Boole’s methods, we can translate this to a logical gate array that looks something like this:

Now here’s the same circuit with the gates colored in relation to the truth table:

Kudos to whoever created THIS GREAT SITE. You can get the TXT FILE and import it to the site and try adding numbers by flicking the switches in the top left corner.

Now it seems a bit too much for adding two binary double digit binary number. Lets rethink this. When adding numbers you start by adding the least-significant digits, writing down the result and remembering the carry. In binary, the result of the addition can be either 1 or 0, and the carry can be 1 or 0. If we look at a truth table of the result addition of two binary numbers, we get the same truth table as a XOR gate – The only way to get a result of 1 is to add 1 and 0. If we look at the truth table of the carry, we get the same truth table as an AND gate – The only way to get a carry of 1 is to add 1 and 1.

Translating this logic to a circuit would look something like this:

Now this makes sense. Get the TXT FILE and test it out.

If the above circuits looks familiar, it’s probably because of the image at the top of the site, showing a microscopic view of a CPU. Now consider that  in the above pictures we have a total of around a 150 transistors (give or take). A modern desktop CPU has around 2 Billion transistors (with super-high-end CPUs are at 5-10 billion). No wonder your PC can run an entire operating system blazing fast while the above circuit tops out at calculating 3+3. What have though is a calculator that can add two binary numbers 2 bits wide (two digits in each number), and output a results 3 bits wide.

But one thing is missing. We are still manually flicking the switches. In the next post, we’ll bring this beast to life with the addition on the clock.

This was a heavy post, but I hope you found it informative. Feel free to leave comments, and ask questions.

 

demo

 

Posted in An introduction to modern computers | Leave a comment

An introduction to modern computers, Part 2 – Basic elctric circuits

Posted on August 19, 2017 by demo

note: This post relies heavily on the basics explained in Part 1.

As was mentioned in the previous post, mankind domesticated electricity. We now have batteries, which are electrically charged objects with two terminals and a known and measured voltage between them:

There’s potential electricity in sockets which can be found in most of the walls around the house – electric tension just waiting to get released through an electric appliance. Mankind also became master of the numbers representing voltage, resistance and current. We can calculate the voltage and resistance to allow a the right amount of current through circuits that drive electromagnetic motors without burning the wires.

At the base of the design of an electrical appliance, such as a computer for example, there’s always a circuit through which electrons flow. They provide the energy to make things happen in the circuit (light up a light bulb for example).

A circuit contains the following elements:

  1. A Source, an electrically charged object with two terminals with voltage between them (can be a battery for example). These two terminals are usually dubbed positive and negative, and it agreed upon that current flows (at least should flow by design) from the positive to the negative terminal, or from the source to the “ground”.
  2. A Load, something that will harness the energy of electron flow and thus induce resistance to the circuit. Having a load in a circuit brings actual meaning to a circuit. Connecting the positive and negative terminals with a conductive wire without a load would make turn the low resistance wire into a load, causing a massive amount of current to flow through a wire. Depending on the amount of electrons or voltage the source can supply, the massive flow of current can create explosive results:

Remember that when there’s tension and resistance involved, current will tend to flow through the path of least resistance. When suddenly introducing a low resistance path to a circuit with a load, you create a “short circuit” for the current which will probably look like the above picture (depending on the amount of current the source can provide). Notice that the current flows directly through the metal screw but does not flow towards the pliers, because the metal screw provides the path of least resistance to alleviate the electric tension. If you’re wondering why the screw burns up while the copper wires don’t, it’s because the screw provides much more resistance, which is analogous to friction which generates heat.

Now lets look at a diagram of a basic DC (direct current, the same we’ve been talking about since the previous post) circuit:

The source and load are labeled, and the symbol at the bottom and top left represents the point of lowest possible electrical tension, or “ground”. The source generates tension measured by 9 volts, which will drive the current through the load towards the ground.  Using the number representing the resistance of the load, you can calculate the number representing the current running from the source to the ground (using Ohms law described in the previous post). Should be simple to understand based on what we know so far.

Another important component that can be found in circuits is a capacitor. The idea behind the capacitor is simple – take two metal plates, put them very close to each other (without them touching). Now saturate one of them with electrons, and drain electrons from the other. What you’ll get is on one plate being pulled towards the overall positive charge on the other plate. Since there’s no medium through which these electrons can flow, they are just stuck on the edge of their plate:

The term “holes” is used above to describe the lack of electrons in atoms, meaning a positive electrical charge.

Now the interesting thing happens when you introduce a capacitor into a DC circuit.  When you connect the capacitor to a source, current will flow as one side of the plate saturates with electrons while the other side has its electrons drain to ground (or the “negative” terminal of the source, which usually represent the exact same thing), and so the capacitor gets “charged”. Eventually, the amount of electrons on the saturated plate will completely counter the flow of new electrons and the current will drop to 0. Everything stops, as if the wire was cut. Now it is possible to disconnect the source, and connect the capacitor to another circuit as a source by itself!

In the above picture, we have the source (a 9 volt battery) , a load (a resistor and a light emitting diode) and a capacitor (in the center of the circuit). If the switch is set to point A, current will flow to the capacitor saturating one side with electrons, and draining the electrons from the other side. Eventually, the capacitor is at its full capacitance, at which point the switch is set to point C, where the electron under electric tension will finally receive a path to reach the other low tension plate. This way goes through a resistor, limiting the current to a controlled and predictable level.

By this point, we’ve covered the basics needed to understand the function of semi-conductors (which are the most basic block of a computer as we know it), which will be discussed in the next post.

Hope you found this post informative. Feel free to leave comments, and ask questions.

demo

Posted in An introduction to modern computers | Leave a comment

Post navigation

  • Older posts

Categories

  • An introduction to modern computers (9)
  • How Operating Systems work (2)
  • Prolog (1)

Posts

  • October 2017 (2)
  • September 2017 (5)
  • August 2017 (5)

Useful Links

Circuit Simulator

Intel 4004 emulator

Intel 8080 User’s Manual

An introduction to modern computers

1 – Electricity basics

2 – Basic electric circuits

3 – Transistors, Boolean algebra and Hex

4 – Clocks and Flip-Flops

5 – Memory and how DRAM works

6 – How the CPU works

7 – Computer Programming with assembly

8A – Intel’s 8080, interrupts and the new stack

8B – I/O, Peripherals and DMA

How Operation Systems Work

Part 1 – The forgotten history of Operation Systems

Part 2 – Privilege control and memory protection

Proudly powered by WordPress
Theme: Flint by Star Verte LLC