Skip to content

Commit

Permalink
Add digital circuits section
Browse files Browse the repository at this point in the history
Section includes: basic logic gates, arithmetics circuits, switching circuits, memory circuits
  • Loading branch information
namberino committed Apr 11, 2024
1 parent c845bbc commit 38cabe6
Show file tree
Hide file tree
Showing 26 changed files with 463 additions and 0 deletions.
146 changes: 146 additions & 0 deletions Digital Circuits/circuits/arithmetic-circuits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Arithmetic digital circuits

Everything that a computer can do can be boiled down to mathematics and arithmetics. So being able to do arithmetics with digital circuits will be the next big step we're going to take.

We'll be building some digital circuits capable of addition and subtraction

___

# Half adder

## Description

This circuit will take in 2 input bits, add them together and output 2 bits.

This circuit will be the foundation for other arithmetic circuits

## Truth table

> Note: h is the high bit, l is the low bit
| a | b | h | l |
| - | - | - | - |
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 0 |

## Structure

This can be created using an AND gate and an XOR gate:

![half adder structure](../images/half-adder-structure.png)

___

# Full adder

## Description

This circuit works the same way as the half adder circuit, but it will take 3 input bits instead of 2 input bits.

## Truth table

| a | b | c | h | l |
| - | - | - | - | - |
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 1 |
| 0 | 1 | 0 | 0 | 1 |
| 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 0 | 1 |
| 1 | 0 | 1 | 1 | 0 |
| 1 | 1 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 |

## Structure

This can be constructed by connecting 2 half adders together

![full adder structure](../images/full-adder-structure.png)

___

# Multi-bit adder

## Description

This circuit functions similarly to how we would do binary addition. It takes in 5 bits (2 2-bit numbers and 1 carry bit), add them together and output a 3-bit number.

Input:
- *ah* *al* is a 2-bit number
- *bh* *bl* is a 2-bit number
- *c* is a carry bit

Output:
- *h* is the high bit
- *m* is the middle bit
- *l* is the low bit

## Truth table

The truth table is very big so I'll just put an example:

| ah | al | bh | bl | c | h | m | l |
| -- | -- | -- | -- | - | - | - | - |
| 1 | 0 | 1 | 0 | 1 | 1 | 0 | 1 |

ah = 1 and al = 0 (Number is 2 in binary)

ah = 1 and al = 0 (Number is 2 in binary)

c = 1 (1 carry bit)

h = 1, m = 0, l = 1 (101 in binary, 5 in decimal)

so 2 + 2 + 1 = 5

## Structure

This circuit can be created using 2 full adders. 1 adder's high bit will carry over to the input of the other adder, this mimics how we carry bits when we manually add binary numbers.

![multi-bit addder structure](../images/multi-bit-adder-structure.png)

> We can keep adding onto this and create adders that can handle more bits like a 16-bit or 32-bit adder
___

# Increment

## Description

It's quite useful to have a circuit that can count up 1 by 1 in computer science and programming. So let's learn about the increment circuit.

This circuit will take in a number and increment it by 1. For this, we will use 16-bit number since it's quite useful to have something that can add multi-bit number.

## Structure

![increment structure](../images/increment-16-structure.png)

___

# Subtraction

## Description

A big part of mathematics is subtraction. So let's learn about a circuit that allows us to subtract a number from another. We'll use 16-bit number because that allows us to subtract bigger number.

## Truth table

Here's an example truth table for how the circuit should function:

| input 1 | input 2 | output |
| --- | --- | --- |
| A | 9 | 1 |
| 8FFF | 7FFF | 1000 |

> Note: The number uses 2's complement, meaning the most significant bit will be the sign bit. If the sign bit is 1 then the number is negative
## Structure

What we need to do when we subtract is to flip the 2nd number to its negative counterpart and add it to the 1st number.

There's a slight problem, which is when we flip a number from positive to negative in binary, for example, we want to change a 16-bit 1 to a 16-bit -1, if we just use a 16-bit inverter, we just changed the number to a 16-bit -2 instead. So what we need to do is to add 1 into the inverted number with the increment circuit we created before.

> Note: A 16-bit inverter will just flip all the bits in a 16-bit number. It will change 1 to 0 and vice versa
![subtraction circuit](../images/subtraction-16-structure.png)
132 changes: 132 additions & 0 deletions Digital Circuits/circuits/basic-gates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Basic logic gates

The logic gates are the building blocks for digital circuits. They represent Boolean logical operations. Let's learn about some of them.

___

# NAND gates

## Description

This is the most fundamental logic gate. In fact, you can build an entire computer using just NAND gates. This gate only produces an output of 0 if all of its input is 1.

This can be made using just 2 switches.

## Truth table

| a | b | out |
| - | - | --- |
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |

## Symbol

![nand gate symbol](../images/nand-gate.png)

___

# NOT gate

## Description

The NOT gate, also known as the inverter gate, takes in 1 input and output 1 output. This circuit will invert the input, so if the input is 1 then the output will be 0 and vice versa.

## Truth table

