diff --git a/index.html b/index.html new file mode 100644 index 0000000..175b7bb --- /dev/null +++ b/index.html @@ -0,0 +1,1375 @@ + + + + + Microcontroller Design + + + +
+
    +
  1. +
+
+
+ +

Introduction

+

+ 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. +

+

+

Audience

+ Of course you should be geek enough to read this book. Although I try to simplify topics, + but if you have knowledge of logical circuits or micro processors, + you'll understand this book easier. +

+

+

What will we learn?

+
    +
  1. + You'll understand how mankind communicated with machines for years + & the way people communicated with computers didn't change. +
  2. +
  3. + You start learning Logical Circuits which is base of computer architecture and organization. +

    + 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. +

    +
  4. +
  5. + Once we learned Logical Circuits, we'll start to design simple circuits and even, computers, memory blocks, etc! +
  6. +
  7. + We'll combine everything we had previously designed and that will be our computer! +
  8. +
  9. + Finaly, we can program our computer, and we actually understood how a computer can understand human language. +
  10. +
+

+
+
+
+

LICENSE

+ This book is published for free. + Everyone can use it as a source for non-commercial, educational and technical purposes. +
+
+ +

+

What is a "micro-controller"?

+ A micro-controller is a small computer, usually System on Chip (SoC) which includes: + + The most famous family of micro-controllers is AVR (stands for: Advanced Virtual RISC). This family of micro-controllers are used in some educational boards (eg. Arduino) and are generally used for educational and industrial purposes. +
+ A micro-controller is the brain of either a simple toy car or a U.S.A.F. drone (and more) depending on its type and the program we've written. +

+

+

What does a microcontroller consist of??

+ In this section, I only explain process core, and in other chapters, we'll study other parts such as memory unit and I/O system. +
    + The process core is usually made up of: +
  1. +
    Arithmetic and Logical Unit (ALU)
    +

    + This unit, does every logical and arithmetic operation. It's the most important part of a microcontroller. +

    +
  2. +
  3. +
    Data Bus
    +

    + This part is simply a bunch of wires which transfer data between two/different parts of process core. +

    +
  4. +
  5. +
    Registers
    +

    + 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. +

    +
  6. +
+

+

+

But, How does it know?! ...

+ OK, now let's talk about How does even a computer know what we want? + The answer is quite simple! for instance, when we write the following x86 family Assemby code: +
+					
+						

+MOV DX, OFFSET MSG +MOV AH, 09 +INT 21H +

+
+We assemble and run the code. it shows a message (e.g. Hello World!). +
+ 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. +

+ 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! +

+

+
+
+
+

How to talk to computer?

+

+ 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++:
+				
+					

+#include <iostream.h> +using namespace std; + +int main(){ + cout << "Hello, World!" << endl; + return 0; +} +

+
+This is called a program and when we *compile* it, it turns to a file, usually named a.out. +Let's run our program: + +

+~:$ ./a.out +Hello, World! +~:$ +

+
+
+ 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. +

+

+ 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'. +

+
+

The Machine Code

+

+ 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! +

+
+

Everything binary

+

+ 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. +

+
+
+

Word size

+

+ 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: + + + + + + + + + + + + + + + + + + + + + +
76543210
00000000
+ Now, we initialized one byte using only zeros, but, we have two forms for a bit, zero or one. + So, how can we convert a binary number to a decimal one?! +

+
+
+

Conversion, It's easy

+

+ 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: + + + + + + + + + + + + + + + + + + + + + +
76543210
00001001
+ Now, we know column 3 and column 0 have value one, it's so easy to calculate the decimal value: +
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 +

+ So, we shall enter 1 in columns 7, 2, 1 and 0. Our result is just like this: + + + + + + + + + + + + + + + + + + + + + +
76543210
10000111
+ And Yes! 10000111 is our result. +

+

+ 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? +

+

+
+
+

Hexadecimal world welcomes you

+

+ 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. + + This table, shows you a simple convertion among binary, decimal and hexadecimal: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
BinaryDecimalHexadecimal
0000000
0001011
0010022
0011033
0100044
0101055
0110066
0111077
1000088
1001099
101010A
101111B
110012C
110113D
111014E
111115F
+ These are our digits. +

+

+ 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: + + + + + + + + + + + + + + + + + + + + + +
76543210
10011110
+ Oh, 10011110 is our result. +

+

+ Let's convert it to hexadecimal! It has 8 bits, + it means it has 2 digits in hexadecimal format. + + So, we draw a hexadecimal conversion table for this number: + + + + + + + + +
10
10011110
+

