Skip to content
ImagineWorld

Sequential Logic

node_idnode-book_the-secret-life-of-programs_chapter-06-sequential-logic
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num6
chapter_titleSequential Logic
governed_byCORPUS
constitutional_citationCONSTITUTION
figure_reviewassets/slp-fig-6-01-cartoon-robot-holding-a-sign-with-partial.png, assets/slp-fig-6-02-circuit-diagram-of-an-inverter-with-feedback.png, assets/slp-fig-6-03-schematic-symbol-for-a-crystal.png, assets/slp-fig-6-04-figure-33-an-or-gate-latch-with.png, assets/slp-fig-6-05-figure-34-an-andor-gate-latch-with.png, assets/slp-fig-6-06-figure-35-an-sr-latch-circuit-diagram.png, assets/slp-fig-6-07-circuit-diagram-of-a-gated-sr-latch.png, assets/slp-fig-6-08-circuit-diagram-of-a-gated-d-latch.png, assets/slp-fig-6-09-timing-diagram-for-a-gated-d-latch.png, assets/slp-fig-6-10-d-flipflop-circuit-diagram-with-clock-d.png, assets/slp-fig-6-11-figure-310-a-d-flipflop-operation-showing.png, assets/slp-fig-6-12-figure-311-a-d-flipflop-positive-edge.png, assets/slp-fig-6-13-d-flipflop-symbol-with-inputs-d-ck.png, assets/slp-fig-6-14-timing-diagram-showing-d-ck-and-q.png, assets/slp-fig-6-15-circuit-diagram-of-a-3bit-ripple-counter.png, assets/slp-fig-6-16-timing-diagram-showing-signal-and-c0-c1.png, assets/slp-fig-6-17-circuit-diagram-of-a-3bit-synchronous-counter.png, assets/slp-fig-6-18-schematic-symbol-for-a-counter-with-inputs.png, assets/slp-fig-6-19-block-diagram-of-an-adder-feeding-into.png, assets/slp-fig-6-20-diagram-showing-input-connected-to-four-registers.png, assets/slp-fig-6-21-block-diagram-of-a-memory-component-with.png, assets/slp-fig-6-22-simplified-memory-chip-schematic-with-address-data.png, assets/slp-fig-6-23-block-diagram-of-row-and-column-addressing.png, assets/slp-fig-6-24-memory-with-address-registers-diagram-showing-d.png, assets/slp-fig-6-25-core-memory-with-woven-wire-grid-and.png, assets/slp-fig-6-26-ibm-card-with-punched-holes-and-character.png, assets/slp-fig-6-27-punched-paper-tape-with-rows-of-holes.png, assets/slp-fig-6-28-core-rope-memory-from-the-apollo-guidance.png, assets/slp-fig-6-29-a-blackandwhite-photo-of-an-open-hard.png, assets/slp-fig-6-30-diagram-of-disk-layout-showing-track-cluster.png, assets/slp-fig-6-31-closeup-of-optical-disk-data-pits-and.png, assets/slp-fig-6-32-circuit-diagram-for-even-parity-generation-and.png
local graph

The combinatorial logic you learned about in the last chapter “goes with the flow.” In other words, the outputs change in response to the inputs. But we can’t build computers out of combinatorial logic alone, because it doesn’t give us any way to remove something from the flow and remember it. You can’t add up all the numbers from 1 to 100, for example, unless you can keep track of where you are.

You’ll learn about sequential logic in this chapter. The term comes from the word sequence, which means “one thing after another in time.” As a human, you have intuitive knowledge about time, just as you do about counting on your fingers, but that doesn’t mean that time is natural for digital circuitry. We have to create it somehow.

Combinatorial logic deals only with the present state of inputs. Sequential logic, however, deals with both the present and the past. In this chapter,

[!visual-conversion] cartoon robot holding a sign with partial text ‘LY GE’ Cropped from the scan (PDF page 109, printed p. 69): cartoon robot holding a sign with partial text ‘LY GE’

you’ll learn about circuitry both for generating time and for remembering things. We’ll trace some of the various technologies that have been used for these purposes from their early roots through the present day.

We measure time using some sort of periodic function, such as the rotation of the Earth. We call one full rotation a day, which we subdivide into smaller units such as hours, minutes, and seconds. We could define a second as 1/86,400th of an Earth rotation, since there are 86,400 seconds in a day.

In addition to using an external event like the rotation of the Earth, we can also generate our own periodic functions by applying certain elements of physics, such as the time that it takes for a pendulum to swing. This technique produced the “tick tock” sound in old grandfather clocks. Of course, to be useful, the pendulum has to be calibrated to the measured length of a second.

With computers, we’re working with electronics, so we need a periodic electrical signal. We could generate one by placing a switch so that it’s whacked by a pendulum. But unless you’re a serious steampunk geek, you probably don’t want a pendulum-powered computer. We’ll learn about more modern approaches in the next section.

Let’s look at a trick we can do with an inverter: we can connect the output to the input, as shown in Figure 3-1.

[!visual-conversion] Circuit diagram of an inverter with feedback loop and square … Cropped from the scan (PDF page 110, printed p. 70): Circuit diagram of an inverter with feedback loop and square wave output over time.

Figure 3-1: An oscillator

This produces feedback, just like what you get when a microphone is too close to a loudspeaker. The output of the inverter bounces back and forth, or oscillates, between 0 and 1. The speed at which it oscillates is a function of the propagation delay (see “Propagation Delay” on page 57), and that tends to vary with temperature. It would be useful to have an oscillator with a stable frequency so that we could generate an accurate time reference.

