## Circuit

Basic arithmetic circuits and ALU

### Addition Circuit

output with **carry** and **sum**

x | y | c | s |
---|---|---|---|

0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 |

1 | 0 | 0 | 1 |

1 | 1 | 1 | 0 |

output with **c_out** and **sum**, input with **x, y, c_in**.

x | y | c_in | c_out | s |
---|---|---|---|---|

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 |

### Full Adder

can produce 4-bit addition, by adding this module, it can make a larger addition.

This structure can also be applied to do substation by true negative sign to positive by `~B+1`

### Subtractor

We can add a XOR to be toggle from subtraction and addition:

$$ x\oplus 1 = x' $$

$$ x\oplus 0 = x $$

### Multiplexers

merge multi inputs to a single output

*S1 and S0 control which input to be output*

If there are multi-multiplexers, we can simplified the system using array-like notation

### Arithmetic logic unit (ALU)

can perform both logical and arithmetic operation

We can build 32-bit ALU by extending, and carry the first control bit (determine arithmetic or logic) to c_1

## More Verilog

**bus**: represents multiple wires

*indicate by drawing slash and write how many wires in this bus*

In Verilog, we use `[]`

notation to indicate bus:

$$ a[3:0] $$

*from three down to zero, 0 indicate the lowest bit, and 3 is the highest bit*

**constant**: we can assign constant in Verilog by following below syntax

```
'define ACONSTANT 8'hd7; // 8 bit, hex, d7
'define ANOTHERCONSTANT 32'd1337; // 32 bit, decimal, 1337
```

**compare**

`A[1:0] == 2'b10; // result is 1 bit, true or false`

野兽后辈tql