├── cover.jpg ├── figures ├── SRFF.png ├── 8bitSR.png ├── ANDGate.png ├── ANDNAND.png ├── NOT-BJT.png ├── NOTGate.png ├── NOTNAND.png ├── ORGate.png ├── XORGate.png ├── BasicMux.png ├── CMOSWaves.png ├── ClockedD.png ├── ClockedSR.png ├── Dregister.png ├── FullAdder.png ├── HalfAdder.png ├── NANDGate.png ├── NANDORNOT.png ├── NORANDNOT.png ├── NOTWaves.png ├── ORNANDNOT.png ├── XNORGate.png ├── final │ ├── RAM.png │ ├── ALU-0.png │ ├── ALU-1.png │ ├── ALU-2.png │ ├── ALU-3.png │ ├── ALU-4.png │ ├── RAM1.png │ ├── ALU-Module.png │ ├── TheCounter.png │ └── Microcontroller.png ├── register.png ├── 2x4Decoder.png ├── ExpandedMux.png ├── RippleCarry.gif ├── SimpleDemux.png ├── SquareWave.png ├── XNORCircuit.png ├── XORCircuit.png ├── not-mosfet.png ├── CMOS-Inverter.png ├── SRFF-activehigh.png ├── SimpleRegisterFile.png ├── AdditionMachine-Step0.png ├── AdditionMachine-Step1.png ├── AdditionMachine-Step2.png ├── AdditionMachine-Step3.png ├── AdditionMachine-Step4.png └── AdvancedRegisterFile.png ├── book.json ├── LICENSE ├── LICENSE.md ├── RESOURCES.md ├── chapter06.md ├── README.md ├── chapter01.md ├── chapter12.md ├── chapter05.md ├── chapter09.md ├── chapter10.md ├── chapter11.md ├── chapter08.md ├── chapter15.md ├── chapter07.md ├── chapter16.md ├── chapter04.md ├── chapter14.md ├── chapter17.md ├── SUMMARY.md ├── chapter13.md ├── chapter02.md └── chapter03.md /cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/cover.jpg -------------------------------------------------------------------------------- /figures/SRFF.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/SRFF.png -------------------------------------------------------------------------------- /figures/8bitSR.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/8bitSR.png -------------------------------------------------------------------------------- /figures/ANDGate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/ANDGate.png -------------------------------------------------------------------------------- /figures/ANDNAND.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/ANDNAND.png -------------------------------------------------------------------------------- /figures/NOT-BJT.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/NOT-BJT.png -------------------------------------------------------------------------------- /figures/NOTGate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/NOTGate.png -------------------------------------------------------------------------------- /figures/NOTNAND.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/NOTNAND.png -------------------------------------------------------------------------------- /figures/ORGate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/ORGate.png -------------------------------------------------------------------------------- /figures/XORGate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/XORGate.png -------------------------------------------------------------------------------- /figures/BasicMux.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/BasicMux.png -------------------------------------------------------------------------------- /figures/CMOSWaves.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/CMOSWaves.png -------------------------------------------------------------------------------- /figures/ClockedD.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/ClockedD.png -------------------------------------------------------------------------------- /figures/ClockedSR.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/ClockedSR.png -------------------------------------------------------------------------------- /figures/Dregister.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/Dregister.png -------------------------------------------------------------------------------- /figures/FullAdder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/FullAdder.png -------------------------------------------------------------------------------- /figures/HalfAdder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/HalfAdder.png -------------------------------------------------------------------------------- /figures/NANDGate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/NANDGate.png -------------------------------------------------------------------------------- /figures/NANDORNOT.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/NANDORNOT.png -------------------------------------------------------------------------------- /figures/NORANDNOT.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/NORANDNOT.png -------------------------------------------------------------------------------- /figures/NOTWaves.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/NOTWaves.png -------------------------------------------------------------------------------- /figures/ORNANDNOT.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/ORNANDNOT.png -------------------------------------------------------------------------------- /figures/XNORGate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/XNORGate.png -------------------------------------------------------------------------------- /figures/final/RAM.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/final/RAM.png -------------------------------------------------------------------------------- /figures/register.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/register.png -------------------------------------------------------------------------------- /figures/2x4Decoder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/2x4Decoder.png -------------------------------------------------------------------------------- /figures/ExpandedMux.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/ExpandedMux.png -------------------------------------------------------------------------------- /figures/RippleCarry.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/RippleCarry.gif -------------------------------------------------------------------------------- /figures/SimpleDemux.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/SimpleDemux.png -------------------------------------------------------------------------------- /figures/SquareWave.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/SquareWave.png -------------------------------------------------------------------------------- /figures/XNORCircuit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/XNORCircuit.png -------------------------------------------------------------------------------- /figures/XORCircuit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/XORCircuit.png -------------------------------------------------------------------------------- /figures/final/ALU-0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/final/ALU-0.png -------------------------------------------------------------------------------- /figures/final/ALU-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/final/ALU-1.png -------------------------------------------------------------------------------- /figures/final/ALU-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/final/ALU-2.png -------------------------------------------------------------------------------- /figures/final/ALU-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/final/ALU-3.png -------------------------------------------------------------------------------- /figures/final/ALU-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/final/ALU-4.png -------------------------------------------------------------------------------- /figures/final/RAM1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/final/RAM1.png -------------------------------------------------------------------------------- /figures/not-mosfet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/not-mosfet.png -------------------------------------------------------------------------------- /figures/CMOS-Inverter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/CMOS-Inverter.png -------------------------------------------------------------------------------- /figures/SRFF-activehigh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/SRFF-activehigh.png -------------------------------------------------------------------------------- /figures/final/ALU-Module.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/final/ALU-Module.png -------------------------------------------------------------------------------- /figures/final/TheCounter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/final/TheCounter.png -------------------------------------------------------------------------------- /figures/SimpleRegisterFile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/SimpleRegisterFile.png -------------------------------------------------------------------------------- /figures/AdditionMachine-Step0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/AdditionMachine-Step0.png -------------------------------------------------------------------------------- /figures/AdditionMachine-Step1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/AdditionMachine-Step1.png -------------------------------------------------------------------------------- /figures/AdditionMachine-Step2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/AdditionMachine-Step2.png -------------------------------------------------------------------------------- /figures/AdditionMachine-Step3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/AdditionMachine-Step3.png -------------------------------------------------------------------------------- /figures/AdditionMachine-Step4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/AdditionMachine-Step4.png -------------------------------------------------------------------------------- /figures/AdvancedRegisterFile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/AdvancedRegisterFile.png -------------------------------------------------------------------------------- /figures/final/Microcontroller.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prp-e/micro-controller-design/HEAD/figures/final/Microcontroller.png -------------------------------------------------------------------------------- /book.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | "download-pdf" 4 | ], 5 | "pluginsConfig": { 6 | "download-pdf": { 7 | "base": "master", 8 | "label": "Download PDF", 9 | "multilingual": false 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | This book is published for free, and everyone can use it as a source for non-commercial, educational and 3 | technical purposes. 4 | And, if you want to read this book, please share it with your friends. This is not a part of license, this is 5 | what author wants you to do. -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # License 2 | 3 | This book is published for free, and everyone can use it as a source for non-commercial, educational and 4 | technical purposes. 5 | And, if you want to read this book, please share it with your friends. This is not a part of license, this is 6 | what author wants you to do. -------------------------------------------------------------------------------- /RESOURCES.md: -------------------------------------------------------------------------------- 1 | # Bibliography 2 | 3 | 1. [But How Do It Know? - The Basic Principles of Computer for Everyone](https://www.amazon.com/But-How-Know-Principles-Computers/dp/0615303765) by **J.Clark Scott** 4 | 2. [Computer Organization and Design, The Hardware/Software Interface, Revised Fourth Edition](https://www.amazon.com/Computer-Organization-Design-Fourth-Architecture/dp/0123747503) by **Davide Patterson**, **John Henessy**, **Morgan Kaufman** 5 | 3. [Logic and Computer Design Fundamentals, Fifth Edition](https://www.amazon.com/Logic-Computer-Design-Fundamentals-5th/dp/0133760634) by **Morris Mano** 6 | 4. [Digital Design: With an Introduction to the Verilog HDL](https://www.amazon.com/Digital-Design-Introduction-Verilog-HDL/dp/0132774208) by **Morris Mano** -------------------------------------------------------------------------------- /chapter06.md: -------------------------------------------------------------------------------- 1 | # Chapter 6 : Combinational Circuits 2 | 3 | In the previous chapter, we saw how we can make logic gates. Of course, the easy way! 4 | In this chapter, we're going to make circuits which are famous as *Combinational*. Because we pick some 5 | well-known gates, and make a circuit out of them. 6 | 7 | ## The Exclusive OR 8 | Do you remember XOR from [chapter four](chapter4.md)? The logical function of XOR was like this: 9 | 10 | ``` 11 | ~AB + A~B 12 | ``` 13 | As you see, we need two NOT gates, two AND gates and one OR gate! Ok, let's make the circuit : 14 | 15 | ![XOR Circuit](figures/XORCircuit.png) 16 | 17 | We also have a gate for XOR, because it's very common in circuits, and in transistor level, it's implemented much simpler. 18 | This is the XOR gate : 19 | 20 | ![XOR Gate](figures/XORGate.png) 21 | 22 | This was the simplest combinational circuit we could ever make. Let's make another one! 23 | 24 | ## The Exclusive NOR 25 | This is another combinational circuit we make in this chapter, the function is almost the same as XOR, but it has a little difference. 26 | 27 | ``` 28 | ~A~B + AB 29 | ``` 30 | 31 | And now, we can make the circuit : 32 | 33 | ![XNOR Circuit](figures/XNORCircuit.png) 34 | 35 | We also have a gate for this function. The gate looks like this : 36 | 37 | ![XNOR Gate](figures/XNORGate.png) 38 | 39 | ## More Logics? 40 | Of course yes! We will make another useful logics in the next chapters. We all studied these six chapters for design and implementation of more logics. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction 2 | 3 | I always wanted to design a micro controller or micro processor, and I managed to model them. Now, I've decided to show people how a micro controller is designed. And, I'm inspired by [But How Do It Know? - The Basic Principles of Computer for Everyone](https://www.amazon.com/But-How-Know-Principles-Computers/dp/0615303765) by **J.Clark Scott**. I've read this book, 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. 4 | 5 | ## Audience 6 | Of course, you need to be geek enough to read this book. I try to simplify topics, but if you have knowledge of logical circuits or micro processors, you'll understand this book easier. 7 | 8 | ## What we learn? 9 | First, you will understand how mankind communicated with machines for years, and the way people communicated with computers is unchanged. Then, you start learning *logical circuits* which is 10 | base of computer architecture and organization. And importance of knowledge of logical circuits is clear, in university (at least all Iranian universities), you should first pass the *Logical Circuits* course, and then 11 | you can take *Computer Architecture* course. After learning logical circuits, we start designing simple circuits and even computers, memory blocks, etc. At the end, we combine everything we designed, and it will become our 12 | computer. Finally, we can program our computer, and we actually understand how a computer can understand human's language. -------------------------------------------------------------------------------- /chapter01.md: -------------------------------------------------------------------------------- 1 | # Chapter 1 : What's a micro controller? 2 | A microcontroller is a small computer, and usually *System on the Chip* (SoC), which include a processor core, memory unit and programmable input/output unit. The most famous family of microcontrollers is **AVR** or **Advanced Virtual RISC** family. This family of microcontrollers are used in boards such as [Arduino](http://arduino.cc), and they're used for industrial and educational purposes. A microcontroller, can control a toy car, and it also can control a US army drone. And this depends on microcontroller type, and the program we write for it. 3 | ## What does a micro controller consist of? 4 | 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. 5 | The process core is usually made up of : 6 | 7 | 1. **Arithmetic and Logical Unit**, also known as ALU. This unit, does every logical and Arithmetic operation, and it's the most important part of a microcontroller. 8 | 9 | 2. **Data Bus**, this part is simply a bunch of wires which transfers data between to parts of process core. 10 | 11 | 3. **Registers** are small memory blocks, and they can store data temporarily and transfer them. We always need them, because they're our temporary memory blocks and while we run a program, we need to store inputs and outputs. 12 | ## But, How do it know?! ... 13 | OK, now let's talk about **How does even a computer know what we want?** . The answer is simple, for example, when we write this code for x86 family : 14 | ```assembly 15 | MOV DX, OFFSET MSG 16 | MOV AH, 09 17 | INT 21H 18 | ``` 19 | and we assemble and run the code, it shows us a message (for example `Hello`). And computers can understand their owners by programs, let's go preciser. Imagine you travel Iran, and of course you can understand Iranians by speaking Persian. And Iranians can communicate with you in Persian. So, if we consider computer a foreigner, the assembly language (and in better word, machine code) is its 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! 20 | -------------------------------------------------------------------------------- /chapter12.md: -------------------------------------------------------------------------------- 1 | # Chapter 12 : The Computer (Theory) 2 | 3 | As we learned some theory and definitions in [chapter ten](chapter10.md), in this chapter we're going to continue 4 | our ***Theory of Design***. A computer without a strong theory behind it, is like a car without engine! This is the 5 | saddest truth about computer engineering! If you can't present a good documentation of you design or build, you'll fail! 6 | To avoid fails, we learn how to design a computer (theory phase) in this chapter! 7 | 8 | ## The Instruction Set 9 | 10 | Every computer, has an ***Instruction Set***. The way instrucions are implemented, is called ***Instruction Set Architecture*** or 11 | in short, ***ISA***. We need to design one for our dear computer, of course! A computer without ISA, is really impossible! There are a few 12 | tips we should follow in our design of ISA : 13 | 14 | * As we want to design a RISC computer, we need to simplify every instrucion we need. For example, a NOR gate can be designed 15 | with an AND gate with inverted inputs, So we don't need to use both NOR and AND gates! 16 | * We should document every step of our design, because it's our ***Computer Organization***. And in final product, that's important to 17 | be mentioned! 18 | * The last tip is that we need to model everything we designed, later, we learn more about modeling. 19 | 20 | ### What Instructions we need? 21 | 22 | As a *real* computer, we need a computer which can handle at least one **logical** and one **arithmetic** Instruction. So, We can make a computer with these instructions : 23 | * AND 24 | * OR 25 | * NAND 26 | * NOR 27 | * ADD (Addition) 28 | * SUB (Substract) 29 | 30 | So, we need to design a unit which can handle *Arithmetic and Logical* instructions. we call this unit ***Arithmetic and Logical Unit*** or ***ALU***. 31 | 32 | ## Computer Organization 33 | 34 | This is the most difficult part, In this part you will learn how to think like an engineer! We know, Addition and Subtraction commands are 35 | implemented by XOR and AND and NOT gates. But, what about NAND and NOR? We can implement NOR using an AND gate like this : 36 | 37 | ![NOR Using AND](figures/NORANDNOT.png) 38 | 39 | Also, we can implement NAND, using an OR gate like this : 40 | 41 | ![NAND Using OR](figures/NANDORNOT.png) 42 | 43 | ## Memory Unit 44 | 45 | I think, using a ***Read Only Memory*** or ***ROM*** is a good idea. But it's actually not! Because it can be programmed easily. So, we need another memory 46 | block, which is known as ***Random Access Memory*** or ***RAM***. In this book, I never detail how to make a RAM or ROM, it makes this book too hard to understand 47 | for people who have no idea about RAM or ROM. So, we know what kind of memory we have. 48 | 49 | ## Starting Implementation 50 | In this chapter, we took a look on the theory side of designing a computer. But, as engineers, we need to join the darker side, and start implement 51 | what we need. In the next chapter, We'll start design of our ALU, then start to connect other things we need to it, it'll be our awesome computer! 52 | 53 | -------------------------------------------------------------------------------- /chapter05.md: -------------------------------------------------------------------------------- 1 | # Chapter 5 : Logical Circuits 2 | Do you remember two logics **NAND** and **NOR** ? These logics are called *Universal Logic*, because 3 | in digital electronics, we make all other logics using these two logics! The smallest part of a logical circuit, is 4 | called a **gate**. each gate is a specific arrangement of transistors. For example, this is a *NOT gate* using MOSFET 5 | transistors : 6 | 7 | ![Figure 1 - NOT Gate using transistors](figures/not-mosfet.png) 8 | 9 | I picked this picture from one of my old projects, and in this book, we won't use any transistor, we just use symbolic schematics 10 | of logic gates to design logical circuits! 11 | 12 | ## The NAND 13 | Although we can make all logics using NOR, I prefer using NAND. This is just my personal opinion, and after reading and understanding 14 | the previous chapter and this chapter, you can make all of these logics using NOR. This is what I call "The magic of boolean algebra". 15 | First, we need to know how NAND works! It works like a key with two switches, and when to switches are off, the output will be on! This is the simplest 16 | definition of NAND. When we want to show it on circuit, we use this shape : 17 | 18 | ![Figure 2 - NAND Gate](figures/NANDGate.png) 19 | 20 | A D-shaped thing with a bubble at the end. This is NAND! You know how it works, because you read the [chapter four](chapter4.md) and you learned what are these functions! 21 | 22 | ### NOT Gate 23 | The NOT gate is another simple and basic gate, you need to know. It's built using a NAND Gate like this: 24 | 25 | ![Figure 3 - NOT using NAND Gate](figures/NOTNAND.png) 26 | 27 | Did you see how it works? Yes! We simply connect two inputs of a NAND gate to a switch. The NOT gate in general, is represented like this : 28 | 29 | ![Figure 4 - NOT Gate](figures/NOTGate.png) 30 | 31 | ### AND Gate 32 | As you remember from the previous chapter, we made NAND function by adding a NOT to AND. So, we know `~(~A) = A `. This means we can add a NOT gate in output of NAND, 33 | and get AND function. Just like this : 34 | 35 | ![Figure 5 - AND Gate using NOT and NAND](figures/ANDNAND.png) 36 | 37 | But in reallity, if you remove the bubble from NAND gate, you will have AND : 38 | 39 | ![Figure 6 - AND Gate](figures/ANDGate.png) 40 | 41 | ### OR Gate 42 | This is the last of these gates! Yes, this is the last, because we will design exclusive logics in next chapters, so the OR gate is the 43 | last gate we will know here! If we apply two NOT gates in the inputs of a NAND gate, it'll become an OR gate. like this : 44 | 45 | ![Figure 7 - OR using NAND and NOT](figures/ORNANDNOT.png) 46 | 47 | But, this is the actuall OR : 48 | 49 | ![Figure 8 - OR Gate](figures/ORGate.png) 50 | 51 | ## Now, we're ready! 52 | Actually, when you know how to represent logic gates, and you know how their functions work, you can design and implement logical circuits. A computer 53 | is much simpler than you think, and the hard part in design and implementation of a computer, is the correct usage and combination of logics. In next chapter, 54 | we design the simplest combinational circuits, and then we start desgining bigger ones. -------------------------------------------------------------------------------- /chapter09.md: -------------------------------------------------------------------------------- 1 | # Chapter 9 : Register File 2 | 3 | In previous chapter, we made 1 bit flip flops, and connected them together, then we had registers. Registers are needed, 4 | but we can't say "We are great computer engineers because we have registers!". Registers have special organization in a computer, 5 | and that's called ___Register File___. In this chapter, we will design a simple one! 6 | 7 | ## The Decoder 8 | Let's make another combinational circuit here. The circuit we are making here, is called ***Decoder*** . 9 | Decoders have *n* selectors, and `2^n` outputs. This is why they're decoders! You give them a binary number as 10 | input and you take a hexadecimal number in the output. Of course, you never get the *exact* hex number at the output, 11 | but you can find hexadecimal notation of input by using a decoder. Let's make one! The simplest decoder we make is a 12 | 2x4 decoder. According to `2^n`, we can also make 1x2 decoder, but it's not actually a real decored. It's an AND gate! 13 | The logical function of a 2x4 decoder is like this : 14 | ``` 15 | S0 = ~A~B 16 | S1 = ~AB 17 | S2 = A~B 18 | S3 = AB 19 | ``` 20 | So, we can implement our decoder like this : 21 | 22 | ![2x4 Decoder](figures/2x4Decoder.png) 23 | 24 | But, how we use this in a *Register File* ? 25 | 26 | ## Simple Register File 27 | We have a 2x4 decoder. So, for now we can make a simple register file with four registers. The outputs of decoder, 28 | will be connected to *Enabler* pin of registers. Just like this : 29 | 30 | ![Simple Register File](figures/SimpleRegisterFile.png) 31 | 32 | This is actually not a good design, it can generate a lot of noises, so we need another device, which allows us to select 33 | one of outputs! 34 | 35 | ## The Multiplexer 36 | You know, we need a device which acts like a decoder, but it does a selection among input data lines. This device is called 37 | a *Multiplexer*. In this book, We call it **Mux**. A mux can be implemented using a decoder, and a bunch of AND/OR gates. Like this : 38 | This is a simple mux : 39 | 40 | ![Basic Mux](figures/BasicMux.png) 41 | 42 | For our register file, we need a mux which can handle 8 bits input and output. So, I connect 8 muxes together, and I'll have 43 | a big mux like this : 44 | 45 | ![Expanded Mux](figures/ExpandedMux.png) 46 | 47 | Now, we can go back and complete our register file. 48 | 49 | ## Advanced Register File 50 | 51 | Now, we add a mux to the register file we designed, so we get this : 52 | 53 | ![Advanced Register File](figures/AdvancedRegisterFile.png) 54 | 55 | S0 and S1 are selectors for *moving* data to registers. we call that situation **Store**. And R0 & R1 help us 56 | *read* data from registers. This is what we call **Load**. We can claim that we have a ***Load/Store Architecture***. 57 | 58 | ## Ready for Architecture! 59 | ***Computer architecture*** is not only engineering. It includes mathematics, philosophy, analysis, etc. We need to combine 60 | all of them, to design an architecture. In next chapter, we will take a look on the philosophy behind computer architecture. 61 | Then, we will start design and implementation of our microprocessor. -------------------------------------------------------------------------------- /chapter10.md: -------------------------------------------------------------------------------- 1 | # Chapter 10 : Computer Architecture 2 | 3 | This chapter is only theory of computer architecture. Actually, we need to know this part before we can start design 4 | and implementation. There are a lot of concepts we should know. In this chapter, I tried to cover the most important 5 | concepts. 6 | 7 | ## Computer Architecture 8 | 9 | The term *architecture* usually used for everything engineers build, and it means all engineers need to know the architecture 10 | of what they made. *Computer Architecture* is simply operations that a computer can do. Do you remember our ***Addition Machine***? 11 | That computer had a simple architecture, it could add two numbers, and if one of inputs was negative (according to the operator of computer, not computer itself!) 12 | it could help us subtract. That's nice, isn't that? So ***All instructions and operations of a computer is its architecture***. This is 13 | the only concept you actually feel when you are a *user*. When you are *engineer* you need to know more concepts. 14 | 15 | ## Backward Compatibility 16 | 17 | Backward compatibility is the most important theory in computer architecture and organization. In 70's, *Intel* made 8085 processor. 18 | Later, they made 8086. Let's see how they implemented Backward compatibility! Imagine a program written for 8085, 8086 must support and 19 | execute that. Every newer designs, should be compatible with older ones. For now, I have a laptop with *Intel Core i5* processor, and I can 20 | execute Windows XP on it. This is Backward compatibility. 21 | 22 | ## Computer Organization 23 | 24 | You know what *computer architecture* means, but what about organization? computer organization, is a level before architecture. In architecture, we just 25 | analyze computers instructions, and after analyzing it, we can tell ***what instructions this computer can do.***. But, the **structure of instructions* 26 | is not revealed yet! When we start studying structure of instructions, we actually study computer organization. 27 | 28 | So, when we say *our computer can multiply*, we just talked about its architecture. But, when we say *our computer includes a multiplier, which is made up of adders* , 29 | we spoke about its organization. So, when we are talking about *what a computer can do* we are speaking about architecture. But, when we start talking about *how instructions are 30 | implemented* , we are talking about organization. 31 | 32 | ## Complex or Reduced? This is the question 33 | 34 | A computer is measured by number of its instructions, so, when this number is less than 100, we call the design ***Reduced Instruction Set Computer*** or in short, 35 | **RISC**. When we have more than 100 instructions in a computer, we call that ***Complex Instruction Set Computer*** or **CISC**. The computer we design in this book, will be 36 | a RISC, because RISCs are easy to study, learn, implement and understand. Also, RISCs are faster than CISCs. 37 | 38 | ## Decisions! 39 | In [chapter two](chapter2.md), we decided to design a computer with the word size of 8 bits. So, we need to make decisions about its architecture and organization. 40 | Next chapter, will be about our design, and then, we start making our computer! 41 | 42 | 43 | -------------------------------------------------------------------------------- /chapter11.md: -------------------------------------------------------------------------------- 1 | # Chapter 11 : Design, Advanced Addition Machine! 2 | 3 | In [chapter seven](chapter7.md) we designed the must simple (and almost useless) computer, *The Addition Machine*. Now, 4 | we need to have a background of design and implementation of complex machines, so we add some useful features to our addition 5 | machine. Our machine had no memory blocks and we couldn't save our inputs and outputs. In this chapter, we will learn how to 6 | add memory blocks to our addition machine. 7 | 8 | ## Managing inputs 9 | Remember our [Register File](chapter9.md)? For now, I want to store my inputs in a register, and then, read from those registers. 10 | If you look at your simple (or scientific) calculator, you will see a button labled *M* or *M+*. That button is used for inserting 11 | the inputs or results in calculator's memory. So, I want to make a memory button for our ***Addition Machine***. Our circuit will be 12 | like this : 13 | 14 | ![Addition Machine - Step 0](figures/AdditionMachine-Step0.png) 15 | 16 | This is a good design now, but no! We can make it better, but that's enough for an ***Advanced Addition Machine***. 17 | 18 | ## A new device? 19 | As we want only *one* register file, we need a multiplexer in the input. It looks like this : 20 | 21 | ![Addition Machine - Step 1](figures/AdditionMachine-Step1.png) 22 | 23 | So, for new system, we only waste four bits for control. But, we need a device which can help us define the path of our 24 | data! A device which can be used for unicast (only one direction), multi-cast (more than one direction) and broadcast (sending data to all directions)!. 25 | 26 | ### Demultiplexer 27 | 28 | A Demultiplexer acts like a backward multiplexer. With a multiplexer, you choose one of data lines, but with a demultiplexer, you 29 | can send a single data line to a certain destination. The simplest one you can make, is a combination of NOTs and ANDs, like this : 30 | 31 | ![Simple demux](figures/SimpleDemux.png) 32 | 33 | The input line, is connected to input of all ANDs, and *S*, which is our selector, connected with a NOT to first AND, and without NOT to 34 | second one. This means, with applying changes in selector(s) we can send our data to different lines! Let's add some deumx's to our 35 | ***Advanced Addition Machine***. 36 | 37 | ## Selection! 38 | 39 | Now, we need to add an 8 bit demux to our Addition Machine. The input lines will look like this : 40 | 41 | ![Addition Machine - Step 2](figures/AdditionMachine-Step2.png) 42 | 43 | Now, we can select A or B inputs of the Adder! Let's add our adder : 44 | 45 | ![Addition Machine - Step 3](figures/AdditionMachine-Step3.png) 46 | 47 | This doesn't *add* anything, because we only switch our inputs? what's our final solution? 48 | 49 | ## Temporary Registers 50 | 51 | As you may find, there's no addition in our ***Advanced Addition Machine***. So, we can add two registers to our addition machine, and those are called 52 | ***Temprorary Registers***. Our machine, with temprorary registers will look like this : 53 | 54 | ![Addition Machine - Final Step](figures/AdditionMachine-Step4.png) 55 | 56 | ## Let's go! 57 | 58 | Now, we designed a simple computer which can work, and it can help us make bigger machines or devices. But, in next chapter, we 59 | start our design of a real micro-processor. The future chapters, help you understand architecture and organization of the computers 60 | which we use everyday. -------------------------------------------------------------------------------- /chapter08.md: -------------------------------------------------------------------------------- 1 | # Chapter 8 : Memory 2 | 3 | Although our *Addition Machine* was a complete computer, but actually, a computer without memory is like a car without seats. You know a car can be driven 4 | without seats, but we add seats, because seats can keep passengers! And this is the point! We want a certain space for keeping data. According to the 5 | [chapter six](chapter6.md), we know that a *combinational circuit* is a circuit which can solve a logic/arithmetic problem without storing the results. 6 | In this chapter, we are going to study a new family of circuits, which are called **Sequential Circuits**. 7 | 8 | ## Everything is NAND! 9 | You need NAND, even here. Of course, we need to know how NAND works and how can make memory blocks only using NAND. The simplest memory block is this : 10 | 11 | ![S-R flip flop](figures/SRFF.png) 12 | 13 | This is called an **active-low S-R flip flop**. If we replace NAND gates with NOR, we will have an **active-high S-R flip flop**. So, as all of the circuits of this book are 14 | active-high, let's see active-high version of our flip flop : 15 | 16 | ![Active High SR flip flop](figures/SRFF-activehigh.png) 17 | 18 | As you can see, `Q` and `~Q` are replaced in the new circuit. It doesn't matter what kind of flip flop you use, but I actually prefer the active-high one. 19 | 20 | ## Basic Improvement 21 | 22 | Consider that lovely NAND based implementation of SR, and if we modify that like this : 23 | 24 | ![SR with enable](figures/ClockedSR.png) 25 | 26 | I added two gates, and one pin. The new pin is called **enable**. It enables the circuit, that means I actually need to turn it on for applying 27 | all changes in the input! The new circuit is called *SR flip-flop with enable*. Later, we will call that **Clocked SR**. 28 | 29 | ## Register 30 | We can consider this flip flop as *one bit memory block* or *one bit register*. But, as we decided in [chapter two](chapter2.md), we want 8 bits registers. 31 | So, we need to connect eight memory blocks together! How is it possible? We just need to pick eight of them, then connect a common enable and reset button to them. 32 | And the input (S pins) will be parallel. The schematics of a register, is like this : 33 | 34 | ![SR based register](figures/8bitSR.png) 35 | 36 | This is the simplest register we can make, but we need a better design for our flip flops to prevent noises and oscillations! So, we don't use this kind of register in our 37 | computer! 38 | 39 | ## The new flip-flop 40 | As we tested the S-R one, we found that S-R can't handle noises, and oscillations. This means we will have a lot of ___meta-stable___ conditions. 41 | As a good solution, we can use this kind of flip-flop : 42 | 43 | ![D flip flop](figures/ClockedD.png) 44 | 45 | Now, we connect 8 bits to D inputs, and one Enabler to all E's we have. finally, we'll get this : 46 | 47 | ![D-based register](figures/Dregister.png) 48 | 49 | ## The final register! 50 | The schematics of a register, is just like this : 51 | 52 | ![Register](figures/register.png) 53 | 54 | It has 8-bit *data-in* or ___Din___ input and also *data-out* or ___Dout___ output. Also, it has an enabler, which is shown by *E*. 55 | 56 | ## What we need now? 57 | Now, we need some organization for _memory management_. Also, we need to see how memory blocks work in action. In next chapters, we will add 58 | some memory blocks to our *Addition Machine* and see how it works, then we design more memory devices for our micro-controller. -------------------------------------------------------------------------------- /chapter15.md: -------------------------------------------------------------------------------- 1 | # Chapter 15 : Microcontroller 2 | In this chapter, we make our dear computer! You've studied previous chapters to learn how to make your very own 3 | computer! Actually, the most important part was *ALU*. In this chapter, we will design a simple and maybe stupid 4 | *Control Unit* and we add a *RAM* from logisim standard library. Then, we can program it! Let's know how we can 5 | do this! 6 | 7 | ## Control Unit 8 | This one, is a bit harder to understand. But don't worry. We're just going to make the simplest ones. We will take 9 | a deeper look on this unit in following chapters (and maybe books!) but for this book, we make a simple one. Let's first 10 | define this unit. Control unit, controls programs loaded in our main memory, and controls what happens in ALU. Actually, 11 | control unit is made up of a device which is called *counter*. A counter is a register , or a bunch of registers which 12 | can count! How? the counting process is controlled by *clock pulse*. But why is it important? A counter can store 13 | address of a program, then its output can be input of the ***Address Bus*** of the RAM. This is the simplest use of 14 | a counter in a computer. But how can we make a counter? we can connect four D flip flops in this arrengement and make 15 | our counter : 16 | 17 | ![The Counter](figures/final/TheCounter.png) 18 | 19 | It has a common *reset* pin, and a clock input. Also, it has a 4-bit output labled *Y*. This is the simplest binary counter 20 | we can make. As it has four bits, it will count from 0 to 15 for us. This means we need a RAM block with 4-bit address bus. 21 | An actual control unit is not that simple, usually it includes some decoders, multiplexers and AND gates to control and activate/deactivate 22 | computer parts. But for our simple programmable computer, this one is enough!. 23 | 24 | ## Memory Unit 25 | We made a counter, which counts address for us. But, which address? Actually, we need a memory block like a RAM. If we look at RAM simply, 26 | it's very similar to register file. But, an actual and functional RAM is much more different. Let's add the RAM : 27 | 28 | ![RAM](figures/final/RAM.png) 29 | 30 | Now, we can store our programs in RAM! But wait, if you look closer at the picture above, you'll find that RAM is not connected 31 | to the clock. So it won't work. This is what you actually need : 32 | 33 | ![RAM](figures/final/RAM1.png) 34 | 35 | Now, let's make the microcontroller! 36 | 37 | ## Combination of Things! 38 | We made a bunch of devices, connected them together and made new devices, and finally, we made ALU. Let's connect the ALU 39 | to RAM. Remember this? 40 | 41 | | Instruction Code | Operand A | Operand B | 42 | |:----------------:|:---------:|:---------:| 43 | | 4 bits | 8 bits | 8 bits | 44 | 45 | And now, we need a special device which is called *Splitter* to split parts we need. Then, we will need smaller splitters 46 | to split instruction code to bits. If you remember, we had this table for instruction codes : 47 | 48 | |Ainvert|Bnegate| S2 | S1 | Instruction | 49 | |:-----:|:-----:|:--:|:--:|:-----------:| 50 | | 0 | 0 | 0 | 0 | AND | 51 | | 0 | 0 | 0 | 1 | OR | 52 | | 1 | 1 | 0 | 0 | NOR | 53 | | 1 | 1 | 0 | 1 | NAND | 54 | | 0 | 0 | 1 | 0 | ADD | 55 | | 0 | 1 | 1 | 0 | SUB | 56 | 57 | let's connect them together! And now, we have this : 58 | 59 | ![Microcontroller](figures/final/Microcontroller.png) 60 | 61 | ## What should we learn now? 62 | After reading these fifteen chapters, you learned how to make your very own computer in gate-level. Now, you can learn programming 63 | and program your computer and test it, or you can learn digital electronics and make it in transistor-level. In following chapter, 64 | we will take a quick look at *assembly* language of our microcontroller, and we try to make some programs we need. It will help you 65 | understand how your real computer works! -------------------------------------------------------------------------------- /chapter07.md: -------------------------------------------------------------------------------- 1 | # Chapter 7 : The First Computer 2 | 3 | Now, you know logic and arithmetic, two basics of computers. Of course, every device with the ability of solving logical and arithmetic 4 | problems, is a computer! Humans are computers, calculators are computers, and that expensive (and almost useless) iPhones are computers, too. 5 | In this chapter, we just make the simplest computer. We need this computer in near future, as a part of other computer. That's interesting, isn't that? 6 | 7 | ## The Function 8 | When we decide to design a device, we need to define its function. For example, imagine a mechanical engineer who wants to design an engine, but 9 | he doesn't describe its function! So, no one will buy that engine, because no one knows how it works or how it's made, or what's its function! 10 | As a good and practical design, I considered **Addition Machine**. It's a simple calculator (or computer) we can design using a few gates. 11 | 12 | ### Boolean Algebra! 13 | We learned boolean algebra in [chapter four](chapter4.md) and for now, we just see it in action! For designing an *Addition Machine* , we need this simple function : 14 | 15 | ``` 16 | Sum = ~AB + ~BA 17 | Carry = AB 18 | ``` 19 | Did you find the point? we just need a XOR and an AND gate! 20 | 21 | ## The Half Adder 22 | Imagine this circuit, which is the implementation of the function in previous part : 23 | 24 | ![Half Adder](figures/HalfAdder.png) 25 | 26 | The truth table for this circuit will be : 27 | 28 | | H.A | A | B | Carry (C) | Sum (S) | 29 | |-----|:---:|:---:|:----------:|:-------:| 30 | | | 0 | 0 | 0 | 0 | 31 | | | 0 | 1 | 0 | 1 | 32 | | | 1 | 0 | 0 | 1 | 33 | | | 1 | 1 | 1 | 0 | 34 | 35 | It makes 0, 1 and 2 for us! It's another interesting thing we can design and implement! but, it's not complete yet! Why? It does not make 3 for us, it has 2 inputs but not 4 outputs! 36 | So, we still need some improvements on the circuit! 37 | 38 | ## The Full Adder 39 | Half adder is good, but it's not everything. Of course, it can't help us make bigger adders, so we need to connect two half adders, and make a new adder wich is called 40 | a **full adder**. A full adder, can actually make all expected outputs for us. This is the full adder : 41 | 42 | ![Full Adder](figures/FullAdder.png) 43 | 44 | Truth table of a full adder is like this : 45 | 46 | | F. A | Carry-in | A | B | Carry(Cout) | Sum(S) | 47 | |------|:--------: |:---:|:---:|:-----------:|:------:| 48 | | | 0 | 0 | 0 | 0 | 0 | 49 | | | 0 | 0 | 1 | 0 | 1 | 50 | | | 0 | 1 | 0 | 0 | 1 | 51 | | | 0 | 1 | 1 | 1 | 0 | 52 | | | 1 | 0 | 0 | 0 | 1 | 53 | | | 1 | 0 | 1 | 1 | 0 | 54 | | | 1 | 1 | 0 | 1 | 0 | 55 | | | 1 | 1 | 1 | 1 | 1 | 56 | 57 | Now, we have ability of making a big adder! And that big adder will be our dear *Addition Machine*. 58 | 59 | ## Ripple-Carry adder 60 | The full adder we've designed is actually one bit. If we want to design a computer with the word size of one bit, we can consider that 61 | full adder as a simple computer. Now, as you remember from [chapter two](chapter2.md), we decided to make a microcontroller with word size 62 | of eight bits! So, I want to make a 8-bit adder. But How?! 63 | 64 | As you can see, each full adder has a carry-in and a carry-out pin, what we need is connecting 8 full adders together, and we need these pins. 65 | If we put one adder , and connect its carry-out to next one's carry-in, then I'll get a *Ripple-Carry adder*. A ripple-carry adder looks like this : 66 | 67 | ![Ripple-Carry adder](figures/RippleCarry.gif) 68 | 69 | Not only 8 bits, we can make the adder with a custom word-size, according to the design of ripple-carry adder. Congratulations! You made your first computer! 70 | 71 | ## Let's talk about computers! 72 | In this chapter, we just designed and implemented a simple computer known as a *Ripple Carry Adder* or *Addition Machine*. It's a complete computer with a single instruction, but 73 | of course it's not functional yet. Because we didn't design a memory unit, a simple control unit, etc. In next chapter, we will add some memory blocks to this computer and then, we start 74 | designing a complete and **functional** computer. -------------------------------------------------------------------------------- /chapter16.md: -------------------------------------------------------------------------------- 1 | # Chapter 16 : Programming and Operating System 2 | The most interesting part of computer engineering is the programming! Specially when you know how it works, 3 | you can make better programs. Now, we designed one, and we want to program it. But wait, let's take a look at 4 | computers made before. 5 | 6 | ## Intel computers! 7 | As you may remember from previous chapters, we had this for showing a simple character on console: 8 | 9 | ```assembly 10 | STK SEGMENT 11 | DW 32 DUP(?) 12 | STK ENDS 13 | 14 | CDS SEGMENT 15 | ASSUME CS:CDS, SS:STK 16 | MAIN PROC FAR 17 | MOV DL, 'A' 18 | MOV AH, 02 19 | INT 21h 20 | MOV AH, 4Ch 21 | INT 21h 22 | MAIN ENDP 23 | CDS ENDS 24 | 25 | END MAIN 26 | ``` 27 | And in [chapter fourteen](chapter14.md), we learned that this code will become ***Machine Code***. And now, 28 | we can assemble our own code, and write programs for that. 29 | 30 | ## The assembler 31 | Assembler is actually a software, used to generate machine code. Days we had no assembler, we had to generate machine code 32 | ourselves. This is hard, isn't it? But that's the only way when you have no operating system or other computer to assemble your code. 33 | Actually, a computer uses *Assembler* , then *Linker* to understand the programs. But in case of simple computers like ours, a simple assembler 34 | is enough. We just want to fill RAM blocks. The assembler sees our code. For example : 35 | 36 | ``` 37 | AND 10, 12 38 | ``` 39 | 40 | and then, as we have two 8 bit inputs, it converts the 10 to `000010101` and 12 to `00001100`. And it will look at this table : 41 | 42 | |Ainvert|Bnegate| S2 | S1 | Instruction | 43 | |:-----:|:-----:|:--:|:--:|:-----------:| 44 | | 0 | 0 | 0 | 0 | AND | 45 | | 0 | 0 | 0 | 1 | OR | 46 | | 1 | 1 | 0 | 0 | NOR | 47 | | 1 | 1 | 0 | 1 | NAND | 48 | | 0 | 0 | 1 | 0 | ADD | 49 | | 0 | 1 | 1 | 0 | SUB | 50 | 51 | and finds that code for AND is `0000`. Then generates this `0x00A0C` as the final code! Let's write long numbers in hexadecimal. 52 | When we use hex, a 20-bit number will be a 5 digits number. So, we are the assembler in this case. Let's write our codes : 53 | 54 | ``` 55 | AND 10, 12 56 | # Hex : 0x00A0C 57 | 58 | ADD 3, 8 59 | # Hex : 0x20308 60 | 61 | SUB 10, 8 62 | # Hex : 0x60A08 63 | ``` 64 | 65 | And this is a simple program in the machine language : 66 | 67 | ``` 68 | 0000 00001010 00001100 69 | 0010 00000011 00001000 70 | 0110 00001010 00001000 71 | ``` 72 | Yes, computer only understands one and zero (please re-study [chapter two](chapter2.md), if you forgot this.) and we need to generate 73 | a bunch of ones and zeros to make a computer work! But, computers actually need a good interface between the system and the user. What's that? 74 | 75 | ## The operating system 76 | Operating System, is the interface! It connects the system to the user. Operating System is a software, which lets other software be installed 77 | and ran on your computer. If you have a PC in home, it may run Windows or Linux as the operating system. If you have a Mac, it may run macOS. 78 | And if you have an iPhone, it runs iOS. There are a lot of operating systems in the world, but a few operating systems are usable. Why? because 79 | other ones are only made for a special purpose. For example, MINIX is made for educational purposes. If you want to learn how UNIX works, you can 80 | study MINIX. But operating systems such as Linux, Windows or macOS are general-purpose. They're made to be the interface.And now you may ask , 81 | will we write an operating system for our computer? Sorry, No! If you pay attention to our assembly code, you will find this line : 82 | 83 | ``` 84 | MOV AH, 4Ch 85 | INT 21h 86 | ``` 87 | The part including `INT` is actually an interrupt. We have no interrupts in our computer. This is the first problem. We also have no loops in our 88 | computer, in instruction set, we didn't define anything to make a loop and any recursive structure. 8086 and many other *real* processors, usually 89 | have instructions like `branch` or `jump`, and those instructions help us make real software, including operating systems! So, we never write operating 90 | system for our little computer. 91 | 92 | ## Making a real computer? 93 | Now, you know a lot about computer architecture and organization. Also, you can make and program a computer in gate-level. This is necessary, trust me. 94 | You always need to know how computers work, otherwise you won't be able to make software. But I'm sure you're curious about real hardware design. The next 95 | chapter (and the last one!) is about that. You will learn how companies design and produce their own hardware. -------------------------------------------------------------------------------- /chapter04.md: -------------------------------------------------------------------------------- 1 | # Chapter 4 : Logical Operations 2 | 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. 3 | 4 | ## How many logical operations we have? 5 | We have some simple logics, and we review all of them here, and we solve some simple problems, and create new logics. 6 | ### NOT 7 | 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! 8 | 9 | |NOT| A | ~A | 10 | |---|:---:|---:| 11 | | | 0 | 1 | 12 | | | 1 | 0 | 13 | 14 | We show `NOT A` in this form : 15 | 16 | ``` 17 | ~A 18 | ``` 19 | 20 | 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. 21 | ### AND 22 | This operation, is very simple, but has two logic inputs. The table of AND is like this : 23 | 24 | |AND| A | B | Out | 25 | |---|:---:|:---:|----:| 26 | | |0 |0 |0 | 27 | | |0 |1 |0 | 28 | | |1 |0 |0 | 29 | | |1 |1 |1 | 30 | 31 | We show `A AND B` in our logical notation like this : 32 | 33 | ``` 34 | AB 35 | ``` 36 | 37 | ### OR 38 | 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 : 39 | 40 | |OR | A | B |Out | 41 | |---|:---:|:---:|---:| 42 | | |0 |0 |0 | 43 | | |0 |1 |1 | 44 | | |1 |0 |1 | 45 | | |1 |1 |1 | 46 | 47 | One of operations needs to be **True**, and it makes whole answer true. 48 | And, we show `A OR B` like this : 49 | ``` 50 | A + B 51 | ``` 52 | ## Truth Table 53 | You saw, we used a table, which includes inputs and outputs, and also operation. This table is called **Truth Table**. 54 | 55 | ## Let's play a game! 56 | So, let's combine some logics and make new ones! The simplest ones are : 57 | ### NAND 58 | It means : 59 | ``` 60 | ~(AB) 61 | ``` 62 | It's `NOT(AND(A, B))` in a simple word. And we draw truth table like this : 63 | 64 | |NAND| A | B | Out | 65 | |----|:---:|:---:|----:| 66 | | | 0 | 0 | 1 | 67 | | | 0 | 1 | 1 | 68 | | | 1 | 0 | 1 | 69 | | | 1 | 1 | 0 | 70 | 71 | ### NOR 72 | It means : 73 | ``` 74 | ~(A+B) 75 | ``` 76 | It's `NOT(OR(A, B))` in a simple word. The truth table is like this : 77 | 78 | |NOR | A | B | Out| 79 | |----|:---:|:---:|---:| 80 | | | 0 | 0 | 1 | 81 | | | 0 | 1 | 0 | 82 | | | 1 | 0 | 0 | 83 | | | 1 | 1 | 0 | 84 | 85 | Is there any more logics? of course yes! But, we will have a view on two others, in this chapter. 86 | 87 | ## Complex Logics 88 | There are to other logics, which are made from other logics, I would like to call them "Complex". Because They're not as simple as NAND or NOR. 89 | Also, we can call them "Exclusive Logics". This is what other engineers called them. 90 | 91 | ### Exclusive OR 92 | This logic, is implemented like this : 93 | ``` 94 | ~AB + A~B 95 | ``` 96 | So, we will have a truth table like this : 97 | 98 | |XOR | A | B |Out | 99 | |----|:---:|:---:|---:| 100 | | | 0 | 0 | 0 | 101 | | | 0 | 1 | 1 | 102 | | | 1 | 0 | 1 | 103 | | | 1 | 1 | 0 | 104 | 105 | ### Exclusive NOR 106 | 107 | This logic is the same as XOR, but with a little difference! If you apply a NOT function to XOR, you'll get XNOR. But, the best implementation of XNOR is this function : 108 | 109 | ``` 110 | ~A~B + AB 111 | ``` 112 | 113 | And we'll get this truth table : 114 | 115 | |XNOR | A | B | Out | 116 | |---- |:---:|:---:|---: | 117 | | | 0 | 0 | 1 | 118 | | | 0 | 1 | 0 | 119 | | | 1 | 0 | 0 | 120 | | | 1 | 1 | 1 | 121 | 122 | ## The journey to computer architecture! 123 | Now, we know logics, and we need to learn about **Logical Circuits**, which are representation of these logics in computer science and electronics. In next chapter, we will learn how to use and design simple logical circuits, and 124 | then, we start designing and implementing our dear micro-controller. Of course you needed these chapters to learn the computer language, but after learning the language, you need to know how a computer is built! -------------------------------------------------------------------------------- /chapter14.md: -------------------------------------------------------------------------------- 1 | # Chapter 14 : Program Structure 2 | 3 | In previous chapter, we built the most important part of our microcontroller. In this chapter, we will take a look 4 | on the theory side of ***programs*** and we will define how our computer will understand programs. This is important indeed, 5 | because we need to write a simple *assembler* for our computer which can generate *machine code* for us. 6 | 7 | ## Programming for a typical computer 8 | You have a typical computer, which is compatible with `Intel x86 Family`. At least more than %90 of computers around me are like that. 9 | So, I can write this piece of art and run it on my own computer, yours, my friend's, etc. 10 | 11 | ```c++ 12 | #include 13 | 14 | using namespace std; 15 | 16 | int main(){ 17 | cout << "Piece of Art!\n"; 18 | return 0; 19 | } 20 | ``` 21 | 22 | But wait! If I install a C/C++ compiler on my mobile phone, tablet or gaming console, it will work! Why? The answer is easy. 23 | We write *programs* for all devices, everyone can use our program. In case of lower level languages like C or C++, we need to install 24 | the compiler on the target device, but actually we can use compiler's options to compile our code for different machines. But, in case 25 | of higher level languages, like Python, we only need the interpreter on the target device, and it will work! 26 | 27 | But let's go deeper, deep inside the Intel x86 instruction set! Let's print the expression ***Piece of Art*** on console using assembly 28 | language : 29 | 30 | ```assembly 31 | 32 | STK SEGMENT 33 | DW 100 DUP(?) 34 | STK ENDS 35 | 36 | DTS SEGMENT 37 | TXT DB 'Piece of Art!', 10, 13, '$' 38 | DTS ENDS 39 | 40 | CDS SEGMENT 41 | ASSUME CS:CDS, SS:STK, DS:DTS 42 | MAIN PROC FAR 43 | MOV AX, SEG DTS 44 | MOV DS, AX 45 | MOV DX, OFFSET TXT 46 | MOV AH, 09H 47 | INT 21H 48 | MOV AH, 4CH 49 | INT 21H 50 | MAIN ENDP 51 | CDS ENDS 52 | END MAIN 53 | ``` 54 | 55 | This is not that hard to write and understand. Anyway, we are not going to talk about assembly programming here. So, have you seen 56 | the code? But It's not actually what computer sees! The computer only sees & understands a bunch of 0 and 1's. So, imagine one the 57 | above code lines. For example this : 58 | 59 | ``` 60 | MOV DS, AX 61 | ``` 62 | 63 | When you *assemble* the code using assembler, it will turn this line to something like this : 64 | 65 | ``` 66 | B8 0000 67 | ``` 68 | 69 | ***NOTE : THIS IS NOT THE EXACT MACHINE CODE OF THAT INSTRUCTION*** 70 | 71 | Letter `B` stands for 4 bits, also 8 stands for 4 bits. We have 8 bits *Instruction Code* and 16 bits *hidden data* in 72 | x86 family ***Object Code***. In this chapter, we actually decide for a simple object code for our microprocessor. 73 | 74 | ## Object Code 75 | Now, we need to decide about a simple *Object Code* structure for our microcontroller. As I mentioned before, we won't 76 | have a register file in our microcontroller and we directly read data from RAM. You may ask why, because we can keep it simple 77 | for future developments and studies. Let's take a look at our *Instruction Table*, but this time, Hexadecimal : 78 | 79 | | Instruction | Code | 80 | |:-----------:|:----:| 81 | | AND | 0x0 | 82 | | OR | 0x1 | 83 | | NOR | 0xC | 84 | | NAND | 0xD | 85 | | ADD | 0x2 | 86 | | SUB | 0x6 | 87 | 88 | Now, we need to upload our *Programs* to the microcontroller. But how? How it can detect the operands? This is a simple structure 89 | I suggest for that : 90 | 91 | | Instruction Code | Input A | Input B | 92 | |:----------------:|:----------:|:--------:| 93 | | 4 bits | 8 bits | 8 bits | 94 | 95 | We have a simple 20-bit object code for our microcontroller. Let's see if we want to add two numbers, for example 15 and 8, 96 | how does it look like? It will be like this : 97 | 98 | | Instruction Code | Input A | Input B | 99 | |:----------------:|:----------:|:--------:| 100 | | 0x2 | 0x0f | 0x08 | 101 | 102 | Now, we need to verify which bits are for which part. The most valuable bits can be for our instruction code, and others can be 103 | for inputs. This is what I suggest : 104 | 105 | | Instruction Code | Input A | Input B | 106 | |:----------------:|:----------:|:---------:| 107 | | Bits 16 - 19 | Bits 8 - 15| Bits 7 - 0| 108 | 109 | So, the code `0x20f08` is the correct object code for `ADD 15, 8`. 110 | 111 | # The Final Step? 112 | Now we have ALU, and we will add RAM to our ALU. Then, we program it and enjoy! The book is almost finished, and there's nothing more 113 | to say about the hardware side of a computer. But what about software and operating system? Be patient, we will take a look at 114 | software and operating systems in following chapters! -------------------------------------------------------------------------------- /chapter17.md: -------------------------------------------------------------------------------- 1 | # Chapter 17 : The Dark Side of The Moon 2 | This is not about Pink Floyd's album, this book has nothing to do with music of course. But, I chose this name because 3 | a lot of *computer engineers* have no idea about design and implementation of the hardware. So, it can be the dark side! 4 | Some people, including some computer engineers and electrical engineers, joined the dark side and made computers. But this is 5 | not all! We had computers before transistors. They were usually electro-mechanical or mechanical. But the theory of computation 6 | is much older than what you think! ***Khawrazmi***, a Persian scientist, is one of the most known people in history of computer 7 | science. Even the word ***Algorithm*** is taken from his name. In 1947, *Transistor* discovered, and that was the starting point of 8 | ***Electronic Computers***. 9 | 10 | ## Digital Electronics 11 | When transistors discovered and manufactured, scientists and engineers found that they can acts like switches. Then, they decided to use 12 | them as switches. A transistor can act like a switch, but first, look at this square wave: 13 | 14 | ![Square Wave](figures/SquareWave.png) 15 | 16 | It starts at zero volts and ends at five volts. It's like a switch which is connected to a five volts battery, and someone pressed button 17 | in defined times. This pulse, can be input or output of a transistor. First, let me show you a NOT gate which is made by a ***Bipolar Junction 18 | Transistor*** or in short, **BJT** transistor. BJT transistors are like two diodes connected in a specific arrengement. Usually, we use *NPN* transitors 19 | to make logical circuits: 20 | 21 | ![RTL/TTL NOT gate](figures/NOT-BJT.png) 22 | 23 | If we apply a voltage on *input* we will have a ***wave form analysis*** like this : 24 | 25 | ![NOT waveforms](figures/NOTWaves.png) 26 | 27 | As you can see, everytime input is high, output is low, and everytime input is low, output is high. Why? when *input* is high, 28 | current can move to the ground, but when it's low, current find another way to the ground, which is our output. 29 | 30 | RTL/TTL technologies are good, but later engineers found that ***Field Effect Transistors*** are better. Then, they decide to use 31 | ***Metal Oxyde Semiconductor Field Effect Transistor***s or in short **MOSFET** transistors for their artworks! FETs have different 32 | structure, but still can act as switch, and even better than BJTs. Also, they can be minimalized better. This is why a lot of digital 33 | chips around us is made up of MOSFETs. MOSFETS are usually two types, nMOS, or ***Negative Channel*** and pMOS or ***Poisitive Channel***. 34 | As engineers wanted the best performance, they just used both of them. When you use both nMOS and pMOS transistors, you actually used 35 | ***Complementary Metal Oxyde Semiconductor*** or *CMOS* technology. This is a CMOS inverter (or the same NOT gat): 36 | 37 | ![CMOS Inverter](figures/CMOS-Inverter.png) 38 | 39 | Let's apply voltage on input, and see what happens! 40 | 41 | ![CMOS Waveform analysis](figures/CMOSWaves.png) 42 | 43 | As you can see, the result is very similar to TTL/RTL design. When you design a digital circuit, you should consider everything! Waste of power, 44 | price, noise-resistance and size. For example, TTL is a good design as it's cheap and has good resistance against noise, but it's not as small as 45 | we want. But CMOS is small enough to be on a chip! Also, there's a method for minimalizing CMOS designs, and implementation of more logics by less 46 | transistors, which is called ***Very Large Scale Integration*** or in short, **VLSI**. I think this is enough, let's take a look on other ways of 47 | implementing a computer in real life. 48 | 49 | ## Integrated Circuits 50 | Imagine a rectangular black thing, which includes twenty transistors. This is actually an integrated circuit. Integrated circuits 51 | are made up of tiny transistors, and they easily can be find in electronics stores. They integrated a lot of circuits on a piece of 52 | semiconductor, connected metal legs to that, and covered with plastic, then you can buy them and use them in your projects. Also, IC's 53 | classified by their technology. For example, you know 7400 series are TTL IC's, or 28 Series are EEPROM's. Anyway, there is one other way, 54 | and you may find that the easiest way to join the dark side! 55 | 56 | ## Program The Hardware 57 | There are some programming languages, which are called ***Hardware Description Language*** or in short, **HDL**. They're easy to use, 58 | and they're actually similar to *C* or *Assembly* programming languages. One of my favorite HDL's is *Verilog*. I program an AND gate in 59 | HDL like this : 60 | 61 | ```verilog 62 | module AND(A, B, F); 63 | input wire A; 64 | input wire B; 65 | output wire F; 66 | 67 | assign F = A & B; 68 | 69 | endmodule 70 | ``` 71 | And for that, we need a ***Complex Programmable Logical Device***, CPLD or a ***Field-Programmable Gate Array***, FPGA. Those devices 72 | have logical applications, but they have no logics, and we have to program them to make them functional. A lot of ligical simulator programs, 73 | like *logisim* can generate Verilog or VHDL code of the designed logical circuit. And then, you can upload the code on your FPGA and have your 74 | very own logical device. And any other way to design and implement a real hardware? Of course there is a lot of other ways. But these ways are 75 | the easiest ways to do that with a low-budget (not sure of this!) and basic knowledge of computers and electronics. 76 | 77 | ## The last part! 78 | This is the last part of the book! I tried to do my best for this one. Actually, this is my first English book, but I'm glad, because I could 79 | share my knowledge and experiences. This book is written to make understanding a computer easier. First, I started it in Summer (2016) and it finished 80 | in Spring (2017). About 8 or 9 months spent on this book, and I hope you enjoy! -------------------------------------------------------------------------------- /SUMMARY.md: -------------------------------------------------------------------------------- 1 | # Summary 2 | 3 | * [License](LICENSE.md) 4 | * [Introduction](README.md) 5 | - [Audience](README.md#audience) 6 | - [What we learn?](README.md#what-we-learn) 7 | ## [Chapter 1](chapter01.md) : What's a microcontroller? 8 | - [What does a microcontroller consist of?](chapter01.md#what-does-a-micro-controller-consist-of) 9 | - [But, How do it know?! ...](chapter01.md#but-how-do-it-know-) 10 | ## [Chapter 2](chapter02.md) : How to talk to a computer 11 | - [The machine code](chapter02.md#the-machine-code) 12 | - [Everything Binary...](chapter02.md#everything-binary) 13 | - [Word size](chapter02.md#word-size) 14 | - [Convertion, it's easy!](chapter02.md#convertion-its-easy) 15 | - [Hexadecimal world welcomes you](chapter02.md#hexadecimal-world-welcomes-you) 16 | - [Mathematics?](chapter02.md#mathematics) 17 | ## [Chapter 3](chapter03.md) : Arithmetic Operations 18 | - [Addition](chapter03.md#addition) 19 | - [Subtraction](chapter03.md#subtraction) 20 | * [Sign/Modulus System](chapter03.md#signmodulus-system) 21 | * [One's Complement System](chapter03.md#ones-complement-system) 22 | * [Two's Complement System](chapter03.md#twos-complement-system) 23 | * [How to subtract](chapter03.md#how-to-subtract) 24 | - [Now, what should we do?](chapter03.md#now-what-should-we-do) 25 | ## [Chapter 4](chapter04.md) : Logical Operations 26 | - [How many logical Operations we have?](chapter04.md#how-many-logical-operations-we-have) 27 | * [NOT](chapter04.md#not) 28 | * [AND](chapter04.md#and) 29 | * [OR](chapter04.md#or) 30 | * [Truth Table](chapter04.md#truth-table) 31 | - [Let's play a game!](chapter04.md#lets-play-a-game) 32 | * [NAND](chapter04.md#nand) 33 | * [NOR](chapter04.md#nor) 34 | - [Complex Logics](chapter04.md#complex-logics) 35 | * [Exclusive OR](chapter04.md#exclusive-or) 36 | * [Exclusive NOR](chapter04.md#exclusive-nor) 37 | - [The journey to computer architecture!](chapter04.md#the-journey-to-computer-architecture) 38 | ## [Chapter 5](chapter05.md) : Logical Circuits 39 | - [The NAND](chapter05.md#the-nand) 40 | - [NOT Gate](chapter05.md#not-gate) 41 | - [AND Gate](chapter05.md#and-gate) 42 | - [OR Gate](chapter05.md#or-gate) 43 | - [Now, we're ready!](chapter05.md#now-were-ready) 44 | ## [Chapter 6](chapter06.md) : Combinational Circuits 45 | - [The Exclusive OR](chapter06.md#the-exclusive-or) 46 | - [The Exclusive NOR](chapter06.md#the-exclusive-nor) 47 | - [More Logics?](chapter06.md#more-logics) 48 | ## [Chapter 7](chapter07.md) : The First Computer 49 | - [The Function](chapter07.md#the-function) 50 | * [Boolean Algebra!](chapter07.md#boolean-algebra) 51 | - [The Half Adder](chapter07.md#the-half-adder) 52 | - [The Full Adder](chapter07.md#the-full-adder) 53 | - [Ripple-Carry Adder](chapter07#ripple-carry-adder) 54 | - [Let's talk about computers!](chapter07.md#lets-talk-about-computers) 55 | ## [Chapter 8](chapter08.md) : Memory 56 | - [Everything is NAND](chapter08.md#everything-is-nand) 57 | - [Basic Improvement](chapter08.md#basic-improvement) 58 | - [Register](chapter08.md#register) 59 | - [The new flip-flop](chapter08.md#the-new-flip-flop) 60 | - [The final register!](chapter08.md#the-final-register) 61 | - [What we need now?](chapter08.md#what-we-need-now) 62 | ## [Chapter 9](chapter09.md) : Register File 63 | - [The Decoder](chapter09.md#the-decoder) 64 | - [Simple Register File](chapter09.md#simple-register-file) 65 | - [The Multiplexer](chapter09.md#the-multiplexer) 66 | - [Advanced Register File](chapter09.md#advanced-register-file) 67 | - [Ready for Architecture!](chapter09.md#ready-for-architecture) 68 | ## [Chapter 10](chapter10.md) : Computer Architecture 69 | - [Computer Architecture](chapter10.md#computer-architecture) 70 | - [Backward Compatibility](chapter10.md#backward-compatibility) 71 | - [Computer Organization](chapter10.md#computer-organization) 72 | - [Complex or Reduced? This is the question](chapter10.md#complex-or-reduced-this-is-the-question) 73 | - [Decisions!](chapter10.md#decisions) 74 | ## [Chapter 11](chapter11.md) : Design, Advanced Addition Machine! 75 | - [Managing Inputs](chapter11.md#managing-inputs) 76 | - [A new device?](chapter11.md#a-new-device) 77 | * [Demultiplexer](chapter11.md#demultiplexer) 78 | - [Selection!](chapter11.md#selection) 79 | - [Temporary Registers](chapter11.md#temporary-registers) 80 | - [Let's go!](chapter11.md#lets-go) 81 | ## [Chapter 12](chapter12.md) : The Computer(Theory) 82 | - [The Instruction Set](chapter12.md#the-instruction-set) 83 | * [What Instructions we need?](chapter12.md#what-instructions-we-need) 84 | - [Computer Organization](chapter12.md#computer-organization) 85 | - [Memory Unit](chapter12.md#memory-unit) 86 | - [Starting Implementation](chapter12.md#starting-implementation) 87 | ## [Chapter 13](chapter13.md) : Arithmetic and Logical Unit 88 | - [Tools we need](chapter13.md#tools-we-need) 89 | - [A note on schematics](chapter13.md#a-note-on-schematics) 90 | - [Start Point](chapter13.md#start-point) 91 | - [More instructions?](chapter13.md#more-instructions) 92 | ## [Chapter 14](chapter14.md) : Program Structure 93 | - [Programming for a typical computer](chapter14.md#programming-for-a-typical-computer) 94 | - [Object Code](chapter14.md#object-code) 95 | - [The Final Step](chapter14.md#the-final-step) 96 | ## [Chapter 15](chapter15.md) : Microcontroller 97 | - [Control Unit](chapter15.md#control-unit) 98 | - [Combination of Things](chapter15.md#combination-of-things) 99 | - [What should we learn now?](chapter15.md#what-should-we-learn-now) 100 | ## [Chapter 16](chapter16.md) : Programming and Operating System 101 | - [Intel computers!](chapter16.md#intel-computers) 102 | - [The assembler](chapter16.md#the-assembler) 103 | - [The operating system](chapter16.md#the-operating-system) 104 | ## [Chapter 17](chapter17.md) : The Dark Side of The Moon 105 | - [Digital Electronics](chapter17.md#digital-electronics) 106 | - [Integrated Circuits](chapter17.md#integrated-circuits) 107 | - [The last part!](chapter17.md#the-last-part) 108 | * [Bibliography](RESOURCES.md) -------------------------------------------------------------------------------- /chapter13.md: -------------------------------------------------------------------------------- 1 | # Chapter 13 : Arithmetic and Logical Unit 2 | 3 | This is the start point! In this chapter, we build the main part of our computer, and we will be able to use it in a logical simulator. 4 | In old days, when computers were weak and expensive, ***ALU*** was an independent part, and it was not integrated with CPU. For example, 5 | a lot of computers used 74181 IC as the ALU (e.g VAX). Today, ALU, Register file, RAM, etc. Are all integrated in one chip and it's called 6 | a ***Microcontroller***. But, to understand computer better, we start a modular design, then we put all together, and we'll have a complete 7 | microcontroller. 8 | 9 | ## Tools we need 10 | 11 | The main tool you need is a computer, and this is the funny part that *we use a computer to design another computer*. But, you need one, because 12 | we don't want to make a computer in real life, we only want to simulate it. 13 | It doesn't matter which operating system your computer runs, Windows, OS X, Linux, etc. You need to install Java on your computer, and you know Java 14 | is freely available. 15 | Then, you need the most useful tool, [Logisim Evolution](https://github.com/reds-heig/logisim-evolution), a good logical simulation software, 16 | which is free to use, distribute and advertise. All the schematics we designed in this book, are designed by this software. 17 | 18 | ## A note on schematics 19 | In the book, we built a lot of devices ourselves. But, ***Logisim*** is pre-packaged with good and useful devices such as 20 | Registers, Flip-Flops, Decoders, Multiplexers, etc. So, In this chapter and next chapters, we are going to use pre-made devices. 21 | 22 | ## Start Point 23 | In [chapter twelve](chapter12), we decided about our instructions. I want to assign a code to each instruction, and that's called 24 | ***Instruction Code*** or ***Operation Code***. We will have table like this at the end: 25 | 26 | |Code |Instruction| 27 | |:-----------------------------------------:|:------------------------------------:| 28 | | We put a hexadecimal/binary code here |We put the instruction here | 29 | 30 | But, we don't have any instructions right now, let's implement AND, our very first instruction : 31 | 32 | ![ALU - Step 0](figures/final/ALU-0.png) 33 | 34 | Then, we need to add OR, but wait! Two outputs for one ALU? Is it possible? of course not! So, we use something called 35 | a **Multiplexer**. So, I add a 4:1 mux, which is large enough to cover all we need! Then, I add the OR instruction, and we will 36 | get something like this : 37 | 38 | ![ALU - Step 1](figures/final/ALU-1.png) 39 | 40 | Now, our table will be like this : 41 | 42 | |S2 | S1 | Instruction | 43 | |:---:|:---:|:-----------:| 44 | | 0 | 0 | AND | 45 | | 0 | 1 | OR | 46 | 47 | And our microcontroller's ALU, can run two simple programs! But this is not enough. You know, even the simplest processors classified as 48 | RISC, such as MIPS, can do more than these two instructions. These are logical instructions, but we need at least one or two arithmetic instructions. 49 | Lets add the instruction *ADD*. After adding that instruction, we will have this : 50 | 51 | | Cin | S2 | S1 | Instruction | 52 | |:---:|:--:|:--:|:-----------:| 53 | | 0 | 0 | 0 | AND | 54 | | 0 | 0 | 1 | OR | 55 | | 0 | 1 | 0 | ADD | 56 | 57 | We actually don't need *Cin* in our instruction codes, because we won't use this ALU to do ***signed addition***. But, we will need that 58 | for subtraction. 59 | 60 | ## More instructions? 61 | 62 | As you know, we are going to make a computer which can do AND, OR, NAND, NOR, ADD and SUB. We implemented AND, OR and ADD. But, how can we 63 | add NAND or NOR? We will a NOT gate, and a 2:1 multiplexer. This is how we can implement these two instructions : 64 | 65 | ![ALU - Step 3](figures/final/ALU-3.png) 66 | 67 | Now we have a table like this : 68 | 69 | |Ainvert|Bnegate| S2 | S1 | Instruction | 70 | |:-----:|:-----:|:--:|:--:|:-----------:| 71 | | 0 | 0 | 0 | 0 | AND | 72 | | 0 | 0 | 0 | 1 | OR | 73 | | 1 | 1 | 0 | 0 | NOR | 74 | | 1 | 1 | 0 | 1 | NAND | 75 | | 0 | 0 | 1 | 0 | ADD | 76 | 77 | Now, our ALU is almost complete, but we still don't have subtraction. Subtraction is a bit complex, there are a lot of different 78 | ways to do subtraction, but we want a perfect way here. As you may remember from [chapter three](chapter3.md), we just suggested 79 | ***Two's Complement*** system for our dear computer. And you remember how that works! First, we need to invert all of the bits (1's become 0, 0's become 1), 80 | then we add 1 unit to the inverted number. *Cin* works like a *plus one* button for us, and *Bnegate* inverts *B* for us. If we add an OR gate, 81 | then we will have subtraction. Like this : 82 | 83 | ![ALU - Step 4](figures/final/ALU-4.png) 84 | 85 | When *Bnegate* is high, automatically *Cin* becomes high on the adder. Now, we can have our ***Instruction Table*** like this : 86 | 87 | |Ainvert|Bnegate| S2 | S1 | Instruction | 88 | |:-----:|:-----:|:--:|:--:|:-----------:| 89 | | 0 | 0 | 0 | 0 | AND | 90 | | 0 | 0 | 0 | 1 | OR | 91 | | 1 | 1 | 0 | 0 | NOR | 92 | | 1 | 1 | 0 | 1 | NAND | 93 | | 0 | 0 | 1 | 0 | ADD | 94 | | 0 | 1 | 1 | 0 | SUB | 95 | 96 | We implemented the most simple ALU we could, and after some modifications, ALU module will look like this : 97 | 98 | ![ALU - Appearance](figures/final/ALU-Module.png) 99 | 100 | I didn't remove *Cin* , because it will be needed when we want to expand our ALU (in following chapters, we will talk about expanding ALU) 101 | 102 | ## Next Step 103 | In these 13 chapters, we learned logic and design, and we've designed a simple ALU. But, ALU is not everything a computer needs. We need a simple 104 | memory block, which can control programs, etc. So, In next chapter, we will learn how to define a program structure, and then we can add memory block 105 | we need. At the end, we can really make our very own programmable microcontroller. -------------------------------------------------------------------------------- /chapter02.md: -------------------------------------------------------------------------------- 1 | # Chapter 2 : How to talk to computer? 2 | 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++. 3 | When we want to print `Hello, World!` on C++, we write some code like this: 4 | ```c 5 | #include 6 | using namespace std; 7 | 8 | int main(){ 9 | cout<<"Hello, World!\n"; 10 | return 0; 11 | } 12 | ``` 13 | This is called a program, and when we *compile* it, it turns to a file, usually named `a.out`. And let's run our program: 14 | ```bash 15 | ~:$ ./a.out 16 | Hello, World! 17 | ~:$ 18 | ``` 19 | And now, we've communicated with our computer, but in language which is close 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. For coding 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 code directly to your hardware?! Now, we need a language which is close to machine's language. 20 | ## The machine code 21 | Machines are awesome, because they only use 0 and 1 to communicate with 22 | each other. Humans are not as smart as computers in this case, because 23 | we use lots of letters in daily communications. English includes 26 24 | 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! 25 | ### Everything Binary... 26 | OK, we told you we have 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 to 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. 27 | ### Word size 28 | Imagine the word `hello` in English language. It has 4 characters. Also, 29 | imagine the number `42`, it has 2 digits. In computers, we need a fixed size, and our words 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 : 30 | 31 | |7|6|5|4|3|2|1|0| 32 | |---|:---:|:---:|:---:|:---:|:---:|:---:|---:| 33 | |0|0|0|0|0|0|0|0| 34 | 35 | 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?! 36 | ### Convertion, it's easy! 37 | Now, let's take a look at a simple binary number for example : `1001`. This number is a 4 bit one, but we defined our word size 8 bits. We convert it to an 8 bit number, we just need to replace missing bits with zeros : 38 | 39 | |7|6|5|4|3|2|1|0| 40 | |---|:---:|:---:|:---:|:---:|:---:|:---:|---:| 41 | |0|0|0|0|1|0|0|1| 42 | 43 | And now, we know column 3 and 0 have value one, it's so easy to calculate: 44 | 45 | ``` 46 | 2^3 + 2^0 47 | ``` 48 | now, we know `2^3` is equal to 8, and `2^0` is equal to one. So, the result is: 49 | 50 | ``` 51 | 8 + 1 = 9 52 | ``` 53 | 54 | It's fine to convert binary numbers to decimal, but how can we convert a decimal number to binary? Now, we try to convert 135 to binary. We all know 135 is equal to `128 + 7`. Now, we can write this number like this : 55 | 56 | ``` 57 | 2^7 + 2^2 + 2^1 + 2^0 58 | ``` 59 | 60 | So, we shall enter 1 in columns 7, 2, 1 and 0. Our result is just like this : 61 | 62 | |7|6|5|4|3|2|1|0| 63 | |---|:---:|:---:|:---:|:---:|:---:|:---:|---:| 64 | |1|0|0|0|0|1|1|1| 65 | 66 | And yes! 10000111 is our result. And there's a qestion, is there any other formats for communicating with computers? Yes! We can use base 16, too. base 16 is known as hexadecimal, and we have digits 0 to F in that format. It's wierd, isn't it? 67 | ### Hexadecimal world welcomes you 68 | 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 sixteen 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 : 69 | 70 | |Binary|Decimal|Hexadecimal| 71 | |------|:-----:|----------:| 72 | |0000 |0 |0 | 73 | |0001 |1 |1 | 74 | |0010 |2 |2 | 75 | |0011 |3 |3 | 76 | |0100 |4 |4 | 77 | |0101 |5 |5 | 78 | |0110 |6 |6 | 79 | |0111 |7 |7 | 80 | |1000 |8 |8 | 81 | |1001 |9 |9 | 82 | |1010 |10 |A | 83 | |1011 |11 |B | 84 | |1100 |12 |C | 85 | |1101 |13 |D | 86 | |1110 |14 |E | 87 | |1111 |15 |F | 88 | 89 | These are our digits, and we learned from this table that every 4 bit is equat to one hex digit. So, we can convert a decimal number to 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 : 90 | 91 | ``` 92 | 2^7 + 2^4 + 2^3 + 2^2 + 2^1 93 | ``` 94 | And now, we can draw binary conversion table for this number like this : 95 | 96 | |7|6|5|4|3|2|1|0| 97 | |---|:---:|:---:|:---:|:---:|:---:|:---:|---:| 98 | | 1 | 0 |0 |1 |1 |1 |1 |0 | 99 | 100 | Oh, 10011110, is our result. let's convert it hexadecimal! It has 8 bits, it means it has 2 digits in hexadecimal format. So, we draw a hexadecimal conversion table for this number : 101 | 102 | | 1 | 0 | 103 | |--------|--------:| 104 | |1001 |1110 | 105 | 106 | According to table, `100` is equal to 9, and `1110` is equal to E. So, our result is `9E`. 107 | 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. 108 | 109 | ## Mathematics? 110 | 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. 111 | -------------------------------------------------------------------------------- /chapter03.md: -------------------------------------------------------------------------------- 1 | # Chapter 3 : Arithmetic Operations 2 | 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?! 3 | 4 | In previous chapter, we talked about how computer reads data. We talked about binary digits. In this chapter, we take a look that addition and subtraction of them. 5 | 6 | ## Addition 7 | How do you add two decimal numbers? for example 105 and 55. This is how we add these to numbers : 8 | 9 | | | 1 | | 10 | |---|:---:|---:| 11 | |1 | 0 | 5 | 12 | | | 5 | 5 | 13 | |1 | 6 | 0 | 14 | 15 | 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 draw 8 bits table now : 16 | 17 | |7 |6 |5 |4 |3 |2 |1 |0 | 18 | |---|:---:|:---:|:---:|:---:|:---:|:---:|--:| 19 | | | | |1 |1 | | | | 20 | |0|0|0|0|1|1|0|0| 21 | |0|0|0|0|0|1|0|0| 22 | |0|0|0|1|0|0|0|0| 23 | 24 | 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. I'll draw a table for this addition : 25 | 26 | |C |7 |6 |5 |4 |3 |2 |1 |0 | 27 | |---|:---:|:---:|:---:|:---:|:---:|:---:|:---:|---:| 28 | |1 |1 | | | | | | | | 29 | | |1 |1 |0 |0 |0 |0 |0 |0 | 30 | | |0 |1 |0 |0 |0 |0 |0 |0 | 31 | |1 |0 |0 |0 |0 |0 |0 |0 |0 | 32 | 33 | 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 to a memory block called **Carry Flag** , and 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? 34 | ## Subtraction 35 | 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 show you three popular way, and at the end, we choose on of them as our standard. 36 | ### Sign/Modulus System 37 | In this system, 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 two examples , for example we convert 36 and -66 to a S/M number : 38 | 39 | |Decimal | S | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 40 | |--------|:---:|:---:|:---:|:---:|:---:|:---:|:---:|---:| 41 | |36 |0 |0 |1 |0 |0 |1 |0 |0 | 42 | |-66 |1 |1 |0 |0 |0 |0 |1 |0 | 43 | 44 | Oh, let's think about this system. This system is very easy-to-use, but have two great problems : 45 | 46 | 1. It makes our word size smaller, as one of our bits wasted. 47 | 2. It makes negative zero condition, and we need to use algorithms designed for this system. These algorithms are not popular, so we don't use S/M. 48 | 49 | There's another easy way, and it's called **One's complement**. 50 | ### One's Complement System 51 | In this system, we just invert bits. Every 0 becomes 1, and every 1 becomes 0. Let's calculate -36 in this system. 52 | 53 | |Decimal | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 54 | |--------|:---:|:---:|:---:|:---:|:---:|:---:|:---:|---:| 55 | |36 | 0 | 0 | 1 | 0 | 0 |1 | 0 | 0 | 56 | |-36 | 1 | 1 | 0 | 1 | 1 |0 | 1 | 1 | 57 | 58 | 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! that's using two's complement system! 59 | 60 | ### Two's Complement System 61 | 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 : 62 | 63 | |Decimal | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 64 | |--------|:---:|:---:|:---:|:---:|:---:|:---:|:---:|---:| 65 | |67 | 0 |1 |0 |0 |0 |0 |1 |1 | 66 | | | 1 |0 |1 |1 |1 |1 |0 |0 | 67 | | | | | | | | | |1 | 68 | |-67 | 1 |0 |1 |1 |1 |1 |0 |1 | 69 | 70 | In this system, we have no negative zero conditions, and all bits used efficiently. Now, we choose this system as our standard, and we'll design our subtraction parts using this system. 71 | 72 | ### How to subtract? 73 | 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 : 74 | 75 | ``` 76 | 125 - 36 77 | ``` 78 | 79 | is actually : 80 | 81 | ``` 82 | 125 + (-36) 83 | ``` 84 | 85 | So, first, we calculate -36 : 86 | 87 | |Decimal |7 |6 |5 |4 |3 |2 |1 |0 | 88 | |--------|:---:|:---:|:---:|:---:|:---:|:---:|:---:|---:| 89 | |36 |0 |0 |1 |0 |0 |1 |0 |0 | 90 | | |1 |1 |0 |1 |1 |0 |1 |1 | 91 | | | | | | | | | |1 | 92 | |-36 |1 |1 |0 |1 |1 |1 |0 |0 | 93 | 94 | And now, we do our addition : 95 | 96 | |C |7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 97 | |---|:---:|:---:|:---:|:---:|:---:|:---:|:---:|---:| 98 | | |1 |1 |1 |1 |1 | | | | 99 | | |0 |1 |1 |1 |1 |1 |0 |1 | 100 | | |1 |1 |0 |1 |1 |1 |0 |0 | 101 | |1 |0 |1 |0 |1 |1 |0 |0 |1 | 102 | 103 | 104 | The carry is here normal, and every subtraction shall 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. 105 | 106 | ## Now, what should we do? 107 | In our *proccess 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 learn logical operations. 108 | --------------------------------------------------------------------------------