A cost-effective way to do this is with a crystal. Yes, very new age. Crystals, like magnets, have a relationship with electricity. If you attach electrodes (wires) to a crystal and give it a squeeze, it’ll generate electricity. And if you put some electricity on those wires, the crystal will bend. This is called the piezoelectric effect, and it was discovered by brothers Paul-Jacques (1855–1941) and Pierre (1859–1906) Curie in the late 1800s. The piezoelectric effect has all sorts of applications. A crystal can pick up sound vibrations, making

a microphone. Sound vibrations generated by applying electricity to crystals are responsible for the annoying beeps made by many appliances. You can spot a crystal in a circuit diagram by the symbol shown in Figure 3-2.

[!visual-conversion] schematic symbol for a crystal Cropped from the scan (PDF page 111, printed p. 71): schematic symbol for a crystal

Figure 3-2: The crystal schematic symbol

A crystal oscillator alternately applies electricity to a crystal and receives electricity back, using electronic single-pole, double-throw switches. The time it takes a crystal to do this is predictable and very accurate. Quartz is one of the best crystal materials to use. That’s why you see advertisements for accurate quartz timepieces. Keep in mind when you see the price tag on a fancy watch that a really good crystal retails for only about 25 cents.

Oscillators give us a way to measure time, as you’ve seen. Computers need to keep time for obvious reasons, like being able to play a video at a consistent speed. But there’s another, lower-level reason why time is important. In Chapter 2, we discussed how propagation delay affects the time that it takes circuitry to do things. Time gives us a way to wait, for example, for the worst-case delay in an adder before looking at the result so that we know it’s stable and correct.

Oscillators supply clocks to computers. A computer’s clock is like the drummer in a marching band; it sets the pace for the circuitry. The maximum clock speed or fastest tempo is determined by the propagation delays.

Component manufacturing involves a lot of statistics because there’s a lot of variance from part to part. The binning process puts components into different bins, or piles, depending on their measured characteristics. The fastest parts that fetch the highest price go into one bin; slower, less expensive parts go into another; and so on. It’s not practical to have an infinite number of bins, so there’s variance within the parts in a bin, although it’s less than the variance for the whole lot of parts. This is one reason why propagation delays are specified as a range; manufacturers provide minimum and maximum values in addition to a typical value. A common logic circuit design error is to use the typical values instead of the minimums and maximums. When you hear about people overclocking their computers, it means they’re gambling that their part was statistically in the middle of its bin and that its clock can be increased by some amount without causing the part to fail.

Now that we have a source of time, let’s try to remember a single bit of information. We can do that with feedback, such as tying the output of an OR gate back to an input, as shown in Figure 3-3. This doesn’t create an oscillator such as we saw in Figure 3-1, since there’s no inversion. Assume that out

starts off at 0 in the circuit in Figure 3-3. Now, if inin goes to 1, outout does too, and because it’s connected to another input it stays that way, even if inin goes back to 0. In other words, it remembers.

Figure 3-3: An OR gate latch

Of course, this scheme needs some work because there’s no way to make outout be 0 again. We need a way to reset it by disconnecting the feedback, as shown in Figure 3-4.

Figure 3-4: An AND-OR gate latch

Note that we’ve labeled the output of the inverter reset‾\overline{reset} . Putting a line over a symbol is hardware-speak meaning “the opposite.” It means that something is true when it’s a 0 and false when it’s a 1. Sometimes this is referred to as active low instead of active high, meaning that it does its thing when it’s 0 instead of 1. The line is pronounced “bar,” so in speech the signal would be referred to as “reset bar.”

When resetreset is low, reset‾\overline{reset} is high, so the output from the OR gate is fed back into the input. When resetreset goes high, reset‾\overline{reset} goes low, breaking that feedback so that outout goes to 0.

Figure 3-5 shows an S-R latch, a slightly cleverer way of building a bit of memory. S-R stands for set-reset. It has active low inputs and complementary outputs, meaning one is active low and one is active high. You could build a version of this that has active high inputs by using NOR gates, but NOR gates are often more power-hungry than NAND gates, in addition to being more complicated and expensive to build.

Figure 3-5: An S-R latch

$\overline{set}$ $\overline{reset}$ Q $\overline{Q}$
0 0 1 1
0 1 1 0
1 0 0 1
1 1 memory memory

[!visual-conversion] Figure 3-3: An OR gate latch with input and output … Cropped from the scan (PDF page 112, printed p. 72): Figure 3-3: An OR gate latch with input and output waveforms

[!visual-conversion] Figure 3-4: An AND-OR gate latch with reset, set, and … Cropped from the scan (PDF page 112, printed p. 72): Figure 3-4: An AND-OR gate latch with reset, set, and out signals and timing diagram

[!visual-conversion] Figure 3-5: An S-R latch circuit diagram with NAND gates … Cropped from the scan (PDF page 112, printed p. 72): Figure 3-5: An S-R latch circuit diagram with NAND gates labeled S and R, inputs set/reset, and outputs Q and Q̅.

The case where both set‾\overline{set} and reset‾\overline{reset} are active is weird and not intended for use, because both outputs are true. Also, if both inputs become inactive (that is, transition from 0 to 1) at the same time, the state of the outputs is not predictable because it’s dependent on the propagation delays.

