Questions and Solutions - Module 3
Questions and Solutions - Module 3
Questions
2. Write the truth table for a 4 input XOR function. Implement it using
least possible number of 4:1, 2:1 multiplexers and NOT gates.
5. Write the truth table for full adder and implement it using i) Only 2:1
multiplexers and NOT gates ii) 2:4 decoders and other necessary
gates.
13. Design and write the Verilog code for a 3 to 8 decoder using 2
to 4 decoders and other necessary gates. Use case statement to
implement 2 to 4 decoder.
14. Write the truth table for 2421 to 8421 code converter. Design
and write the Verilog code for this converter using 4:1 multiplexer(s)
and other necessary gates. Write behavioral Verilog code to
implement the same.
20. Write the hierarchical code for 4-to-16 binary decoder using
FOR GENERATE and IF GENERATE statements.
Solutions
To implement the Boolean function f(a,b,c,d,e)=bd′
+cd+a′cf(a, b, c, d, e) = bd' + cd + a'c using
Shannon's expansion and an 8:1 multiplexer, we
need to follow the steps below.
1. Shannon’s Expansion
Shannon’s Expansion allows us to express a
Boolean function in terms of a selected variable.
We can apply Shannon's expansion to the function
with a,b,ca, b, c as the select lines of the
multiplexer.
We want to implement the function using a 3-
variable multiplexer (since a,b,ca, b, c will be
the select lines of the 8:1 multiplexer), so the
expansion will be based on the values of the
selected variable aa.
Shannon’s Expansion for the function
f(a,b,c,d,e)f(a, b, c, d, e) with aa as the selected
variable is:
f(a,b,c,d,e)=a′⋅f(0,b,c,d,e)+a⋅f(1,b,c,d,e)f(a, b, c, d,
e) = a' \cdot f(0, b, c, d, e) + a \cdot f(1, b, c, d, e)
This means we need to compute two functions:
f(0,b,c,d,e)f(0, b, c, d, e) when a=0a = 0
f(1,b,c,d,e)f(1, b, c, d, e) when a=1a = 1
Let’s now break down the function f(a,b,c,d,e)f(a,
b, c, d, e).
2. Simplifying the Given Function
The original function is:
f(a,b,c,d,e)=bd′+cd+a′cf(a, b, c, d, e) = bd' + cd +
a'c
We need to compute f(0,b,c,d,e)f(0, b, c, d, e) and
f(1,b,c,d,e)f(1, b, c, d, e).
For a=0a = 0 (i.e., when a′=1a' = 1):
The function simplifies to:
f(0,b,c,d,e)=bd′+cdf(0, b, c, d, e) = bd' + cd
For a=1a = 1 (i.e., when a′=0a' = 0):
The function simplifies to:
f(1,b,c,d,e)=cf(1, b, c, d, e) = c
3. Implementation Using 8:1 Multiplexer
We now have two expressions based on the value
of aa:
When a=0a = 0, the function is f(0,b,c,d,e)=bd′
+cdf(0, b, c, d, e) = bd' + cd.
When a=1a = 1, the function is f(1,b,c,d,e)=cf(1, b,
c, d, e) = c.
The function is now ready to be implemented using
an 8:1 multiplexer.
Inputs to the 8:1 Multiplexer:
The select lines of the 8:1 multiplexer will be
a,b,ca, b, c.
We will need to evaluate the function for all
combinations of bb and cc when a=0a = 0 and
when a=1a = 1.
So, for the multiplexer:
When a=0a = 0, we have:
o f(0,b,c,d,e)=bd′+cdf(0, b, c, d, e) = bd' + cd.
When a=1a = 1, we have:
o f(1,b,c,d,e)=cf(1, b, c, d, e) = c.
// Function for a = 0
assign mux_input[0] = 0; // (a=0, b=0, c=0)
assign mux_input[1] = d; // (a=0, b=0, c=1)
assign mux_input[2] = d'; // (a=0, b=1, c=0)
assign mux_input[3] = d' + d; // (a=0, b=1, c=1)
// Function for a = 1
assign mux_input[4] = 0; // (a=1, b=0, c=0)
assign mux_input[5] = 1; // (a=1, b=0, c=1)
assign mux_input[6] = 0; // (a=1, b=1, c=0)
assign mux_input[7] = 1; // (a=1, b=1, c=1)
endmodule
6. Explanation of the Verilog Code:
Inputs: The module accepts 5 inputs: a,b,c,d,ea, b,
c, d, e.
Multiplexer Inputs: The 8 inputs for the
multiplexer are defined in the array mux_input[7:0]
based on the truth table we derived.
o For a=0a = 0, we use the expression bd′
Answer 5:
Implement Sum (S) using 2:1 Multiplexers
To compute A⊕B⊕Cin can break it into two stages
using 2:1 multiplexers.
1. First Multiplexer: Calculate A⊕BA. We use a 2:1
multiplexer to compute A⊕B:
o Select line = A
o Inputs = B and B’
Verilog code
module full_adder (
input A, B, Cin,
output Sum, Cout
);
wire AB_xor, A_and_B;
// XOR of A and B
assign AB_xor = (A & ~B) | (~A & B);
// AND of A and B
assign A_and_B = A & B;
// Carry-out calculation
assign Cout = (A_and_B & ~Cin) | (AB_xor &
Cin);
endmodule
Verilog code:
module full_adder (
input A, B, Cin,
output Sum, Cout
);
wire [3:0] decoder_out; // Output of the 2:4
decoder
// 2:4 decoder to decode A and B
decoder2to4 decoder_inst (
.A(A),
.B(B),
.out(decoder_out)
);
module decoder2to4 (
input A, B,
output reg [3:0] out
);
always @(A or B) begin
case({A, B})
2'b00: out = 4'b0001;
2'b01: out = 4'b0010;
2'b10: out = 4'b0100;
2'b11: out = 4'b1000;
default: out = 4'b0000;
endcase
end
endmodule
Answer 6:
3-State Gate (Tri-State Gate)
A 3-state gate (also known as a tri-state buffer)
is a digital circuit component used in electronics,
particularly in systems where multiple devices are
connected to a shared bus or communication line,
such as in memory systems or data transfer. It has
three distinct output states:
1. Logic 0 (Low)
2. Logic 1 (High)
3. High Impedance (Z), also known as tri-state or
floating
The key feature of a 3-state gate is that it can
disconnect itself from the circuit entirely, allowing
other devices to take control of the line. This
makes it extremely useful in bus systems, where
multiple devices might need to send data over the
same wire, but only one device should be able to
transmit at any given time.
Operation of a 3-State Gate
A 3-state gate (or tri-state buffer) operates like a
regular gate (e.g., an AND gate, OR gate, etc.) in
two states:
1. Active State (either logic 0 or 1):
o When the 3-state gate is enabled, it behaves
effectively disconnected.
E = 1: The output follows the input (either 0 or 1).
Answer 7:
+-----------------------+
D0 ----> | |
| 2-to-1 MUX 1 |--------> Y0
D1 ----> | |
+-----------------------+
|
D2 ----> | |
| 2-to-1 MUX 2 |--------> Y1
D3 ----> | |
+-----------------------+
|
.
.
.
.
.
|
+-------------------+
S ------------> | Select Line |
+-------------------+
MUX1: Selects between D0 and D1 based on the
select line S. The output is Y0
MUX2: Selects between D2 and D3 based on the
select line S. The output is Y1.
Answer 12:
Comparator truth table
Answer 13:
We are required to implement the 3-to-8 decoder
using two 2-to-4 decoders and other necessary
gates. To achieve this, we can use a hierarchical
structure:
1. Use the 2-to-4 decoders to handle smaller chunks
of the logic (using only 2 input bits at a time).
2. We can use the 3rd input bit as a control signal for
the two 2-to-4 decoders.
3-to-8 Decoder Using Two 2-to-4 Decoders
A 2-to-4 decoder takes 2 inputs and produces 4
outputs. The basic idea is to use the 3rd input bit
as a selector between two 2-to-4 decoders, each
handling the other two input bits.
Answer 15:
First, let's use a 2-to-4 line decoder to decode
the inputs a and b. The outputs of the decoder
will be D0, D1, D2, and D3, where D0 = a′b′,
D1 = a′b, D2 = ab′, and D3 = ab.
Verilog code
module combinational_circuit(
input a,
input b,
output [1:0] Y
);
// Decoder implementation
assign D0 = ~a & ~b;
assign D1 = ~a & b;
assign D2 = a & ~b;
assign D3 = a & b;
// Output Y
assign Y[0] = F1;
assign Y[1] = F2 | F3;
endmodule
Answer 16:
An 8-to-3 priority encoder is a combinational
circuit that has 8 input lines and 3 output lines. The
outputs represent the binary encoding of the
highest-priority input (i.e., the input with the least
subscript number) that is high. Additionally, we are
required to include an output Z that will indicate
whether any input is high.
Truth table:
Explanation of Outputs:
Y2, Y1, Y0: These are the 3 output bits
representing the binary encoding of the highest-
priority input that is set to 1.
Z: This output is high (1) if at least one input is
high, otherwise, it is low (0).
Verilog Code Using For Loop:
We can write the behavioral Verilog code for an
8-to-3 priority encoder using a for loop. The for
loop will iterate over the inputs in a decreasing
priority order (from I7 to I0) and set the
corresponding output values based on the highest
priority input that is active.
Verilog code
module priority_encoder (
input [7:0] I, // 8-bit input
output reg [2:0] Y, // 3-bit output (Y2, Y1, Y0)
output reg Z // Output Z to indicate if any input
is high
);
endmodule
Behavior of code:
The always block triggers whenever the input I
changes.
The for loop starts from the highest priority input
(I7) and checks each input down to I0.
As soon as a high input is found, the corresponding
binary encoding (the value of i) is assigned to Y,
and Z is set to 1.
The break statement ensures that the loop stops
after finding the first high input, as that
corresponds to the highest priority.
Z output: The Z output is set to 1 as soon as any
input is found to be high. If none of the inputs are
high, Z remains 0.
Example Simulation:
For the input I = 8'b01000000
The highest priority active input is I6, so the output
will be Y = 3'b110 (binary for 6) and Z = 1.
For the input I=8′b00000000
No inputs are active, so the output will be Y =
3'b000 (default value) and Z = 0.
Answer 17:
Verilog code:
module comparator (
input [1:0] A, // 2-bit signed number A (a1 a0)
input [1:0] B, // 2-bit signed number B (b1 b0)
output reg AB, // A > B (AB = 1)
output reg EQ, // A == B (EQ = 1)
output reg BA // B > A (BA = 1)
);
always @ (A or B) begin
// Initialize outputs
AB = 0;
EQ = 0;
BA = 0;
endmodule
Answer 19:
A 3 to 8 decoder has 3 inputs and 8 outputs, each
output corresponding to one of the minterms (m0
to m7). To implement the function F1(A,B,C) =
∑m(2,4,7), you can use the outputs of the
decoder that correspond to minterms 2, 4, and 7.
Connect the outputs of the decoder for these
minterms to an OR gate.
Explanation:
The 3 to 8 decoder will generate outputs for each
minterm based on the input combination. By
connecting the outputs corresponding to minterms 2, 4,
and 7 to an OR gate, you can implement the function
F1. This is because an OR gate will output a high signal
if any of its inputs are high, which matches the
behavior of the sum of minterms.
For F2(A,B,C) = ∑m(0,3), connect the outputs of
the decoder corresponding to minterms 0 and 3 to
another OR gate.
Explanation:
Similar to F1, you connect the outputs for
minterms 0 and 3 to an OR gate to implement F2.
The OR gate will output high if either of these
minterms is high.
Answer 20:
Design Overview:
We will break the decoder into smaller blocks using
a hierarchical approach.
Use the generate block to instantiate 16 output
lines.
We will use FOR GENERATE to create multiple
blocks of decoding logic and IF GENERATE to
conditionally generate specific outputs for each
input combination.
Verilog code:
module decoder_4_to_16 (
input [3:0] A, // 4-bit input
output reg [15:0] Y // 16-bit output
);
generate
// Loop through all 16 outputs
for (i = 0; i < 16; i = i + 1) begin : decoder_logic
// Conditional assignment based on input A
if (i == A) begin
assign Y[i] = 1; // Set the output to 1 when i
equals A
end
else begin
assign Y[i] = 0; // Set all other outputs to 0
end
end
endgenerate
endmodule
Conclusion:
This Verilog code for a 4-to-16 decoder uses
hierarchical design with FOR GENERATE and IF
GENERATE statements. It makes the design modular,
where each output is conditionally set based on the
input value. This approach is efficient and scalable for
decoding logic.