diff --git a/index.html b/index.html new file mode 100644 index 0000000..175b7bb --- /dev/null +++ b/index.html @@ -0,0 +1,1375 @@ + + +
+ ++ I always wanted to design a micro-controller or micro-processor, so I managed to model them. Now, I've decided to show people how a micro-controller is designed. +
++ I'm inspired by the book I've read But How Do It Know? - The Basic Principles of Computer for Everyone by J. Clark Scott and also studied MIPS, ARM and PowerPC architectures. So, in this book, I try to simplify a micro-processor and turn it to a micro-controller. +
++
+
+ importance of Logical Circuits knowledge is undeniable.
+
+ In all universities (at least Iran's) you must pass Logical Circuits
+ course before taking Computer Architecture course.
+
+
+
+ This unit, does every logical and arithmetic operation. It's the most important part of a microcontroller. +
++ This part is simply a bunch of wires which transfer data between two/different parts of process core. +
++ Registers are small memory blocks. They can store data temporarily and transfer them. We always need registers, because they're our temporary memory blocks and while we run a program, we need to store inputs and outputs. +
++
++ To Imagine: You're a tourist in Iran, you can understand Iranians by speaking Persian, + And Iranians can also communicate you in Persian. So, if we consider each computer a foreigner, we can communicate with them by Assembly language (Machine languages, preciser). + It's computers' language and we need to communicate with it in its own language. +++We assemble and run the code. it shows a message (e.g.+MOV DX, OFFSET MSG +MOV AH, 09 +INT 21H +
+Hello World!). +
+ In this book, you'll find how can we generate a simple machine language, and we can do simple projects with our small computer, and enjoy! +
+ + ++ In previous chapter, we talked about how a computer can understand us. In this chapter, we learn how to communicate with our computer, and how to command it. +
+Let's take a look at a common *programming language*, for example, C++: ++ And now, we've communicated with our computer, but in a language similiar to ours. + This kind of programming languages are called High Level languages. They're close to human language, and they're easy to learn. + +++This is called a program and when we *compile* it, it turns to a file, usually named+#include <iostream.h> +using namespace std; + +int main(){ + cout << "Hello, World!" << endl; + return 0; +} +
+a.out. +Let's run our program: ++++~:$ ./a.out +Hello, World! +~:$ +
+
+ There are a lot of high level languages such as C++, Python, Ruby, Go, etc. We use them when we need. + For example, for coding a school project you may use C, or Go and to code a commercial project, you may use python, as it's easy to learn + and also have a lot of libraries. +
++ But, what if you want to talk (code) directly to your hardware?! Now, we need a language which is close to machines'. +
+
+ Machines are awesome, because they only use 0 and 1 to communicate with each other.
+ Humans are not as smart as computers in this case. we use a lots of letters/(phonemes in conversation) in daily communications.
+ English includes 26 letters, Persian includes 32 letters, and there are languages with more than 100 letters.
+ Computers have only two letters, 0 and 1. Now, let's take a look at these letters!
+
+ OK, we told you there is two letters in machine language. So, we need to convert usual letters and numbers to words that computer can understand. + We use base 2 as a key to machine language. In base 10 - the regular format of daily mathematics - we have digits 0 through 9, but in base 2, we have only 0 and 1. + Every digit here is called a binary digit , or in short, a bit. But a single bit alone is not enough for us. +
++ Imagine the word "Hello" in English language. It has 4 characters. Also, + imagine the number "42", it has 2 digits. In computers, we need a fixed size, + and each word can't be bigger or smaller than that. We call this fixed space word size. + We need word size to compute carry digit, overflow and underflow, etc. +
++ One of the most popular word sizes, is a byte, which is made up of 8 bits. + A simple byte is mapped like this: +
| 7 | +6 | +5 | +4 | +3 | +2 | +1 | +0 | +
| 0 | +0 | +0 | +0 | +0 | +0 | +0 | +0 | +
+ Now, let's take a look at a simple binary number (e.g. 1001).
+ This number is a 4 bit one, but we defined our word size 8 bits.
+
+ In order to convert it to an 8 bit number, we just need to replace missing bits with zeros: +
| 7 | +6 | +5 | +4 | +3 | +2 | +1 | +0 | +
| 0 | +0 | +0 | +0 | +1 | +0 | +0 | +1 | +
2^3 + 2^1 = 8 + 1 = 9
+
+
+ It's fine to convert binary numbers to decimal, but how can we convert a decimal number to binary?
+
let's try to convert 135 to binary. We all know 135 is equal to 128 + 7.
+
+ So, we can write this number like:
+ 2^7 + 2^2 + 2^1 + 2^0
+
| 7 | +6 | +5 | +4 | +3 | +2 | +1 | +0 | +
| 1 | +0 | +0 | +0 | +0 | +1 | +1 | +1 | +
+ But there's a question, is there any other format(s) to communicate with computers? Yes! We can use base 16, too. + base 16 is known as hexadecimal, and we have digits 0 through F in that format. It's weird, isn't it? +
+ ++ Now, let's talk about digits. In base 10, we have 10 digits, In base 3, we have 3 digits, + but what about 16? In base 16 we have 16 digits. But how can we code our digits? + It's so easy, we use Latin letters instead of numbers. +
| Binary | +Decimal | +Hexadecimal | + +
| 0000 | +00 | +0 | +
| 0001 | +01 | +1 | +
| 0010 | +02 | +2 | +
| 0011 | +03 | +3 | +
| 0100 | +04 | +4 | +
| 0101 | +05 | +5 | +
| 0110 | +06 | +6 | +
| 0111 | +07 | +7 | +
| 1000 | +08 | +8 | +
| 1001 | +09 | +9 | +
| 1010 | +10 | +A | +
| 1011 | +11 | +B | +
| 1100 | +12 | +C | +
| 1101 | +13 | +D | +
| 1110 | +14 | +E | +
| 1111 | +15 | +F | +
+ We learned from this table that every 4 bit is equal to one hex digit.
+ So, we can convert a decimal number to a hexadecimal one using this table,
+ and our knowledge of binary calculations. Let's think about 158.
+
158 is 128 + 30. So, We can write this number like this:
+
+
+
+2^7 + 2^4 + 2^3 + 2^2 + 2^1
+
+
+
+ And now, we can draw binary conversion table for this number like this:
+ | 7 | +6 | +5 | +4 | +3 | +2 | +1 | +0 | +
| 1 | +0 | +0 | +1 | +1 | +1 | +1 | +0 | +
+ Let's convert it to hexadecimal! It has 8 bits, + it means it has 2 digits in hexadecimal format. +
| 1 | +0 | +
| 1001 | +1110 | +
+ According to the previous table
+ 1001 is equal to 9 and 1110 is qual to E.
+ So, our result is 9E.
+
+ In this sections, we learned about how computer sees data. + A computer reads data in binary format, + but we simplify it by showing binary numbers in hexadecimal, + and of course, we translate hexadecimal instructions for computer later. +
++ We learned how to communicate with a computer, and now, + we need to know how to do arithmetic operations using a computer. +
++ In next chapter, we just take a look at how a computer can do arithmetic operations. +
++ We do simple arithmetic operations in our daily life. + For example, when you buy a candy for $2 , and you give the seller $5 banknote, he will give you $3. + This is a simple subtraction we use in daily life. + Or, if you want to buy a toy car for $100, and you have $80 in your pocket, you may go to bank and you will take $20 from your account. + This is a simple addition and subtraction in daily life. But how a computer do that?! +
++ In previous chapter, we talked about how computer reads data. + We talked about binary digits. + In this chapter, we take a look at/that addition and subtraction of them. +
++ How do you add two decimal numbers? for example 105 and 55. This is how we add these two numbers: +
| carry | ++ | 1 | ++ |
|---|---|---|---|
| number 1 | +1 | +0 | +5 | +
| +number2 | ++ | 5 | +5 | +
| result | +1 | +6 | +0 | +
+ Now, we can do the same for 1100 and 0100. I know we have decided to use 8 bit word size, and so I've dran a 8 bits table: +
+| bit number | +7 | +6 | +5 | +4 | +3 | +2 | +1 | +0 | +
|---|---|---|---|---|---|---|---|---|
| carry | ++ | + | + | 1 | +1 | ++ | + | + |
| first number | +0 | +0 | +0 | +0 | +1 | +1 | +0 | +0 | +
| second one | +0 | +0 | +0 | +0 | +0 | +1 | +0 | +0 | +
| result | +7 | +6 | +5 | +1 | +0 | +0 | +0 | +0 | +
+ If we use a smaller word size, this carry can't be displayed in output, and that will result an error. + OK, we just did a simple addition. Let's talk about a bigger one, 11000000 and 01000000. +
| C | +7 | +6 | +5 | +4 | +3 | +2 | +1 | +0 | +
| 1 | +1 | ++ | + | + | + | + | + | + |
| + | 1 | +1 | +0 | +0 | +0 | +0 | +0 | +0 | +
| + | 0 | +1 | +0 | +0 | +0 | +0 | +0 | +0 | +
| 1 | +0 | +0 | +0 | +0 | +0 | +0 | +0 | +0 | +
+ Addition in binary is very, very easy. + But, what about subtraction? we need a subtraction for our microcontroller, too. + But how can we implement our subtraction? +
++ It's a bit difficult to do a subtraction in computers, because we have no subtraction circuit. + We have Negator and Adder instead of subtractor. + So, we have different ways to make a number negative. +
++ I'll show you three popular ways, and at the end, we choose on of them as our standard. +
++ In the first method, we have a sign bit, which is equal to the most valuable bit. + If it's 1, number is negative, and if it's 0, number is positive. +
| Decimal | +Sign bit | +6 | +5 | +4 | +3 | +2 | +1 | +0 | + +
| 36 | +0 | +0 | +1 | +0 | +0 | +1 | +0 | +0 | +
| -66 | +1 | +1 | +0 | +0 | +0 | +0 | +1 | +0 | +
+ This system is very easy-to-use, but have two great problems: +
+ There's another easy way, and it's called One's complement, the second method. +
| Decimal | +7 | +6 | +5 | +4 | +3 | +2 | +1 | +0 | + +
| 36 | +0 | +0 | +1 | +0 | +0 | +1 | +0 | +0 | +
| -36 | +1 | +1 | +0 | +1 | +1 | +0 | +1 | +1 | +
+ This system is much better than S/M. but still have a great problem. + It still makes negative zero, and it's not good for us. + So, we have a final solution! +
++ Our final solution is the third (and last) method: Two's complement. +
++ This system is very similar to one's complement, + but we just add 1 to one's complement, + and it makes two's complement for us. +
| Description | +Decimal | +7 | +6 | +5 | +4 | +3 | +2 | +1 | +0 | + +
| to binary | +67 | +0 | +1 | +0 | +0 | +0 | +0 | +1 | +1 | +
| Inverted | ++ | 1 | +0 | +1 | +1 | +1 | +1 | +0 | +0 | +
| Add with 1 | +1 | ++ | + | + | + | + | + | + | 1 | +
| Result | +-67 | +1 | +0 | +1 | +1 | +1 | +1 | +0 | +1 | +
+ Now, we have chosen this system as our standard, + and we'll design our subtraction parts using this system. +
++ OK, now we are able to make negative numbers. + So, a subtraction operation is simply addition of a number, to a negative number. +
+So, for example:
+
+
+125 - 36 is actually equivalent to 125 + (-36)
+
+
+
+ | Decimal | +7 | +6 | +5 | +4 | +3 | +2 | +1 | +0 | + +
| 36 | +0 | +0 | +1 | +0 | +0 | +1 | +0 | +0 | +
| + | 1 | +1 | +0 | +1 | +1 | +0 | +1 | +1 | +
| 1 | ++ | + | + | + | + | + | + | 1 | +
| -36 | +1 | +1 | +0 | +1 | +1 | +1 | +0 | +0 | +
| Decimal | +Carry | +7 | +6 | +5 | +4 | +3 | +2 | +1 | +0 | + +
| + | + | 1 | +1 | +1 | +1 | +1 | ++ | + | + |
| 125 | ++ | 0 | +1 | +1 | +1 | +1 | +1 | +0 | +1 | +
| -36 | ++ | 1 | +1 | +0 | +1 | +1 | +1 | +0 | +0 | +
| + | 1 | +0 | +1 | +0 | +1 | +1 | +0 | +0 | +1 | +
+ The carry has a role now, we call it Sign Flag + and when we want to design our final process core, + we will consider a bit for this bit, too. +
+ In next chapter, we'll learn logical operations, which are very important in every computer. ++ In our process core , we will use two's complement system as the main system. + Now, we need another type of operations, called Logical Operations. +
++ In next chapter, we'll learn logical operations, which are very important in every computer. +
++ Let's talk about logic! Do you know what logic is? + Logic is somekind of mathematics, mixed with philosophy in simple word. + It's started by Greek philosopher, Aristotle. Simply, logic says: + John is taller than Ali, Ali is taller than Ahmed, so, John is taller than Ahmed. + This was a very, very simple example of a logical problem in our daily life. + We can do this example for everything measurable in our daily life, souch as area, height, weight, etc. +
++ We have some simple logics, and we review all of them here, and we solve some simple problems, and create new logics. +
++ As we have binary system, we use 0 for everything off and 1 for everything on. + 0 for everything true and 1 for everything false. + So, here we have just one variable called A. +
| NOT | +A | +~A | + +
| + | 0 | +1 | +
| + | 1 | +0 | +
NOT A in the form ~A.
+
+ + This notation, helps us write logical functions. +
+ (Functions are operations we do on one or more variables, and + it has unique answer per inputs from a bunch of variables.) +
+ ++ This operation, is very simple, but has two logic inputs. +
| AND | +A | +B | +Out | + +
| + | 0 | +0 | +0 | +
| + | 1 | +0 | +0 | +
| + | 0 | +1 | +0 | +
| + | 1 | +1 | +1 | +
A AND B in logical notation in the form AB.
+
+ + Of course, you remember famous question from Shakespear's novel Hamlet: +
To be, or not to be; this is the question!+ Now, we're going to explain what OR is. +
| OR | +A | +B | +Out | + +
| + | 0 | +0 | +0 | +
| + | 1 | +0 | +1 | +
| + | 0 | +1 | +1 | +
| + | 1 | +1 | +1 | +
+ we show A OR B in the form A+B.
+
+ You saw, we used a table, which includes operation, operands and output. This table is called Truth Table. +
++ So, let's combine some logics and make new ones! The simplest ones are: +
+
+ It means ~(AB) - it's NOT(AND(A, B)) in simple words.
+
| NAND | +A | +B | +Out | + +
| + | 0 | +0 | +1 | +
| + | 0 | +1 | +1 | +
| + | 1 | +0 | +1 | +
| + | 1 | +1 | +0 | +
+ It means ~(A+B) - it's NOT(OR(A, B)) in a simple word.
+
| NOR | +A | +B | +Out | + +
| + | 0 | +0 | +1 | +
| + | 0 | +1 | +0 | +
| + | 1 | +0 | +0 | +
| + | 1 | +1 | +0 | +
+ Is there any more logics? of course yes! But, we will have a view on only two others, in this chapter. +
++ There are to other logics, which are made from other logics, I would like to call them "Complex + Logic"s because They're not as simple as AND or OR. Also, we can call them "Exclusive Logic"s. + This is what other engineers called them! +
+
+ XOR is implemented as ~AB + A~B.
+
| XOR | +A | +B | +Out | + +
| + | 0 | +0 | +0 | +
| + | 1 | +0 | +1 | +
| + | 0 | +1 | +1 | +
| + | 1 | +1 | +0 | +
+ This logic is the same as XOR, but with a little difference!
+ if you apply NOT function to XOR, you'll get XOR. However, the
+ best implementation of XNOR is ~A~B + AB
+
| XNOR | +A | +B | +Out | + +
| + | 0 | +0 | +1 | +
| + | 0 | +1 | +0 | +
| + | 1 | +0 | +0 | +
| + | 1 | +1 | +1 | +
+ Now, we know logics, and we need to learn about Logical Circuits, + which are representation of these logics in computer science and electronics. +
++ In the next chapter, we will learn how to use and design simple logical circuits. + After that, we'll start to design and implement our dear micro-controller. +
++ Of course you must have read these chapters to learn the computer language. + but after learning the language, you need to know how a computer is built! +
+