The circuit in Figure 3-5 has a nice property that the circuit in Figure 3-4 does not, which is that its design is symmetrical. That means the propagation delays are similar for both the setset and resetreset signals.

Now that we have some way of remembering information, let’s look at what it takes to remember something at a point in time. The circuit in Figure 3-6 has an extra pair of gates added to the inputs.

Figure 3-6: A gated S-R latch

As you can see, when the gate‾\overline{gate} input is inactive (high), it doesn’t matter what set‾\overline{set} and reset‾\overline{reset} are doing; the outputs won’t change because the inputs to the S and R gates will both be 1.

Because we want to remember one bit of information, the next improvement we can make is to add an inverter between the set‾\overline{set} and reset‾\overline{reset} inputs so that we need only a single data input, which we’ll abbreviate as DD . This modification is shown in Figure 3-7.

Figure 3-7: A gated D latch

Now, if DD is a 1 when the gate‾\overline{gate} is low, the QQ output will be set to 1. Likewise, if DD is a 0 when the gate‾\overline{gate} is low, the QQ output will be set to 0. Changes on DD

[!visual-conversion] Circuit diagram of a gated S-R latch with set, reset, … Cropped from the scan (PDF page 113, printed p. 73): Circuit diagram of a gated S-R latch with set, reset, and gate inputs.

[!visual-conversion] Circuit diagram of a gated D latch with D, gate, … Cropped from the scan (PDF page 113, printed p. 73): Circuit diagram of a gated D latch with D, gate, set, reset inputs and Q, Q̅ outputs.

when gate‾\overline{\text{gate}} is high have no effect. That means we can remember the state of DD . You can see this in the timing diagram shown in Figure 3-8.

Figure 3-8: A gated D latch timing diagram

The problem with this circuit is that changes in DD pass through whenever the gate‾\overline{\text{gate}} is low, as you can see in the shaded section. This means we have to count on DD being “well-behaved” and not changing when the “gate” is “open.” It would be better if we could make the opening instantaneous. We’ll see how to do that in the next section.

As we discussed in the last section, we want to minimize the chances of getting incorrect results due to changing data. The way that’s commonly done is to use the transition between logic levels to grab the data instead of grabbing it when the logic level has a particular value. These transitions are called edges. You can think of an edge as a decision criterion for time. Back in Figure 3-8, you can see the almost-instantaneous transition between logic levels. Edge-triggered latches are called flip-flops.

Latches are a building block used to make flip-flops. We can construct a positive edge-triggered flip-flop called a D flip-flop by cleverly combining three S-R latches, as shown in Figure 3-9. Positive edge-triggered means that the flip-flop operates on the transition from a logic 0 to a logic 1; a negative edge-triggered flip-flop would operate on the transition from a logic 1 to a logic 0.

Figure 3-9: A D flip-flop design

[!visual-conversion] Timing diagram for a gated D latch showing D, gate, … Cropped from the scan (PDF page 114, printed p. 74): Timing diagram for a gated D latch showing D, gate, and Q signals with Q←D and D ignored phases.

[!visual-conversion] D flip-flop circuit diagram with clock, D, Q, Q̅, S̅, … Cropped from the scan (PDF page 114, printed p. 74): D flip-flop circuit diagram with clock, D, Q, Q̅, S̅, R̅ inputs and NAND gates

This circuit can be somewhat mind-boggling. The two gates on the right form an S-R latch. We know from Figure 3-5 that those outputs won’t change unless either S‾\overline{S} or R‾\overline{R} goes low.

Figure 3-10 shows how the circuit behaves for various values of DD and clock. The thin lines show logic 0s; the thick lines are logic 1s.

Figure 3-10: A D flip-flop operation

Starting at the left, you can see that when the clock is 0, the value of DD doesn’t matter because both S‾\overline{S} and R‾\overline{R} are high, so the state of the latch on the right-hand side of Figure 3-9 is unchanged. Moving toward the right, you can see in the next two diagrams that if R‾\overline{R} is low, changing the value of DD has no effect. Likewise, the two rightmost diagrams show that if S‾\overline{S} is low, changing the value of DD has no effect. The upshot is that changes to DD have no effect when the clock is either high or low.

Now, let’s look at what happens when the clock changes from low to high, as shown in Figure 3-11.

Figure 3-11: A D flip-flop positive edge operation

[!visual-conversion] Figure 3-10: A D flip-flop operation showing five circuit states … Cropped from the scan (PDF page 115, printed p. 75): Figure 3-10: A D flip-flop operation showing five circuit states with logic 0s and 1s.

[!visual-conversion] Figure 3-11: A D flip-flop positive edge operation showing four … Cropped from the scan (PDF page 115, printed p. 75): Figure 3-11: A D flip-flop positive edge operation showing four circuit states with logic 0s and 1s.

You can see on the left that when the clock is low and DD is high, S‾\overline{S} and R‾\overline{R} are high, so nothing changes. But when the clock changes to 1, S‾\overline{S} goes low, which changes the state of the flip-flop. On the right, you can see similar behavior when DD is low and the clock goes high, causing R‾\overline{R} to go low and changing the flip-flop state. You saw in Figure 3-10 that no other changes matter.

In 1918 British physicists William Eccles and Frank Jordan invented the first electronic version of a flip-flop, which used vacuum tubes. Figure 3-12 shows the diagram for a slightly less antique DD flip-flop called the 7474.

Figure 3-12: A D flip-flop

