How Clocks Turn Manual Logic into Automated Calculation?
Source: Dev.to
How We Perform Addition
To understand how a computer works, let’s first look at how we add a list of numbers, such as [24, 34, 77].
- Add the first two numbers:
24 + 34 = 58. - Store the intermediate result (58) in memory or write it down.
- Add the final number to the intermediate result:
58 + 77 = 135.
In this process we used our mind as a “memory” to hold the number 58.
Designing a Simple Circuit
Circuit Components
- 8‑bit switch – why do we need it?
- Adder – performs the addition.
- Latch (memory) – holds the intermediate result.
Addition Sequence Using the Circuit
| Step | Latch (memory) | Input | Adder Calculation | Action |
|---|---|---|---|---|
| Start | 0 | 24 | 0 + 24 = 24 | Store 24 in the latch by pressing the switch (closes the circuit). |
| Next | 24 | 34 | 24 + 34 = 58 | Store 58 in the latch. |
| Next | 58 | 77 | 58 + 77 = 135 | Store 135 in the latch. |
In theory this looks perfect, but a real circuit won’t work this way.
The Problem: Feedback‑Loop Error
Electricity travels incredibly fast. When you press a manual switch, the gate stays open for as long as your finger is on the button. Even a tiny press (e.g., 0.1 s) is seen by the CPU as a “High” signal for thousands of nanoseconds.
Because the latch output is fed back into the adder, a feedback loop occurs:
| Time | Situation | Result |
|---|---|---|
| 0 s | Button pressed. | 0 (memory) + 24 (input) = 24. |
| 0.1 s (still pressing) | Latch sees the High signal, adds the same 24 again. | 24 + 24 = 48. |
| 0.2 s | Repeats. | 48 + 24 = 72. |
| … | … | … |
Before you can even lift your finger, the circuit has added the same number hundreds of times, producing a giant, incorrect number. This is the Feedback Loop Error.
Root cause: the circuit stays “open” for too long. We need it to open only once per click, regardless of how long the button is held.
Understanding the Button Press (Scenarios)
| Press # | Hold Time | Release Time |
|---|---|---|
| 1 | 1 s | 2 s |
| 2 | 3 s | 1 s |
| 3 | 0.5 s | — |
If plotted, the waveform looks like this:
- High State (top) – button pressed.
- Low State (bottom) – button not pressed.
The Four Stages of a Signal
- Rising Edge – the exact moment the button is pressed (0 → 1).
- High Level – duration while the button is held down.
- Falling Edge – the exact moment the button is released (1 → 0).
- Low Level – duration while the button is not pressed.
The Rising Edge and Falling Edge occur only once per press, no matter how long the button is held. If we make the circuit react only to the Rising Edge, we get exactly one addition per click.
Latch vs. Flip‑Flop
| Feature | Latch (Level‑Triggered) | Flip‑Flop (Edge‑Triggered) |
|---|---|---|
| Activation | Active for the entire High level | Activates only on the Rising Edge |
| Control | Hard to control (continuous) | Precise, single‑pulse operation |
Solution: replace the latch with a Flip‑Flop and drive it with the button’s CLK (clock) pin.
The Solution: Edge Triggering
Redesigning the Circuit
- Replace the latch with a Flip‑Flop.
- Connect the adder’s output directly to the Flip‑Flop’s data input.
- Connect the button to the Flip‑Flop’s CLK pin.
Operation Sequence
| Step | Flip‑Flop State | Input | Adder Calculation | Action |
|---|---|---|---|---|
| Start | 0 | 24 | 0 + 24 = 24 | On the Rising Edge, the Flip‑Flop captures 24 and ignores the rest of the press. |
| Next | 24 | 34 | 24 + 34 = 58 | Capture 58 on the next Rising Edge. |
| Next | 58 | 77 | 58 + 77 = 135 | Capture 135 on the next Rising Edge. |
Some CPU circuits (e.g., counters) are built from flip‑flops.
Automating with a Clock Signal
Clock Cycle Overview
| Cycle | Stage | Description |
|---|---|---|
| 1 | Adder Stage | Rising Edge – inputs are presented to the adder. High Level – adder performs the addition. Falling Edge – adder finishes computation (propagation delay). Low Level – circuit waits, allowing the result to stabilize. |
| 2 | Capture Stage | Rising Edge – flip‑flop latches the adder’s result and the system moves to the next number. High/Falling/Low Levels – preparation for the next addition. |
The Brain: Control Unit
In a real CPU, the Control Unit acts like a conductor. Using an internal counter, it tracks clock cycles and tells every part of the computer exactly when to push data and when to save it.
Why an Adder Is Not Edge‑Triggered
Unlike latches or flip‑flops, an adder has no clock pin. It does not wait for a signal to start adding; it is always “on.” As soon as any input changes, the output (sum) begins changing immediately.
Delay – the only timing consideration for an adder is its propagation delay, the time it takes for the new sum to become stable after the inputs change. This delay is why we need separate clocked stages (adder → capture) to ensure reliable operation.
Propagation Delay and Clock Speed
The output isn’t “instant” because of propagation delay – the physical time it takes electricity to travel through all the transistors inside the adder.
We must make sure our clock isn’t too fast, or the flip‑flop might try to store the result before the adder has finished the calculation!
Adding the Numbers [24, 34, 77]
The computer breaks the work into cycles. Each cycle follows the “heartbeat” of the clock.
| Cycle | Phase | What Happens |
|---|---|---|
| 1 – The Work | Rising edge | The flip‑flop starts at 0. The system pushes 0 and the first input (24) into the adder. |
| High / Falling / Low levels | The adder performs the addition. The system waits during the Low level to ensure the propagation delay is over and the result (24) is stable. | |
| 2 – The Save | Rising edge | The flip‑flop captures the 24 from the adder. The control unit then selects the next number (34) for the next step. |
| High / Falling / Low levels | Nothing (the data is simply stored). | |
| 3 – The Work | Rising edge | The flip‑flop (now holding 24) and the new input (34) are sent to the adder. |
| High / Falling / Low levels | The adder works through the addition. By the end of the Low level, the result 58 is ready. | |
| 4 – The Save | Rising edge | The flip‑flop captures 58. The control unit selects the final number (77). |
| 5 – The Final Work | Rising edge | The flip‑flop sends 58 and the input 77 to the adder. |
| High / Falling / Low levels | The adder completes the calculation. | |
| 6 – The Final Result | Rising edge | The flip‑flop captures the final total: 135. |
Controlled Automation
By the end of these cycles, the addition is perfectly finished. This is controlled automation: every step has its own specific “moment” on the clock wave, so operations never collide or interfere with each other.
The primary job of the clock is synchronization.
- Clock speed must be chosen based on the slowest circuit in the system. In our example, the adder is the most complex part.
- If the clock is set too fast, the flip‑flop might try to capture data before the adder has finished its work.
- The Low level of the clock must be long enough to cover the adder’s propagation delay, guaranteeing that every captured result is 100 % accurate.
Controlled Automation (Repository)
The above description outlines the timing and synchronization strategy used in the repository.