+

+ 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. +

+
+
+
+

Mathematics?

+

+ 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. +

+
+
+
+

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. +

+
+

Addition

+

+ How do you add two decimal numbers? for example 105 and 55. This is how we add these two numbers: + + + + + + + + + + + + + + + + + + + + + + + + + +
carry1
number 1105
+number255
result160
+ We start from ones, then we add carry of ones to decimal. +

+ 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 number76543210
carry11
first number00001100
second one00000100
result76510000
+ Did you see, we used to empty bits for our carry. +

+

+ 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. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
C76543210
11
11000000
01000000
100000000
+ In column C, we've stored our carry. + This means, we need a word size bigger than 8 bits, if we care about the final result. + But, currently we don't care about it, and we just store it in a memory block called + Carry Flag. we'll talk about it later. +

+

+ 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? +

+
+
+

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. +

+
+

Sign/Modulus System

+

+ 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. + + Let's see an example, for example we convert 36 and -66 to a S/M number: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
DecimalSign bit6543210
3600100100
-6611000010
+ Oh, let's think about this system. +

+

+ This system is very easy-to-use, but have two great problems: +

    +
  1. It makes our word size smaller, as one of our bits wasted.
  2. +
  3. It makes negative zero condition, and we need to use algorithms designed for this system.
  4. +
+ Algorithms to solve negative zero condition are not very popular, so we won't use S/M. +

+
+
+

One's complement

+

+ There's another easy way, and it's called One's complement, the second method. + + In this system, we just invert bits. + Every 0 becomes 1, and every 1 becomes 0. + Let's calculate -36 in this system: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Decimal76543210
3600100100
-3611011011
+

+

+ 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! +

+
+
+

Two's complement

+

+ 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. + + Let's calculate -67 in this system: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
DescriptionDecimal76543210
to binary6701000011
Inverted10111100
Add with 111
Result-6710111101
+ In this system, we have no negative zero conditions, and all bits are used efficiently. +

+

+ Now, we have chosen this system as our standard, + and we'll design our subtraction parts using this system. +

+
+
+

How to Subtract?

+

+ 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) +

+
+
+ + So, first, we calculate -36: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Decimal76543210
3600100100
11011011
11
-3611011100
+
+ + And now, we do our addition: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
DecimalCarry76543210
11111
12501111101
-3611011100
101011001
+ The carry is normal here, and every subtraction have one, + now, we can do every subtraction with this method. +

+

+ 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. +
+
+
+

Now, what should we do?

+

+ 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. +

+
+
+
+

Logical Operations

+

+ 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. +

+
+

How many logical operations do we have?

+

+ We have some simple logics, and we review all of them here, and we solve some simple problems, and create new logics. +

+
+

NOT

+

+ 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. + + Let's do `NOT` operation on A! + + + + + + + + + + + + + + + +
NOTA~A
01
10
+ We show 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.) +

+

+
+
+

AND

+

+ This operation, is very simple, but has two logic inputs. + + The table of AND is like this: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ANDABOut
000
100
010
111
+ We show A AND B in logical notation in the form AB. +

+
+
+

OR

+

+ 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. + + This table can explain this operation + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ORABOut
000
101
011
111
+ One of operands need to be True and it makes whole answer True. +

+

+ we show A OR B in the form A+B. +

+
+
+
+

Truth Table

+

+ You saw, we used a table, which includes operation, operands and output. This table is called Truth Table. +

+
+
+

Let's play a game!

+

+ So, let's combine some logics and make new ones! The simplest ones are: +

+
+

NAND

+

+ It means ~(AB) - it's NOT(AND(A, B)) in simple words. + + NAND Truth Table + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NANDABOut
001
011
101
110
+

+
+
+

NOR

+

+ It means ~(A+B) - it's NOT(OR(A, B)) in a simple word. + + NOR Truth Table + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NORABOut
001
010
100
110
+

+

+ Is there any more logics? of course yes! But, we will have a view on only two others, in this chapter. +

+
+
+
+

Complex logics

+

+ 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! +

+
+

eXclusive OR

+

+ XOR is implemented as ~AB + A~B. + + eXclusive OR (XOR) truth table + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
XORABOut
000
101
011
110
+

+
+
+

eXclusive NOR

+

+ 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 Truth Table + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
XNORABOut
001
010
100
111
+

+
+
+
+

The Journey to Computer Architecture!

+

+ 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! +

+
+
+ + +