0% found this document useful (0 votes)
16 views12 pages

Verilog and VHDL Code for Logic Circuits

The document contains the solutions to 4 questions regarding digital logic circuits. It includes Verilog and VHDL code for: 1) A circuit that outputs HIGH if the 4-bit input is >12 or <3. 2) A circuit with two switches that controls a lamp. 3) A 5-bit odd parity generator for a 4-bit input. 4) A 6-bit subtractor circuit that subtracts two 6-bit numbers.

Uploaded by

Hamna Sajjad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views12 pages

Verilog and VHDL Code for Logic Circuits

The document contains the solutions to 4 questions regarding digital logic circuits. It includes Verilog and VHDL code for: 1) A circuit that outputs HIGH if the 4-bit input is >12 or <3. 2) A circuit with two switches that controls a lamp. 3) A 5-bit odd parity generator for a 4-bit input. 4) A 6-bit subtractor circuit that subtracts two 6-bit numbers.

Uploaded by

Hamna Sajjad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ASSIGNMENT 2

NAME: Hamna Sajjad

ROLL NO.: EL-19004

CLASS: SE-EL (section A)

SUBJECT: Digital Logic Circuit

COURSE CODE: TC-201


QUESTION: 01
Write the Verilog code for a circuit the produce a HIGH output only if the input, represented by
a 4-bit binary number, is greater than twelve or less than three. [CLO-02]

VERILOG CODE:
module NumberDetector (Sum, a, b, c, d)

output Sum;

input a, b, c, d;

assign Sum={(a&&b&&c&&d)||(a&&b&&c&&!d)||(a&&b&&!c&&d)||(!a&&!b&&c&&!d)
||(!a&&!b&&!c&&d)||(!a&&!b&&!c&&!d)}

end module
CIRCUIT:

a
b
c
d

Sum

SIMULATION:

d
Sum
QUESTION: 02
Write the Verilog code for logic circuit that meet the following requirements:

A battery-powered lamp in a room is to be operated from two switches, one at the back door
and one at the front door. The lamp is to be on if the front switch is on and the back switch is
off, or if the front switch is off and the back switch is on. The lamp is to be off if both switches
are off or if both switches are on. Let a HIGH output represent the on condition and a LOW
output represent the off condition. [CLO 2]

VERILOG CODE:
module Switch (X, A, B)

output X;

input A, B;

assign X = {(A&&!B)||(!A&&B)}

end module

CIRCUIT:

A
B X
SIMULATION:

B
X

QUESTION: 03
Parity bits are often used in digital communication for error detection and correction. The
simplest of these involve transmitting one additional bit with the data, a parity bit. In these
system the parity bit is attached to a group of transmitting bits to make the total number of 1s
in a group always even or always odd. An even parity bit makes number of 1s even and odd
parity bit makes the total number of 1s odd. Write the VHDL for parity generator that generates
a 5 bit odd parity generation for a 4 bit input number/group. [CLO 2]

VHDL CODE:
entity Parity_generator is
port(B1, B2, B3, B4: in bit;
P : out bit);
end Parity_generator;

architecture logic_circuit of Parity_generator is


begin
P <= (B1 or B2 or B3 or(not B4)) and (B1 or B2 or (not B3) or B4) and
(B1 or (not B2) or B3 or B4) and (B1 or (not B2) or (not B3) or (not B4)) and
((not B1) or B2 or B3 or B4) and ((not B1) or B2 or (not B3) or (not B4)) and
((not B1) or (not B2) or B3 or (not B4)) and ((not B1) or (not B2) or (not B3) or B4);
end logic_circuit;
CIRCUIT:

SIMULATION:
QUESTION: 04
Write the Verilog code for 6 bit subtractor i.e.
S(6bit) = A(6bit) – B(6bit)
Where,
A,B is a 6 bit signed number

VERILOG CODE:
(PART 01)

module FullAdder(S, Co, A, B, Ci);


output S, Co;
input A, B, Ci;
wire Abar, Bbar, Cibar, x1, x2, x3, x4, x5, x6, x7, x8;
not(Abar, A);
not(Bbar, B);
not(Cibar, Ci);
and(x1, Abar, Bbar, Ci);
and(x2, Abar, B, Cibar);
and(x3, A, Bbar, Cibar);
and(x4, A, B, Ci);
and(x5, Abar, B, Ci);
and(x6, A, Bbar, Ci);
and(x7, A, B, Cibar);
and(x8, A, B, Ci);
or(S, x1, x2, x3, x4);
or(Co, x5, x6, x7, x8);
end module
CIRCUIT:

SIMULATION:
VERILOG CODE:
(PART 02)

module AdderBit6(S, Co, A, B, Ci);

output [5:0]S;

output Co;

input [5:0]A, B;

input Ci;

wire [4:0]x;

FullAdder FA0(S[0], x[0], A[0], B[0], Ci);