The D flip-flop has complementary QQ and Q‾\overline{Q} (outputs and S‾\overline{S} (set) and R‾\overline{R} (reset) inputs. It’s a little confusing, as the diagram shows SS and RR ; it’s the combination of those with the ∘\circ that make them S‾\overline{S} and R‾\overline{R} . So, except for the mysterious things on the left-hand side, it’s just like our S-R latch. The mysterious things are two extra inputs, DD for data and CKCK for clock, which is represented by the triangle. It’s positive edge-triggered, so the value of the DD input is stored whenever the signal on the CKCK goes from a 0 to a 1.

Edge-triggered devices have other timing considerations in addition to propagation delay. There is the setup time, which is the amount of time before the clock edge that the signal must be stable, and the hold time, which is the amount of time after the clock edge that the signal must be stable. These are shown in Figure 3-13.

Figure 3-13: Setup and hold times

D Don’t care setup hold Don’t care
CK
Q Prior state Propagation Valid output

As you can see, we don’t have to care what’s happening on the DD input except during the setup and hold times surrounding the clock edge. And, as with all other logic, the output is stable after the propagation delay time and stays stable independent of the DD input. Setup and hold times are typically denoted by tsetupt_{\text{setup}} and tholdt_{\text{hold}} .

The edge behavior of flip-flops works well with clocks. We’ll see an example in the next section.

[!visual-conversion] D flip-flop symbol with inputs D, CK, R and outputs … Cropped from the scan (PDF page 116, printed p. 76): D flip-flop symbol with inputs D, CK, R and outputs S, Q, Q̅

[!visual-conversion] Timing diagram showing D, CK, and Q signals with setup, … Cropped from the scan (PDF page 116, printed p. 76): Timing diagram showing D, CK, and Q signals with setup, hold, propagation, and valid output phases.

Counting is a common application of flip-flops. For example, we could count time from an oscillator and drive a display with a decoder to make a digital clock. Figure 3-14 shows a circuit that produces a 3-bit number ( C2C_2 , C1C_1 , C0C_0 ) that is the count of the number of times the signal changes from 0 to 1. The reset‾\overline{\text{reset}} signal can be used to set the counter to 0.

Figure 3-14: A 3-bit ripple counter

This counter is called a ripple counter because the result ripples from left to right, not because it’s useful for counting bottles of cheap wine. C0C_0 changes C1C_1 , C1C_1 changes C2C_2 , and so on if there are more bits. Since the DD input of each flip-flop is connected to its Q‾\overline{Q} output, it will change state on every positive transition of the CKCK signal.

This is also called an asynchronous counter because everything just happens when it gets around to it. The problem with asynchronous systems is that it’s hard to know when to look at the result. The outputs ( C2C_2 , C1C_1 , C0C_0 ) are invalid during rippling. You can see how it takes longer to get a result for each successive bit in Figure 3-15, where the gray areas represent undefined values due to propagation delay.

Figure 3-15: Ripple counter timing

The timing diagram on the left shows that we get a valid 3-bit number after the propagation delays settle out. But on the right, you can see that we’re trying to count faster than the propagation delays permit, so there are times where no valid number is produced.

This is a variation of the problem we saw with the ripple-carry adder back in Figure 2-41. Just as we were able to solve that problem with the carry look-ahead design, we can address the ripple problem with a synchronous counter design.

[!visual-conversion] Circuit diagram of a 3-bit ripple counter with three D … Cropped from the scan (PDF page 117, printed p. 77): Circuit diagram of a 3-bit ripple counter with three D flip-flops labeled C0, C1, C2.

[!visual-conversion] Timing diagram showing signal and C0, C1, C2 waveforms over … Cropped from the scan (PDF page 117, printed p. 77): Timing diagram showing signal and C0, C1, C2 waveforms over time with gray areas indicating undefined values.

Unlike the ripple counter, the synchronous counter outputs all change at the same time (in sync). This implies that all the flip-flops are clocked in parallel. A 3-bit synchronous counter is shown in Figure 3-16.

Figure 3-16: A 3-bit synchronous counter

You can see that all the flip-flops in the counter change state at the same time because they’re all clocked at the same time. Although propagation delay is still a factor in knowing when the outputs are valid, the cascade effect has been eliminated.

Counters are yet another functional building block, which means they have their own schematic symbol. In this case it’s yet another rectangular box, as you can see in Figure 3-17.

Figure 3-17: A counter schematic symbol

The figure includes a number of inputs we haven’t seen before. Counters are available that have some or all of these inputs. Most counters have a CLRCLR input that clears the counter, setting it to 0. Also common is an ENEN input that enables the counter—the counter doesn’t count unless enabled. Some counters can count in either direction; the U/D‾U/\overline{D} input selects up or down. Finally, some counters have data inputs D0−nD_{0-n} and a load signal LDLD that allows the counter to be set to a specific value.

Now that we have counters, we can use them to keep track of time. But that’s not the only thing we can do with flip-flops. We’ll start learning how to remember large amounts of information in the next section.

D flip-flops are good for remembering things. It’s a common enough application that you can get registers, which are a bunch of D flip-flops in a single package that share a common clock. Figure 3-18 shows an example of a register holding the result of addition using the adder circuit discussed earlier.

[!visual-conversion] Circuit diagram of a 3-bit synchronous counter with D flip-flops … Cropped from the scan (PDF page 118, printed p. 78): Circuit diagram of a 3-bit synchronous counter with D flip-flops and logic gates.

[!visual-conversion] Schematic symbol for a counter with inputs D0-n, CK, CLR, … Cropped from the scan (PDF page 118, printed p. 78): Schematic symbol for a counter with inputs D0-n, CK, CLR, U/D, EN, LD and output Q0-n.

[!visual-conversion] Block diagram of an adder feeding into a 7-bit register … Cropped from the scan (PDF page 119, printed p. 79): Block diagram of an adder feeding into a 7-bit register with clock and enable inputs.

Figure 3-18: A register holding an adder result

Once the output of the adder has been clocked into the register, the operands can change without changing the result. Note that registers often have enable inputs similar to those we saw for counters.

We’ve seen that flip-flops are useful when we need to remember a bit and that registers are handy when we need to remember a collection of bits. What do we do when we need to remember a lot more information, though? For example, what if we want to be able to store several different addition results?

Well, we can start with a big pile of registers. But now we have a new problem: how to specify the register we want to use. This situation looks like Figure 3-19.

[!visual-conversion] Diagram showing input connected to four registers (Register₀ to Registerₙ), … Cropped from the scan (PDF page 119, printed p. 79): Diagram showing input connected to four registers (Register₀ to Registerₙ), each with an output line merging into a single output.

Figure 3-19: Multiple registers

One way to solve this problem is to assign each register a number, as in the figure. We can have this number or address specify the register using one of our standard building blocks, the decoder from “Building Decoders” on page 63. The decoder outputs are connected to the enable inputs on the registers.

Next we need to be able to select the output from the addressed register. Fortunately, we learned how to build selectors in “Building Selectors” on page 65, and they’re just what we need.

Systems often have multiple memory components that need to be hooked together. Time for yet another of our standard building blocks: the tri-state output.

Putting it all together, a memory component looks like Figure 3-20.

[!visual-conversion] Block diagram of a memory component with Registers, Selector, Decoder, … Cropped from the scan (PDF page 120, printed p. 80): Block diagram of a memory component with Registers, Selector, Decoder, and Tri-state buffer, labeled with input, write enable, address, read enable, and output.

Figure 3-20: A memory component

Memory components have a lot of electrical connections. If we want to do something with 32-bit numbers, we would need 32 connections each for the inputs and the outputs, plus connections for the address, control signals, and power. Programmers don’t have to worry about how to fit circuitry into packages or how to route wires, but hardware designers do. We can cut down on the number of connections by realizing that memory rarely needs to be read and written at the same time. We can get by with one set of data connections plus a read/write control. Figure 3-21 shows a schematic of a simplified memory chip. The enable control turns the whole thing on and off so that multiple memory chips can be connected together.

[!visual-conversion] Simplified memory chip schematic with address, data, read/write, and enable … Cropped from the scan (PDF page 120, printed p. 80): Simplified memory chip schematic with address, data, read/write, and enable signals.

Figure 3-21: A simplified memory chip

You’ll notice that the figure uses big fat arrows for the address and data instead of showing the individual signals. We call groups of related signals buses, so the memory chip has an address bus and a data bus. Yup, it’s mass transit for bits.

The next challenge in memory chip packaging comes when the memory size increases and lots of address bits need connections. Referring back to Table 1-2 in Chapter 1, we’d need 32 address connections for a 4-GiB memory component.

Memory designers and road planners deal with similar traffic-management issues. Many cities are organized into grids, and that’s also how memory chips are laid out internally. You can see several rectangular

regions that are chunks of memory in the CPU photomicrograph shown back in Figure 2-3. The address is partitioned into two chunks: a row address and a column address. A memory location is addressed internally using the intersection of the row and column, as shown in Figure 3-22.

[!visual-conversion] Block diagram of row and column addressing with decoders and … Cropped from the scan (PDF page 121, printed p. 81): Block diagram of row and column addressing with decoders and memory array

Figure 3-22: Row and column addressing

Obviously we don’t need to worry about the number of address lines in the 16-location memory shown in this figure. But what if there were a lot more? We could halve the number of address lines by multiplexing the row and column addresses. All we would need is registers on the memory chip to save them, as shown in Figure 3-23.

[!visual-conversion] Memory with address registers diagram showing D flip-flops, decoders, and … Cropped from the scan (PDF page 121, printed p. 81): Memory with address registers diagram showing D flip-flops, decoders, and memory array with CAS and RAS strobes.

Figure 3-23: Memory with address registers

Since the address comes in two parts, it follows that performance would be better if we only had to change one part, such as by setting the row address and then varying the column address. This is what we find in today’s large memory chips.

Memory chips are described by their size in depth × width format. For example, a 256 × 8 chip would have 256 8-bit wide memory locations; a 64 Mib × 1 chip would have 64 mebibits.

The memory we’ve talked about so far is called random-access memory, or RAM. With RAM, the entire width of any memory location can be read or written in any order.

Static RAM, or SRAM, is expensive but fast. It takes six transistors for each bit. Because transistors take up space, SRAM isn’t a great choice for storing billions or trillions of bits.

Dynamic memory (DRAM) is a clever hack. Electrons are stored in microscopic buckets called capacitors, using only one transistor for the lids. The problem is, these buckets leak, so it’s necessary to refresh the memory every once in a while, which means regularly topping off the buckets. You have to be careful that the topping off doesn’t occur at a critical time that would conflict with accessing the memory; this was a problem with one of the first DRAM-based computers, the DEC LSI-11. One of the interesting side effects of DRAM is that the buckets leak more when light shines on them. This enables them to be used as digital cameras.

DRAM is used for large memory chips because of its high density (number of bits per area). Large memory chips mean lots of addresses, which means that DRAM chips use the multiplexed addressing scheme discussed in the previous section. Because of other internal design considerations, it’s only faster to save the row address using the row address strobe and then to vary the column address via the column address strobe. It’s an overused term, but rows are sometimes called pages. It’s comparable to reading a book like this one; it’s much easier to scan a page than it is to flip pages. Or, as stated by the great performance pioneer Jimmy Durante, best performance is a-ras-a-ma-cas. This is a very important consideration in programming: keeping things that are used together in the same row greatly improves performance.

Both SRAM and DRAM are volatile memory, which means that data can be lost when the power is interrupted. Core memory is an antique non-volatile type of RAM that stores bits in toroidal (doughnut-shaped) pieces of iron, which you can see in Figure 3-24. Toroids were magnetized in one direction for a 0 and the other for a 1. The physics of toroids is cool because they’re very resistant to electromagnetic interference from outside the doughnut. In this type of memory, cores were arranged in a grid called a plane with row and column wires through them. There was also a third wire, called the sense wire, because the only way to read the state of a bit was to try to change it and then sense what happened. Of course, if you sensed that it changed, you had to change it back or the data would be lost, making the bit useless. That required a lot of circuitry in addition to all the stitching. Core was actually three-dimensional memory, as planes were assembled into bricks.

While core is antique technology, the nonvolatile characteristic is still prized, and research continues making commercially practical magneto-resistive memory that combines the best of core memory and RAM.

[!visual-conversion] Core memory with woven wire grid and magnetic cores Cropped from the scan (PDF page 123, printed p. 83): Core memory with woven wire grid and magnetic cores

Figure 3-24: Core memory

Read-only memory, or ROM, is not a very accurate name. Memory that could only be read but never written wouldn’t be useful. Even though the name has stuck, it’s more accurate to say that ROM is write-once memory. ROM can be written once and then read multiple times. ROM is important for devices that need to have a program built in, such as a microwave oven; you wouldn’t want to have to program your microwave every time you needed popcorn.

One of the early forms of ROM was the Hollerith card, which later became known as the IBM card, shown in Figure 3-25. Bits were punched into pieces of paper. Really! They were pretty cheap because American inventor Herman Hollerith (1860–1929) was big into cutting corners. Hollerith invented the card in the late 1800s, although it might be more accurate to say that he appropriated the idea from the Jacquard loom, which was invented by Joseph Marie Jacquard in 1801. The Jacquard loom used punched cards to control the weaving pattern. Of course, Jacquard borrowed the idea from Basile Bouchon, who had invented a punched paper tape–controlled loom in 1725. Sometimes it’s hard to distinguish between invention and appropriation, because the future is built on the past. Keep this in mind when you hear people arguing for longer and more restrictive patent and copyright laws; progress slows if we can’t build on the past.

[!visual-conversion] IBM card with punched holes and character rows Cropped from the scan (PDF page 123, printed p. 83): IBM card with punched holes and character rows

Figure 3-25: An IBM card

Early IBM card readers used switches to read the bits. Cards would be slid under a row of springy wires that poked through the holes and made contact with a piece of metal on the other side. Later versions, which worked by shining light through the holes onto a row of photodetectors on the other side, were considerably faster.

Punched paper tape is a related ROM technology; rolls of paper tape with holes punched in it were used to represent bits (see Figure 3-26). Tape had an advantage over cards in that dropping a deck of cards would scramble the data. Then again, tape could tear and was difficult to repair; many a masking tape repair job clogged up the works.

[!visual-conversion] Punched paper tape with rows of holes Cropped from the scan (PDF page 124, printed p. 84): Punched paper tape with rows of holes

Figure 3-26: Punched paper tape

Cards and tape were very slow because they had to be physically moved in order to be read.

A ROM variation called core rope memory was used in the Apollo flight computer (see Figure 3-27). Because it could be written only by sewing, it was impervious to interference—which is important in the harsh environment of space.

[!visual-conversion] Core rope memory from the Apollo guidance computer Cropped from the scan (PDF page 124, printed p. 84): Core rope memory from the Apollo guidance computer

Figure 3-27: Core rope memory from the Apollo guidance computer

IBM cards and paper tape were sequential memory; that is, the data was read in order. Card readers couldn’t go backward, so they were really only good for long-term storage of data. The contents had to be read into some sort of RAM in order to be used. The first commercial availability of a single-chip microprocessor, the Intel 4004 in 1971, created demand for better program storage technology. These first microprocessors were used for devices like calculators that ran a fixed program. Along came mask-programmable ROM. A mask is a stencil used as part of the integrated circuit–manufacturing process. You’d write a program and send the bit pattern off to a semiconductor manufacturer along with a really big check. They’d turn it into a mask, and you’d get back a chip containing your program. It was read-only because there was no way to change it without writing another big check and having a different mask made. Mask-programmable ROM could be read in a random-access manner.

Masks were so expensive that they could be justified only for high-volume applications. Along came programmable read-only memory (PROM), ROM chips that you could program yourself, but only once. The original mechanism for PROM involved melting nichrome (a nickel-chromium alloy) fuses on the chip. Nichrome is the same stuff that makes the glowing wires in your toaster.

People would go through a big pile of PROM chips quickly when developing a program. Engineers are pain-adverse, so next came erasable programmable read-only memory (EPROM). These chips were like PROMs, except that they had a quartz window on top and you could erase them by putting them under a special ultraviolet light.

Life got better with the introduction of electrically erasable programmable read-only memory (what a mouthful!), or EEPROM. This is an EPROM chip that can be erased electrically—no light, no quartz window. Erasing EEPROM is comparatively very slow, though, so it’s not something you want to do a lot. EEPROMs are technically RAM, since it’s possible to read and write the contents in any order. But because they’re slow to write and more expensive than RAM, they’re used as a substitute for ROMs.

It takes time to talk to memory. Imagine you had to go to the store every time you needed a cup of flour. It’s much more practical to go to the store once and bring home a whole sack of flour. Larger memory devices use this principle. Think warehouse shopping for bits.

Disk drives, also known as mass storage, are great for storing immense amounts of data. An 8-TB drive cost less than $200 when this book was written. They’re often referred to as mass storage. Some religious institutions use mass storage for their ceremonies in between use. Disk drives store bits on rotating magnetic platters, sort of like a lazy Susan. Bits periodically come around to where you’re sitting, and you use your hand to pluck them off or put them on. In a disk drive, your hand is replaced by the disk head.

Disk drives are relatively slow compared to other types of memory. If you want something that just passed by the head, you have to wait almost an entire rotation for it to come around again. Modern disks spin at 7,200 rotations per minute (RPM), which means a rotation takes slightly longer than 8 milliseconds. The big problem with disk drives is that they’re mechanical and wear out. Bearing wear is one of the big causes of disk failure. The difference between commercial and consumer-grade devices is primarily the amount of grease in the bearing—manufacturers are able to charge hundreds of dollars for something that costs less than a penny. Disk drives store data by magnetizing areas on the disk, which makes them nonvolatile just like core memory.

Disk drives are a trade-off between speed and density. They’re slow because of the time it takes for the bits you want to show up under the head, but because the data is being brought to the head, no space is required for address and data connections, unlike, for example, in a DRAM. Figure 3-28 shows the insides of a disk drive. They’re built in sealed containers because dust and dirt would cause them to fail.

[!visual-conversion] A black-and-white photo of an open hard disk drive showing … Cropped from the scan (PDF page 126, printed p. 86): A black-and-white photo of an open hard disk drive showing the platter, spindle, and actuator arm.

Figure 3-28: A disk drive

Disks are block-addressable rather than byte-addressable. A block (historically called a sector) is the smallest unit that can be accessed. Disks have historically had 512-byte sectors, although newer devices have 4,096-byte sectors. That means in order to change a byte on a disk, you have to read an entire block, change the byte, and then write back the entire block. Disks contain one or more platters that are laid out as shown in Figure 3-29.

[!visual-conversion] Diagram of disk layout showing Track, Cluster, Sector, and Geometric … Cropped from the scan (PDF page 127, printed p. 87): Diagram of disk layout showing Track, Cluster, Sector, and Geometric sector labels.

Figure 3-29: Disk layout

Since all of the sectors contain the same number of bits, the bit density (bits/mm²) is greater at the center of each platter than it is at the outer edge. This is wasteful because there’s clearly room to cram more bits onto the outer tracks. Newer disks address this problem by dividing the disk into a set of radial zones, effectively having more sectors in the outer zones than in the inner ones.

There are a couple of numbers that describe the performance of disk drives. Modern disks have a head on an actuator arm that moves radially across the disk; the position of the head divides the disks into tracks. The seek time is the amount of time that it takes to move the head from one track to another. It would, of course, be much faster to have one head per track so that seeking wasn’t necessary; you could get that on very old disk drives, but the tracks are too close together on modern disks to make that practical. In addition to the seek time, there’s the time it takes for the part of the disk you’re interested in to rotate so that it’s under the head, called rotational latency, which as we saw above is in the millisecond range.

Disk drives are often called hard drives. Originally, all disk drives were hard drives. The distinction arose when cheap removable storage devices called floppy disks appeared on the scene. Floppy disks were bendable, so calling the other type “hard” made them easy to differentiate.

An antiquated variation on disk drives is magnetic drum storage, which was just what it sounds like: a rotating magnetic drum with stripes of heads on it.

Magnetic tape is another nonvolatile storage technology that uses reels of magnetized tape. It is way slower than a disk drive, and it can take a long time to wind the tape to the requested position. Early Apple computers used consumer-grade audio cassettes for magnetic tape storage.

Optical disks are similar to magnetic disks except that they use light instead of magnetism. You know these as CDs and DVDs. A big advantage of optical disks is that they can be mass-produced via printing. Preprinted

disks are ROMs. PROM-equivalent versions that can be written once (CD-R, DVD-R) are also available, as are versions that can be erased and rewritten (CD-RW). Figure 3-30 shows a close-up of a portion of an optical disk.

[!visual-conversion] close-up of optical disk data pits and lands Cropped from the scan (PDF page 128, printed p. 88): close-up of optical disk data pits and lands

Figure 3-30: Optical disk data

Flash memory is the most recent incarnation of EEPROM. It’s good solution for some applications, like music players and digital cameras. It works by storing electrons in buckets just like DRAM. In this case, the buckets are bigger and better built so they don’t leak. But the lid hinges on the buckets eventually wear out if they’re opened and closed too many times. Flash memory can be erased more quickly than EEPROM and is cheaper to make. It works like RAM for reading and also for writing a blank device filled with 0s. But although 0s can be turned into 1s, they can’t be turned back without being erased first. Flash memory is internally divided into blocks, and only blocks can be erased, not individual locations. Flash memory devices are random-access for reads, and block-access for writes.

Disk drives are slowly being replaced by solid-state disk drives, which are pretty much just flash memory packaged up to look like a disk drive. Right now their price per bit is much higher than spinning disks, but that’s expected to change. Because flash memory wears out, solid-state drives include a processor that keeps track of the usages in different blocks and tries to even it out so that all blocks wear out at the same rate.

You never know when a stray cosmic ray is going to hit a piece of memory and corrupt the data. It would be nice to know when this happens and even nicer to be able to repair the damage. Of course, such improvements cost money and are not typically found in consumer-grade devices.

We’d like to be able to detect errors without having to store a complete second copy of the data. And that wouldn’t work anyway, because we wouldn’t know which copy was correct. We could store two extra copies and assume that the matching pair (if any) is the right one. Computers designed for very harsh environments do this. They also use a more expensive circuit

design that doesn’t burn up when hit by a proton. For example, the space shuttle had redundant computers and a voting system in the event that an error was detected.

We can test for a 1-bit error using a method called parity. The idea is to add up the number of bits that are set to 1 and use an extra bit to store whether that sum is an odd or even number. We can do this by taking the XOR of the bits. There are two forms of this: in even parity the sum of the bits is used, and in odd parity the complement of the sum of the bits is used. This choice may seem, well, odd, but the nomenclature comes from the number of 1s or 0s including the parity bit.

The left half of Figure 3-31 shows the calculation of even parity; there are four 1s, so the parity is 0. The right half shows the checking of the parity; a 0 out means that the data is good, or at least as good as we can tell with parity. The big problem with parity is that it’s one place where two wrongs sure look like a right; it only catches odd numbers of errors.

[!visual-conversion] Circuit diagram for even parity generation and checking with XOR … Cropped from the scan (PDF page 129, printed p. 89): Circuit diagram for even parity generation and checking with XOR gates and bit inputs 0-8.

Figure 3-31: Even parity generation and checking

There are more complicated methods, such as Hamming codes, invented by American mathematician Richard Hamming (1915–1998), which take more bits and allow for more errors to be detected and for some to be corrected. Error checking and correcting (ECC) memory chips are available that include this circuitry. They’re typically used in big data centers, not in consumer devices.

Methods like parity are good for data that is constantly changing. There are less expensive methods that allow for verification of static block data, such as a computer program. The simplest of these is the checksum, where the contents of every data location are summed into some nn-bit value and the overflow bits are thrown away. The checksum can be compared against the program, usually just before it is run. The larger the checksum value (that is, larger nn), the lower the chance of getting a false positive.

Cyclic redundancy checks, or CRCs, are a mathematically better replacement for checksums. Hash codes are another. The goal is to calculate a verification number that is unique enough for the data so that for most changes, the check will no longer be correct.

The techniques used to make PROMs, EEPROMs, and flash aren’t just limited to memory. We’ll soon see how computer hardware is constructed from logic circuits. And since you’re learning programming, you know that programs include logic in their code, and you may know that computers expose logic to programs via their instruction sets. What’s the difference between doing that in hardware versus software? It’s a blurry line. To a large degree, there is little distinction except that it’s much easier to build software since there are no additional costs other than design time.

You’ve probably heard the term firmware, which originally just referred to software in a ROM. But most firmware now lives in flash memory or even RAM, so the difference is minimal. And it’s even more complicated than that. It used to be that chips were designed by geeks who laid out circuits by sticking colored masking tape on big sheets of clear Mylar. In 1979 American scientists and engineers Carver Mead and Lynn Conway changed the world with their publication of Introduction to VLSI Systems, which helped kick-start the electronic design automation (EDA) industry. Chip design became software. Chips today are designed using specialized programming languages such as Verilog, VHDL, and SystemC.

Much of the time, a computer programmer is simply given a piece of hardware to use. But you might get the opportunity to participate in the design of a system that includes both hardware and software. The design of the interface between hardware and software is critical. There are countless examples of chips with unusable, unprogrammable, and unnecessary features.

Integrated circuits are expensive to make. In the early days, all chips were full custom designs. Chips are built up in layers, with the actual components on the bottom and metal layers on top to wire them together. Gate arrays were an attempt to lower the cost for some applications; a set of pre-designed components was available, and only the metal layers were custom. Just like with memory, these were supplanted by PROM-equivalent versions that you could program yourself. And there was an EPROM equivalent that could be erased and reprogrammed.

Modern field-programmable gate arrays (FPGAs) are the flash memory equivalent; they can be reprogrammed in software. In many cases, using an FPGA is cheaper than using other components. FPGAs are very rich in features; for example, you can get a large FPGA that contains a couple of ARM processor cores. Intel recently purchased Altera and may include FPGAs on its processor chips. There’s a good chance you’ll work on a project containing one of these devices, so be prepared to turn your software into hardware.

In this chapter, you’ve learned where computers get their sense of time. You were introduced to sequential logic, which, along with combinatorial logic from Chapter 2, provides us with all of the fundamental hardware building blocks. And you’ve learned something about how memory is built. We’ll put all of this knowledge together to make a computer in Chapter 4.