Design and Implementation of a 4-bit ALU
The purpose of this lab is:
An Arithmetic and Logic Unit (ALU) is a combinational circuit that performs
logic and arithmetic micro-operations on a pair of n-bit operands (ex.
A[3:0] and B[3:0]). The operations performed by an ALU are controlled by
a set of function-select inputs. In this lab you will design a 4-bit
ALU with 3 function-select inputs: Mode M, Select S1 and S0 inputs. The
mode input M selects between a Logic (M=0) and Arithmetic (M=1) operation.
The functions performed by the ALU are specified in Table I.
|
||||
M = 0 Logic
|
||||
|
|
|
FUNCTION
|
OPERATION (bit wise)
|
|
|
|
AiBi
|
AND
|
|
|
|
Ai + Bi
|
OR
|
|
|
|
AiÅ
Bi
|
XOR
|
|
|
|
(AiÅ
Bi)’
|
XNOR
|
M = 1 Arithmetic
|
||||
|
|
|
FUNCTION
|
OPERATION
|
|
|
|
A
|
Transfer A
|
|
|
|
A + 1
|
Increment A by 1
|
|
|
|
A + B
|
Add A and B
|
|
|
|
A + B + 1
|
Increment the sum of A and B by 1
|
|
|
|
A + B'
|
A plus one's complement of B
|
|
|
|
A - B
|
Subtract B from A (i.e. B' + A + 1)
|
|
|
|
A' + B
|
B plus one's complement of A
|
|
|
|
B - A
|
B minus A (or A' + B + 1)
|
Figure 1: Block diagram of the 4-bit ALU.
When doing arithmetic, you need to decide how to represent negative
numbers. As is commonly done in digital systems, negative numbers are represented
in two’s complement. This has a number of advantages over the sign
and magnitude representation such as easy addition or subtraction of mixed
positive and negative numbers. Also, the number zero has a unique representation
in two’s complement. The two’s complement of a n-bit number N is
defined as,
2n - N = (2n - 1 - N) + 1
5 0 1 0 1 --> 1 0 1 0 (bit wise complement)
+ 1
1 0 1 1 (two’s complement)
Numbers represented in two’s complement lie within the range -(2n-1)
to +(2n-1 - 1). For a 4-bit number this means that
the number is in the range -8 to +7. There is a potential problem
we still need to be aware of when working with two's complement, i.e. over-
and underflow as is illustrated in the example below,
0 1 0 0 (=carry Ci)
+5 0 1 0 1
+4 + 0 1 0 0
+901 0 0 1 = -7!
also,
1 0 0 0 (=carry Ci)
-7 1 0 0 1
-2 + 1 1 1 0
-910 1 1 1 = +7!
Both calculations give the wrong results (-7 instead of +9 or +7
instead of -9) which is caused by the fact that the result +9 or -9 is
out of the allowable range for a 4-bit two’s complement number. Whenever
the result is larger than +7 or smaller than -8 there is an overflow or
underflow and the result of the addition or subtraction is wrong. Overflow
and underflow can be easily detected when the carry out of the most significant
stage (i.e. C4 ) is different from the carry out of the previous
stage (i.e. C3).
You can assume that the inputs A and B are in two’s complement
when they are presented to the input of the ALU.
b. Design strategies
When designing the ALU we will follow the principle "Divide and Conquer" in order to use a modular design that consists of smaller, more manageable blocks, some of which can be re-used. Instead of designing the 4-bit ALU as one circuit we will first design a one-bit ALU, also called a bit-slice. These bit-slices can then be put together to make a 4-bit ALU.
There are different ways to design a bit-slice of the ALU. One method consists of writing the truth table for the one-bit ALU. This table has 6 inputs (M, S1, S0, C0, Ai and Bi) and two outputs Fi and Ci+1. This can be done but may be tedious when it has to be done by hand.
An
alternative way is to split the ALU into two modules, one Logic and one
Arithmetic module. Designing each module separately will be easier than
designing a bit-slice as one unit. A possible block diagram of the ALU
is shown in Figure 2. It consists of three modules: 2:1 MUX, a Logic unit
and an Arithmetic unit.
Figure 2: Block diagram of a bit-slice ALU
c.Displaying the results. In order the easily see the output of the ALU you will display the results on the seven-segment displays and the LEDs (LD).
1.The result of the logic operation can be displayed on the LEDs (LD). Use also one of these LEDs to display the overflow flag V.
2.Since you are working with a 4-bit representation for 2's complement numbers, the maximum positive number is +7 and the most negative number is –8. Thus a single seven-segment display can be used to show the magnitude of the number. Use another seven-segment display for the “-“ sign (e.g. use segment “g”).
3.There is one complication when using more than one of the seven-segment displays on the Digilab board, as can be seens from the connections of the LED segments of the displays. You will notice that the four seven-segment displays share the same cathodes A, B, ..., G). This implies that one cannot directly connect the signals for the segments of the magnitude and sign to these terminals, since that would short the outputs of the gates which would damage the FPGA!. How could you solve this problem? Sketch a possible solution in your lab notebook. (Hint: You can alternate the signals applied to the cathodes between those of the Magnitude and Sign displays. If you do this faster than 30 times per second the eye will not notice the flickering. You will also need to alternate the anode signals). What type of circuit will be needed to accomplish this? You can make use of an on-chip clock, called OSC4 that provides clock signals of 8MHz, 500KHz, 590Hz and 15Hz.
4.Figure
3 shows a schematic of the overall system, consisting of the ALU, Decoder
and Switching circuit, and Displays on the Digilab board.
Figure 3: Overall system, including the 4-bit ALU and display units.
d. Tasks: Do the following tasks prior to coming to the lab. Write the answers to all questions in your lab notebook prior to coming to the lab. There is no on-line submission for the pre-lab. Ask the TA to sign pre-lab section in your lab notebook at the start of the lab session. You will also need to include answer to the pre-lab questions in your lab report.
c.
Write an HDL file
As part of the pre-lab, you can choose any of the three methods. Briefly justify why you chose a particular design method. Explain the design procedure and give the logic diagram or the HDL file. In case you use a MUX, you need also to give the schematic or the HDL file for the MUX.
Figure 4: Schematic block diagram of the arithmetic unit.
|
|
|
(A Logic) |
|
|
|
(B Logic) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.
|
|
|
|
|
|
|
|
|
|
|
|
|
5.Design the switching circuit that is needed to use the two seven-segment displays (see section c3 above).
As this project is more complicated than earlier projects, it will be important that you be very systematic during the design. Each macro should be simulated and errors corrected before you proceed. Failing to do so will make it difficult to debug the system.
Task 1:
When working on Xilinx, keep the project on the hard disk. At the
end of the lab you should copy your project back to a directory on your
account for future use. If the file is too large, you can easily
zip it by going to the Project Manager winder and selecting FILE-> ARCHIVE
PROJECT. This will zip the entire project with all the necessary project
libraries.
1. Course Title, Lab no, Lab title, your name and date
2. Section on the Pre-lab explaining the design of each block and giving the answers to each task.
3. Section on the lab experiment:
a.
Brief description of the goals.
b. Brief explanation of the design approach, the overall schematic
and of each macro.
c. Copy of the schematics and HDL source code (as a screen capture).
Label the schematics and comment on the source code.
d. Logic simulation (screen capture of the waveforms; label the outputs
to prove that the circuit functions properly).
e. Discussion of the results indicating that the circuit functions
properly.
4.
Conclusion and discussion.
The lab report is an important part of the laboratory. Write it carefully, be clear and well organized. It is the only way to convey that you did a great job in the lab. It is preferred (but not necessary) that you type the lab report.
1. M. Mano and C. Kime, "Logic and Computer Design Fundamentals", 2nd Edition, Prentice Hall, Upper Saddle River, NJ, 2001.2. R. Katz, "Contemporary Logic Design", Benjamin/Cummings Publ., Reading, MA, 1994.3. J. Wakerly, "Digital Design", 3rd Edition, Prentice Hall, Upper Saddle River, 2000.