JOHN F. WAKERLY WAK
DIGITAL DESIGN DIGITAL DESIGN
PRINCIPLES & PRACTICES PRINCIPLES & PRACTICES
THIRD EDITION ISBN 0-13-769191-2 TH
EDIT
This newly revised book blends academic precision 9 0 00 0
and practical experience in an authoritative introduc- PREN
tion to basic principles of digital design and practical FPO H
requirements in both board-level and VLSI systems.
With over twenty years of experience in both univer- 9 780137 691913
sity and industrial settings, John Wakerly has directly
taught thousands of engineering students, indirectly
taught tens of thousands through his books, and
directly designed real digital systems representing
tens of millions of dollars of revenue.
The book covers the fundamental building blocks of
digital design across several levels of abstraction,
from CMOS gates to hardware design languages.
Important functions such as gates, decoders, multi-
plexers, flip-flops, registers, and counters are dis-
cussed at each level.
New edition features include de-emphasis of manual
turn-the-crank procedures and MSI design, and earli-
er coverage of PLDs, FPGAs, and hardware design
languages to get maximum leverage from modern
components and software tools. HDL coverage now
includes VHDL as well as ABEL.
PRENTICE HALL
Upper Saddle River, NJ 07458
http://www.prenhall.com
KERLY THIRD EDITION
D I GI TA L
DESIGN
PRINCIPLES & PRACTICES
IRD J O H N F. WA K E R LY
TION
Robert McFadden
NTICE
ALL
DO NOT 1Hi,I'mJohn....
chapter
COPY
DO NOT
COPYIntroduction
•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• WDO NOTelcome to the world of digital design. Perhaps you’re a com-
puter science student who knows all about computer software
and programming, but you’re still trying to figure out how all
COPYthat fancy hardware could possibly work. Or perhaps you’re
an electrical engineering student who already knows some-
thing about analog electronics and circuit design, but you wouldn’t know a
bit if it bit you. No matter. Starting from a fairly basic level, this book will
show you how to design digital circuits and subsystems.
DO NOTWe’ll give you the basic principles that you need to figure things out,
and we’ll give you lots of examples. Along with principles, we’ll try to
convey the flavor of real-world digital design by discussing current,
practical considerations whenever possible. And I, the author, will often
refer to myself as “we” in the hope that you’ll be drawn in and feel that we’re
COPYwalking through the learning process together.
1.1 About Digital Design
Some people call it “logic design.” That’s OK, but ultimately the goal of
DO NOTdesign is to build systems. To that end, we’ll cover a whole lot more in this
text than just logic equations and theorems.
This book claims to be about principles and practices. Most of the prin-
ciples that we present will continue to be important years from now; some
Copyright © 1999 by John F. Wakerly Copying Prohibited 1
2 Chapter 1 Introduction
may be applied in ways that have not even been discovered yet. As for practices,
they may be a little different from what’s presented here by the time you start
DO NOT COPYworking in the field, and they will certainly continue to change throughout your
career. So you should treat the “practices” material in this book as a way to rein-
force principles, and as a way to learn design methods by example.
One of the book's goals is to present enough about basic principles for you
to know what's happening when you use software tools to turn the crank for you.
DO NOT COPYThe same basic principles can help you get to the root of problems when the
tools happen to get in your way.
Listed in the box on this page, there are several key points that you should
learn through your studies with this text. Most of these items probably make no
sense to you right now, but you should come back and review them later.
DO NOT COPYDigital design is engineering, and engineering means “problem solving.”
My experience is that only 5%–10% of digital design is “the fun stuff”—the
creative part of design, the flash of insight, the invention of a new approach.
Much of the rest is just “turning the crank.” To be sure, turning the crank is much
DO NOT COPYeasier now than it was 20 or even 10 years ago, but you still can’t spend 100% or
even 50% of your time on the fun stuff.
DO NOT COPYIMPORTANT • Good tools do not guarantee good design, but they help a lot by taking the pain out
THEMES IN of doing things right.
DO NOT COPYDIGITALDESIGN
• Digital circuits have analog characteristics.
• Know when to worry and when not to worry about the analog aspects of digital
design.
• Always document your designs to make them understandable by yourself and others.
• Associate active levels with signal names and practice bubble-to-bubble logic
design.
DO NOT COPY• Understand and use standard functional building blocks.
• Design for minimum cost at the system level, including your own engineering effort
as part of the cost.
• State-machine design is like programming; approach it that way.
• Use programmable logic to simplify designs, reduce cost, and accommodate last-
DO NOT COPYminutemodifications.
• Avoid asynchronous design. Practice synchronous design until a better methodology
comes along.
• Pinpoint the unavoidable asynchronous interfaces between different subsystems and
the outside world, and provide reliable synchronizers.
DO NOT COPY• Catching a glitch in time saves nine.
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 1.2 Analog versus Digital 3
Besides the fun stuff and turning the crank, there are many other areas in
which a successful digital designer must be competent, including the following:
DO NOT COPY• Debugging. It’s next to impossible to be a good designer without being a
good troubleshooter. Successful debugging takes planning, a systematic
approach, patience, and logic: if you can’t discover where a problem is,
find out where it is not!
DO NOT COPY• Business requirements and practices. A digital designer’s work is affected
by a lot of non-engineering factors, including documentation standards,
component availability, feature definitions, target specifications, task
scheduling, office politics, and going to lunch with vendors.
• Risk-taking. When you begin a design project you must carefully balance
DO NOT COPYrisks against potential rewards and consequences, in areas ranging from
new-component selection (will it be available when I’m ready to build the
first prototype?) to schedule commitments (will I still have a job if I’m
late?).
DO NOT COPY• Communication. Eventually, you’ll hand off your successful designs to
other engineers, other departments, and customers. Without good commu-
nication skills, you’ll never complete this step successfully. Keep in mind
that communication includes not just transmitting but also receiving; learn
to be a good listener!
DO NOT COPYIn the rest of this chapter, and throughout the text, I’ll continue to state
some opinions about what’s important and what is not. I think I’m entitled to do
so as a moderately successful practitioner of digital design. Of course, you are
always welcome to share your own opinions and experience (send email to
DO NOT [email protected]).
1.2 Analog versus Digital
Analog devices and systems process time-varying signals that can take on any analog
DO NOT COPYvalue across a continuous range of voltage, current, or other metric. So do digital digital
circuits and systems; the difference is that we can pretend that they don’t! A
digital signal is modeled as taking on, at any time, only one of two discrete
DO NOT COPYDigital computers have been around since the 1940s, and have been in
values, which we call 0 and 1 (or LOW and HIGH, FALSE and TRUE, negated 0
and asserted, Sam and Fred, or whatever). 1
widespread commercial use since the 1960s. Yet only in the past 10 to 20 years
has the “digital revolution” spread to many other aspects of life. Examples of
once-analog systems that have now “gone digital” include the following:
DO NOT COPY• Still pictures. The majority of cameras still use silver-halide film to record
images. However, the increasing density of digital memory chips has
allowed the development of digital cameras which record a picture as a
Copyright © 1999 by John F. Wakerly Copying Prohibited
4 Chapter 1 Introduction
DO NOT COPY640×480 or larger array of pixels, where each pixel stores the intensities of
DO its red, green and blue color components as 8 bits each. This large amount
DO of data, over seven million bits in this example, may be processed and
DO compressed into a format called JPEG with as little as 5% of the original
DO
DO NOT COPYstorage size, depending on the amount of picture detail. So, digital cameras
DO rely on both digital storage and digital processing.
DO
DO • Video recordings. A digital versatile disc (DVD) stores video in a highly
compressed digital format called MPEG-2. This standard encodes a small
fraction of the individual video frames in a compressed format similar to
NOT COPYJPEG, and encodes each other frame as the difference between it and the
previous one. The capacity of a single-layer, single-sided DVD is about 35
billion bits, sufficient for about 2 hours of high-quality video, and a two-
layer, double-sided disc has four times that capacity.
NOT COPY• Audio recordings. Once made exclusively by impressing analog wave-
forms onto vinyl or magnetic tape, audio recordings now commonly use
digital compact discs (CDs). A CD stores music as a sequence of 16-bit
numbers corresponding to samples of the original analog waveform, one
sample per stereo channel every 22.7 microseconds. A full-length CD
NOT COPYrecording (73 minutes) contains over six billion bits of information.
• Automobile carburetors. Once controlled strictly by mechanical linkages
(including clever “analog” mechanical devices that sensed temperature,
pressure, etc.), automobile engines are now controlled by embedded
microprocessors. Various electronic and electromechanical sensors con-
NOT COPYvert engine conditions into numbers that the microprocessor can examine
to determine how to control the flow of fuel and oxygen to the engine. The
microprocessor’s output is a time-varying sequence of numbers that
operate electromechanical actuators which, in turn, control the engine.
NOT COPY• The telephone system. It started out a hundred years ago with analog
microphones and receivers connected to the ends of a pair of copper wires
(or was it string?). Even today, most homes still use analog telephones,
which transmit analog signals to the phone company’s central office (CO).
However, in the majority of COs, these analog signals are converted into a
NOT COPYdigital format before they are routed to their destinations, be they in the
same CO or across the world. For many years the private branch exchanges
(PBXs) used by businesses have carried the digital format all the way to the
desktop. Now many businesses, COs, and traditional telephony service
NOT COPYproviders are converting to integrated systems that combine digital voice
with data traffic over a single IP (Internet Protocol) network.
• Traffic lights. Stop lights used to be controlled by electromechanical timers
that would give the green light to each direction for a predetermined
amount of time. Later, relays were used in controllers that could activate
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 1.2 Analog versus Digital 5
the lights according to the pattern of traffic detected by sensors embedded
in the pavement. Today’s controllers use microprocessors, and can control
DO NOT COPYthe lights in ways that maximize vehicle throughput or, in some California
cities, frustrate drivers in all kinds of creative ways.
• Movie effects. Special effects used to be made exclusively with miniature
clay models, stop action, trick photography, and numerous overlays of film
on a frame-by-frame basis. Today, spaceships, bugs, other-worldly scenes,
DO NOT COPYand even babies from hell (in Pixar’s animated feature Tin Toy) are synthe-
sized entirely using digital computers. Might the stunt man or woman
someday no longer be needed, either?
The electronics revolution has been going on for quite some time now, and
DO NOT COPYthe “solid-state” revolution began with analog devices and applications like
transistors and transistor radios. So why has there now been a digital revolution?
There are in fact many reasons to favor digital circuits over analog ones:
• Reproducibility of results. Given the same set of inputs (in both value and
DO NOT COPYtime sequence), a properly designed digital circuit always produces exactly
the same results. The outputs of an analog circuit vary with temperature,
power-supply voltage, component aging, and other factors.
• Ease of design. Digital design, often called “logic design,” is logical. No
special math skills are needed, and the behavior of small logic circuits can
DO NOT COPYbe visualized mentally without any special insights about the operation of
capacitors, transistors, or other devices that require calculus to model.
• Flexibility and functionality. Once a problem has been reduced to digital
form, it can be solved using a set of logical steps in space and time. For
DO NOT COPYexample, you can design a digital circuit that scrambles your recorded
voice so that it is absolutely indecipherable by anyone who does not have
your “key” (password), but can be heard virtually undistorted by anyone
who does. Try doing that with an analog circuit.
DO NOT COPY• Programmability. You’re probably already quite familiar with digital com-
puters and the ease with which you can design, write, and debug programs
for them. Well, guess what? Much of digital design is carried out today by
writing programs, too, in hardware description languages (HDLs). These hardware description
languages allow both structure and function of a digital circuit to be language (HDL)
DO NOT COPYspecified or modeled. Besides a compiler, a typical HDL also comes with hardware model
simulation and synthesis programs. These software tools are used to test
the hardware model’s behavior before any real hardware is built, and then
synthesize the model into a circuit in a particular component technology.
• Speed. Today’s digital devices are very fast. Individual transistors in the
DO NOT COPYfastest integrated circuits can switch in less than 10 picoseconds, and a
complete, complex device built from these transistors can examine its
Copyright © 1999 by John F. Wakerly Copying Prohibited
6 Chapter 1 Introduction
SHORT TIMES A microsecond (µsec) is 10−6 second. A nanosecond (ns) is just 10−9 second, and a
DO NOT COPYpicosecond (ps) is 10−12 second. In a vacuum, light travels about a foot in a nanosec-
ond, and an inch in 85 picoseconds. With individual transistors in the fastest
integrated circuits now switching in less than 10 picoseconds, the speed-of-light
delay between these transistors across a half-inch-square silicon chip has become a
DO NOT COPYlimiting factor in circuit design.
DO NOT COPYinputs and produce an output in less than 2 nanoseconds. This means that
such a device can produce 500 million or more results per second.
• Economy. Digital circuits can provide a lot of functionality in a small
space. Circuits that are used repetitively can be “integrated” into a single
“chip” and mass-produced at very low cost, making possible throw-away
DO
NOT COPYitems like calculators, digital watches, and singing birthday cards. (You
may ask, “Is this such a good thing?” Never mind!)
• Steadily advancing technology. When you design a digital system, you
almost always know that there will be a faster, cheaper, or otherwise better
DO NOT COPYtechnology for it in a few years. Clever designers can accommodate these
expected advances during the initial design of a system, to forestall system
obsolescence and to add value for customers. For example, desktop com-
puters often have “expansion sockets” to accommodate faster processors
or larger memories than are available at the time of the computer’s
introduction.
So, that’s enough of a sales pitch on digital design. The rest of this chapter will
DO NOT COPYgive you a bit more technical background to prepare you for the rest of the book.
1.3 Digital Devices
gate The most basic digital devices are called gates and no, they were not named after
DO NOT COPYthe founder of a large software company. Gates originally got their name from
their function of allowing or retarding (“gating”) the flow of digital information.
In general, a gate has one or more inputs and produces an output that is a func-
tion of the current input value(s). While the inputs and outputs may be analog
conditions such as voltage, current, even hydraulic pressure, they are modeled
DO NOT COPYas taking on just two discrete values, 0 and 1.
Figure 1-1 shows symbols for the three most important kinds of gates. A
2-input AND gate, shown in (a), produces a 1 output if both of its inputs are 1;
otherwise it produces a 0 output. The figure shows the same gate four times, with
the four possible combinations of inputs that may be applied to it and the result-
DO NOT COPYANDgate
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 1.4 Electronic Aspects of Digital Design 7
00 011
001
DO(a) 00
NOT COPY1 0 1
0 011
1 111
(b) 0 10 1
DO(c) 0 NOT COPY1 0
F i g u r e 1 - 1 Digital devices: (a) AND gate; (b) OR gate; (c) NOT gate or inverter.
ing outputs. A gate is called a combinational circuit because its output depends combinational
DO NOT COPYonly on the current input combination.
A 2-input OR gate, shown in (b), produces a 1 output if one or both of its OR gate
inputs are 1; it produces a 0 output only if both inputs are 0. Once again, there are
four possible input combinations, resulting in the outputs shown in the figure.
DO NOT COPYA NOT gate, more commonly called an inverter, produces an output value NOT gate
that is the opposite of the input value, as shown in (c). inverter
We called these three gates the most important for good reason. Any digital
function can be realized using just these three kinds of gates. In Chapter 3 we’ll
show how gates are realized using transistor circuits. You should know, however,
DO NOT COPYthat gates have been built or proposed using other technologies, such as relays,
vacuum tubes, hydraulics, and molecular structures.
A flip-flop is a device that stores either a 0 or 1. The state of a flip-flop is flip-flop
the value that it currently stores. The stored value can be changed only at certain state
times determined by a “clock” input, and the new value may further depend on
DO NOT COPYthe flip-flop’s current state and its “control” inputs. A flip-flop can be built from
a collection of gates hooked up in a clever way, as we’ll show in Section 7.2.
A digital circuit that contains flip-flops is called a sequential circuit
because its output at any time depends not only on its current input, but also on
the past sequence of inputs that have been applied to it. In other words, a sequen-
DO NOT COPYtial circuit has memory of past events.
sequential circuit
memory
1.4 Electronic Aspects of Digital Design
Digital circuits are not exactly a binary version of alphabet soup—with all due
DO NOT COPYrespect to Figure 1-1, they don’t have little 0s and 1s floating around in them. As
we’ll see in Chapter 3, digital circuits deal with analog voltages and currents,
and are built with analog components. The “digital abstraction” allows analog
behavior to be ignored in most cases, so circuits can be modeled as if they really
DO NOT COPYdid process0sand 1s.
Copyright © 1999 by John F. Wakerly Copying Prohibited
8 Chapter 1 Introduction
DO NOTFigure 1-2 COPYVoltage Outputs Noise Inputs
Margin
Logic values and noise
margins. logic 1 logic 1
DO NOT COPYinvalid
logic 0
logic 0
DO NOT COPYOne important aspect of the digital abstraction is to associate a range of
analog values with each logic value (0 or 1). As shown in Figure 1-2, a typical
gate is not guaranteed to have a precise voltage level for a logic 0 output. Rather,
DO NOT COPYnoisemargin
it may produce a voltage somewhere in a range that is a subset of the range
guaranteed to be recognized as a 0 by other gate inputs. The difference between
the range boundaries is called noise margin—in a real circuit, a gate’s output can
be corrupted by this much noise and still be correctly interpreted at the inputs of
other gates.
Behavior for logic 1 outputs is similar. Note in the figure that there is an
“invalid” region between the input ranges for logic 0 and logic 1. Although any
DO NOT COPYgiven digital device operating at a particular voltage and temperature will have a
fairly well defined boundary (or threshold) between the two ranges, different
devices may have different boundaries. Still, all properly operating devices have
their boundary somewhere in the “invalid” range. Therefore, any signal that is
within the defined ranges for 0 and 1 will be interpreted identically by different
DO NOT COPYdevices. This characteristic is essential for reproducibility of results.
It is the job of an electronic circuit designer to ensure that logic gates
produce and recognize logic signals that are within the appropriate ranges. This
is an analog circuit-design problem; we touch upon some aspects of this in
DO NOT COPYChapter 3. It is not possible to design a circuit that has the desired behavior
under every possible condition of power-supply voltage, temperature, loading,
and other factors. Instead, the electronic circuit designer or device manufacturer
DO NOT COPYspecifications
provides specifications that define the conditions under which correct behavior
is guaranteed.
As a digital designer, then, you need not delve into the detailed analog
behavior of a digital device to ensure its correct operation. Rather, you need only
examine enough about the device’s operating environment to determine that it is
operating within its published specifications. Granted, some analog knowledge
is needed to perform this examination, but not nearly what you’d need to design
DO NOT COPYa digital device starting from scratch. In Chapter 3, we’ll give you just what you
need.
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 1.5 Software Aspects of Digital Design 9
DO NOT COPY
DO NOT COPYFigure 1-3
A logic-design
Quarter-size logic symbols, copyright 1976 by Micro Systems Engineering template.
DO NOT COPY1.5 Software Aspects of Digital Design
Digital design need not involve any software tools. For example, Figure 1-3
shows the primary tool of the “old school” of digital design—a plastic template
for drawing logic symbols in schematic diagrams by hand (the designer’s name
DO NOT COPYwas engraved into the plastic with a soldering iron).
Today, however, software tools are an essential part of digital design.
Indeed, the availability and practicality of hardware description languages
(HDLs) and accompanying circuit simulation and synthesis tools have changed
the entire landscape of digital design over the past several years. We’ll make
DO NOT COPYextensive use of HDLs throughout this book.
In computer-aided design (CAD) various software tools improve the computer-aided design
(CAD)
designer’s productivity and help to improve the correctness and quality of
designs. In a competitive world, the use of software tools is mandatory to obtain
high-quality results on aggressive schedules. Important examples of software
DO NOT COPYtools for digital design are listed below:
• Schematic entry. This is the digital designer’s equivalent of a word proces-
sor. It allows schematic diagrams to be drawn “on-line,” instead of with
paper and pencil. The more advanced schematic-entry programs also
DO NOT COPYcheck for common, easy-to-spot errors, such as shorted outputs, signals
that don’t go anywhere, and so on. Such programs are discussed in greater
detail in Section 12.1.
• HDLs. Hardware description languages, originally developed for circuit
modeling, are now being used more and more for hardware design. They
DO NOT COPYcan be used to design anything from individual function modules to large,
multi-chip digital systems. We’ll introduce two HDLs, ABEL and VHDL,
at the end of Chapter 4, and we’ll provide examples in both languages in
the chapters that follow.
DO NOT COPY• HDL compilers, simulators, and synthesis tools. A typical HDL software
package contains several components. In a typical environment, the
designer writes a text-based “program,” and the HDL compiler analyzes
Copyright © 1999 by John F. Wakerly Copying Prohibited
10 Chapter 1 Introduction
DO NOT COPYthe program for syntax errors. If it compiles correctly, the designer has the
DO option of handing it over to a synthesis tool that creates a corresponding
DO circuit design targeted to a particular hardware technology. Most often,
DO before synthesis the designer will use the compiler’s results as input to a
DO
DO NOT COPY“simulator” to verify the behavior of the design.
DO
DO • Simulators. The design cycle for a customized, single-chip digital integrat-
DO ed circuit is long and expensive. Once the first chip is built, it’s very
difficult, often impossible, to debug it by probing internal connections
(they are really tiny), or to change the gates and interconnections. Usually,
NOT COPYchanges must be made in the original design database and a new chip must
be manufactured to incorporate the required changes. Since this process
can take months to complete, chip designers are highly motivated to “get
it right” (or almost right) on the first try. Simulators help designers predict
the electrical and functional behavior of a chip without actually building it,
NOT COPYallowing most if not all bugs to be found before the chip is fabricated.
• Simulators are also used in the design of “programmable logic devices,”
introduced later, and in the overall design of systems that incorporate many
individual components. They are somewhat less critical in this case
NOT COPYbecause it’s easier for the designer to make changes in components and
interconnections on a printed-circuit board. However, even a little bit of
simulation can save time by catching simple but stupid mistakes.
• Test benches. Digital designers have learned how to formalize circuit sim-
ulation and testing into software environments called “test benches.” The
NOT COPYidea is to build a set of programs around a design to automatically exercise
its functions and check both its functional and its timing behavior. This is
especially useful when small design changes are made—the test bench can
be run to ensure that bug fixes or “improvements” in one area do not break
something else. Test-bench programs may be written in the same HDL as
NOT COPYthe design itself, in C or C++, or in combination of languages including
scripting languages like PERL.
• Timing analyzers and verifiers. The time dimension is very important in
digital design. All digital circuits take time to produce a new output value
NOT COPYin response to an input change, and much of a designer’s effort is spent
ensuring that such output changes occur quickly enough (or, in some cases,
not too quickly). Specialized programs can automate the tedious task of
drawing timing diagrams and specifying and verifying the timing relation-
ships between different signals in a complex system.
NOT COPY• Word processors. Let’s not forget the lowly text editor and word processor.
These tools are obviously useful for creating the source code for HDL-
based designs, but they have an important use in every design—to create
documentation!
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 1.5 Software Aspects of Digital Design 11
PROGRAMMABLE Later in this book you’ll learn how programmable logic devices (PLDs) and field-
DO NOT COPYLOGIC DEVICES programmable gate arrays (FPGAs) allow you to design a circuit or subsystem by
VERSUS writing a sort of program. PLDs and FPGAs are now available with up to millions of
DO NOT COPYSIMULATION
gates, and the capabilities of these technologies are ever increasing. If a PLD- or
FPGA-based design doesn’t work the first time, you can often fix it by changing the
program and physically reprogramming the device, without changing any compo-
nents or interconnections at the system level. The ease of prototyping and modifying
PLD- and FPGA-based systems can eliminate the need for simulation in board-level
design; simulation is required only for chip-level designs.
The most widely held view in industry trends says that as chip technology
advances, more and more design will be done at the chip level, rather than the board
DO NOT COPYlevel. Therefore, the ability to perform complete and accurate simulation will
become increasingly important to the typical digital designer.
However, another view is possible. If we extrapolate trends in PLD and FPGA
capabilities, in the next decade we will witness the emergence of devices that include
not only gates and flip-flops as building blocks, but also higher-level functions such
DO NOT COPYas processors, memories, and input/output controllers. At this point, most digital
designers will use complex on-chip components and interconnections whose basic
functions have already been tested by the device manufacturer.
In this future view, it is still possible to misapply high-level programmable
functions, but it is also possible to fix mistakes simply by changing a program;
DO NOT COPYdetailed simulation of a design before simply “trying it out” could be a waste of time.
Another, compatible view is that the PLD or FPGA is merely a full-speed simulator
for the program, and this full-speed simulator is what gets shipped in the product!
Does this extreme view have any validity? To guess the answer, ask yourself
the following question. How many software programmers do you know who debug
DO NOT COPYa new program by “simulating” its operation rather than just trying it out?
In any case, modern digital systems are much too complex for a designer to
have any chance of testing every possible input condition, with or without simula-
tion. As in software, correct operation of digital systems is best accomplished
through practices that ensure that the systems are “correct by design.” It is a goal of
DO NOT COPYthis text to encourage such practices.
In addition to using the tools above, designers may sometimes write spe-
cialized programs in high-level languages like C or C++, or scripts in languages
DO NOT COPYlike PERL, to solve particular design problems. For example, Section 11.1 gives
a few examples of C programs that generate the “truth tables” for complex
combinational logic functions.
Although CAD tools are important, they don’t make or break a digital
designer. To take an analogy from another field, you couldn’t consider yourself
DO NOT COPYto be a great writer just because you’re a fast typist or very handy with a word
processor. During your study of digital design, be sure to learn and use all the
Copyright © 1999 by John F. Wakerly Copying Prohibited
12 Chapter 1 Introduction
tools that are available to you, such as schematic-entry programs, simulators,
and HDL compilers. But remember that learning to use tools is no guarantee that
DO NOT COPYyou’ll be able to produce good results. Please pay attention to what you’re
producing with them!
1.6 Integrated Circuits
DO NOT COPYA collection of one or more gates fabricated on a single silicon chip is called an
integrated circuit (IC) integrated circuit (IC). Large ICs with tens of millions of transistors may be half
an inch or more on a side, while small ICs may be less than one-tenth of an inch
on a side.
Regardless of its size, an IC is initially part of a much larger, circular wafer,
up to ten inches in diameter, containing dozens to hundreds of replicas of the
DO NOT COPYwafer
same IC. All of the IC chips on the wafer are fabricated at the same time, like
pizzas that are eventually sold by the slice, except in this case, each piece (IC
die chip) is called a die. After the wafer is fabricated, the dice are tested in place on
the wafer and defective ones are marked. Then the wafer is sliced up to produce
DO NOT COPYthe individual dice, and the marked ones are discarded. (Compare with the pizza-
maker who sells all the pieces, even the ones without enough pepperoni!) Each
unmarked die is mounted in a package, its pads are connected to the package
pins, and the packaged IC is subjected to a final test and is shipped to a customer.
DO NOT COPYSome people use the term “IC” to refer to a silicon die. Some use “chip” to
refer to the same thing. Still others use “IC” or “chip” to refer to the combination
of a silicon die and its package. Digital designers tend to use the two terms inter-
changeably, and they really don’t care what they’re talking about. They don’t
require a precise definition, since they’re only looking at the functional and elec-
DO NOT COPYIC trical behavior of these things. In the balance of this text, we’ll use the term IC to
refer to a packaged die.
A DICEY A reader of the second edition wrote to me to collect a $5 reward for pointing out my
DO NOT COPYDECISION “glaring” misuse of “dice” as the plural of “die.” According to the dictionary, she
said, the plural form of “die” is “dice” only when describing those little cubes with
dots on each side; otherwise it’s “dies,” and she produced the references to prove it.
Being stubborn, I asked my friends at the Microprocessor Report about this
issue. According to the editor,
DO NOT COPYThere is, indeed, much dispute over this term. We actually stopped using
the term “dice” in Microprocessor Report more than four years ago. I
DO NOT COPYactually prefer the plural “die,” … but perhaps it is best to avoid using
the plural whenever possible.
So there you have it, even the experts don’t agree with the dictionary! Rather
than cop out, I boldly chose to use “dice” anyway, by rolling the dice.
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 1.6 Integrated Circuits 13
DO NOT COPYpin1 pin 1 pin 28
Figure 1-4
pin 20 Dual in-line pin (DIP)
packages: (a) 14-pin;
pin 1 pin 14 (b) 20-pin; (c) 28-pin.
0.1"
pin 11
DO NOT COPY0.1" pin 8 0.1" pin 15
(a) 0.3" (b) 0.3" (c) 0.6"
In the early days of integrated circuits, ICs were classified by size—small,
DO NOT COPYmedium, or large—according to how many gates they contained. The simplest
type of commercially available ICs are still called small-scale integration (SSI), small-scale integration
(SSI)
and contain the equivalent of 1 to 20 gates. SSI ICs typically contain a handful of
gates or flip-flops, the basic building blocks of digital design.
DO NOT COPYThe SSI ICs that you’re likely to encounter in an educational lab come in a
14-pin dual in-line-pin (DIP) package. As shown in Figure 1-4(a), the spacing dual in-line-pin (DIP)
between pins in a column is 0.1 inch and the spacing between columns is 0.3 package
inch. Larger DIP packages accommodate functions with more pins, as shown in
(b) and (c). A pin diagram shows the assignment of device signals to package pin diagram
DO NOT COPYpins, or pinout. Figure 1-5 shows the pin diagrams for a few common SSI ICs. pinout
Such diagrams are used only for mechanical reference, when a designer needs to
determine the pin numbers for a particular IC. In the schematic diagram for a7400740274047408 7410
DO NOT COPYFigure 1-5 Pin diagrams for a few 7400-series SSI ICs.
1 VCC 14 1 VCC 14 1 VCC 14 1 VCC 14 1 VCC 14
2 13 2
3 12 3 NOT13 2 13 2 COPY13 2 13
12 3 12 3 12 3 12
DO4 11 4 11 4 11 4 11 4 11
5 10 5 10 5 10 5 10 5 10
6 96 9 6 96 96 9
7 GND 8 7 GND 8 7 GND 8 7 GND 8 7 GND 8
DO NOT COPY7411 7420 7421 7430 7432
1 VCC 14 1 VCC 14 1 VCC 14 1 VCC 14 1 VCC 14
2 13 2 13 2 13 2 13 2 13
3 12 3 NOT12 3 12 3 COPY12 3 12
4 11 4 11 4 11 4 11 4 11
5 10 5 10 5 10 5 10 5 10
96 96
DO6 9 6 96 9
7 GND 8 7 GND 8 7 GND 8 7 GND 8 7 GND 8
Copyright © 1999 by John F. Wakerly Copying Prohibited
14 Chapter 1 Introduction
TINY-SCALE In the coming years, perhaps the most popular remaining use of SSI and MSI,
DO NOT COPYINTEGRATION especially in DIP packages, will be in educational labs. These devices will afford
students the opportunity to “get their hands” dirty by “breadboarding” and wiring up
simple circuits in the same way that their professors did years ago.
However, much to my surprise and delight, a segment of the IC industry has
actually gone downscale from SSI in the past few years. The idea has been to sell
DO NOT COPYindividual logic gates in very small packages. These devices handle simple functions
that are sometimes needed to match larger-scale components to a particular design,
or in some cases they are used to work around bugs in the larger-scale components
or their interfaces.
An example of such an IC is Motorola’s 74VHC1G00. This chip is a single
DO NOT COPY2-input NAND gate housed in a 5-pin package (power, ground, two inputs, and one
output). The entire package, including pins, measures only 0.08 inches on a side, and
is only 0.04 inches high! Now that’s what I would call “tiny-scale integration”!
DO NOT COPYdigital circuit, pin diagrams are not used. Instead, the various gates are grouped
functionally, as we’ll show in Section 5.1.
Although SSI ICs are still sometimes used as “glue” to tie together larger-
scale elements in complex systems, they have been largely supplanted by pro-
grammable logic devices, which we’ll study in Sections 5.3 and 8.3.
The next larger commercially available ICs are called medium-scale
DO NOT COPYmedium-scale
integration (MSI) integration (MSI), and contain the equivalent of about 20 to 200 gates. An MSI
IC typically contains a functional building block, such as a decoder, register, or
counter. In Chapters 5 and 8, we’ll place a strong emphasis on these building
DO NOT COPYblocks. Even though the use of discrete MSI ICs is declining, the equivalent
building blocks are used extensively in the design of larger ICs.
large-scale integration Large-scale integration (LSI) ICs are bigger still, containing the equivalent
(LSI) of 200 to 200,000 gates or more. LSI parts include small memories, micro-
DO NOT COPYprocessors, programmable logic devices, and customized devices.
STANDARD
LOGIC
DO NOT COPYFUNCTIONS
Many standard “high-level” functions appear over and over as building blocks
in digital design. Historically, these functions were first integrated in MSI cir-
cuits. Subsequently, they have appeared as components in the “macro” libraries
for ASIC design, as “standard cells” in VLSI design, as “canned” functions in
PLD programming languages, and as library functions in hardware-description
languages such as VHDL.
Standard logic functions are introduced in Chapters 5 and 8 as 74-series
DO NOT COPYMSI parts, as well as in HDL form. The discussion and examples in these chap-
ters provide a basis for understanding and using these functions in any form.
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 1.7 Programmable Logic Devices 15
The dividing line between LSI and very large-scale integration (VLSI) is very large-scale
fuzzy, and tends to be stated in terms of transistor count rather than gate count. integration (VLSI)
DO NOT COPYAny IC with over 1,000,000 transistors is definitely VLSI, and that includes
most microprocessors and memories nowadays, as well as larger programmable
logic devices and customized devices. In 1999, the VLSI ICs as large as 50
million transistors were being designed.
DO NOT COPY1.7 Programmable Logic Devices
There are a wide variety of ICs that can have their logic function “programmed”
into them after they are manufactured. Most of these devices use technology that
also allows the function to be reprogrammed, which means that if you find a bug
DO NOT COPYin your design, you may be able to fix it without physically replacing or rewiring
the device. In this book, we’ll frequently refer to the design opportunities and
methods for such devices.
Historically, programmable logic arrays (PLAs) were the first program-
mable logic devices. PLAs contained a two-level structure of AND and OR gates
DO NOT COPYwith user-programmable connections. Using this structure, a designer could
programmable logic
array (PLA)
accommodate any logic function up to a certain level of complexity using the
well-known theory of logic synthesis and minimization that we’ll present in
Chapter 4.
PLA structure was enhanced and PLA costs were reduced with the intro-
DO NOT COPYduction of programmable array logic (PAL) devices. Today, such devices are
programmable array
logic (PAL) device
generically called programmable logic devices (PLDs), and are the “MSI” of the programmable logic
programmable logic industry. We’ll have a lot to say about PLD architecture and device (PLD)
technology in Sections 5.3 and 8.3.
DO NOT COPYThe ever-increasing capacity of integrated circuits created an opportunity
for IC manufacturers to design larger PLDs for larger digital-design applica-
tions. However, for technical reasons that we’ll discuss in \secref{CPLDs}, the
basic two-level AND-OR structure of PLDs could not be scaled to larger sizes.
Instead, IC manufacturers devised complex PLD (CPLD) architectures to complex PLD (CPLD)
DO NOT COPYachieve the required scale. A typical CPLD is merely a collection of multiple
PLDs and an interconnection structure, all on the same chip. In addition to the
individual PLDs, the on-chip interconnection structure is also programmable,
providing a rich variety of design possibilities. CPLDs can be scaled to larger
sizes by increasing the number of individual PLDs and the richness of the inter-
DO NOT COPYconnection structure on the CPLD chip.
At about the same time that CPLDs were being invented, other IC manu-
facturers took a different approach to scaling the size of programmable logic
chips. Compared to a CPLD, a field-programmable gate arrays (FPGA) contains
a much larger number of smaller individual logic blocks, and provides a large,
DO NOT COPYdistributed interconnection structure that dominates the entire chip. Figure 1-6
field-programmable
gate array (FPGA)
illustrates the difference between the two chip-design approaches.
Copyright © 1999 by John F. Wakerly Copying Prohibited
16 Chapter 1 Introduction
DO NOT COPYPLD PLD PLD PLD
DO NOT COPYProgrammableInterconnect
PLD PLD PLD PLD
DO NOT COPY(a) (b) =logicblock
F igur e 1 - 6 Large programmable-logic-device scaling approaches: (a) CPLD; (b) FPGA.
Proponents of one approach or the other used to get into “religious” argu-
DO NOT COPYments over which way was better, but the largest manufacturer of large
programmable logic devices, Xilinx Corporation, acknowledges that there is a
place for both approaches and manufactures both types of devices. What’s more
important than chip architecture is that both approaches support a style of design
in which products can be moved from design concept to prototype and produc-
DO NOT COPYtion in a very period of time short time.
Also important in achieving short “time-to-market” for all kinds of PLD-
based products is the use of HDLs in their design. Languages like ABEL and
VHDL, and their accompanying software tools, allow a design to be compiled,
synthesized, and downloaded into a PLD, CPLD, or FPGA literally in minutes.
DO NOT COPYThe power of highly structured, hierarchical languages like VHDL is especially
important in helping designers utilize the hundreds of thousands or millions of
gates that are provided in the largest CPLDs and FPGAs.
DO NOT COPY1.8 Application-Specific ICs
Perhaps the most interesting developments in IC technology for the average
DO NOT COPYsemicustomIC
digital designer are not the ever-increasing chip sizes, but the ever-increasing
opportunities to “design your own chip.” Chips designed for a particular, limited
product or application are called semicustom ICs or application-specific ICs
application-specific IC (ASICs). ASICs generally reduce the total component and manufacturing cost of
(ASIC) a product by reducing chip count, physical size, and power consumption, and
nonrecurring
DO NOT COPYengineering(NRE)
they often provide higher performance.
The nonrecurring engineering (NRE) cost for designing an ASIC can
exceed the cost of a discrete design by $5,000 to $250,000 or more. NRE charges
cost are paid to the IC manufacturer and others who are responsible for designing the
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 1.8 Application-Specific ICs 17
internal structure of the chip, creating tooling such as the metal masks for manu-
facturing the chips, developing tests for the manufactured chips, and actually
DO NOT COPYmaking the first few sample chips.
The NRE cost for a typical, medium-complexity ASIC with about 100,000
gates is $30–$50,000. An ASIC design normally makes sense only when the
NRE cost can be offset by the per-unit savings over the expected sales volume of
the product.
DO NOT COPYThe NRE cost to design a custom LSI chip—a chip whose functions, inter-
custom LSI
nal architecture, and detailed transistor-level design is tailored for a specific
customer—is very high, $250,000 or more. Thus, full custom LSI design is done
only for chips that have general commercial application or that will enjoy very
high sales volume in a specific application (e.g., a digital watch chip, a network
DO NOT COPYinterface, or a bus-interface circuit for a PC).
To reduce NRE charges, IC manufacturers have developed libraries of
standard cells including commonly used MSI functions such as decoders,
registers, and counters, and commonly used LSI functions such as memories,
programmable logic arrays, and microprocessors. In a standard-cell design, the
DO NOT COPYlogic designer interconnects functions in much the same way as in a multichip
standard cells
standard-cell design
MSI/LSI design. Custom cells are created (at added cost, of course) only if abso-
lutely necessary. All of the cells are then laid out on the chip, optimizing the
layout to reduce propagation delays and minimize the size of the chip. Minimiz-
DO NOT COPYing the chip size reduces the per-unit cost of the chip, since it increases the
number of chips that can be fabricated on a single wafer. The NRE cost for a
standard-cell design is typically on the order of $150,000.
Well, $150,000 is still a lot of money for most folks, so IC manufacturers
have gone one step further to bring ASIC design capability to the masses. A gate
DO NOT COPYarray is an IC whose internal structure is an array of gates whose interconnec- gate array
tions are initially unspecified. The logic designer specifies the gate types and
interconnections. Even though the chip design is ultimately specified at this very
low level, the designer typically works with “macrocells,” the same high-level
functions used in multichip MSI/LSI and standard-cell designs; software
DO NOT COPYexpands the high-level design into a low-level one.
The main difference between standard-cell and gate-array design is that the
macrocells and the chip layout of a gate array are not as highly optimized as
those in a standard-cell design, so the chip may be 25% or more larger, and
therefore may cost more. Also, there is no opportunity to create custom cells in
DO NOT COPYthe gate-array approach. On the other hand, a gate-array design can be complet-
ed faster and at lower NRE cost, ranging from about $5000 (what you’re told
initially) to $75,000 (what you find you’ve spent when you’re all done).
The basic digital design methods that you’ll study throughout this book
DO NOT COPYapply very well to the functional design of ASICs. However, there are additional
opportunities, constraints, and steps in ASIC design, which usually depend on
the particular ASIC vendor and design environment.
Copyright © 1999 by John F. Wakerly Copying Prohibited
18 Chapter 1 Introduction
printed-circuit board
DO NOT COPY(PCB)
1.9 Printed-Circuit Boards
An IC is normally mounted on a printed-circuit board (PCB) [or printed-wiring
board (PWB)] that connects it to other ICs in a system. The multilayer PCBs
printed-wiring board used in typical digital systems have copper wiring etched on multiple, thin layers
(PWB)
PCB traces
DO NOT COPYmil
of fiberglass that are laminated into a single board about 1/16 inch thick.
Individual wire connections, or PCB traces are usually quite narrow, 10 to
25 mils in typical PCBs. (A mil is one-thousandth of an inch.) In fine-line PCB
fine-line technology, the traces are extremely narrow, as little as 4 mils wide with 4-mil
spacing between adjacent traces. Thus, up to 125 connections may be routed in a
DO NOT COPYsurface-mount
one-inch-wide band on a single layer of the PCB. If higher connection density is
needed, then more layers are used.
Most of the components in modern PCBs use surface-mount technology
technology (SMT) (SMT). Instead of having the long pins of DIP packages that poke through the
board and are soldered to the underside, the leads of SMT IC packages are bent
to make flat contact with the top surface of the PCB. Before such components
are mounted on the PCB, a special “solder paste” is applied to contact pads on
DO NOT COPYthe PCB using a stencil whose hole pattern matches the contact pads to be
soldered. Then the SMT components are placed (by hand or by machine) on the
pads, where they are held in place by the solder paste (or in some cases, by glue).
Finally, the entire assembly is passed through an oven to melt the solder paste,
which then solidifies when cooled.
DO NOT COPYSurface-mount component technology, coupled with fine-line PCB tech-
nology, allows extremely dense packing of integrated circuits and other
components on a PCB. This dense packing does more than save space. For very
high-speed circuits, dense packing goes a long way toward minimizing adverse
DO NOT COPYanalog phenomena, including transmission-line effects and speed-of-light
limitations.
multichip module To satisfy the most stringent requirements for speed and density, multichip
DO NOT COPY(MCM)
modules (MCMs) have been developed. In this technology, IC dice are not
mounted in individual plastic or ceramic packages. Instead, the IC dice for a
high-speed subsystem (say, a processor and its cache memory) are bonded
directly to a substrate that contains the required interconnections on multiple
layers. The MCM is hermetically sealed and has its own external pins for power,
ground, and just those signals that are required by the system that contains it.
DO NOT COPY1.10 Digital-Design Levels
Digital design can be carried out at several different levels of representation and
abstraction. Although you may learn and practice design at a particular level,
from time to time you’ll need to go up or down a level or two to get the job done.
DO NOT COPYAlso, the industry itself and most designers have been steadily moving to higher
levels of abstraction as circuit density and functionality have increased.
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 1.10 Digital-Design Levels 19
The lowest level of digital design is device physics and IC manufacturing
processes. This is the level that is primarily responsible for the breathtaking
DO NOT COPYadvances in IC speed and density that have occurred over the past decades. The
effects of these advances are summarized in Moore’s Law, first stated by Intel Moore’s Law
founder Gordon Moore in 1965: that the number of transistors per square inch in
an IC doubles every year. In recent years, the rate of advance has slowed down to
doubling about every 18 months, but it is important to note that with each dou-
DO NOT COPYbling of density has also come a doubling of speed.
This book does not reach down to the level of device physics and IC
processes, but you need to recognize the importance of that level. Being aware of
likely technology advances and other changes is important in system and
product planning. For example, decreases in chip geometries have recently
DO NOT COPYforced a move to lower logic-power-supply voltages, causing major changes in
the way designers plan and specify modular systems and upgrades.
In this book, we jump into digital design at the transistor level and go all
the way up to the level of logic design using HDLs. We stop short of the next
DO NOT COPYlevel, which includes computer design and overall system design. The “center”
of our discussion is at the level of functional building blocks.
To get a preview of the levels of design that we’ll cover, consider a simple A
design example. Suppose you are to build a “multiplexer” with two data input B Z
bits, A and B, a control input bit S, and an output bit Z. Depending on the value S
DO NOT COPYof S, 0 or 1, the circuit is to transfer the value of either A or B to the output Z. This
idea is illustrated in the “switch model” of Figure 1-7. Let us consider the design Figure 1-7
Switch model for
multiplexer function.
of this function at several different levels.
Although logic design is usually carried out at higher level, for some func-
tions it is advantageous to optimize them by designing at the transistor level. The
DO NOT COPYmultiplexer is such a function. Figure 1-8 shows how the multiplexer can be
designed in “CMOS” technology using specialized transistor circuit structures
VCC F i g u r e 1 - 8
DO NOT COPYMultiplexer design using
CMOS transmission gates.
DA O NOT COPY
Z
DB O NOT COPY
S
Copyright © 1999 by John F. Wakerly Copying Prohibited
20 Chapter 1 Introduction
DO NOT COPYTable 1-1 SAB Z
Truth table for the0000
multiplexer function.
001 0
DO NOT COPY0 1 0 1
0111
1000
101 1
DO NOT COPY1 1 0 0
1111
called “transmission gates,” discussed in Section 3.7.1. Using this approach, the
multiplexer can be built with just six transistors. Any of the other approaches
that we describe require at least 14 transistors.
In the traditional study of logic design, we would use a “truth table” to
DO NOT COPYdescribe the multiplexer’s logic function. A truth table list all possible combina-
tions of input values and the corresponding output values for the function. Since
the multiplexer has three inputs, it has 23 or 8 possible input combinations, as
shown in the truth table in Table 1-1.
Once we have a truth table, traditional logic design methods, described in
DO NOT COPYSection 4.3, use Boolean algebra and well understood minimization algorithms
to derive an “optimal” two-level AND-OR equation from the truth table. For the
multiplexer truth table, we would derive the following equation:
DO NOT COPYZ = S′⋅A+S⋅B
This equation is read “Z equals not S and A or S and B.” Going one step further,
we can convert the equation into a corresponding set of logic gates that perform
the specified logic function, as shown in Figure 1-9. This circuit requires 14
transistors if we use standard CMOS technology for the four gates shown.
DO NOT COPYA multiplexer is a very commonly used function, and most digital logic
technologies provide predefined multiplexer building blocks. For example, the
74x157 is an MSI chip that performs multiplexing on two 4-bit inputs simulta-
neously. Figure 1-10 is a logic diagram that shows how we can hook up just one
bit of this 4-bit building block to solve the problem at hand. The numbers in
DO NOT COPYcolor are pin numbers of a 16-pin DIP package containing the device.
Figure 1-9A
Gate-level logic diagramS
DO NOT COPYfor multiplexer function.
ASN Z
SN
SB
B
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 1.10 Digital-Design Levels 21
74x157
15 G
DO NOT COPYS 1 S
A 2 1A 1Y 4 Z
DOB 3 1B 2Y 7
5 2A
6 2B NOT COPY3Y 9
11 3A Figure 1-10
10 3B
Logic diagram for a
DO14 4A
13 4B NOT COPY4Y 12
multiplexer using an
MSI building block.
We can also realize the multiplexer function as part of a programmable
logic device. Languages like ABEL allow us to specify outputs using Boolean
equations similar to the one on the previous page, but it’s usually more conve-
nient to use “higher-level” language elements. For example, Table 1-2 is an
DO NOT COPYABEL program for the multiplexer function. The first three lines define the
name of the program module and specify the type of PLD in which the function
will be realized. The next two lines specify the device pin numbers for inputs and
output. The “WHEN” statement specifies the actual logic function in a way that’s
DO NOT COPYvery easy to understand, even though we haven’t covered ABEL yet.
An even higher level language, VHDL, can be used to specify the multi-
plexer function in a way that is very flexible and hierarchical. Table 1-3 is an
example VHDL program for the multiplexer. The first two lines specify a
standard library and set of definitions to use in the design. The next four lines
DO NOT COPYspecify only the inputs and outputs of the function, and purposely hide any
details about the way the function is realized internally. The “architecture”
section of the program specifies the function’s behavior. VHDL syntax takes a
little getting used to, but the single “when” statement says basically the same
DO NOT COPYthing that the ABEL version did. A VHDL “synthesis tool” can start with this
module chap1mux Table 1-2
ABEL program for
the multiplexer.
DO NOT COPYA, B, S
title 'Two-input multiplexer example'
CHAP1MUX device 'P16V8'
pin 1, 2, 3;
Z pin 13 istype 'com';
equations
DO NOT COPYWHEN S == 0 THEN Z = A; ELSE Z = B;
end chap1mux
Copyright © 1999 by John F. Wakerly Copying Prohibited
22 Chapter 1 Introduction
Table 1-3
VHDL program for
DO NOT COPYthemultiplexer.
library IEEE;
use IEEE.std_logic_1164.all;
entity Vchap1mux is
port ( A, B, S: in STD_LOGIC;
out STD_LOGIC );
DO NOT COPYZ:
end Vchap1mux;
architecture Vchap1mux_arch of Vchap1mux is
begin
Z <= A when S = '0' else B;
end Vchap1mux_arch;
DO NOT COPYbehavioral description and produce a circuit that has this behavior in a specified
target digital-logic technology.
By explicitly enforcing a separation of input/output definitions (“entity”)
and internal realization (“architecture”), VHDL makes it easy for designers to
define alternate realizations of functions without having to make changes else-
DO NOT COPYwhere in the design hierarchy. For example, a designer could specify an
alternate, structural architecture for the multiplexer as shown in Table 1-4. This
architecture is basically a text equivalent of the logic diagram in Figure 1-9.
Going one step further, VHDL is powerful enough that we could actually
DO NOT COPYdefine operations that model functional behavioral at the transistor level (though
we won’t explore such capabilities in this book). Thus, we could come full circle
by writing a VHDL program that specifies a transistor-level realization of the
DO NOT COPYTable 1-4
multiplexer equivalent to Figure 1-8.
“Structural” VHDL architecture Vchap1mux_gate_arch of Vchap1mux is
signal SN, ASN, SB: STD_LOGIC;
program for the
DO NOT COPYmultiplexer.
begin
U1: INV (S, SN);
U2: AND2 (A, SN, ASN);
U3: AND2 (S, B, SB);
U4: OR2 (ASN, SB, Z);
end Vchap1mux_gate_arch;
DO NOT COPY1.11 The Name of the Game
Given the functional and performance requirements for a digital system, the
name of the game in practical digital design is to minimize cost. For board-level
designs—systems that are packaged on a single PCB—this usually means min-
imizing the number of IC packages. If too many ICs are required, they won’t all
DO NOT COPYboard-leveldesign
fit on the PCB. “Well, just use a bigger PCB,” you say. Unfortunately, PCB sizes
are usually constrained by factors such as pre-existing standards (e.g., add-in
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 1.12 Going Forward 23
boards for PCs), packaging constraints (e.g., it has to fit in a toaster), or edicts
from above (e.g., in order to get the project approved three months ago, you fool-
DO NOT COPYishly told your manager that it would all fit on a 3 × 5 inch PCB, and now you’ve
got to deliver!). In each of these cases, the cost of using a larger PCB or multiple
PCBs may be unacceptable.
Minimizing the number of ICs is usually the rule even though individual IC
costs vary. For example, a typical SSI or MSI IC may cost 25 cents, while an
DO NOT COPYsmall PLD may cost a dollar. It may be possible to perform a particular function
with three SSI and MSI ICs (75 cents) or one PLD (a dollar). In most situations,
the more expensive PLD solution is used, not because the designer owns stock in
the IC company, but because the PLD solution uses less PCB area and is also a
lot easier to change if it’s not right the first time.
DO NOT COPYIn ASIC design, the name of the game is a little different, but the impor-
ASIC design
tance of structured, functional design techniques is the same. Although it’s easy
to burn hours and weeks creating custom macrocells and minimizing the total
gate count of an ASIC, only rarely is this advisable. The per-unit cost reduction
DO NOT COPYachieved by having a 10% smaller chip is negligible except in high-volume
applications. In applications with low to medium volume (the majority), two
other factors are more important: design time and NRE cost.
A shorter design time allows a product to reach the market sooner, increas-
ing revenues over the lifetime of the product. A lower NRE cost also flows right
DO NOT COPYto the “bottom line,” and in small companies may be the only way the project can
be completed before the company runs out of money (believe me, I’ve been
there!). If the product is successful, it’s always possible and profitable to
“tweak” the design later to reduce per-unit costs. The need to minimize design
time and NRE cost argues in favor of a structured, as opposed to highly opti-
DO NOT COPYmized, approach to ASIC design, using standard building blocks provided in the
ASIC manufacturer’s library.
The considerations in PLD, CPLD, and FPGA design are a combination of
the above. The choice of a particular PLD technology and device size is usually
made fairly early in the design cycle. Later, as long as the design “fits” in the
DO NOT COPYselected device, there’s no point in trying to optimize gate count or board area—
the device has already been committed. However, if new functions or bug fixes
push the design beyond the capacity of the selected device, that’s when you must
DO NOT COPYwork very hard to modify the design to make it fit.
1.12 Going Forward
This concludes the introductory chapter. As you continue reading this book,
keep in mind two things. First, the ultimate goal of digital design is to build
DO NOT COPYsystems that solve problems for people. While this book will give you the basic
tools for design, it’s still your job to keep “the big picture” in the back of your
mind. Second, cost is an important factor in every design decision; and you must
Copyright © 1999 by John F. Wakerly Copying Prohibited
24 Chapter 1 Introduction
consider not only the cost of digital components, but also the cost of the design
activity itself.
DO NOT COPYFinally, as you get deeper into the text, if you encounter something that you
think you’ve seen before but don’t remember where, please consult the index.
I’ve tried to make it as helpful and complete as possible.
DO NOT COPYDrill Problems
1.1 Suggest some better-looking chapter-opening artwork to put on page 1 of the next
edition of this book.
1.2 Give three different definitions for the word “bit” as used in this chapter.
1.3 Define the following acronyms: ASIC, CAD, CD, CO, CPLD, DIP, DVD, FPGA,
DO NOT COPYHDL, IC, IP, LSI, MCM, MSI, NRE, OK, PBX, PCB, PLD, PWB, SMT, SSI,
VHDL, VLSI.
1.4 Research the definitions of the following acronyms: ABEL, CMOS, JPEG,
MPEG, OK, PERL, VHDL. (Is OK really an acronym?)
1.5 Excluding the topics in Section 1.2, list three once-analog systems that have
DO NOT COPY“gone digital” since you were born.
1.6 Draw a digital circuit consisting of a 2-input AND gate and three inverters, where
DO NOT COPYan inverter is connected to each of the AND gate’s inputs and its output. For each
of the four possible combinations of inputs applied to the two primary inputs of
this circuit, determine the value produced at the primary output. Is there a simpler
circuit that gives the same input/output behavior?
1.7 When should you use the pin diagrams of Figure 1-5 in the schematic diagram of
a circuit?
DO NOT COPY1.8 What is the relationship between “die” and “dice”?
DO NOT COPY
DO NOT COPY
DO NOT COPY
Copyright © 1999 by John F. Wakerly Copying Prohibited
••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••DO NOT 2c h a p t e r
COPY
DO NOT
COPYNumber Systems and Codes
DDO NOTigital systems are built from circuits that process binary digits—
0s and 1s—yet very few real-life problems are based on binary
numbers or any numbers at all. Therefore, a digital system
COPYdesigner must establish some correspondence between the bina-
ry digits processed by digital circuits and real-life numbers,
events, and conditions. The purpose of this chapter is to show you how
familiar numeric quantities can be represented and manipulated in a digital
system, and how nonnumeric data, events, and conditions also can be
DO NOTrepresented.
The first nine sections describe binary number systems and show how
addition, subtraction, multiplication, and division are performed in these
systems. Sections 2.10–2.13 show how other things, such as decimal num-
bers, text characters, mechanical positions, and arbitrary conditions, can be
COPYencoded using strings of binary digits.
Section 2.14 introduces “n-cubes,” which provide a way to visualize
the relationship between different bit strings. The n-cubes are especially
useful in the study of error-detecting codes in Section 2.15. We conclude the
chapter with an introduction to codes for transmitting and storing data one
DO NOTbitatatime.
Copyright © 1999 by John F. Wakerly Copying Prohibited 21
22 Chapter 2 Number Systems and Codes
2.1 Positional Number Systems
DO NOT COPYThe traditional number system that we learned in school and use every day in
positional number business is called a positional number system. In such a system, a number is rep-
system resented by a string of digits where each digit position has an associated weight.
DO NOT COPYweight
The value of a number is a weighted sum of the digits, for example:
1734 = 1·1000 + 7· 100 + 3·10 + 4·1
Each weight is a power of 10 corresponding to the digit’s position. A decimal
point allows negative as well as positive powers of 10 to be used:
5185.68 = 5· 1000 + 1·100 + 8·10 + 5· 1 + 6·0.1 + 8·0.01
DO NOT COPYIn general, a number D of the form d1d0.d−1d−2 has the value
D = d1·101 + d0·100 + d–1·10–1 + d–2·10–2
base Here, 10 is called the base or radix of the number system. In a general positional
radix number system, the radix may be any integer r ≥ 2, and a digit in position i has
DO NOT COPYweight ri. The general form of a number in such a system is
dp–1dp–2 · · · d1d0 . d–1d–2· · ·d–n
DO NOT COPYradixpoint
where there are p digits to the left of the point and n digits to the right of the
point, called the radix point. If the radix point is missing, it is assumed to be to
the right of the rightmost digit. The value of the number is the sum of each digit
multiplied by the corresponding power of the radix:
DO NOT COPY∑D = p–1 di⋅ri
i = –n
high-order digit Except for possible leading and trailing zeroes, the representation of a
number in a positional number system is unique. (Obviously, 0185.6300 equals
185.63, and so on.) The leftmost digit in such a number is called the high-order
or most significant digit; the rightmost is the low-order or least significant digit.
most significant digit
low-order digit
DO NOT COPYleastsignificant digit
As we’ll learn in Chapter 3, digital circuits have signals that are normally
binary digit in one of only two conditions—low or high, charged or discharged, off or on.
bit
DO NOT COPYbinaryradix
The signals in these circuits are interpreted to represent binary digits (or bits)
that have one of two values, 0 and 1. Thus, the binary radix is normally used to
represent numbers in a digital system. The general form of a binary number is
bp–1bp–2 · · · b1b0 . b–1b–2· · ·b–n
DO NOT COPYanditsvalueis
∑B = p – 1 bi ⋅ 2i
i = –n
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 2.2 Octal and Hexadecimal Numbers 23
In a binary number, the radix point is called the binary point. When dealing with binary point
binary and other nondecimal numbers, we use a subscript to indicate the radix
DO NOT COPYof each number, unless the radix is clear from the context. Examples of binary
numbers and their decimal equivalents are given below.
100112 = 1· 16 + 0 ·8 + 0· 4 + 1 ·2 + 1· 1 = 1910
1000102 = 1· 32 + 0 ·16 + 0·8 + 0 ·4 + 1· 2 + 0 ·1 = 3410
DO NOT COPY101.0012 = 1·4 + 0·2 + 1·1 + 0·0.5 + 0·0.25 + 1·0.125 = 5.12510
The leftmost bit of a binary number is called the high-order or most significant MSB
bit (MSB); the rightmost is the low-order or least significant bit (LSB).
LSB
DO NOT COPY2.2 Octal and Hexadecimal Numbers
Radix 10 is important because we use it in everyday business, and radix 2 is
important because binary numbers can be processed directly by digital circuits.
Numbers in other radices are not often processed directly, but may be important
DO NOT COPYfor documentation or other purposes. In particular, the radices 8 and 16 provide
convenient shorthand representations for multibit numbers in a digital system.
The octal number system uses radix 8, while the hexadecimal number sys-
tem uses radix 16. Table 2-1 shows the binary integers from 0 to 1111 and their
octal, decimal, and hexadecimal equivalents. The octal system needs 8 digits, so
DO NOT COPYit uses digits 0–7 of the decimal system. The hexadecimal system needs 16 dig-
octal number system
hexadecimal number
system
its, so it supplements decimal digits 0–9 with the letters A–F. hexadecimal digits
A–F
The octal and hexadecimal number systems are useful for representing
multibit numbers because their radices are powers of 2. Since a string of three
bits can take on eight different combinations, it follows that each 3-bit string can
DO NOT COPYbe uniquely represented by one octal digit, according to the third and fourth col-
umns of Table 2-1. Likewise, a 4-bit string can be represented by one
hexadecimal digit according to the fifth and sixth columns of the table.
Thus, it is very easy to convert a binary number to octal. Starting at the binary to octal
DO NOT COPYbinary point and working left, we simply separate the bits into groups of three conversion
and replace each group with the corresponding octal digit:
1000110011102 = 100 011 001 1102 = 43168
111011011101010012 = 011 101 101 110 101 0012 = 3556518
DO NOT COPYThe procedure for binary to hexadecimal conversion is similar, except we use
binary to hexadecimal
groups of four bits: conversion
1000110011102 = 1000 1100 11102 = 8CE16
DO NOT COPY111011011101010012 = 00011101 1011 1010 10012 = 1DBA916
In these examples we have freely added zeroes on the left to make the total num-
ber of bits a multiple of 3 or 4 as required.
Copyright © 1999 by John F. Wakerly Copying Prohibited
24 Chapter 2 Number Systems and Codes
Table 2-1 BinaryDecimal Octal 3-Bit 4-Bit
Binary, decimal, 0 0 String String
NOT COPY0
DOoctal, and 000 Hexadecimal 0000
0
hexadecimal
11 1 001 1 0001
numbers.
NOT10 2 2 COPY010 2 0010
DO 11 3 3 011 3 0011
100 4 4 100 4 0100
101 5 5 101 5 0101
6 6 110 6 0110
7 7 111 7 0111
8 10 — 8 1000
DO NOT COPY110
111
1000
1001 9 11 — 9 1001
DO NOT COPY1010 10
101111
110012
12 — A 1010
13 — B 1011
14 — C 1100
1101 13 15 — D 1101
1110 14 16 — E 1110
DO NOT COPY1111
15 17 — F 1111
If a binary number contains digits to the right of the binary point, we can
convert them to octal or hexadecimal by starting at the binary point and working
right. Both the left-hand and right-hand sides can be padded with zeroes to get
multiples of three or four bits, as shown in the example below:
DO NOT COPY10.10110010112 = 010 . 101 100 101 1002 = 2.54548
= 0010 . 1011 0010 11002 = 2 .B2C16
Converting in the reverse direction, from octal or hexadecimal to binary, is
very easy. We simply replace each octal or hexadecimal digit with the corre-
sponding 3- or 4-bit string, as shown below:
octal or hexadecimal to
DO NOT COPYbinaryconversion
NOT COPY13578 = 001 011 101 1112
DO 2046 .178 = 010 000 100 110 . 001 1112
BEAD16 = 1011 1110 1010 11012
9F. 46C16 = 1001 111 . 0100 0110 11002
The octal number system was quite popular 25 years ago because of certain
minicomputers that had their front-panel lights and switches arranged in groups
DO NOT COPYof three. However, the octal number system is not used much today, because of
byte the preponderance of machines that process 8-bit bytes. It is difficult to extract
individual byte values in multibyte quantities in the octal representation; for
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 2.3 General Positional Number System Conversions 25
WHEN I’M 64 As you grow older, you’ll find that the hexadecimal number system is useful for
DO NOT COPYmore than just computers. When I turned 40, I told friends that I had just turned 2816.
The “16” was whispered under my breath, of course. At age 50, I’ll be only 3216.
People get all excited about decennial birthdays like 20, 30, 40, 50, …, but you
should be able to convince your friends that the decimal system is of no fundamental
significance. More significant life changes occur around birthdays 2, 4, 8, 16, 32, and
DO NOT COPY64, when you add a most significant bit to your age. Why do you think the Beatles
sang “When I’m sixty-four”?
DO NOT COPYexample, what are the octal values of the four 8-bit bytes in the 32-bit number
with octal representation 123456701238?
In the hexadecimal system, two digits represent an 8-bit byte, and 2n digits
represent an n-byte word; each pair of digits constitutes exactly one byte. For
example, the 32-bit hexadecimal number 5678ABCD16 consists of four bytes
DO NOT COPYwith values 5616, 7816, AB16, and CD16. In this context, a 4-bit hexadecimal digit
is sometimes called a nibble; a 32-bit (4-byte) number has eight nibbles. Hexa- nibble
decimal numbers are often used to describe a computer’s memory address space.
For example, a computer with 16-bit addresses might be described as having
read/write memory installed at addresses 0–EFFF16, and read-only memory at
DO NOT COPYaddresses F000–FFFF16. Many computer programming languages use the prefix
“0x” to denote a hexadecimal number, for example, 0xBFC0000. 0x prefix
2.3 General Positional Number System Conversions
DO NOT COPYIn general, conversion between two radices cannot be done by simple substitu-
tions; arithmetic operations are required. In this section, we show how to convert
a number in any radix to radix 10 and vice versa, using radix-10 arithmetic.
In Section 2.1, we indicated that the value of a number in any radix is given
by the formula
DO NOT COPY∑D = p–1 di⋅ri
radix-r to decimal
conversion
i = –n
where r is the radix of the number and there are p digits to the left of the radix
point and n to the right. Thus, the value of the number can be found by convert-
DO NOT COPYing each digit of the number to its radix-10 equivalent and expanding the
formula using radix-10 arithmetic. Some examples are given below:
1CE816 = 1 ·163 + 12· 162 + 14 ·161 + 8· 160 = 740010
F1A316 = 15 ·163 + 1· 162 + 10 ·161 + 3· 160 = 6185910
DO NOT COPY436.58 = 4·82 + 3·81 + 6·80 + 5·8–1 = 286.62510
132.34 = 1 ·42 + 3· 41 + 2 ·40 + 3 ·4–1 = 30.7510
Copyright © 1999 by John F. Wakerly Copying Prohibited
26 Chapter 2 Number Systems and Codes
A shortcut for converting whole numbers to radix 10 is obtained by rewrit-
ing the expansion formula as follows:
DO NOT COPYD = ((···((dp–1)·r + dp–2)·r + ···) ··· r + d1)·r + d0
That is, we start with a sum of 0; beginning with the leftmost digit, we multiply
the sum by r and add the next digit to the sum, repeating until all digits have been
processed. For example, we can write
DO NOT COPYF1AC16 = (((15)·16 + 1·16 + 10)·16 + 12
Although this formula is not too exciting in itself, it forms the basis for a
very convenient method of converting a decimal number D to a radix r. Consider
what happens if we divide the formula by r. Since the parenthesized part of the
formula is evenly divisible by r, the quotient will be
decimal to radix-r
DO NOT COPYconversion
Q = (· · ·((dp–1)· r + dp–2)·r + · · ·)· r + d1
and the remainder will be d0. Thus, d0 can be computed as the remainder of the
long division of D by r. Furthermore, the quotient Q has the same form as the
DO NOT COPYoriginal formula. Therefore, successive divisions by r will yield successive dig-
its of D from right to left, until all the digits of D have been derived. Examples
are given below:
179 ÷ 2 = 89 remainder 1 (LSB)
DO NOT COPY÷2=44remainder1
÷2 = 22 remainder 0
DO NOT COPY÷2=11remainder0
÷2 = 5 remainder 1
÷2 = 2 remainder 1
÷2 = 1 remainder 0
÷2 = 0 remainder 1 (MSB)
DO 17910 = 101100112
NOT COPY467 ÷ 8 = 58 remainder 3 (least significant digit)
÷8 = 7 remainder 2
÷ 8 = 0 remainder 7 (most significant digit)
DO 46710 = 7238
NOT COPY3417 ÷ 16 = 213 remainder 9 (least significant digit)
÷ 16 = 13 remainder 5
÷ 16 = 0 remainder 13 (most significant digit)
341710 = D5916
DO NOT COPYTable 2-2 summarizes methods for converting among the most common radices.
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 2.3 General Positional Number System Conversions 27
DO NOT COPYTable 2-2 Conversion methods for common radices.
Conversion Method Example
DO NOT COPYHexadecimal Substitution 101110110012 = 101 1101 10012 = 5D916Binary to
Octal Substitution 101110110012 = 10 111 011 0012 = 27318
Summation 101110110012 = 1 ⋅ 1024 + 0 ⋅ 512 + 1 ⋅ 256 + 1 ⋅ 128 + 1 ⋅ 64
+ 0 ⋅ 32 + 1 ⋅ 16 + 1 ⋅ 8 + 0 ⋅ 4 + 0 ⋅ 2 + 1 ⋅ 1 = 149710
Decimal
DO NOT COPYOctalto
Binary Substitution 12348 = 001 010 011 1002
Hexadecimal Substitution 12348 = 001 010 011 1002 = 0010 1001 11002 = 29C16
12348 = 1 ⋅ 512 + 2 ⋅ 64 + 3 ⋅ 8 + 4 ⋅ 1 = 66810
DO NOT COPYHexadecimalto
Decimal Summation
Binary Substitution C0DE16 = 1100 0000 1101 11102
Octal
DO NOT COPYDecimal
Substitution C0DE16 = 1100 0000 1101 11102 = 1 100 000 011 011 1102 = 1403368
Summation C0DE16 = 12 ⋅ 4096 + 0 ⋅ 256 + 13 ⋅ 16 + 14 ⋅ 1 = 4937410
Decimal to
Division 10810 ÷ 2 = 54 remainder 0 (LSB)
DOBinary ÷2 = 27 remainder 0
÷2 = 13 remainder 1
NOT COPY÷2=6remainder 1
÷2 = 3 remainder 0
÷2 = 1 remainder 1
÷2 = 0 remainder 1
(least significant digit)
DO 10810 = 11011002 (MSB)
Octal Division NOT COPY10810 ÷ 8 = 13 remainder 4
÷8 = 1 remainder 5
DOHexadecimal Division ÷8 = 0 remainder 1 (most significant digit)
10810 = 1548
NOT COPY10810 ÷ 16 = 6 remainder 12 (least significant digit)
÷16 = 0 remainder 6 (most significant digit)
NOT COPY10810=6C16
DO
Copyright © 1999 by John F. Wakerly Copying Prohibited
28 Chapter 2 Number Systems and Codes
Table 2-3
Binary addition and
DO NOT COPYsubtraction table.
cin or bin x y cout s bout d
0 00 0 0 0 0
0 01 0 1 1 1
DO NOT COPY0 1 0 0 1 0 1
0 11 1 0 0 0
1 00 0 1 1 1
1 01 1 0 1 0
1 10 1 0 0 0
1 11 1 1 1 1
DO NOT COPY2.4 Addition and Subtraction of Nondecimal Numbers
Addition and subtraction of nondecimal numbers by hand uses the same tech-
nique that we learned in grammar school for decimal numbers; the only catch is
that the addition and subtraction tables are different.
Table 2-3 is the addition and subtraction table for binary digits. To add two
DO NOT COPYbinaryaddition
binary numbers X and Y, we add together the least significant bits with an initial
carry (cin) of 0, producing carry (cout) and sum (s) bits according to the table. We
continue processing bits from right to left, adding the carry out of each column
DO NOT COPYintothe nextcolumn’ssum.
Two examples of decimal additions and the corresponding binary additions
are shown in Figure 2-1, using a colored arrow to indicate a carry of 1. The same
examples are repeated below along with two more, with the carries shown as a
bit string C:
DO NOT COPYC 101111000 C 001011000
X 190 10111110 X 173 10101101
DO NOT COPYY +141 + 10001101
X + Y 331 101001011 Y + 44 + 00101100
X + Y 217 11011001
C 011111110 C 000000000
01111111 X 170
+ 00111111 Y + 85
X + Y 255
10111110
DO NOT COPYX 127 10101010
Y + 63 + 01010101
X + Y 190
11111111
binary subtraction Binary subtraction is performed similarly, using borrows (bin and bout)
instead of carries between steps, and producing a difference bit d. Two examples
of decimal subtractions and the corresponding binary subtractions are shown in
Figure 2-2. As in decimal subtraction, the binary minuend values in the columns
are modified when borrows occur, as shown by the colored arrows and bits. The
minuend
DO NOT COPYsubtrahend
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 2.4 Addition and Subtraction of Nondecimal Numbers 29
DO NOT COPYY +141 + 1 0 0 0 1 1 0 11111 1 11
X 190 10111110 X 173 10101101
Y + 44 + 0 0 1 0 1 1 0 0
X+Y 331 1 0 1 0 0 1 0 1 1 X+Y 217 11011001
Figure 2-1 Examples of decimal and corresponding binary additions.
DO NOT COPYexamples from the figure are repeated below along with two more, this time
showing the borrows as a bit string B:
B 001111100
X 229 11100101
DO NOT COPYY − 46 − 00101110
B 011011010
X 210 11010010
Y −109 − 01101101
X − Y 183 10110111 X − Y 101 01100101
B 010101010
B 000000000
DO NOT COPYX 170 10101010 X 221 11011101
Y − 85 − 01010101 Y − 76 − 01001100
A very common use of subtraction in computers is to compare two numbers. For comparing numbers
DO NOT COPYexample, if the operation X − Y produces a borrow out of the most significant bit
X − Y 85 01010101 X − Y 145 10010001
position, then X is less than Y; otherwise, X is greater than or equal to Y. The rela-
tionship between carries and borrow in adders and subtractors will be explored
in Section 5.10.
DO NOT COPYAddition and subtraction tables can be developed for octal and hexadeci-
mal digits, or any other desired radix. However, few computer engineers bother
to memorize these tables. If you rarely need to manipulate nondecimal numbers,
Must borrow 1, yielding
DO NOT COPYthenewsubtraction10–1=1
Figure 2-2
After the first borrow, the new Examples of decimal
DO NOTsubtraction for this column is COPYand corresponding
0–1, so we must borrow again. binary subtractions.
The borrow ripples through three columns
to reach a borrowable 1, i.e.,
100 = 011 (the modified bits)
+ 1 (the borrow)
minuend NOT0 10 1 1 10 10 COPYX 210
11100101 Y – 109
DOsubtrahend –00101110 0 10 10 0 1 10 0 10
X 229 11010010
Y – 46 –01101101
difference X – Y 183 10110111 X–Y 101 01100101
Copyright © 1999 by John F. Wakerly Copying Prohibited
30 Chapter 2 Number Systems and Codes
then it’s easy enough on those occasions to convert them to decimal, calculate
results, and convert back. On the other hand, if you must perform calculations in
DO NOT COPYbinary, octal, or hexadecimal frequently, then you should ask Santa for a pro-
grammer’s “hex calculator” from Texas Instruments or Casio.
If the calculator’s battery wears out, some mental shortcuts can be used to
facilitate nondecimal arithmetic. In general, each column addition (or subtrac-
tion) can be done by converting the column digits to decimal, adding in decimal,
DO NOT COPYand converting the result to corresponding sum and carry digits in the nondeci-
mal radix. (A carry is produced whenever the column sum equals or exceeds the
radix.) Since the addition is done in decimal, we rely on our knowledge of the
decimal addition table; the only new thing that we need to learn is the conversion
from decimal to nondecimal digits and vice versa. The sequence of steps for
mentally adding two hexadecimal numbers is shown below:
DO NOT COPYhexadecimaladdition
C 1100 1 1 00
DO NOT COPYX 1 9 B 9 16 1
Y + C 7 E 6 16 + 12
9 11 9
7 14 6
X+Y E 1 9 F 16 14 17 25 15
14 16+1 16+9 15
E19F
DO NOT COPY2.5 Representation of Negative Numbers
So far, we have dealt only with positive numbers, but there are many ways to rep-
resent negative numbers. In everyday business, we use the signed-magnitude
system, discussed next. However, most computers use one of the complement
DO NOT COPYnumber systems that we introduce later.
2.5.1 Signed-Magnitude Representation
signed-magnitude
DO NOT COPYsystem
In the signed-magnitude system, a number consists of a magnitude and a symbol
indicating whether the magnitude is positive or negative. Thus, we interpret dec-
imal numbers +98, −57, +123.5, and −13 in the usual way, and we also assume
that the sign is “+” if no sign symbol is written. There are two possible represen-
tations of zero, “+0” and “−0”, but both have the same value.
The signed-magnitude system is applied to binary numbers by using an
extra bit position to represent the sign (the sign bit). Traditionally, the most sig-
nificant bit (MSB) of a bit string is used as the sign bit (0 = plus, 1 = minus), and
DO NOT COPYsignbit
the lower-order bits contain the magnitude. Thus, we can write several 8-bit
signed-magnitude integers and their decimal equivalents:
010101012 = +8510
DO NOT COPY011111112 = +12710
110101012 = –8510
111111112 = –12710
000000002 = +010 100000002 = –010
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 2.5 Representation of Negative Numbers 31
The signed-magnitude system has an equal number of positive and nega-
tive integers. An n-bit signed-magnitude integer lies within the range −(2n−1−1)
DO NOT COPYthrough +(2n−1−1), and there are two possible representations of zero.
Now suppose that we wanted to build a digital logic circuit that adds
signed-magnitude numbers. The circuit must examine the signs of the addends
to determine what to do with the magnitudes. If the signs are the same, it must
add the magnitudes and give the result the same sign. If the signs are different, it
DO NOT COPYmust compare the magnitudes, subtract the smaller from the larger, and give the
signed-magnitude
adder
result the sign of the larger. All of these “ifs,” “adds,” “subtracts,” and “com-
pares” translate into a lot of logic-circuit complexity. Adders for complement
number systems are much simpler, as we’ll show next. Perhaps the one redeem-
ing feature of a signed-magnitude system is that, once we know how to build a
DO NOT COPYsigned-magnitude adder, a signed-magnitude subtractor is almost trivial to
signed-magnitude
build—it need only change the sign of the subtrahend and pass it along with the subtractor
minuend to an adder.
2.5.2 Complement Number Systems
DO NOT COPYWhile the signed-magnitude system negates a number by changing its sign, a
complement number system negates a number by taking its complement as complement number
system
defined by the system. Taking the complement is more difficult than changing
the sign, but two numbers in a complement number system can be added or sub-
DO NOT COPYtracted directly without the sign and magnitude checks required by the signed-
magnitude system. We shall describe two complement number systems, called
the “radix complement” and the “diminished radix-complement.”
In any complement number system, we normally deal with a fixed number
of digits, say n. (However, we can increase the number of digits by “sign exten-
DO NOT COPYsion” as shown in Exercise 2.23, and decrease the number by truncating high-
order digits as shown in Exercise 2.24.) We further assume that the radix is r, and
that numbers have the form
D = dn–1dn–2· · · d1d0 .
DO NOT COPYThe radix point is on the right and so the number is an integer. If an operation
produces a result that requires more than n digits, we throw away the extra high-
order digit(s). If a number D is complemented twice, the result is D.
2.5.3 Radix-Complement Representation
DO NOT COPYIn a radix-complement system, the complement of an n-digit number is obtained radix-complement
by subtracting it from r n. In the decimal number system, the radix complement system
is called the 10’s complement. Some examples using 4-digit decimal numbers
(and subtraction from 10,000) are shown in Table 2-4.
By definition, the radix complement of an n-digit number D is obtained by
DO NOT COPYsubtracting it from rn. If D is between 1 and rn − 1, this subtraction produces
10’s complement
Copyright © 1999 by John F. Wakerly Copying Prohibited
32 Chapter 2 Number Systems and Codes
Table 2-4
Examples of 10’s and
DO NOT COPY9s’complements.
Number 10’s 9s’
1849 complement complement
8151 8150
2067 7933 7932
DO NOT COPY100 9900 9899
7 9993 9992
8151 1849 1848
0 10000 (= 0) 9999
another number between 1 and r n − 1. If D is 0, the result of the subtraction is rn,
DO NOT COPYwhich has the form 100 ⋅ ⋅ ⋅ 00, where there are a total of n + 1 digits. We throw
away the extra high-order digit and get the result 0. Thus, there is only one rep-
resentation of zero in a radix-complement system.
computing the radix
DO NOT COPYcomplement
It seems from the definition that a subtraction operation is needed to com-
pute the radix complement of D. However, this subtraction can be avoided by
rewriting rn as (r n − 1) + 1 and r n − D as ((r n − 1) − D) + 1. The number rn − 1
has the form mm ⋅ ⋅ ⋅ mm, where m = r − 1 and there are n m’s. For example,
10,000 equals 9,999 + 1. If we define the complement of a digit d to be r − 1 − d,
then (rn − 1) − D is obtained by complementing the digits of D. Therefore, the
DO NOT COPYradix complement of a number D is obtained by complementing the individual
Table 2-5 Complement
DO NOT COPYDigitcomplements. Decimal
Digit Binary Octal 9 Hexadecimal
0 1 7 F
10 6 8 E
DO NOT COPY2 – 5 7 D
3– 46 C
4– 35 B
5– 24 A
6– 1 3 9
DO NOT COPY7 – 0 2 8
8– –1 7
9– –0 6
A– – – 5
DO NOT COPYB – – – 4
C–– – 3
D–– – 2
E– – – 1
F– – – 0
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 2.5 Representation of Negative Numbers 33
digits of D and adding 1. For example, the 10’s complement of 1849 is 8150 + 1,
or 8151. You should confirm that this trick also works for the other 10’s-comple-
DO NOT COPYment examples above. Table 2-5 lists the digit complements for binary, octal,
decimal, and hexadecimal numbers.
2.5.4 Two’s-Complement Representation
For binary numbers, the radix complement is called the two’s complement. The two’s complement
DO NOT COPYMSB of a number in this system serves as the sign bit; a number is negative if
and only if its MSB is 1. The decimal equivalent for a two’s-complement binary
number is computed the same way as for an unsigned number, except that the
weight of the MSB is −2n−1 instead of +2 n−1. The range of representable num- weight of MSB
DO NOT COPYbers is −(2 n−1) through +(2 n−1 −1). Some 8-bit examples are shown below:
1710 = 000100012 −9910 = 100111012
⇓ . complement bits ⇓ . complement bits
11101110
+1
DO NOT COPY111011112 = −1710
01100010
+1
011000112 = 9910
DO NOT COPY11910 =
01110111 −12710 = 10000001
⇓ . complement bits ⇓ . complement bits
10001000 01111110
+1 +1
100010012 = −11910 011111112 = 12710
DO NOT COPY010 =
000000002 −12810 = 100000002
⇓ . complement bits ⇓ . complement bits
11111111 01111111
+1
DO NOT COPY1 000000002 = 010
+1
100000002 = −12810
A carry out of the MSB position occurs in one case, as shown in color above. As
in all two’s-complement operations, this bit is ignored and only the low-order n
bits of the result are used.
In the two’s-complement number system, zero is considered positive
DO NOT COPYbecause its sign bit is 0. Since two’s complement has only one representation of
zero, we end up with one extra negative number, −(2 n−1), that doesn’t have a pos- extra negative number
itive counterpart.
We can convert an n-bit two’s-complement number X into an m-bit one, but
some care is needed. If m > n, we must append m − n copies of X’s sign bit to the
DO NOT COPYleft of X (see Exercise 2.23). That is, we pad a positive number with 0s and a
negative one with 1s; this is called sign extension. If m < n, we discard X’s n − m sign extension
Copyright © 1999 by John F. Wakerly Copying Prohibited
34 Chapter 2 Number Systems and Codes
leftmost bits; however, the result is valid only if all of the discarded bits are the
same as the sign bit of the result (see Exercise 2.24).
DO NOT COPYMost computers and other digital systems use the two’s-complement sys-
tem to represent negative numbers. However, for completeness, we’ll also
describe the diminished radix-complement and ones’-complement systems.
DO NOT COPY*2.5.5 Diminished Radix-Complement Representation
diminished radix- In a diminished radix-complement system, the complement of an n-digit number
complement system D is obtained by subtracting it from r n−1. This can be accomplished by comple-
DO NOT COPY9s’complement
menting the individual digits of D, without adding 1 as in the radix-complement
system. In decimal, this is called the 9s’ complement; some examples are given
in the last column of Table 2-4 on page 32.
*2.5.6 Ones’-Complement Representation
The diminished radix-complement system for binary numbers is called the ones’
complement. As in two’s complement, the most significant bit is the sign, 0 if
positive and 1 if negative. Thus there are two representations of zero, positive
DO NOT COPYones’complement
zero (00 ⋅ ⋅ ⋅ 00) and negative zero (11 ⋅ ⋅ ⋅ 11). Positive number representations
are the same for both ones’ and two’s complements. However, negative number
representations differ by 1. A weight of −(2n−1 − 1), rather than −2n−1, is given
to the most significant bit when computing the decimal equivalent of a ones’-
DO NOT COPYcomplement number. The range of representable numbers is −(2n−1 − 1) through
+(2n−1 − 1). Some 8-bit numbers and their ones’ complements are shown below:
1710 = 000100012
DO NOT COPY⇓ .
−9910 = 100111002
⇓.
111011102 = −1710 011000112 = 9910
11910 = 011101112
⇓.
DO NOT COPY100010002 = −11910
−12710 = 100000002
⇓.
011111112 = 12710
DO NOT COPY010 = 000000002 (positive zero)0000000
⇓.
000 0111111112 = 010 (negative zero)
The main advantages of the ones’-complement system are its symmetry
and the ease of complementation. However, the adder design for ones’-
complement numbers is somewhat trickier than a two’s-complement adder (see
DO NOT COPYExercise 7.67). Also, zero-detecting circuits in a ones’-complement system
* Throughout this book, optional sections are marked with an asterisk.
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 2.6 Two’s-Complement Addition and Subtraction 35
either must check for both representations of zero, or must always convert
DO NOT COPY11⋅⋅⋅11to00⋅⋅⋅00.
*2.5.7 Excess Representations
Yes, the number of different systems for representing negative numbers is exces-
sive, but there’s just one more for us to cover. In excess-B representation, an
m-bit string whose unsigned integer value is M (0 ≤ M < 2m) represents the
DO NOT COPYsigned integer M − B, where B is called the bias of the number system.
excess-B representation
bias
For example, an excess−2m−1 system represents any number X in the range excess-2m−1 system
−2m−1 through +2m−1 − 1 by the m-bit binary representation of X + 2m−1 (which
is always nonnegative and less than 2m). The range of this representation is
DO NOT COPYexactly the same as that of m-bit two’s-complement numbers. In fact, the repre-
sentations of any number in the two systems are identical except for the sign bits,
which are always opposite. (Note that this is true only when the bias is 2m−1.)
The most common use of excess representations is in floating-point num-
DO NOT COPYber systems (see References).
2.6 Two’s-Complement Addition and Subtraction
2.6.1 Addition Rules
DO NOT COPYA table of decimal numbers and their equivalents in different number systems,
Table 2-6, reveals why the two’s complement is preferred for arithmetic opera-
tions. If we start with 10002 (−810) and count up, we see that each successive
two’s-complement number all the way to 01112 (+710) can be obtained by add-
ing 1 to the previous one, ignoring any carries beyond the fourth bit position.
DO NOT COPYThe same cannot be said of signed-magnitude and ones’-complement numbers.
Because ordinary addition is just an extension of counting, two’s-complement two’s-complement
addition
numbers can thus be added by ordinary binary addition, ignoring any carries
beyond the MSB. The result will always be the correct sum as long as the range
of the number system is not exceeded. Some examples of decimal addition and
DO NOT COPYthe corresponding 4-bit two’s-complement additions confirm this:
0011 +3 −2 1110
+ 0100 + +4 + −6 + 1010
0111DO NOT COPY+7 −8 1 1000
+6 0110 +4 0100
+ −3 + 1101
DO NOT COPY+3 10011
+ −7 + 1001
−3 1101
Copyright © 1999 by John F. Wakerly Copying Prohibited
36 Chapter 2 Number Systems and Codes
DO NOT COPYDecimal
Ta b l e 2 - 6 Decimal and 4-bit numbers.
Two’s Ones’ Signed Excess
Complement Complement Magnitude 2 m−1
DO NOT−8 1000 COPY— — 0000
DO −7 1001 1111 0001
DO −6 1010 1000 1110 0010
DO −5 1011 1001
1010 1101 0011
NOT−4 1100 COPY1011 1100 0100
−3 1101 1011 0101
−2 1110 1100 1010 0110
−1 1111 1101
1110 1001 0111
0000 1111 or 0000 1000 or 0000
NOT COPY0 1000
1 0001 0001 0001 1001
2 0010 0010 0010 1010
3 0011 0011 0011 1011
4 0100 0100 0100 1100
COPY0101
NOT5 0101 0101 1101
6 0110 0110 0110 1110
7 0111 0111 0111 1111
2.6.2 A Graphical View
Another way to view the two’s-complement system uses the 4-bit “counter”
DO NOT COPYshown in Figure 2-3. Here we have shown the numbers in a circular or
“modular” representation. The operation of this counter very closely mimics that
DO NOT COPYFigure 2-3
of a real up/down counter circuit, which we’ll study in Section 8.4. Starting
0000
1111 0001
A modular counting 1110 –1 +0 +1 0010
representation of 4-bit
two’s-complement 1101 –2 +2 0011
DO NOT COPYnumbers.
–3 +3
Subtraction of 1100 –4 +4 0100 Addition of
positive numbers positive numbers
DO NOT COPY–5 +5
1011 –6 0101
+6
–7 –8 +7
1010 0110
1001 1000 0111
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 2.6 Two’s-Complement Addition and Subtraction 37
with the arrow pointing to any number, we can add +n to that number by
counting up n times, that is, by moving the arrow n positions clockwise. It is also
DO NOT COPYevident that we can subtract n from a number by counting down n times, that is,
by moving the arrow n positions counterclockwise. Of course, these operations
give correct results only if n is small enough that we don’t cross the discontinuity
between −8 and +7.
What is most interesting is that we can also subtract n (or add −n) by mov-
DO NOT COPYing the arrow 16 − n positions clockwise. Notice that the quantity 16 − n is what
we defined to be the 4-bit two’s complement of n, that is, the two’s-complement
representation of −n. This graphically supports our earlier claim that a negative
number in two’s-complement representation may be added to another number
simply by adding the 4-bit representations using ordinary binary addition. Add-
DO NOT COPYing a number in Figure 2-3 is equivalent to moving the arrow a corresponding
number of positions clockwise.
2.6.3 Overflow
If an addition operation produces a result that exceeds the range of the number
DO NOT COPYsystem, overflow is said to occur. In the modular counting representation of overflow
Figure 2-3, overflow occurs during addition of positive numbers when we count
past +7. Addition of two numbers with different signs can never produce over-
flow, but addition of two numbers of like sign can, as shown by the following
DO NOT COPYexamples:
−3 1101 +5 0101
+ +6 + 0110
+11 1011 = −5
+7 0111
+ −6 + 1010
−9 10111 = +7
DO NOT COPY−8 1000
+ −8 + 1000 + +7 + 0111
Fortunately, there is a simple rule for detecting overflow in addition: An overflow rules
DO NOT COPYaddition overflows if the signs of the addends are the same and the sign of the
−16 10000 = +0 +14 1110 = −2
sum is different from the addends’ sign. The overflow rule is sometimes stated in
terms of carries generated during the addition operation: An addition overflows
if the carry bits cin into and cout out of the sign position are different. Close exam-
ination of Table 2-3 on page 28 shows that the two rules are equivalent—there
DO NOT COPYare only two cases where cin ≠ cout, and these are the only two cases where x = y
and the sum bit is different.
2.6.4 Subtraction Rules
DO NOT COPYTwo’s-complement numbers may be subtracted as if they were ordinary two’s-complement
unsigned binary numbers, and appropriate rules for detecting overflow may be subtraction
formulated. However, most subtraction circuits for two’s-complement numbers
Copyright © 1999 by John F. Wakerly Copying Prohibited
38 Chapter 2 Number Systems and Codes
do not perform subtraction directly. Rather, they negate the subtrahend by taking
its two’s complement, and then add it to the minuend using the normal rules for
DO NOT COPYaddition.
Negating the subtrahend and adding the minuend can be accomplished
with only one addition operation as follows: Perform a bit-by-bit complement of
the subtrahend and add the complemented subtrahend to the minuend with an
DO NOT COPYinitial carry (cin) of 1 instead of 0. Examples are given below:
1 — cin 1 — cin
+4 0100 0100 +3 0011 0011
− +3 − 0011 + 1100
DO NOT COPY+3 10001
− +4 − 0100 + 1011
−1 1111
1 — cin 1 — cin
+300110011 −3 1101 1101
− −4− 1100+ 0011 − −4 − 1100 + 0011
DO NOT COPY+70111 +1 1 0001
Overflow in subtraction can be detected by examining the signs of the min-
uend and the complemented subtrahend, using the same rule as in addition. Or,
using the technique in the preceding examples, the carries into and out of the
DO NOT COPYsign position can be observed and overflow detected irrespective of the signs of
inputs and output, again using the same rule as in addition.
An attempt to negate the “extra” negative number results in overflow
according to the rules above, when we add 1 in the complementation process:
−(−8) = −1000 = 0111
DO NOT COPY+ 0001
1000 = −8
However, this number can still be used in additions and subtractions as long as
DO NOT COPYthe final result does not exceed the number range:
1 — cin
0100DO NOT COPY+4 −3 1101 1101
+ 1000 + −8 − −8 − 1000 + 0111
−4
1100 +5 1 0101
2.6.5 Two’s-Complement and Unsigned Binary Numbers
Since two’s-complement numbers are added and subtracted by the same basic
binary addition and subtraction algorithms as unsigned numbers of the same
DO NOT COPYlength, a computer or other digital system can use the same adder circuit to han-
dle numbers of both types. However, the results must be interpreted differently
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section 2.6 Two’s-Complement Addition and Subtraction 39
DO NOT COPYWe introduced a graphical representation of the 4-bit two’s-complement
depending on whether the system is dealing with signed numbers (e.g., −8 signed vs. unsigned
through +7) or unsigned numbers (e.g., 0 through 15). numbers
system in Figure 2-3. We can relabel this figure as shown in Figure 2-4 to obtain
a representation of the 4-bit unsigned numbers. The binary combinations occupy
the same positions on the wheel, and a number is still added by moving the arrow
a corresponding number of positions clockwise, and subtracted by moving the
DO NOT COPYarrowcounterclockwise.
An addition operation can be seen to exceed the range of the 4-bit unsigned
number system in Figure 2-4 if the arrow moves clockwise through the disconti- carry
nuity between 0 and 15. In this case a carry out of the most significant bit
position is said to occur.
DO NOT COPYLikewise a subtraction operation exceeds the range of the number system if
the arrow moves counterclockwise through the discontinuity. In this case a bor-
row out of the most significant bit position is said to occur. borrow
DO NOT COPYber n by counting clockwise 16 − n positions. This is equivalent to adding the
From Figure 2-4 it is also evident that we may subtract an unsigned num-
4-bit two’s-complement of n. The subtraction produces a borrow if the corre-
sponding addition of the two’s complement does not produce a carry.
In summary, in unsigned addition the carry or borrow in the most signifi-
cant bit position indicates an out-of-range result. In signed, two’s-complement
DO NOT COPYaddition the overflow condition defined earlier indicates an out-of-range result.
The carry from the most significant bit position is irrelevant in signed addition in
the sense that overflow may or may not occur independently of whether or not a
DO NOT COPYcarryoccurs.
DO NOT COPY1111 0000
0001
1110 15 0 1 0010
1101 14 2 0011 Figure 2-4
13 3 A modular counting
representation of 4-bit
4 0100 unsigned numbers.
DO NOT COPYSubtraction Addition
1100 12
11 5
1011 10 0101
6
7
0110
0111
DO NOT COPY1001 1000 9 8
1010
Copyright © 1999 by John F. Wakerly Copying Prohibited
40 Chapter 2 Number Systems and Codes
*2.7 Ones’-Complement Addition and Subtraction
Another look at Table 2-6 helps to explain the rule for adding ones’-complement
DO NOT COPYnumbers. If we start at 10002 (−710) and count up, we obtain each successive
ones’-complement number by adding 1 to the previous one, except at the transi-
tion from 11112 (negative 0) to 00012 (+110). To maintain the proper count, we
must add 2 instead of 1 whenever we count past 11112. This suggests a technique
DO NOT COPYfor adding ones’-complement numbers: Perform a standard binary addition, but
add an extra 1 whenever we count past 11112.
Counting past 11112 during an addition can be detected by observing the
carry out of the sign position. Thus, the rule for adding ones’-complement num-
bers can be stated quite simply:
• Perform a standard binary addition; if there is a carry out of the sign posi-
ones’-complement
DO NOT COPYaddition
tion, add 1 to the result.
DO NOT COPYend-aroundcarry
This rule is often called end-around carry. Examples of ones’-complement addi-
tion are given below; the last three include an end-around carry:
+3 0011 +4 0100 +5 0101
+ −7 + 1000 + −5 + 1010
−3 1100 −0 1111
+6 0110 −0 1111
DO NOT COPY+ +4 + 0100
+7 0111
−2 1101
+ −5 + 1010 + −3 + 1100 + −0 + 1111
DO NOT COPY−7 10111
+1
1000
+3 10010 −0 11110
+1 +1
0011 1111
Following the two-step addition rule above, the addition of a number and
its ones’ complement produces negative 0. In fact, an addition operation using
this rule can never produce positive 0 unless both addends are positive 0.
As with two’s complement, the easiest way to do ones’-complement sub-
DO NOT COPYones’-complement
subtraction traction is to complement the subtrahend and add. Overflow rules for ones’-
complement addition and subtraction are the same as for two’s complement.
Table 2-7 summarizes the rules that we presented in this and previous sec-
DO NOT COPYtions for negation, addition, and subtraction in binary number systems.
DO NOT COPY
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section *2.8 Binary Multiplication 41
DO NOT COPYNumberSystem
Ta b l e 2 - 7 Summary of addition and subtraction rules for binary numbers.
Addition Rules Negation Rules Subtraction Rules
Unsigned Add the numbers. Result is out of Not applicable Subtract the subtrahend
DO NOTrange if a carry out of the MSB COPYfrom the minuend. Result is
occurs. out of range if a borrow out
of the MSB occurs.
Signed magnitude (same sign) Add the magnitudes; Change the number’s Change the sign bit of the
overflow occurs if a carry out of sign bit. subtrahend and proceed as
MSB occurs; result has the same
sign. in addition.
DO NOT COPY(opposite sign) Subtract the
smaller magnitude from the larg-
er; overflow is impossible; result
DO NOT COPYTwo’scomplement
has the sign of the larger.
Add, ignoring any carry out of Complement all bits of Complement all bits of the
the MSB. Overflow occurs if the the number; add 1 to the subtrahend and add to the
carries into and out of MSB are result. minuend with an initial
different. carry of 1.
DO NOT COPYOnes’complement
Add; if there is a carry out of the Complement all bits of Complement all bits of the
MSB, add 1 to the result. Over- the number. subtrahend and proceed as
flow if carries into and out of in addition.
MSB are different.
DO NOT COPY*2.8 Binary Multiplication
In grammar school we learned to multiply by adding a list of shifted multipli-
cands computed according to the digits of the multiplier. The same method can
be used to obtain the product of two unsigned binary numbers. Forming the
DO NOT COPYshifted multiplicands is trivial in binary multiplication, since the only possible
shift-and-add
multiplication
unsigned binary
multiplication
values of the multiplier digits are 0 and 1. An example is shown below:
11 1011 multiplicand
× 13× 1101 multiplier
DO NOT COPY33 1011 shifted multiplicands
110 00000
product
DO NO} T COPY143101100
1011000
10001111
Copyright © 1999 by John F. Wakerly Copying Prohibited
42 Chapter 2 Number Systems and Codes
DO NOT COPYpartialproduct
Instead of listing all the shifted multiplicands and then adding, in a digital
system it is more convenient to add each shifted multiplicand as it is created to a
partial product. Applying this technique to the previous example, four additions
and partial products are used to multiply 4-bit numbers:
DO NOT COPY11
× 13
1011 multiplicand
× 1101 multiplier
0000 partial product
1011 shifted multiplicand
NOT COPY01011
0000↓
001011
1011↓↓
DO partial product
shifted multiplicand
partial product
shifted multiplicand
0110111 partial product
1011↓↓↓ shifted multiplicand
10001111 product
DO NOT COPYIn general, when we multiply an n-bit number by an m-bit number, the resulting
product requires at most n + m bits to express. The shift-and-add algorithm
requires m partial products and additions to obtain the result, but the first addi-
tion is trivial, since the first partial product is zero. Although the first partial
product has only n significant bits, after each addition step the partial product
DO NOT COPYgains one more significant bit, since each addition may produce a carry. At the
same time, each step yields one more partial product bit, starting with the right-
most and working toward the left, that does not change. The shift-and-add
algorithm can be performed by a digital circuit that includes a shift register, an
DO NOT COPYadder, and control logic, as shown in Section 8.7.2.
signed multiplication Multiplication of signed numbers can be accomplished using unsigned
multiplication and the usual grammar school rules: Perform an unsigned multi-
plication of the magnitudes and make the product positive if the operands had
the same sign, negative if they had different signs. This is very convenient in
DO NOT COPYsigned-magnitude systems, since the sign and magnitude are separate.
In the two’s-complement system, obtaining the magnitude of a negative
number and negating the unsigned product are nontrivial operations. This leads
us to seek a more efficient way of performing two’s-complement multiplication,
described next.
Conceptually, unsigned multiplication is accomplished by a sequence of
two’s-complement
DO NOT COPYmultiplication
unsigned additions of the shifted multiplicands; at each step, the shift of the mul-
tiplicand corresponds to the weight of the multiplier bit. The bits in a two’s-
complement number have the same weights as in an unsigned number, except
for the MSB, which has a negative weight (see Section 2.5.4). Thus, we can per-
DO NOT COPYform two’s-complement multiplication by a sequence of two’s-complement
additions of shifted multiplicands, except for the last step, in which the shifted
Copyright © 1999 by John F. Wakerly Copying Prohibited
Section *2.9 Binary Division 43
multiplicand corresponding to the MSB of the multiplier must be negated before
it is added to the partial product. Our previous example is repeated below, this
DO NOT COPYtime interpreting the multiplier and multiplicand as two’s-complement numbers:
−5 1011 multiplicand
DO× −3× NOT1101 multiplier COPY
00000 partial product
11011 shifted multiplicand
111011 partial product
00000↓ shifted multiplicand
1111011 partial product
DO NOT COPY11011↓↓ shifted multiplicand
11100111 partial product
00101↓↓↓ shifted and negated multiplicand
00001111 product
DO NOT COPYHandling the MSBs is a little tricky because we gain one significant bit at each
step and we are working with signed numbers. Therefore, before adding each
shifted multiplicand and k-bit partial product, we change them to k + 1 signifi-
cant bits by sign extension, as shown in color above. Each resulting sum has
DO NOT COPYk + 1 bits; any carry out of the MSB of the k + 1-bit sum is ignored.
*2.9 Binary Division
The simplest binary division algorithm is based on the shift-and-subtract method shift-and-subtract
that we learned in grammar school. Table 2-8 gives examples of this method for division
DO NOT COPYunsigned decimal and binary numbers. In both cases, we mentally compare the unsigned division
19 COPYTable 2-8
11 )217 Example of
long division.
DO NOT110
10011 quotient COPY
1011 )11011001 dividend COPY
10110000 shifted divisor
107 0101000 reduced dividend
99 0000000 shifted divisor
8 101000 reduced dividend
DO NOT000000 shifted divisor
101000 reduced dividend
NOT10110
10011
1011
DO shifted divisor
reduced dividend
shifted divisor
1000 remainder
Copyright © 1999 by John F. Wakerly Copying Prohibited
44 Chapter 2 Number Systems and Codes
reduced dividend with multiples of the divisor to determine which multiple of
the shifted divisor to subtract. In the decimal case, we first pick 11 as the greatest
DO NOT COPYmultiple of 11 less than 21, and then pick 99 as the greatest multiple less than
107. In the binary case, the choice is somewhat simpler, since the only two
choices are zero and the divisor itself.
Division methods for binary numbers are somewhat complementary to
binary multiplication methods. A typical division algorithm accepts an n+m-bit
DO NOT COPYdividend and an n-bit divisor, and produces an m-bit quotient and an n-bit
division overflow remainder. A division overflows if the divisor is zero or the quotient would take
more than m bits to express. In most computer division circuits, n = m.
Division of signed numbers can be accomplished using unsigned division
and the usual grammar school rules: Perform an unsigned division of the magni-
tudes and make the quotient positive if the operands had the same sign, negative
DO NOT COPYsigneddivision
if they had different signs. The remainder should be given the same sign as the
dividend. As in multiplication, there are special techniques for performing divi-
sion directly on two’s-complement numbers; these techniques are often
DO NOT COPYimplemented in computer division circuits (see References).
2.10 Binary Codes for Decimal Numbers
Even though binary numbers are the most appropriate for the internal computa-
DO NOT COPYtions of a digital system, most people still prefer to deal with decimal numbers.
As a result, the external interfaces of a digital system may read or display deci-
mal numbers, and some digital devices actually process decimal numbers
directly.
The human need to represent decimal numbers doesn’t change the basic
DO NOT COPYnature of digital electronic circuits—they still process signals that take on one of
only two states that we call 0 and 1. Therefore, a decimal number is represented
in a digital system by a string of bits, where different combinations of bit values
in the string represent different decimal numbers. For example, if we use a 4-bit
string to represent a decimal number, we might assign bit combination 0000 to
DO NOT COPYdecimal digit 0, 0001 to 1, 0010 to 2, and so on.
A set of n-bit strings in which different bit strings represent different num-
bers or other things is called a code. A particular combination of n bit-values is
called a code word. As we’ll see in the examples of decimal codes in this section,
there may or may not be an arithmetic relationship between the bit values in a
code word and the thing that it represents. Furthermore, a code that uses n-bit
code
DO NOT COPYcodeword
strings need not contain 2n valid code words.
At least four bits are needed to represent the ten decimal digits. There are
billions and billions of different ways to choose ten 4-bit code words, but some
DO NOT COPYof the more common decimal codes are listed in Table 2-9.
binary-coded decimal Perhaps the most “natural” decimal code is binary-coded decimal (BCD),
(BCD) which encodes the digits 0 through 9 by their 4-bit unsigned binary representa-
Copyright © 1999 by John F. Wakerly Copying Prohibited