Programmable and Computational Devices Notes
Programmable and Computational Devices Notes
1. Adder circuits
So far we have assumed that the outputs of sensors that we are monitoring are simple TRUE/FALSE
values that can be represented in one bit, and the decisions we make about what to do next are
similarly TRUE/FALSE. It is more common for real world sensors to produce values that are
measurement amounts that lie in a number range, and are converted to binary numbers by an analogue
to digital converter. Processing these results to produce new control signals that we apply to influence
the external world will normally entail arithmetic operations. In this lecture we introduce circuits that
can perform arithmetic operations on binary data by looking at the circuit for a binary adder.
234
+167
401
1 1
The result of 4+7 (in the least significant digit position, A0 B0) gives a result bigger than ten. So we
write 1 in the answer’s least significant digit (Q0), and then carry 1 into the next position to the left
(Q1). Binary addition is similar, but using base 2. Here is an example, adding A=0001 to B=0011 to
get a result Q=0100:
0001 A 3 A 2 A1 A0
+0011 + B3 B2 B 1 B 0
0100 Q3 Q 2 Q 1 Q 0
1 1
The result of 1+1 in the least significant bit (A0 B0) gives a result of two, which is too big to write into
the least significant bit result Q0. So we write a 0 in Q0 and generate a carry into the next column to
the left Q1. In the Q1 column, the result of 0+1 (A1 B1) plus the incoming carry generates another carry
into the next column to the left, and so on.
We would like to build a circuit that accomplishes this binary function, i.e. a 4-bit adder circuit. The
circuit has 8 inputs (A3,A2,A1,A0,B3,B2,B1,B0) which is too many for simple design by Karnaugh map.
However, we can simplify the problem by spotting that the four columns are doing essentially the
same thing. We just need to design one circuit that works for a single column, then connect four
copies of this circuit together to obtain the required 4-bit adder. Let’s zoom in on one column of the
addition, for example:
1
0001 A 3 A 2 A1 A0
+0011 + B3 B2 B 1 B 0
0100 Q3 Q 2 Q 1 Q 0
1 1 COUT CIN
We can see that there are three inputs (a bit from the first number A, a bit from the second number B,
and a carry in CIN from the column to the right). There are two outputs (the result for that column Q,
and the carry out COUT to the column to the left). We now write down the truth table for the required
behaviour:
Inputs Outputs
A B CIN Q COUT
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1
A carry is generated when there are two or more ones in the inputs. The sum is 1 when there is an odd
number of ones at the inputs. We can use a Karnaugh map to extract the required circuit to perform
the single column of the addition:
𝐴. 𝐵
AB AB
CIN 00 01 11 10 CIN 00 01 11 10
0 0 1 0 1 0 0 0 1 0
1 1 0 1 0 1 0 1 1 1
𝐴. 𝐶
𝐵. 𝐶
𝑄 = 𝐴̅. 𝐵 . 𝐶 + 𝐴̅. 𝐵. 𝐶̅ + 𝐴. 𝐵. 𝐶 + 𝐴. 𝐵 . 𝐶̅ 𝐶 = 𝐴. 𝐵 + 𝐵. 𝐶 + 𝐴. 𝐶
These Boolean equations can readily be turned into logic gates to give us a single bit full adder circuit.
A single-bit adder is of little use. What we would really like to do is add together multiple-bit
numbers. The full-adder is designed specifically in order to simplify the construction of such parallel
adder circuits as shown below.
2
B3 A3 B2 A2 B1 A1 B0 A0
CIN=0
B A CIN B A CIN B A CIN B A CIN
Full Adder Full Adder Full Adder Full Adder
COUT SUM COUT SUM COUT SUM COUT SUM
Q3 Q2 Q1 Q0
This can be compared with our specific example of addition repeated below:
0001 A 3 A 2 A1 A0
+0011 + B3 B2 B 1 B 0
0100 Q3 Q 2 Q 1 Q 0
0 0 1 1
An n-bit pure binary number can represent any number in the range 0 to 2n-1. For example, if we use 4
bits to represent a number, then there are 16 different values that can be represented:
0001
+1111
0000
11 1 1 1
We can tell that overflow has occurred, because the final carry output of the final full adder has
generated a 1. A bit that is used to give us information about the status of a result is called a status
flag. We can use the final carry out as a status flag that warns us if overflow has occurred. When our
addition has completed, we check the status of the flag to see if the result is OK. If the overflow flag is
zero then all is well. If the overflow flag is one then we know that we have a problem.
In this case the overflow happened because we used a rather small number of bits (4) in our adder. In
real life situations we would be more likely to use 16, 24 or 32 bits where overflow would be unusual.
4
2. Signed Binary
So far everything we have said about binary has related only to positive numbers. Indeed, the
representation format that we have developed so far (called unsigned binary, or pure binary). cannot
deal with negative numbers. For many purposes we would never expect to encounter a negative
number, and unsigned binary is perfectly satisfactory. However, for many other purposes we do need
to be able to deal with negative numbers and we therefore need to develop a more sophisticated
approach to number representation.
The obvious way to apply this in digital systems is to use one of the bits to represent the sign, usually
a 0 for + and a 1 for -. So +6 would be represented as 0110 and –6 would be 1110. This is called the
signed magnitude representation. The values of the possible 4-bit binary numbers are shown below.
i.e. we interpret the leftmost bit as a sign bit which multiplies the binary number formed by the
remainder of the bits.
Although this method is straightforward and conceptually obvious, it is not the normal method used in
digital systems design for integer data. This is because it is hard to find cheap, efficient, fast circuits
that will carry out arithmetic operations (add, subtract, multiply, etc.) on numbers encoded using this
format. Imagine that we want to build a circuit that adds two numbers. We would need to check the
sign bits of the numbers. If they are the same then we add the numbers and attach the same sign bit to
the result. If the sign bits are different, then we need to find out which number is smaller and which is
larger and then subtract the smaller from the larger and give the result the sign bit of the larger
number. This complicated thought process results in a very complicated circuit.
5
2.2 The 2s complement approach to representing negative numbers
The standard approach to binary representation of integers that can take positive or negative values is
the 2s complement number system. This has the property that the most significant bit is interpreted as
having a negative weight -2n-1. Here are some examples:
The 4-bit number 1010 is interpreted as meaning 1×-8 + 0×4 + 1×2 + 0×1 = (-6)10
The 6-bit number 111010 is interpreted as meaning 1×-32 + 1×16 + 1×8 + 0×4 + 1×2 + 0×1 = (-6)10
The 6-bit number 001010 is interpreted as meaning 0×-32 + 0×16 + 1×8 + 0×4 + 1×2 + 0×1 = (10)10
Note that in all cases, the most significant bit has been interpreted as having a negative weight. This
representation is called the 2s complement. The name comes from the fact that if we want to find the
complement (i.e. negative) of a number when represented in n-bits, we subtract that number from 2n.
So for example if we want to know what –6 is when represented using 4-bits, we take the binary
number 6, i.e. 110, and subtract it from 24, i.e. 10000, using the normal rules of unsigned binary
arithmetic. 10000-110=1010. So our 4-bit representation of –6 is 1010.
There is a much easier way in practice to work out what the negative of a number is: we complement
each of the bits and then add 1. So for example if we want to know what –6 is represented as a 4-bit
binary number we do this:
Form the number +6 in 4-bits, i.e. 0110.
Complement each bit (i.e. replace each 0 by a 1 and each 1 by a 0) to get 1001.
Finally we add 1, which gives us 1010.
However, with negative numbers, if we want to convert a 4-bit number to a 6-bit number, we must put
1s in front. For example
-6 written as a 4-bit number is 1010, i.e. 1×-8 + 0×4 + 1×2 + 0×1 = (-6)10
-6 written as a 6-bit number is 111010, i.e. 1×-32 + 1×16 + 1×8 + 0×4 + 1×2 + 0×1 = (-6)10
We can generalise this by noting that any positive number will have an msb of 0. Any negative
number will have an msb of 1. So we can think of this process as taking the value of the msb and
copying it into the new bit positions that we are putting in front of the number. This process is called
sign extension. So, for example,
0110 sign extended to be an 8-bit number is 00000110
1010 sign extended to be an 8-bit number is 11111010
6
Number 2s complement representation
-8 1000
-7 1001
-6 1010
-5 1011
-4 1100
-3 1101
-2 1110
-1 1111
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
The numbers that can be represented lie in the range -24-1 = -8 to +24-1-1 = +7. Using more bits in the
numbers will give us a wider range of representable numbers.
Note that all negative numbers have an msb of 1 and all positive numbers have an msb of 0. The msb
of a 2s complement is therefore often referred to as the sign bit (but make sure that you understand
that this is not the same kind of sign bit as is used for signed-magnitude numbers).
Finally, in unsigned binary, we are sometimes sloppy about how many bits we are using to represent a
number, and don’t bother to talk about leading zeroes. So we might say that in binary the number 6 is
110. But strictly speaking if we are using 4-bits, it should be 0110 or 8-bits it should be 00000110.
When we use 2s complement, it is really important to be clear about how many bits we are using. The
denary number 6 expressed as 4-bit 2s complement number is 0110. We must not forget the leading
zero. If we just write 110, this would be interpreted as a 3-bit number. The 2s complement
interpretations of 3-bit numbers are shown below:
Number 3-bit 2s complement representation
-4 100
-3 101
-2 110
-1 111
0 000
1 001
2 010
3 011
The 3-bit number 110 represents the denary number –2, not +6.
But this is correct without any modification. An adder for unsigned binary works without modification
for 2s complement. This is why 2s complement is the normal representation format for integer
numbers in digital systems.
Unsigned binary: the result has overflowed if the COUT from the most significant bit is 1
Signed binary: the result has overflowed if
o 2 positive numbers add to give a negative result or
o 2 negative numbers add to give a positive result
Here are some examples for 4-bit numbers. Remember the range that can be represented in 4-bits is:
Unsigned binary: 0 to 15
2s complement -8 to +7
8
1001 Denary 9 1001 Denary -7
+1110 Denary 14 +1110 Denary -2
0111 Denary 7 0111 Denary 7
1 0 0 0 1 0 0 0
Overflow (COUT3=1) Overflow: 2 negative numbers
add to give a positive result
The first example shows a situation where the unsigned result of 15+2 cannot be represented as a 4-bit
number. However, the 2s complement interpretation of the bit patterns as -1+2 poses no problem.
The second example shows a computation (7+2) which is fine for unsigned binary, but for 2s
complement the result is too large to represent in 4-bits.
Finally, it is important to notice that in this lecture we have used unrealistically small numbers of bits
to represent our numbers (typically 4). This has been useful in illustrating the basic principles in an
accessible way, but may give you the wrong impression that overflows are common. In normal
situations we would use numbers at least 16-bits long, and overflows would be uncommon.
B A B A B A B A
3 3 2 2 1 1 0 0
Q Q Q Q
3 2 1 0
The device produces the output Q in the normal. Way. It also has two additional outputs, called status
flags.
The C flag (short for Carry) indicates that an overflow has occurred if the adder is being used to
process unsigned numbers.
The V flag (short for overflow; we don’t use the letter O because it looks too much like a zero)
indicates that an overflow has occurred if the circuit is being used for 2s complement addition.
The circuit itself doesn’t know (or care) whether the user is intending the numbers to be interpreted as
unsigned or signed. That’s a matter for the user to decide, but having made that decision the user must
check whichever flag (C or V) is appropriate to the interpretation that they are using.
9
3. Programmable Logic Devices
In this lecture we will look at programmable logic devices. These are devices that are implemented as
large arrays of logic gates, and can implement large and complex designs on a single chip. We will
also look briefly at the original technologies for fixed-function logic integrated circuits that have
nowadays been largely replaced by programmable logic.
Moulding compound
Lead frame
Bond wire
Chip
Pins
Early integrated circuit technologies could only fit a small number of logic gates (less than 100) onto a
single chip. The logic integrated circuits were mass produced by many different manufacturers
according to standardised specifications and functions. Each of the integrated circuits was given a
serial number (normally starting with the digits 74) that indicated its function. The SSI (small scale
integration) devices had fewer than 10 logic gates on a single chip. Examples are shown below for the
7400 quad-NAND gate and the 7402 quad-NOR gate:
The MSI (medium scale integration) devices had typically 10-100 logic gates on a single chip, used to
construct some useful sub-systems such 4-bit adders (74286), multiplexers (74150), ALUs (74382)
etc.
10
sales volume that is relatively small) the devices are mass produced without any defined function and
have some way to customise them to a particular function by programming.
Programmable logic devices (PLDs) are based on the insight that any combinational logic can be
expressed in sum-of-products ( AND of OR ) form. Furthermore, sequential functions (e.g. counters)
can be realised by taking standard flip-flops and customising the combinational logic that is used to
drive their inputs. A PLD directly implements a programmable sum-of-products function. It typically
consists of four stages:
An input stage (which also generates the complements of the inputs)
An AND plane, evaluating products of inputs
An OR plane, evaluating the sums of these products
An output stage, which may also include flip flops
An example of a very simple (and rather primitive) programmable device is shown below.
Inputs
A B
AND plane
A.B
A.B
Product
A.B
A.B
Fuses
OR plane
O0 O1 O2 O3
Outputs
Our simple device has two inputs A, B (a more realistic device might have 10-20 inputs). The inputs
and their complements are fed to a group of AND gates (called the AND plane of the device) to
produce product terms, 𝐴̅. 𝐵 , 𝐴̅. 𝐵, 𝐴. 𝐵, 𝐴. 𝐵. These product terms are connected to the inputs to OR
gates (the OR plane of the device), which form the outputs O1, O2, … The important thing to notice is
that the connections to the OR gates are programmable. In this example device, each of the lines
feeding the OR gates has a fuse. A fuse is a piece of metal that normally provides electrical
conduction, but will melt if an excessively large current is passed through it. Once the fuse has melted,
there will no longer be an electrical connection between its end points. We program the device by
figuring out which of the connections to the OR gates we want to keep and which we want to remove.
The programming stage consists of blowing the fuses on the lines for the OR gate connections that we
don’t want.
As an example, suppose we want to program the device to perform the function
𝑂 = 𝐴̅. 𝐵 + 𝐴. 𝐵
𝑂 = 𝐴. 𝐵
𝑂 =𝐴+𝐵
𝑂 = 𝐴̅ + 𝐵
11
The programming stage would blow the fuses to give the following:
A B
A.B
A.B
A.B
A.B
O0 O1 O2 O3
Note that for O2 and O3 we have had to use some logic identities to get the function we want into the
form offered by the product lines:
𝑂 = 𝐴 + 𝐵 = 𝐴. 𝐵 + 𝐴. 𝐵 + 𝐴̅. 𝐵
𝑂 = 𝐴̅ + 𝐵 = 𝐴̅. 𝐵 + 𝐴. 𝐵 + 𝐴̅. 𝐵
A ABCD
B X X
D
In the array notation, instead of showing multiple inputs to the gate, we draw a single line to the gate.
The input lines are drawn perpendicular to this line, and are selectively connected. If there is cross then
the lines are connected; if there is no cross the line is not connected. (So the example above has a line
C which is present in the array but not connected to our logic gate.)
12
A B
A.B
A.B
A.B
A.B
O0 O1 O2 O3
𝑂 = 𝐴̅. 𝐵 + 𝐴. 𝐵
𝑂 = 𝐴. 𝐵
𝑂 = 𝐴 + 𝐵 = 𝐴. 𝐵 + 𝐴. 𝐵 + 𝐴̅. 𝐵
𝑂 = 𝐴̅ + 𝐵 = 𝐴̅. 𝐵 + 𝐴. 𝐵 + 𝐴̅. 𝐵
The locations of the crosses are collectively known as the fuse map for programming the device to this
particular function.
OTP devices were succeeded by generations of devices that avoided fuses and used newer
technologies that allowed the programming of the device to be erased. The first generation could only
be erased by exposing the device to ultra-violet light. Modern generations are electrically erasable:
they can be switched to a programming mode and reprogrammed just using electrical signals applied to
their pins.
1
The programming data is normally referred to as a fuse-map for historic reasons, even though modern PLDs don’t
actually use fuses
13
4 Memory devices
In the next few lectures, we will look at computer and microcontroller systems. These store data items
in a memory, and perform computations on these data items to produce new data items. The sequence
of computations that is performed is a computer program, which is also a set of binary words stored in
a memory. In this lecture we will look at the commonest types of memory device: RAM and ROM.
We now store each of these items in the storage locations inside the memory device:
8 bits
Item 0 = 72 (01001000)
Item 1 = 49 (00110001) a2
Item 2 = 67 (01000011)
a1 Address
Item 3 = 53 (00110101)
a0
Item 4 = 43 (00101011)
Item 5 = 57 (00111001)
Item 6 = 61 (00111101)
Item 7 = 37 (00100101)
d7 d6 d5 d4 d3 d2 d1 d0
Data
To read out the data for a particular student, we apply the number that identifies that student at the
address input (a2a1a0). This will cause the data output (d7..d0) to take on the value corresponding to
that student. So for example, if we set a2a1a0 equal to 101 this will cause data output to take on the
value 00111001, which is the mark for student number 5.
w1
w2
w3
w4
w5
w6
w7
a2 a1 a0
This decodes a 3-bit address, but the generalisation to larger addresses is straightforward. For any
possible combinations of the address inputs ( a2..a0), one of the word lines (w7..w0) will go high, and
all others will go low. These word lines are then fed across to a bank of cells, like this:
Select
Address
decoder
a2 a1 a0 d7 d6 d5 d4 d3 d2 d1 d0
Address input Data output
The contents of the cells determine what type of memory we are dealing with. There are two main
types of memory
RAM (Random Access Memory): new data items can be stored into the memory; old data items
can be read out of the memory
ROM (Read only memory): the data contained is fixed at the time of programming and cannot be
changed until the device is reprogrammed. We can read data items that are stored in the memory
but can’t write new ones in.
15
Address
decoder
a2 a1 a0
d7 d6 d5 d4 d3 d2 d1 d0
Address
decoder
a2 a1 a0 Read enable
d7 d6 d5 d4 d3 d2 d1 d0 Data output
The address decoder will select one of the words. At each of the cross-over locations in the memory
array is a latch that can store a 1 or a 0. When we use the RAM, we can read data from a row in the
array (so we set the “Read enable” signal high to enable the data from the selected word to be carried
onto the output data lines). Alternatively we can write new data into the memory array. This is done
by setting “Write enable” high, which means that data input will be connected to the selected row and
the old data at that row will be overwritten with the new data.
16
With ROM, the memorised data is represented by the presence of absence of a connection between the
wires of the product lines and the wires that feed the OR plane. When we turn the circuit off and then
back on again, the connections are still exactly in the state we left them. So the contents of the
memory are still there. ROM is non-volatile memory.
4.7 Summary
Modern electronic design is usually carried out on programmable logic devices, which can
accommodate designs containing many thousands of logic gates. Designs are usually entered into
CAD tools using some kind of language based on Boolean algebra, and are then automatically
translated to the data needed to program the chip. Modern devices are reprogrammable, so that faults
in a design can be fixed when they come to light.
17
5. Microcontrollers Part 1: The Arithmetic Logic Unit (ALU)
Digital electronics is often used for control of an engineering system. We use sensors to measure the
state of the system, which are converted to a digital form by an analogue-to-digital converter (ADC).
We then use digital electronics to create a controller that devises new input values to drive the
physical system toward the desired goal. These new input values are passed through a digital-to-
analogue converter (DAC) to produce new signals to apply to actuators that drive the physical
parameter that we are trying to control.
Sensor ADC
Physical Control
system system
Actuator DAC
The controller often requires implementation of algorithms that contain a lot of arithmetic (for
example, we might solve a differential equation on a succession of samples of the physical system).
One could build a lot of digital circuits to implement all of the different arithmetic operations that are
required and then chain these circuits together. However, that would be unusual. Such problems are
usually handled using a microcontroller, a small computer-on-a-chip which can apply different types
of operations to our data under the control of programming bits. In the final two lectures we look at
the basic ideas of microcontrollers.
In earlier lectures we noted that there are two different types of number system that are widely used:
the unsigned pure binary system (which can only represent positive numbers) and the 2s complement
system (which can represent both positive and negative numbers). We need to make a choice of which
to use, and then ensure that this choice is consistently applied throughout our system. In practice this
means using the appropriate type of ADC and DAC (there are versions that use unsigned and there are
versions that use 2s complement) and then to ensure that our controller applies the operations
appropriate to our choice. The 2s complement system is often the more useful choice in practice, so in
this lecture we will use 2s complement throughout.
Sequencing Instruction
and control stream
The input/output (I/O) unit connects to some of the pins on the chip which are used to send and
receive data to and from the outside world. The data entering at the I/O unit contributes to a larger
pool of data which contains the input values and results of all of the computations that the
microcontroller is working on. A central processing unit (CPU) takes the data items a few at a time,
performs some useful operation on them, and then writes the results back into the data pool. A
sequencing and control unit manages the movement of data around the system.
18
5.1 The Arithmetic Logic Unit (ALU)
We start by looking at the arithmetic logic unit, a key component of the central processing unit (CPU)
of a microcontroller or a microprocessor. Suppose we have two data items A and B and both consist
of four bits (labelled A3A2A1A0 and B3B2B1B0). An ALU has the following appearance.
A3
A2 4
A1 A A
A0
F3 4
F2
F F1 F
B3 F0
B2 B 4
B1 S B S
B0
3
S2S1S0
Our diagrams become very cluttered if we show each of the individual wires A3A2A1A0 that
collectively make up our input A, so the notation on the right is usually used. The slash indicates that
a group of wires are acting together to carry a useful value; the number 4 indicates that in this case
there are 4 wires in the group.
The output F is some function of the inputs A and B. Many different functions can be performed on
the input operands. Which function should be performed (at any given point in time) is selected by the
input S. This input is called the opcode. In this example ALU the opcode S has 3 bits. There are 23=8
possible different 3-bit values, so this example ALU has 8 possible different functions that it can
apply to the input operands. The operation performed by the ALU depends on the states of the three
opcode input pins S0 to S2 as summarised below:
S2 S1 S0 Operation
0 0 0 CLEAR
0 0 1 B MINUS A
0 1 0 A MINUS B
0 1 1 A PLUS B
1 0 0 A XOR B
1 0 1 A AND B
1 1 0 A OR B
1 1 1 SET
When (for example) S=110, each of the bits of A, B are ORed together (F0=A0 OR B0, F1=A1 OR B1,
etc.). This operation is called a bitwise OR. When S=101 each of the bits of A, B are bitwise ANDed
together (F0=A0 AND B0, F1=A1 AND B1, etc.).
19
Suppose (for example) that we have the data items A, B shown below. The value that F takes depends
on S and is shown in the table
A = 0101
B = 0001
We spell out the word PLUS in full, because we have already used the + sign to refer to the logical OR
operation in our Boolean algebra.
So
A PLUS B = 0110 = 0×-8 + 1×4 + 1×2 + 0×1 = (6)10
A MINUS B = 0100 = 0×-8 + 1×4 + 0×2 + 0×1 = (4)10
B MINUS B = 1100 = 1×-8 + 1×4 + 0×2 + 0×1 = (-4)10
Remember that in 2s complement, the most significant bit (msb) has negative weight. For a 4-bit
binary number, the msb is the multiplier for 23=8.
Sometimes it is useful to generate a value of 0000 or 1111 that we can write into the pool of data.
Hence the provision of the CLEAR operation when S=000 and SET when S=111.
20
6. ALU Construction
6.1 Creating the logical part of the ALU
Now we will look at how this ALU is constructed, starting with the logic operations (AND, OR, XOR).
d0
d1 4-to-1 Output
d2 Multiplexer
d3
s1 s0
This device picks out one its inputs (d3, d2, d1, d0) and steers it through to the output. Which input is
chosen is determined by the bits s1s0. Selecting s1s0 =00 steers d0 to the output, s1s0=01 steers d1 to the
output and so on.
A0
B0
A0
B0 Multiplexer Output0
A0
B0
1
s1 s0
We also use a similar arrangement to deal with A1B1, A2B2 and A3B3.
Finally, we use the bit S2 to select between the output of this logical unit and the output of the
arithmetic unit (which we will look at later):
21
Output from
arithmetic unit
A0
B0 2-to-1 F0
A0 Multiplexer
B0 4-to-1
A0 Multiplexer
B0 s2
1
s1 s0
(3)10 = (0011)2
If we want to form -3, we flip all of the bits to get 1100. We then add 1:
1100
+ 0001
1101
So (-3)10 is (1101)2.
We also noted in lecture 11 that in 2s complement, the most-significant-bit has negative weight. So
The 4-bit number 0011 is interpreted as meaning 0×-8 + 0×4 + 1×2 + 1×1 = (3)10
The 4-bit number 1101 is interpreted as meaning 1×-8 + 1×4 + 0×2 + 1×1 = (-3)10
This approach also works for negating a negative number back to a positive number. So to negate -3,
we take its binary representation:
(-3)10 is (1101)
Flip all the bits: (0010)2
Add 1 gives (0011)2
So (+3)10 is (0011)2.
22
B3 A3 B2 A2 B1 A1 B0 A0
CIN=0
B A CIN B A CIN B A CIN B A CIN
Full Adder Full Adder Full Adder Full Adder
COUT SUM COUT SUM COUT SUM COUT SUM
Q3 Q2 Q1 Q0
Suppose now that we want to subtract, i.e. to devise a circuit that will compute A-B. As a specific
example, consider the following numbers
A = 0100 (this is the binary representation of the denary number 4)
B = 0010 (this is the binary representation of the denary number 2)
We can perform subtraction by negating B and then just using a standard adder circuit to add the result
to A. To negate a number, we take its 2s complement by reversing all of its bits and adding 1.
Flip all bits of B gives 1101, then add 1 gives 1110.
We now add 0100 to 1110 using a standard adder:
0100
+1110
0010
11 1 0 0
(The final carry out is unimportant for 2s complement numbers, for reasons explained in lecture 2.)
Our result is +2, as expected. So to turn our adder into a subtractor, we need to add hardware to flip all
bits of B and add 1 to the result. This is accomplished by the following subtractor circuit:
B3 A3 B2 A2 B1 A1 B0 A0
CIN=1
B A CIN B A CIN B A CIN B A CIN
Full Adder Full Adder Full Adder Full Adder
COUT SUM COUT SUM COUT SUM COUT SUM
Q3 Q2 Q1 Q0
The inverters on the B inputs flip all of the bits in the number B. Setting CIN=1 at the start of the carry
chain accomplished the addition of 1 that is required to complete the 2s complement of the number B.
23
XOR
A B C
0 0 0 A FLIP?
0 1 1 C Y
B X
1 0 1
1 1 0
We take an XOR gate, treat one of its inputs as the data input X and the other as the control input
FLIP? (1=yes, 0=no). Now if FLIP=1 then Y=X; if flip=0 then Y=X.
B3 A3 B2 A2 B1 A1 B0 A0
ADD/SUB
Q3 Q2 Q1 Q0
When the control input is low (the ‘add’ state), the carry input is low and the output of each XOR gate
will be the same as the corresponding B input (since X 0 X ). So the result is addition. When the
control input is high, however, the carry input is high and the output of each XOR gate is the
complement of the corresponding B input (𝑋 ⊕ 1 = 𝑋). As a result the circuit performs subtraction.
4 Cin
A
4
F
Cout
4
B S
3
In this example, both the C and the V flag would be set to 1. It is then up to us to design our system in
a way that can respond appropriately to this error notification.
6.8 Summary
We have looked at how it can be useful to have circuits that can accomplish a range of different
arithmetic or logical operations on their inputs A, B where the particular operation that should be
performed is indicated by an opcode input S. Such devices are called arithmetic logic units (ALUs)
and are the basis of microcontroller and microprocessor systems.
25
7 Microcontrollers Part 2: Overall organisation
A microcontroller is essentially a computer on a single chip, and it acquires its particular function
through a program, which is a sequence of binary instruction words. These binary words are stored in
a memory and are fetched one at a time and cause a particular operation to be carried out on particular
data items. Using a microcontroller is beneficial because to tackle different engineering systems with
different requirements, we don’t need to build a new piece of hardware each time. Instead we use a
standard piece of microcontroller hardware and create a program to solve the particular engineering
problem that we are working on.
In the previous lecture, we looked at the ALU which can perform a variety of different functions on
data inputs, the function being selected by an opcode. In this lecture we will look at how the data that
is operated on is stored and moved around the device.
Suppose we have system where 3 devices are collecting data and at intervals they will send their data
to device 4 for processing. Suppose we just try to connect them together like this:
Device 1
Device 2
Device 4
Device 3
Shared
wire
The outputs of the 3 devices would interfere with each other when some are trying to drive a 0 (a low
voltage signal) and others is trying to drive a 1 (a high voltage signal). The resulting logic value on the
output would be indeterminate (probably at an intermediate voltage level that doesn’t correspond to a
1 or a 0).
The way that we solve this is by using a special device that has an additional possible output
behaviour other than driving a 0 or driving a 1. A device with a tri-state output has the ability to
electrically disconnect itself from its output terminal. When this happens the output will not drive a 0
26
or drive a 1. This is called the high impedance state (Hi-Z, sometimes just abbreviated to Z). A tri-
state buffer is shown below:
OE OE A B
0 0 Hi-Z
A B 0 1 Hi-Z
1 0 0
1 1 1
OE is an additional control input whose function is Output Enable. When OE=1, the B output just
follows the A input. However, when OE=0 the device is electrically disconnected from the output B:
B goes into the Hi-Z state.
Now, if we imagine that we have several devices and we want them to share the same output wire, we
connect them as follows:
OE1
Device 1
OE2
Device 2
OE3 Device 4
Device 3
Shared
wire
When OE1=1, OE2=0, OE3=0 device 1 can send its data to device 4 without any interference from
device 2 or device 3 (both of whose outputs are in the Hi-Z state, and thus are effectively disconnected
from the wire). Similarly when OE1=0, OE2=1, OE3=0 device 2 can send its data to device 4.
This arrangement will work well as long as our design ensures that we can never have two or more of
the OE control signals high at the same time.
7.1.2 Busses
A bus is a set of shared wires used for communication by electronic devices, where any given device
will only need to transmit intermittently. At any given time, only one device can be driving data onto
the bus. The data can be received by just one device, or by many. Normally each wire in the bus will
carry one bit of a data word or a control word.
27
Here is our example of 3 devices intermittently sending data to a fourth assuming that the data sent
consists of 8-bit binary numbers:
OE1
Device 1
8
OE2
Device 2 8 8
OE3 Device 4
Device 3 8
8-bit Bus
The slash with the number 8 next to it is the standard way of showing that the connection in fact
comprises of 8 wires carrying 1-bit signals that act as different parts of the data byte.
The control unit of the microcontroller decides for each clock cycle which devices should be able to
connect with the bus. This is done by it driving the appropriate values for the signals OE1, OE2, …
7.3 Registers
ALUs operate on data under the control of the opcode input S. We need somewhere to store the values
that the ALU operates on, and also somewhere to store the results produced. The device that is used is
a register.
A digital electronic device capable of storing one-bit of information has been introduced in the form
of the D-type flip-flop. Once every clock cycle, the D input is read by the device and the Q output is
set to be in the same state (low or high). This ‘latching’ occurs on either the rising or the falling edge
of the clock pulse, depending on the device. Importantly, the output then remains in the same state
until the next clock cycle, regardless of further changes in the input. In other words, the circuit
remembers the state of the input signal.
CLK
Q
D
Q
D
CLK
Q
To use this circuit to remember a whole byte of bits, it simply needs to be duplicated so that instead of
using a single flip-flop with one input and output, we use eight flip-flops all governed by the same
clock signal:
28
CLK
CLK
D7 D Q Q7
CLK
D6 D Q CLK
Q6
CLK
D5 D Q Q5
CLK
8 CLK 8
D4 D Q Q4 D0-7 D0-7 Q0-7 Q0-7
CLK
D3 D Q Q3
CLK
D2 D Q Q2
CLK
D1 D Q Q1
CLK
D0 D Q Q0
This device is an 8-bit register. When the clock edge occurs, the value of the 8-bit data item D0-7 is
read into the 8-bit memory Q0-7 and held constant for the remainder of the clock cycle.
Input/Output
Register
D0-7 Q0-7 8
Bus
This requires an additional control input that sets whether the register is to input (read information
from the bus) or output (write information to the bus). As usual, the value of this signal is decided by
the sequencing and control unit of the microcontroller.
29
7.5 The organisation of a simple microcontroller
A conceptually simple device can be built using three busses:
reg3
reg2
reg1
reg0
Control Machine
unit Instruction
A
Determines value
of S and which
F registers connect
to which bus
BS
Two of the busses connect to the inputs of the ALU and the third connects to its output. A bank of
registers (reg0, reg1, …) is available. (In this example we have shown 4 registers, but most
microcontrollers would have a larger number.) These registers can be connected or disconnected from
the busses, and can act as input or output to any of the busses. The control unit decides what should
happen on each clock cycle, and produces the appropriate control bits to make this happen.
Suppose, for example, that on one cycle we wish to add the values stored in reg1 and reg2 and put the
result into reg3. The control unit will generate the appropriate value for the opcode S (S=011 means
add) and apply this value to the S input of the ALU. It will also generate the appropriate control bits to
connect the output of reg1 to the A-bus, the output of reg2 to the B-bus and the input of reg3 to the F-
bus. The ALU will then perform the addition of the values in reg1 and reg2 and write the result back
to reg3.
The flow of information along the bus (reading, writing, arithmetic and logical operations) is
controlled by a central control unit. These control signals are generated by reading a list of coded
instructions (or a program) stored in a special block of program memory.
30
System busses
PC ALU … I/O
7.6.2 Registers
Several general-purpose registers for temporarily storing data are available and will
connect/disconnect to/from the system busses as directed by the control unit. Also, several special
purpose registers can be accessed through the system bus. The status register, SR, is a second output
from the ALU. This register is used to store the status flags (e.g. the C flag and the V flag) that result
from any arithmetic operations. Some instructions are conditional on the state of one or more flag in
SR so the sequence of instructions can be controlled.
The program counter, PC, is another special purpose register. It holds the address, in program
memory, of the next instruction to be executed. Normally this register is incremented automatically
after each clock cycle. It is possible, however, to access PC via the system bus and therefore jump to
any section of the program, possibly conditionally on the state of SR. In this way, loops and ‘if-then’
operations can be realised.
The instruction register is a dedicated temporary storage register that simply stores the current
instruction to be executed. It is written to from the program memory and read by the control unit. The
control unit interprets the coded instructions and sends appropriate signals (read/write) to the registers
connected to the system bus and also controls the operation of the ALU by means of the opcode.
A final set of special purpose registers are the input/output registers, I/O. These behave exactly like
the general purpose registers except that the logic levels they contain can be read from or written to
using external pins on the chip housing the device. In some ways, these are the most important
registers as they provide the only way that the processor can communicate with the outside world and
actually perform a useful task.
7.6.3 Programming
One of the distinctive features of a computer is its ability to follow a pre-set list of instructions, or a
program. In the microcontroller shown above, these instructions are stored in the form of a list of
coded binary numbers in a special block of program memory. The position of the next instruction (or
address) is held in the program counter register, PC, and the instructions, when read from memory, are
held in the instruction register.
31
7.7 Embedded computing
Consumers nowadays are all familiar with computer-type devices, whether they be desktop PCs,
tablets or the subsystem of smartphones that can run apps. Embedded computers are computing
devices that are hidden inside other products and are not perceived by the user to be “a computer”.
Examples include the engine management or antilock braking systems of a car, the heating, lighting
and ventilation controller of a smart building, the controller of a microwave oven, a camera or a
dishwasher, etc.
The block diagram shown in section 7.6.1 could apply to a microprocessor or a microcontroller. The
main differences between microprocessors and microcontrollers are:
Microprocessors are normally powerful, but expensive. They are used mainly in desktop
computers, laptops, tablets and smartphones. They are normally designed to be one chip within a
computer larger system that is spread across many chips. Other chips would include external
RAM which holds both program instructions and also data and controllers for external
input/output devices. They normally also require some kind of external file storage device, such as
a hard disk or a solid state drive. Microprocessors are normally designed to be used within a
system that the user perceives to be a computer, and as such they support a wide range of different
programs and expect the user to frequently change the program that is running in RAM. They are
usually optimized for high performance, which means a very high clock speed. However, high
clock speed gives rise to high power consumption, which means that lifetime for battery operated
equipment can be short.
Microcontrollers are purpose-designed to operate in embedded systems, and normally place the
whole of the computer system onto a single chip. The expectation is that they will only run one
single program (which is normally stored in an internal ROM inside the chip); this program will
start running as soon as the chip is powered up and will keep running until it is powered down.
Putting all the required resources onto single chip means that the amount of RAM and ROM
available is small. They don’t need high performance, but do need low power consumption, so the
clock speeds tend to be low (10-80 MHz would be typical). Microcontrollers would normally have
some general purpose input-output pins (GPIO) that can be connected directly to sensor and
actuator devices. Microcontrollers are often aimed at systems with very tight budget constraints,
and low cost devices are readily available that have very small word lengths and very limited
resources on-chip. This not only reduces cost, but also minimises the power drain on batteries.
That having been said, there also very powerful microcontrollers available for more challenging
applications, but these cost more.
Other special functions are helper circuits that accomplish things that could be done by the processor
through software, but are done more efficiently in special hardware thus freeing up the processor to
focus on other tasks. An example of this would be the generation of pulse-width modulated (PWM)
waveforms. PWM turns an output alternately on then off in very rapid succession in order to achieve
graduated control of an external device, such as a motor or a light. The external device that receives
this waveform responds to the time averaged value of the waveform. By controlling duty cycle (the
ratio of the on-time to the cycle time) we can continuously vary the average voltage that the controlled
device experiences. In lab 7 we looked at the control of the brightness of an LED using PWM on an
Arduino board with a supply voltage of 5V:
32
Duty cycle 50% Average voltage = 2.5 V
Time
This PWM output could, of course, be accomplished by making the program software issue
instructions at regular intervals to change the state of the output. However, that keeps the processor
rather busy and we’d like to keep the processor free to concentrate on more important things. A
hardware PWM generator looks like this:
From processor
Comparison Compare
Counter
circuit value register
Output pin
The microcontroller is equipped with a counter circuit that just goes through a count sequence
0,1,2,…,255 on successive clock cycles, and then wraps back round to 0 to start all over again. To
start up a PWM waveform, the processor just spends one instruction cycle to write one number into a
register (the compare value register). The comparison circuit outputs 1 when the counter value is
lower than the compare register value, and a 0 otherwise. So, for example, to get a 25% duty cycle, we
write ¼ × 255 ≈ 63 into the register by issuing the Arduino instruction analogWrite(pin, 63).
Counter value
Compare value = 63
Output pin
7.9 Summary
Microcontrollers and microprocessors are given their function through a program. This is a series of
binary instruction words that are processed sequentially on successive clock cycles. The instructions
are decoded into control inputs for the various resources on the chip to determine what data movement
and what arithmetic or logical operations should be performed on that clock cycle. Microcontrollers
are computing devices that are targeted specifically at embedded computers
33
Index
1. Adder circuits 1
1.2 Binary addition 1
1.3 Adder overflow 3
2. Signed Binary 5
2.1 The signed-magnitude representation 5
2.2 The 2s complement approach to representing negative numbers 6
2.3 Sign extension of 2s complement binary numbers 6
2.4 Using 2s complement numbers 6
2.6 Addition of 2s complement numbers 7
2.7 Overflow in 2s complement arithmetic 8
2.8 Adder circuit with overflow detection 9
3. Programmable Logic Devices 10
3.1 SSI and MSI logic devices 10
3.2 Programmable Logic Devices 10
3.3 Array notation 12
3.4 Programming technologies 13
3.5 Design tools 13
4 Memory devices 14
4.1 Memory addressing 14
4.2 Memory device internal organisation 14
4.3 Read only memory (ROM) 15
4.4 Random Access Memory (RAM) 16
4.5 Volatile and non-volatile memory 16
4.6 Flash memory 17
4.7 Summary 17
5. Microcontrollers Part 1: The Arithmetic Logic Unit (ALU) 18
5.1 The Arithmetic Logic Unit (ALU) 19
6. ALU Construction 21
6.1 Creating the logical part of the ALU 21
6.2 Creating the arithmetic unit 22
6.2.1 Negating 2s complement binary numbers 22
6.2.2 A subtractor circuit 22
6.2.3 A programmable bit-flipper 23
6.2.4 An adder/subtractor circuit 24
6.3 Extending the bit width of an ALU 24
6.4 Status flags 25
6.7 More complex ALUs 25
6.8 Summary 25
7 Microcontrollers Part 2: Overall organisation 26
7.1 Communicating within the device 26
7.1.1 Tri-state buffers 26
7.1.2 Busses 27
7.3 Registers 28
7.4 Connecting a register to a bus 29
7.5 The organisation of a simple microcontroller 30
7.5.1 Alternative microcontroller organisations 30
7.6 Microprocessors and Microcontrollers 30
7.6.1 Block Diagram 30
7.6.2 Registers 31
34
7.6.3 Programming 31
7.7 Embedded computing 32
7.8 Microcontroller special function hardware units 32
7.9 Summary 33
35