FullAdder FA1(S[1], x[1], A[1], B[1], x[0]);

FullAdder FA2(S[2], x[2], A[2], B[2], x[1]);

FullAdder FA3(S[3], x[3], A[3], B[3], x[2]);

FullAdder FA4(S[4], x[4], A[4], B[4], x[3]);

FullAdder FA5(S[5], Co, A[5], B[5], x[4]);

end module

CIRCUIT:
SIMULATION:

VERILOG CODE:
(PART 03)

module Bit_6_Subtractor(S, Co, A, B, Ci);

output [5:0]S;

output Co;

input [5:0]A, B;

input Ci;

wire [5:0]X;

xor(X[0],B[0],Ci);

xor(X[1],B[1],Ci);

xor(X[2],B[2],Ci);

xor(X[3],B[3],Ci);

xor(X[4],B[4],Ci);

xor(X[5],B[5],Ci);

AdderBit6(S, Co, A, X, Ci);

end module
CIRCUIT:

SIMULATION:

Common questions

Powered by AI

In the parity generator design, the parity bit ensures the total number of 1s in the input data results in an odd count, which provides error detection. The VHDL code for odd parity generation uses a sequence of logical OR and NOT operations to ensure that the combined state of the four input bits and the parity bit (P) results in an odd number of 1s. The specific driven logic accounts for permutations of the inputs to guarantee this condition .

The lamp circuit represents a logical XOR gate architecture applied to physical control. In this design, the lamp's state changes based on the toggled states of two switches located at different physical points (back door and front door). Such a circuit design can be applied in scenarios requiring control mechanisms from multiple locations, such as lighting systems in hallways or staircases, allowing operation from either end, enhancing convenience and accessibility .

The NumberDetector circuit is useful because it performs a specific logic function of detecting if an input value, represented by a 4-bit binary number, meets defined conditions. It outputs HIGH when the number is greater than twelve or less than three. This type of conditional detection is crucial in digital systems for sorting, filtering, and controlling processes based on specific criteria, especially in applications where threshold detection or specific state actions are required .

The module Switch achieves its functionality by using an exclusive OR (XOR) behavior with the expression (A&&!B)||(!A&&B). This condition results in a HIGH output when only one of the two switches, either the front switch (A) or the back switch (B), is HIGH. If both switches are in the same state (both HIGH or both LOW), the output will be LOW. This implements the desired logic where the lamp is on if the switches differ and off if they are the same .

The redundancy in the parity generator VHDL code arises from the multiple OR operations combined with NOT operations, resulting in a consistently odd number of 1s regardless of input combinations. This redundancy might be structured to safeguard against errors and ensure robust error detection across various input permutations. By extensively covering possible input conditions, the design increases reliability and ensures that any errors in input bits impact the parity correctly, maintaining the odd parity condition .

A VHDL-described parity checker offers several advantages in digital communication systems, including automation in error detection processes and integration ease with other digital circuits. It can quickly verify the integrity of transmitted data by identifying whether the parity condition (odd or even) is satisfied, thereby allowing for efficient troubleshooting and correction of errors. Additionally, VHDL provides flexibility and scalability in design adjustments to meet varying system requirements .

The use of FullAdders within the 6-bit subtractor module is essential because it facilitates binary addition using the 2's complement method for performing subtraction. Each FullAdder takes two input bits and a carry-in, producing a sum and a carry-out, allowing the cascading of bits across the 6-bit length. This mechanism supports building a scalable, modular approach to binary arithmetic by combining simple adder circuits to handle complex multi-bit operations efficiently, while also handling arithmetic overflow .

The Sum equation in the NumberDetector displays concepts of boolean algebra and digital logic circuit design. It uses a combination of AND, OR, and NOT operations to construct a logic function that evaluates specific binary input combinations, detecting when an input is greater than twelve or less than three. This functionality is an application of conditional logic, where the boolean expressions evaluate different possible states of the input bits to produce a desired output condition .

The 6-bit subtractor module performs subtraction by employing XOR gates and a 6-bit adder module internally. Initially, the subtraction is translated into addition of the 2’s complement of the subtrahend. The subtrahend (B) is inverted using XOR gates with the carry-in bit (Ci), which is set to 1. This effectively generates the 2's complement of B. The addition is then executed using the AdderBit6 module, which sums the original minuend (A) with the 2's complement of the subtrahend to produce the final result, S, and a carry-out, Co .

The module for the 6-bit addition handles carry propagation by using a series of FullAdders linked in sequence, where each adder receives a carry bit from its predecessor. This chaining is crucial because it propagates any carry-over generated at a lower significant bit position to subsequent higher positions, ensuring accurate arithmetic results and handling possible overflow efficiently. Such a design is important for maintaining precision in digital systems that require reliable numerical computations, like in financial or control systems applications .

You might also like