A rebuild of 6.191 Lectures 1 to 9, from voltages to pipelined circuits. It starts from zero, puts the ideas that generalize first, and then drills the problem types this material is usually tested with. The worked code is from your own lab1-yumelody, lab2-yumelody and lab3-yumelody.
Every topic below is one of these five ideas with details attached. If you can explain each idea in a sentence and draw its picture, the details have somewhere to go.
A wire carries a voltage, and voltages pick up noise. Digital circuits survive because every gate accepts a sloppy input and produces a clean output, so errors do not accumulate.
The same idea recurs at every level of the course. A flip-flop cleans up timing the way a gate cleans up voltage. A valid bit cleans up the question of whether data is real. The pattern is: define what counts as acceptable, demand more of outputs than of inputs, and the gap between them is your safety margin.
A combinational circuit has no memory, so it is completely described by a table of outputs for every input. Any such table can be built from AND, OR and NOT, or from NAND alone.
This chain always works, which is why combinational design is mechanical. The table doubles with every added input, so real designs do not flatten everything into one table. They build small blocks (a full adder, a mux) and compose them. The skill tested is moving between adjacent boxes in either direction.
After an input changes, the output stays at its old value for at least the contamination delay and reaches its new value by the propagation delay. In between it is garbage.
The slow bound, tPD, sets how fast the clock can run. The fast bound, tCD, decides whether a value changes too early and corrupts the register that is still capturing the previous one. Every timing problem is one of these two inequalities.
Put registers around a combinational function and feed the output back. The function computes the next state, the clock decides when it becomes the current state. That loop is every sequential circuit.
A counter, a finite state machine, your folded multiplier, your cache and your whole processor all have this shape. In Minispec the registers are Reg, the logic is a rule, and the outputs are methods. Once you see the shape, an unfamiliar module is read the same way every time: what is the state, what is the next-state function.
Cutting a long computation with registers lets several inputs be in progress at once. Each result takes at least as long as before, but results come out more often.
| Way to build the same function | Clock period | Latency | Throughput | Area |
|---|---|---|---|---|
| Combinational: all the logic in one cycle | long | one long cycle | one result per long cycle | all the logic |
| Pipelined: same logic cut by registers | short | several short cycles | one result per short cycle | all the logic plus registers |
| Folded: one small piece reused over several cycles | short | several short cycles | one result per several cycles | one piece plus control |
Lab 3 built all three for multiplication and sorting. Lab 6 is the pipelined row applied to a processor. There is no best row. The question is always which of time, throughput, area and power the design is short of.
Four voltage thresholds define a logic family. Outputs must be more extreme than inputs are required to be. The difference is the noise margin.
| Threshold | Meaning |
|---|---|
| VOL | A gate driving 0 must output at or below this |
| VIL | A gate must treat any input at or below this as 0 |
| VIH | A gate must treat any input at or above this as 1 |
| VOH | A gate driving 1 must output at or above this |
They must be ordered VOL < VIL < VIH < VOH. Inputs between VIL and VIH are in the forbidden zone, where the gate promises nothing.
If every input to a device is a valid input (below VIL or above VIH), then after tPD every output is a valid output (below VOL or above VOH). Valid in gives valid out. This is the contract that lets you stack gates without thinking about voltages again.
The voltage transfer characteristic plots a device's output voltage against its input voltage. For the discipline to hold with positive noise margins, the curve must stay out of two forbidden boxes, which forces it to be steeper than slope 1 somewhere in the middle. A device with gain below 1 everywhere, such as a wire or a resistor divider, cannot restore signals.
With VIL = 1.5 V the output must be at least VOH = 4.5 V for every input at or left of 1.5. With VIH = 3.5 V it must be at most VOL = 0.5 V for every input at or right of 3.5. The curve may do anything in between, and it has to fall 4 V while the input moves 2 V, so the gain there exceeds 1.
Recall
Low margin = VIL − VOL = 1.2 − 0.3 = 0.9 V. High margin = VOH − VIH = 4.4 − 3.0 = 1.4 V. The guarantee must hold for both values, so the tolerable noise is the smaller one, 0.9 V.
The wire between an output and the next input adds noise. If the output only just met the input requirement, any noise would push it into the forbidden zone. The stricter output rule leaves room for that noise.
No. At an input of exactly VIL = 2.0 V, which is a valid 0, the output is 4.0 V, below VOH = 4.5 V. Valid in gave invalid out. Either lower VIL to about 1.0 V or lower VOH to 4.0 V, and then recheck the ordering and the margins.
No. The output range [VOL, VOH] is wider than the input range [VIL, VIH] it must be crossed within. Covering a larger output swing over a smaller input swing needs gain above 1 somewhere.
A truth table with n inputs has 2n rows. Write one AND term for each row where the output is 1, OR them together, and you have a correct circuit. Simplifying only makes it smaller.
| A | B | AND | OR | XOR | NAND | NOR |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 | 0 |
A′ means NOT A. Adjacent 1s in the map differ in one variable, and that variable drops out of the term. Bigger rectangles of 1s give smaller terms.
| Name | AND form | OR form | Use |
|---|---|---|---|
| Identity, null | a·1 = a, a·0 = 0 | a+0 = a, a+1 = 1 | Remove constants |
| Complement | a·a′ = 0 | a+a′ = 1 | The step that removes a variable |
| Distributive | a·(b+c) = a·b + a·c | a+(b·c) = (a+b)·(a+c) | Factor out a common term |
| Combining | a·b + a·b′ = a | (a+b)·(a+b′) = a | What a Karnaugh rectangle does |
| Absorption | a·(a+b) = a | a + a·b = a | A bigger term swallows a smaller one |
| De Morgan | (a·b)′ = a′ + b′ | (a+b)′ = a′·b′ | Push a NOT through, flipping the operator |
Universal gates. NAND alone can build everything: NOT a = NAND(a, a). a AND b = NOT(NAND(a, b)). a OR b = NAND(NOT a, NOT b), by De Morgan. NOR is universal the same way. AND and OR without NOT are not universal, because they can never make an output fall when an input rises.
Lab 1 made you write functions gate by gate. This one is not the flat sum of products (which would be four 4-input ANDs). It uses structure: a 4-bit value is a power of two when exactly one pair has exactly one bit and the other pair has none. Finding structure like this is what beats the mechanical method.
Recall
Output is 1 for ABC = 011, 101, 110, 111. Full form: A′BC + AB′C + ABC′ + ABC.
Duplicate ABC twice (x = x + x): (A′BC + ABC) + (AB′C + ABC) + (ABC′ + ABC) = BC + AC + AB. Select majority in the tool above to check.
Factor A: A·(B + B′·C). By the second distributive law B + B′C = (B + B′)(B + C) = B + C. So F = A·(B + C) = AB + AC.
a + b = (a′·b′)′ by De Morgan = NAND(a′, b′). Each inversion is a NAND with its inputs tied together. Total 3 NAND gates.
The table has 23 = 8 rows and each row's output is a free choice of 0 or 1, so 28 = 256 functions. In general 2(2n).
Outer NOT over an OR: (A·B)′ · (C′)′ = (A′ + B′)·C = A′C + B′C.
Both are monotonic: raising an input can never lower the output. Any circuit built only from them is also monotonic, so it can never implement NOT.
Big circuits are small circuits copied and wired together. A Minispec function is a description of that wiring. Loops and parameters run at compile time and leave only gates behind.
An n-bit pattern read as unsigned is the sum of its bits times powers of two. Two's complement gives the top bit the weight −2n−1 instead of +2n−1. The range is −2n−1 to 2n−1 − 1. To negate, invert every bit and add 1. The payoff is that one adder handles signed and unsigned numbers, and subtraction is addition of the negation.
| You write | You get |
|---|---|
| Bit#(1) x = a & b; | A wire named x driven by an AND gate. Reassigning x later makes a new wire with the same name. |
| if (s == 1) r = a; else r = b; | A multiplexer. Both a and b are computed. s only selects. |
| for (Integer i = 0; i < n; …) | n copies of the loop body. Nothing loops at run time. |
| rca#(32) | The parameter is fixed at compile time. rca#(4) and rca#(32) are different circuits from one description. |
| case (x) … | A wide multiplexer, or a lookup table as in your seven_segment_decoder. |
tPD of a circuit is the largest sum of gate tPDs along any input-to-output path. tCD is the smallest sum of gate tCDs along any path. The critical path of a ripple-carry adder is the carry chain, so its delay grows linearly with width. Faster adders spend area to shorten that chain.
Ripple-carry delay grows as n. A carry-lookahead adder computes carries with a tree, so its delay grows as log n, at the cost of more gates. A carry-select adder sits between: it computes the upper half for both possible carries and picks one with a mux.
multiplexer8_4way.Recall
6 = 0110. Invert: 1001. Add 1: 1010. Check: −8 + 2 = −6.
1011 = −8 + 2 + 1 = −5.
The worst path enters at bit 0, ripples through the carry of adders 0 to 30, then goes carry-in to sum in adder 31. For this estimate take carry-out from any input as 2 ns: 31 × 2 + 3 = 65 ns. As a formula, (n − 1)·tcarry + tsum.
addSub subtract with an adder?a − b = a + (−b) = a + ~b + 1. It inverts b and feeds 1 into the carry-in of bit 0, so the +1 is free.
Barrel: 5 layers × 32 bits = 160 muxes, with 5 muxes on the longest path. A 32-way mux is 31 two-way muxes, and 32 of them is 992 muxes, also 5 deep. Same delay, about six times the area.
rca#(4), how many times does the for loop execute when the circuit runs?Zero. The loop runs in the compiler and produces four chained full adders. At run time there is only combinational logic settling.
As unsigned, yes: 7 + 1 = 8. As two's complement, no: 7 + 1 produced −8. Signed overflow happens when two operands with the same sign give a result with the other sign.
A CMOS gate is two switch networks. The pull-up connects the output to power, the pull-down connects it to ground. They are built as duals so exactly one conducts for every input.
| Transistor | Conducts when gate input is | Passes well | Lives in |
|---|---|---|---|
| nFET | 1 | 0 (ground) | Pull-down network |
| pFET | 0 | 1 (power) | Pull-up network |
Raising an input turns nFETs on and pFETs off. That can only strengthen the path to ground and weaken the path to power, so the output can fall or stay but never rise. A single gate therefore implements only functions where raising an input never raises the output: NOT, NAND, NOR, and compound forms such as (A·B + C)′. AND is a NAND followed by an inverter.
| Gate | Pull-down (nFETs) | Pull-up (pFETs) | Transistors |
|---|---|---|---|
| NOT A | A | A | 2 |
| NAND(A, B) | A in series with B | A in parallel with B | 4 |
| NOR(A, B) | A in parallel with B | A in series with B | 4 |
| (A·B + C)′ | (A series B) parallel C | (A parallel B) series C | 6 |
Recall
Pull-down: (A parallel B) in series with C. Pull-up is the dual: (A series B) in parallel with C. Six transistors. Check one row: A = 1, B = 0, C = 1 makes the pull-down conduct, so F = 0, and (1 + 0)·1 = 1 inverted is 0.
No. With A = B = 0, raising C from 0 to 1 raises F from 0 to 1. A single gate cannot make its output rise when an input rises. Build (A·B + C)′ and follow it with an inverter: 8 transistors.
If both conduct for some input, power is shorted to ground and the output is an invalid middle voltage. If neither conducts, the output floats and keeps whatever charge it had. Both break the static discipline.
An nFET passes a strong 0 but a weak 1, and a pFET passes a strong 1 but a weak 0. Using each for the value it passes well gives outputs that reach all the way to the rails, which is what restores the signal.
A D flip-flop copies its input to its output at the rising clock edge and ignores it the rest of the time. For that to be reliable, the input must be steady in a small window around the edge.
Setup time is how long before the edge D must be stable. Hold time is how long after. If D changes inside the window the flip-flop can go metastable: its output hovers at an invalid voltage for an unbounded time.
Take any path from one register, through combinational logic, to another register on the same clock.
An FSM is a set of states, a start state, a rule for the next state given the current state and input, and a rule for the output. In a Moore machine the output depends on the state only. In a Mealy machine it depends on the state and the current input, so it can react a cycle earlier and often needs fewer states. N states need at least ⌈log2 N⌉ state bits. One-hot encoding uses N bits and simpler logic.
flowchart LR S0((S0
out 0)) -->|1| S1((S1 saw 1
out 0)) S0 -->|0| S0 S1 -->|1| S1 S1 -->|0| S2((S2 saw 10
out 0)) S2 -->|0| S0 S2 -->|1| S3((S3 saw 101
out 1)) S3 -->|0| S2 S3 -->|1| S1
Recall
Setup: tCLK ≥ 0.5 + 4 + 0.4 = 4.9 ns, about 204 MHz. Hold: 0.1 + 0.3 = 0.4 ≥ 0.2, met with 0.2 ns to spare. These are the default slider values above.
Logic tCD = 0. Fast path = 0.1 + 0 = 0.1 ns, less than 0.3. Hold is violated. No clock period fixes this. Add a buffer with at least 0.2 ns of contamination delay on that wire.
It is a race within a single clock edge. The same edge that launches the new value from the first register is the edge at which the second register is capturing the old one. How long until the next edge is irrelevant.
One clock serves every path, so the slowest path decides: 1 + 7 + 1 = 9 ns. Speeding up the 3 ns or 5 ns path changes nothing until the 7 ns path is shortened.
Binary: ⌈log2 6⌉ = 3 flip-flops, with two of the eight codes unused. One-hot: 6 flip-flops, one per state.
In S3 the last inputs were 1, 0, 1. After another 0 the most recent inputs end in 1, 0, which is the prefix S2 remembers. Going to S0 would miss the overlapping match in 10101.
A module is registers plus rules. Each cycle every rule reads the current register values, computes, and schedules writes. All writes land together at the clock edge.
| Construct | Hardware | Rule to remember |
|---|---|---|
| Reg#(T) x(init) | Flip-flops with a reset value | Reading x gives the value from the start of the cycle |
| x <= v; | Wire into the flip-flop input | Takes effect at the next edge. At most one write per register per cycle |
| rule | The next-state logic | Runs every cycle. Local variables are wires |
| input | Wire from the parent | Has a default when not driven |
| method | Combinational output | Computed from registers, visible this cycle |
Because writes are simultaneous, a <= b; b <= a; swaps the two registers. Both right-hand sides are read before either write happens.
Read it with Idea 4. The state is a, b, prod and the counter i. The next-state function has two cases: start, or do one more step. The output is valid only when the counter says all n bits are done. The counter is an FSM whose states are idle, 1, 2, … n.
Recall
x <= y; y <= x + y;. What are x and y after one cycle? After two?Cycle 1 reads x = 3, y = 5 and writes x = 5, y = 8. Cycle 2 reads x = 5, y = 8 and writes x = 8, y = 13. The write to x does not affect the y computed in the same cycle.
FoldedMultiplier#(4) gets a valid input in cycle 0. In which cycle is getResult first valid?Cycle 0 writes i = 1. Cycles 1, 2 and 3 process bits 1, 2 and 3 and write i = 2, 3, 4. In cycle 4 the register i reads 4 = n, so the result is valid in cycle 4. In general, n cycles after the input.
Bit#(8) t = x + 1; and x <= x + 1; inside a rule?The first names a wire that carries x + 1 right now, in this cycle. The second schedules the register x to hold x + 1 from the next cycle on.
getResult return a Maybe?The module takes several cycles, and the consumer needs to know whether prod is a finished product or a partial sum. The valid bit carries that information next to the data.
Throughput is set by the clock period, and the clock period is set by the slowest stage. Cut the circuit so the stages are balanced, and cut every path the same number of times.
| Quantity | Definition | For a K-stage pipeline |
|---|---|---|
| Clock period | Time between rising edges | max stage delay + register overhead |
| Latency | Time from an input entering to its result leaving | K × clock period |
| Throughput | Results per unit time | 1 / clock period |
Click between blocks to place or remove a pipeline register. There is always one register at the output. Try to reach an 8 ns period with the fewest cuts, then cut everywhere and see what the unbalanced blocks cost.
A K-stage pipeline is well formed when every path from any input to any output passes through exactly K registers. If one path crosses two registers and another crosses one, values from different inputs meet in the same gate and the result is wrong. The drawing method: draw a line across the circuit that every signal crosses in the same direction, and place a register wherever the line crosses a wire.
Each register reads the one before it, and all three are written in the same rule. Because reads see old values, the three assignments describe three stages working on three different inputs in the same cycle. The Invalid case passes bubbles along, so a cycle with no input produces a cycle with no output three cycles later.
Recall
Unpipelined: 24 ns period, 24 ns latency, 1 result per 24 ns.
Two stages: the options split the total as 3|21, 8|16, 10|14, 16|8, 20|4. Best is 10|14: period 14 ns, latency 28 ns, 1 per 14 ns.
Three stages: 3+5 | 2+6 | 4+4 gives 8|8|8. Period 8 ns, latency 24 ns, 1 per 8 ns. Use the tool above to check.
Period = the largest block = 6 ns. Latency = 6 × 6 = 36 ns. Throughput 1 per 6 ns. Six stages gave only a 4× speed-up in throughput over 24 ns because the stages are unbalanced: the 2 ns stage wastes 4 ns every cycle.
Period = 8 + 1 = 9 ns. Latency = 27 ns, now worse than the 24 ns combinational circuit. Throughput 1 per 9 ns. Pipelining never improves latency and real registers make it worse.
No. The A path crosses the register and the direct path does not, so B combines the previous cycle's A result with the current cycle's direct input. Add a register on the direct path as well.
PipelineMath, an input arrives in cycle 0. In which cycle does out show its result?The edge ending cycle 0 writes stage1. The edge ending cycle 1 writes stage2. The edge ending cycle 2 writes result. out reads the register, so it is valid during cycle 3.
The same function can be built many ways. Each way is a point with coordinates (area, delay, power). Design is choosing the point that fits the constraint you actually have.
| Lab 3 multiplier | How | Area | Clock period | Cycles per result | Results in flight |
|---|---|---|---|---|---|
| multiply_by_adding | n adders chained combinationally | n adders | n adder delays | 1 | 1 |
| FoldedMultiplier | 1 adder, used n times | 1 wide adder + registers | about 1 adder delay | n | 1 |
| FastFoldedMultiplier | Shift the product right so the adder is only n+1 bits | 1 narrow adder + registers | shorter still | n | 1 |
| Pipelined (not in the lab) | n adders with registers between | n adders + n registers | about 1 adder delay | 1 (after filling) | n |
The folded module is this loop with the loop variable turned into a register. That conversion is general: any loop that unrolls into a chain can instead be folded into one copy plus a counter, trading time for area.
Dynamic power is roughly α·C·V2·f: how often nodes switch, how much capacitance they drive, the supply voltage squared, and the clock frequency. Lowering voltage helps the most but slows the gates. Running slower, or switching fewer nodes, saves power directly. A folded design has less capacitance but runs for more cycles, so energy per result may not improve.
Recall
Combinational: 48 ns per result. Folded: 16 × 4 = 64 ns per result. The combinational design wins on both latency and throughput here. The folded design wins on area, and on clock period if the multiplier shares a clock with other logic that could run at 4 ns.
Period = 48/4 + 1 = 13 ns. Latency = 4 × 13 = 52 ns. Throughput = 1 per 13 ns, about 3.7 times the combinational design, for the cost of three extra rows of registers.
Power scales with V2: 0.82 = 0.64, so 36% less. In practice the gates also get slower at lower voltage, so the frequency may have to drop too.