2019EE616
2019EE616
2019EE616
(NAROWAL CAMPUS)
Computer Architecture
Lab manual
Submitted to:
Submitted by:
*Muhammad Owais (2019-EE-616)
Rashid Sharif (2019-EE-632)
Abdullah (2019-EE-613)
2. In second step, after downloading the file it is the time to run the license file.
Design all basic logic gates in Verilog and verify their truth table also.
Objective
Theory
Introduction
Truth Tables
Truth tables are used to help show the function of a logic gate. If you are unsure
about truth tables and need guidence on how go about drawning them for individual
gates or logic circuits then use the truth table section link.
Logic gates
Digital systems are said to be constructed by using logic gates. These gates are the
AND, OR, NOT, NAND, NOR, EXOR and EXNOR gates. The basic operations are
described below with the aid of truth tables.
AND gate
The AND gate is an electronic circuit that gives a high output (1) only if all its
inputs are high. A dot (.) is used to show the AND operation i.e. A.B. Bear in mind
that this dot is sometimes omitted i.e. AB
OR gate
The OR gate is an electronic circuit that gives a high output (1) if one
or more of its inputs are high. A plus (+) is used to show the OR
operation.
NOT gate
The NOT gate is an electronic circuit that produces an inverted version of the
input at its output. It is also known as an inverter. If the input variable is A,
the inverted output is known as NOT A. This is also shown as A', or A with
a bar over the top, as shown at the outputs. The diagrams below show two
ways that the NAND logic gate can be configured to produce a NOT gate. It
can also be done using NOR logic gates in the same way.
NAND gate
This is a NOT-AND gate which is equal to an AND gate followed by a
NOT gate. The outputs of all NAND gates are high if any of the inputs
are low. The symbol is an AND gate with a small circle on the output.
The small circle represents inversion.
NOR gate
EXOR gate
EXNOR gate
The 'Exclusive-NOR' gate circuit does the opposite to the EOR gate. It will give a
low output if either, but not both, of its two inputs are high. The symbol is an
EXOR gate with a small circle on the output. The small circle represents inversion.
Procedure
1. Define the module: Start by defining a Verilog module for the gate you want
to implement.
2. Write the gate equation: In Verilog, you can write the Boolean equation for
the gate using logic operators such as "&&" for AND and "||" for OR.
3. Simulate the gate: To test the functionality of the gate, you can create a test
bench module that instantiates the gate module and provides inputs.
4. Compile and run the simulation: Once you have written the gate module and
test bench module, you can compile and run the simulation using a Verilog
simulator such as eda playground or VCS. The simulator will generate
waveforms showing the input and output signals, allowing you to verify the
functionality of the gate.
5. Use the gate in a larger design: Once you have verified the functionality of
the gate, you can use it in a larger Verilog design by instantiating the gate
module and connecting its input and output ports to other modules.
1.1Objective
To understand how to use different tools involved in Verilog software and how to
implement adder and subtractor digital circuits using Verilog.
1.2Theory
1.2.1Adder
The digital logic circuit in electronics that is extensively used for addition of
numbers is known as adder. Adders are used in processors that calculates addresses
and similar activities.
1.2.1Half Adder
A half adder is a combinational logic circuit. It has two inputs ( one bit each ) termed
as A and B that generates sum ( S ) and carry ( C ) as output. Half adder is designed
using two logic gates that is AND gate and XOR gate. The output of sum is obtained
from XOR gate and the output of carry is obtained from AND gate.
1.2.3Full Adder
A full adder is a combinational logic circuit. It has three inputs ( one bit each ) termed
as A, B and Cin that generates sum ( S ) and carry ( C )as output. Full adder is
designed using two XOR gate, two AND gate, and one OR gate. The output of sum
is obtained from XOR gate and the output of carry is obtained from OR gate. Shown
in circuit below.
Fig 1:Truth table for Half adder
Sum = (A ⊕ B) ⊕ Cin
Carry = AB + BCin + ACin
1.2.4Half subtractor
Half subtractor is a combination circuit with two inputs and two outputs which
is difference and borrow. It produces the difference between the two binary bits at
the input and also produces an output (Borrow) to indicate if a 1 has been borrowed.
In the subtraction (A-B), A is called a Minuend bit and B is called as Subtrahend
bit.
1.3 Procedure
In first step, we have converted the boolean expressions into the Verilog form.
We have made function for individual circuits which are half adder, full adder,
half subtractor and full subtractor.
We have made code for test bench in which we have used wire for declaring
output and we have used reg to declare an input variable.
We have made two functions in test bench code, one for truth table and other
for showing the verification of truth table.
1.4 Output
Design the adder and subtractor using same logic using Verilog.
Objective
To understand the concept of parallel use of numbers for addition and subtraction.
Theory
Step 1: Determine the number of bits in the two binary numbers to be added. Let's
assume we are adding two 4-bit numbers A and B.
Step 2: Construct a truth table for the binary addition of the two numbers. The truth
table should have 3 input columns (A, B, and Carry-In) and 2 output columns (Sum
and Carry-Out).
Step 3: Implement the truth table using logic gates. The circuit should have 4 full
adders, one for each bit position. Connect the Carry-Out of each adder to the Carry-
In of the next adder.
Step 4: Connect the inputs (A and B) and the outputs (Sum and Carry-Out) to the
appropriate terminals of the circuit.
Step 5: Test the circuit using different combinations of input values and verify that
the outputs are correct.
Step 2: Take the two's complement of the number to be subtracted (B). This can be
done by inverting all the bits in B and then adding 1.
Step 3: Add the two numbers using the binary parallel adder circuit as described
above. However, use the two's complement of B as the second input to the adder.
Step 4: Ignore the Carry-Out of the last adder and interpret the Sum output as the
result of the subtraction.
Step 5: Test the circuit using different combinations of input values and verify that
the outputs are correct.
Note: In step 2, the two's complement is used to avoid having to build a separate
binary parallel subtractor circuit. The two's complement of a binary number is
equivalent to subtracting that number from 2^n, where n is the number of bits in the
number.
Software simulations
Experiment 5
Design different combination of multiplexer using Verilog and also write down
its test bench code.
Objective
Theory
Multiplexing is the generic term used to describe the operation of sending one or
more analogue or digital signals over a common transmission line at different times
or speeds and as such, the device we use to do just that is called the multiplexer.
The multiplexer, shortened to “MUX” or “MPX”, is a combinational logic circuit
designed to switch one of several input lines through to a single common output line
by the application of a control signal. Multiplexers operate like very fast acting
multiple position rotary switches connecting or controlling multiple input lines
called “channels” one at a time to the output.
Multiplexers, or MUX’s, can be either digital circuits made from high speed logic
gates used to switch digital or binary data or they can be analogue types using
transistors, MOSFET’s or relays to switch one of the voltage or current inputs
through to a single output.
The most basic type of multiplexer device is that of a one-way rotary switch as
shown.
The rotary switch, also called a wafer switch as each layer of the switch is known as
a wafer, is a mechanical device whose input is selected by rotating a shaft. In other
words, the rotary switch is a manual switch that you can use to select individual data
or signal lines simply by turning its inputs “ON” or “OFF”. So how can we select
each data input automatically using a digital device.
In digital electronics, multiplexers are also known as data selectors because they can
“select” each input line, are constructed from individual Analogue Switches encased
in a single IC package as opposed to the “mechanical” type selectors such as normal
conventional switches and relays.
They are used as one method of reducing the number of logic gates required in a
circuit design or when a single data line or data bus is required to carry two or more
different digital signals. For example, a single 8-channel multiplexer.
Generally, the selection of each input line in a multiplexer is controlled by an
additional set of inputs called control lines and according to the binary condition of
these control inputs, either “HIGH” or “LOW” the appropriate data input is
connected directly to the output. Normally, a multiplexer has an even number of
2n data input lines and a number of “control” inputs that correspond with the number
of data inputs.
Note that multiplexers are different in operation to Encoders. Encoders are able to
switch an n-bit input pattern to multiple output lines that represent the binary coded
(BCD) output equivalent of the active input.
We can build a simple 2-line to 1-line (2-to-1) multiplexer from basic logic NAND
gates as shown.
2-input Multiplexer Design
The input A of this simple 2-1 line multiplexer circuit constructed from standard
NAND gates acts to control which input ( I0 or I1 ) gets passed to the output at Q.
From the truth table above, we can see that when the data select input, A is LOW at
logic 0, input I1 passes its data through the NAND gate multiplexer circuit to the
output, while input I0 is blocked. When the data select A is HIGH at logic 1, the
reverse happens and now input I0 passes data to the output Q while input I1 is
blocked.
So by the application of either a logic “0” or a logic “1” at A we can select the
appropriate input, I0 or I1 with the circuit acting a bit like a single pole double throw
(SPDT) switch.
As we only have one control line, (A) then we can only switch 2 1 inputs and in this
simple example, the 2-input multiplexer connects one of two 1-bit sources to a
common output, producing a 2-to-1-line multiplexer. We can confirm this in the
following Boolean expression.
4-to-1 Channel Multiplexer
The Boolean expression for this 4-to-1 Multiplexer above with inputs A to D and
data select lines a, b is given as:
Q = abA + abB + abC + abD
In this example at any one instant in time only ONE of the four analogue switches
is closed, connecting only one of the input lines A to D to the single output at Q. As
to which switch is closed depends upon the addressing input code on lines “a” and
“b“.
So for this example to select input B to the output at Q, the binary input address
would need to be “a” = logic “1” and “b” = logic “0”. Thus we can show the selection
of the data through the multiplexer as a function of the data select bits as shown.
Multiplexer Input Line Selection
Adding more control address lines, (n) will allow the multiplexer to control more
inputs as it can switch 2n inputs but each control line configuration will connect only
ONE input to the output.
Then the implementation of the Boolean expression above using individual logic
gates would require the use of seven individual gates consisting
of AND, OR and NOT gates as shown.
Procedure
sel: an input bus for the select lines that determine which data input to
output.
2. Write the Verilog code for the multiplexer logic using an always block and a
case statement to select the output based on the select lines
3. Define the module using the module keyword and include the input and output
ports:
4. Instantiate the module in your Verilog design hierarchy, connecting the input
and output ports to signals or other modules.
6. Compile and simulate the design using a Verilog simulator like ModelSim or
VCS.
7. Debug the simulation by examining the waveforms to ensure the module
behaves as expected.
Software Simulation
4x1 Mux
Lab 6# Barrel Shifter
Objective
The objective of implementing a barrel shifter is to efficiently perform bitwise shift
operations on binary data by shifting the bits by a variable amount specified by a
shift amount input.
Theory
Barrel Shifter
A barrel shifter is a digital circuit that can shift a data word by a specified number
of bits without the use of any sequential logic, only pure combinational logic. Its has
a control input that specifies the number of bit positions that it shifts by. The Barrel
Shifter is similar to the Shift Register (Multi-bit), except that bits shifted of the
register are shifted back into the opposite end of the register. For example, in right
shift operations, the LSBs shifted out of the register are shifted into the MSBs.
Barrel shifters are applicable for digital signal processors and processors.
The diagram below shows a right-shifting barrel shifter for 32-bit words.
With more complex multiplexers and some extra circuitry for dealing with end fill
options, a barrel shifter can handle all of the standard bit shift instruction in a
processor's instruction set.
We start with the simplest and most direct way to program a right-shift barrel shifter
in Verilog HDL, using a case statement with all possible rotation combinations.
We will design a simple 8-bit barrel shifter that rotates and arbitrary number of bits
to the right. The circuit has an 8-bit data input, data, and a 3-bit control signal, amt,
which specifies the amount to be rotated.
The design uses a selected signal assignment statement to exhaustively list all the
combinations of the amt signal and the corresponding rotated results.
SystemVerilog implementation using a case statement
The test bench uses a for loop to iterate through all possibilities of the signal
indicating the amount to rotate.
In the image of the scope you can see how the values are shifted to the right amt bits,
towards the LSB bits and how the MSBs are filled with the overflowing bits, rotating
the information from left to right.
This way of designing the barrel shifter implies the use of a wide 3 to 8 multiplexer
Procedure
shift_amt: an input bus for the shift amount, which determines the
number of bits to shift the data.
shift_dir: an input bus for the shift direction, which determines whether
to perform a left shift or right shift.
3. Define the module using the module keyword and include the input and output
ports.
4. Instantiate the module in your Verilog design hierarchy, connecting the input
and output ports to signals or other modules.
6. Compile and simulate the design using a Verilog simulator like ModelSim or
VCS.
Software simulation
Lab#7 Register File
Objective
The objective of implementing a register file in Verilog is to provide a block of
storage elements that can store and retrieve binary data, typically used in a processor
design for storing CPU register values.
Theory
Register File
Introduction
Any value provided on output selection port (4-bit input Sel_o1) is used to select the content
of the corresponding register to provide as output on the 32-bit output port Op1 on the positive
edge of clock when the read enable switch is high.
Any value provided on output selection port (4-bit input Sel_o2) is used to select the content
of the corresponding register to provide as output on the 32-bit output port Op2 on the positive
edge of clock when the read enable switch is high.
Figure 1. Block Diagram of Register File Read operation
Any value provided on the input selection port (4-bit input Sel_i1) is used to select the
corresponding register into which the value from the 32-bit input port 1 is written to on
the positive edge of clock when the write enable switch is high.
2. Define the internal storage elements for the register file. This can be done
using a Verilog array, where each element of the array represents a single
register in the file. The number of registers in the file and the width of each
register can be specified using parameters.
3. Write the Verilog code for the register file logic using always blocks and
conditional statements to handle read and write operations.
4. Define the module using the module keyword and include the input and output
ports.
5. Instantiate the module in your Verilog design hierarchy, connecting the input
and output ports to signals or other modules.
6. Create a test bench to verify the functionality of the register file by generating
input stimuli, applying them to the input ports of the module, and monitoring
the output. Use $display or $monitor statements to print out the values of the
input and output signals.
7. Compile and simulate the design using a Verilog simulator like ModelSim or
VCS.
Software Simulation
Lab#8 ALU
Objective
The objective of implementing an Arithmetic Logic Unit (ALU) in Verilog is to
provide a hardware component that can perform arithmetic and logical operations
on binary data.
Theory
The inputs to an ALU are the data to be operated on, called operands, and a code
indicating the operation to be performed; the ALU's output is the result of the
performed operation. In many designs, the ALU also has status inputs or outputs, or
both, which convey information about a previous operation or the current operation,
respectively, between the ALU and external status registers.
Signals
An ALU has a variety of input and output nets, which are the electrical
conductors used to convey digital signals between the ALU and external circuitry.
When an ALU is operating, external circuits apply signals to the ALU inputs and, in
response, the ALU produces and conveys signals to external circuitry via its outputs.
Data
Opcode
The opcode input is a parallel bus that conveys to the ALU an operation selection
code, which is an enumerated value that specifies the desired arithmetic or logic
operation to be performed by the ALU. The opcode size (its bus width) determines
the maximum number of distinct operations the ALU can perform; for example, a
four-bit opcode can specify up to sixteen different ALU operations. Generally, an
ALU opcode is not the same as a machine language opcode, though in some cases it
may be directly encoded as a bit field within a machine language opcode.
Status
Outputs
The status outputs are various individual signals that convey supplemental
information about the result of the current ALU operation. General-purpose ALUs
commonly have status signals such as:
Carry-out, which conveys the carry resulting from an addition operation, the
borrow resulting from a subtraction operation, or the overflow bit resulting
from a binary shift operation.
Parity, which indicates whether an even or odd number of bits in Y are logic
one.
Upon completion of each ALU operation, the status output signals are usually stored
in external registers to make them available for future ALU operations (e.g., to
implement multiple-precision arithmetic) or for controlling conditional branching.
The collection of bit registers that store the status outputs are often treated as a single,
multi-bit register, which is referred to as the "status register" or "condition code
register".
Inputs
The status inputs allow additional information to be made available to the ALU when
performing an operation. Typically, this is a single "carry-in" bit that is the stored
carry-out from a previous ALU operation.
Circuit operation
The combinational logic circuitry of the 74181 integrated circuit, an early four-bit
ALU
An ALU is a combinational logic circuit, meaning that its outputs will change
asynchronously in response to input changes. In normal operation, stable signals are
applied to all of the ALU inputs and, when enough time (known as the "propagation
delay") has passed for the signals to propagate through the ALU circuitry, the result
of the ALU operation appears at the ALU outputs. The external circuitry connected
to the ALU is responsible for ensuring the stability of ALU input signals throughout
the operation, and for allowing sufficient time for the signals to propagate through
the ALU before sampling the ALU result.
Procedure
op: a control input that selects the operation to be performed by the ALU.
2. Define the internal signals and variables needed to perform the selected
operation(s). This may include intermediate results, flags, and control signals.
3. Write the Verilog code for the ALU logic using case or if statements to handle
different operations:
For logical operations (e.g., AND, OR, XOR), use Verilog's bitwise
operators (&, |, ^) to compute the result.
For shift operations (e.g., left shift, right shift), use Verilog's shift operators
(<<, >>) to compute the result.
4. Define the module using the module keyword and include the input and output
Instantiate the module in your Verilog design hierarchy, connecting the input and
output ports to signals or other modules.
5. Create a test bench to verify the functionality of the ALU by generating input
stimuli, applying them to the input ports of the module, and monitoring the
output. Use $display or $monitor statements to print out the values of the input
and output signals.
6. Compile and simulate the design using a Verilog simulator like ModelSim or
VCS.
7. Debug the simulation by examining the waveforms to ensure the module behaves
as expected.
Software Simulation