

# Subtraction
## Subtraction
- You can use the same adder circuitry to support subtraction
- convert **B** into it's negative value
- flip all the bits (NOT gate)
- add 1 (give a 1 to **Cin**)
[Adder/Subtractor test vector](../../assets/addersubtractor4bit_test_vector.txt)
## Supporting `slt`
You can even use your adder for *set on less than*
- set the adder to do subtraction
- look at the highest bit of the result
+ if it is 1, then the subtraction result was negative, so set result to 1
+ if it is 0, then the subtraction result was not negative, so set result to 0
## Exercise
- Work on getting subtraction and `slt` working in your 4-bit ALU
## Assignment
- [Assignment 6](../../assignments/assignment-6)
- make a 32-bit ALU
- `add`, `sub`, `and`, `or`, `not`, `xor`, `slt`, and anything else you want
- you can use Logisim's built-in components
- try not to duplicate circuitry for `add`, `sub`, and `slt`.
- submit screenshots of circuits *and* test vector (with comments)
# Sequential Logic
## Combinational VS Sequential
- A **combinational circuit** is:
+ Relies solely on the input to resolve its output
+ Stateless / memoryless / doesn't depend on previous states
---
- A **sequential circuit** is:
+ Relies on input *and* current state to resolve its output
+ Has memory
## S-R Latch
- A "set reset" latch is implemented with

- Let's fill in the truth table together
$S$ | $R$ | $Q$ | $\overline{Q}$ | $Q_\text{next}$ | $\overline{Q}_\text{next}$ |
0 | 0 | 0 | 1 | | |
0 | 0 | 1 | 0 | | |
1 | 0 | 0 | 1 | | |
1 | 0 | 1 | 0 | | |
0 | 1 | 0 | 1 | | |
0 | 1 | 1 | 0 | | |
## D Latch
- A "data" latch is implemented with

- Let's fill in the truth table together
$C$ | $D$ | $Q_\text{next}$ |
0 | 0 | |
0 | 1 | |
1 | 0 | |
1 | 1 | |
## Clocks
- Latches are controlled by clocks that regularly trigger


## Exercise
- Experiment with these in Logisim
- How can you get it to store a 1 in the latch?
- How can you get it to store a 0 in the latch?


## D Flip Flop
- A (falling edge-triggered) **D flip flop** can be implemented with two D latches in series
- This configuration creates a **delay** so that the output is only changed on the **falling edge** of the clock

- a rising edge-triggered flip flop can be constructed similarly
## Registers
- A **register** is a multi-bit memory component
- Registers are commonly implemented with an array of D flip flops
## Register Files
- A **register file** consists of an array of registers that can be read and written to

## Register Files
- **Reading** is implemented with multiplexers

## Register Files
- **Writing** is implemented with a decoder