| in | out |
| -- | --- |
| 0 | 1 |
| 1 | 0 |

## Structure

This gate can be created using 1 NAND gate:

![not gate structure](../images/not-gate-structure.png)

## Symbol

![not gate symbol](../images/not-gate.png)

___

# AND gate

## Description

This gate will take in 2 or more input and only output a 1 if all inputs are 1.

## Truth table

| a | b | out |
| - | - | --- |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |

## Structure

This gate can be created using a NAND gate and a NOT gate because it's basically the opposite of a NAND gate:

![and gate structure](../images/and-gate-structure.png)

## Symbol

![and gate symbol](../images/and-gate.png)

___

# OR gate

## Description

This gate will take in 2 or more input and only output a 0 if all inputs are 0.

## Truth table

| a | b | out |
| - | - | --- |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |

## Struture

This gate can be created using a NAND gate and 2 NOT gate because so as to flip the 2 inputs:

![or gate structure](../images/or-gate-structure.png)

## Symbol

![or gate symbol](../images/or-gate.png)

___

# XOR (Exclusive OR) gate

## Description

This gate takes in 2 or more input and will only output a 1 if there's an odd number of 1s in the inputs. So this gate is quite useful if you want to differentiate between odd and even number of bits.

## Truth table

| a | b | out |
| - | - | --- |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |

## Structure

This gate can be created using 1 NAND gate, 1 OR gate and 1 AND gate:

![xor gate structure](../images/xor-gate-structure.png)

## Symbol

![xor gate symbol](../images/xor-gate.png)
99 changes: 99 additions & 0 deletions Digital Circuits/circuits/memory-circuits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Memory circuits

A very useful function of a computer is its ability to "remember" information, which is why memory circuits are very important. It is extremely useful to be able to hold data inputed into a circuit, so let's learn about some memory circuits

___

# Data latch

## Description

A data latch is capable of outputing and storing a single bit. It takes in 1 input bit as data and 1 input bit as a store signal. A store signal is used to indicate whether the circuit should store the input data or not. When the store signal is 1, the value of data is outputed and stored. When the store signal is 0, the value of data is ignored and the previously stored value is outputed

## Truth table

> Note: st is the store signal, d is the input data bit
| st | d | Effect |
| -- | - | ------ |
| 1 | 0 | output set to 0 |
| 1 | 1 | output set to 1 |
| 0 | 1 | output set to previously stored value |
| 0 | 0 | output set to previously stored value |

## Structure

This circuit can be created using 2 AND gates, 2 NOR (inverted OR) gates and 1 NOT gate.

A useful concept to remember is that when you connect the output of 2 NOR gates to the inputs of each other, you create a latch circuit. This circuit will hold on to its previous state, what we need to do is to lock the input to the NOR latch circuit if st is not set. we can do this using AND gates

A NOR latch has this structure:

![nor latch structure](../images/nor-latch-structure.png)

Data latch structure:

![latch structure](../images/latch-structure.png)

___

# Data Flip-Flop (DFF)

## Description

This circuit is similar to the data latch, it is capable of storing and outputing 1 bit of information, but this circuit's output only change when a clock signal input goes HIGH (change from 0 to 1). This event is called a clock tick.

> A clock will continously go HIGH (0 to 1) and LOW (1 to 0). This helps with timing in a computer.
If the store signal is 1 at the time of the clock tick. the input data bit will become the new output and gets stored into the circuit. Otherwise, the output doesn't change.

## Truth table

| st | d | output at clock tick |
| -- | - | ------ |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
| 0 | 1 | previously stored data |
| 0 | 0 | previously stored data |

## Structure

This circuit will need 2 data latches, 1 for latching the data input and wait for the clock to go HIGH, 1 for storing the actual data. We'll also need 2 AND gates and 1 NOT gate to validate the store signal and check if it's a 1 or 0.

> Note: clk is the clock signal
![dff structure](../images/dff-structure.png)

___

# Register

## Description

The register is a very important component as it is responsible for storing immediate data for quick access. A register can hold multiple bits of data. We'll learn how a 2-bit register works. We can later on expand this 2-bit register work hold more bits using the same technique.

The register functions similarly to the data flip-flop, except it can store multiple bits.

## Structure

This circuit can be created with 2 DFFs. It is quite simple. With this simple structure, we can keep adding onto it and create a register that can store however many bits we want.

![register structure](../images/register-structure.png)

___

# RAM

## Description

The RAM (Random Access Memory) is a memory unit with multiple different registers that we can address and use.

For this circuit, we'll build a simple RAM circuit with 2 16-bit registers that we can address using a 1-bit address signal (ad). When ad is 1, the second register will be picked. When ad is 0, the first register will be picked.

## Structure

This circuit will need 2 16-bit registers, a switch to select which registers to use based on the ad signal, and a 16-bit multiplexer to select which of the 2 registers to output.

> Note: d16 is the 16-bit number to be stored.
![ram structure](../images/ram-structure.png)
Loading

0 comments on commit 38cabe6

Please sign in to comment.