Professional Documents
Culture Documents
LAB MANUAL
III.BE.VII
By
NAJEEMULLA BAIG
Assistant Professor
i
S. No. CONTENTS PAGE
No.
1 Vision of the Institution I
2 Mission of the Institution I
3 Vision of the Department II
4 Mission of the Department II
5 PEOs II
6 POs III
7 PSOs IV
8 Introduction to VIVADO IV
9 General Guidelines & Safety instructions XXVII
LIST OF THE EXPERIMENTS
PART-A
1. 1Write structural and dataflow Verilog HDL models for a) 4-bit ripple 1
0carry adder. b) 4-bit carry Adder – cum Subtractor. c) 2-digit BCD adder /
subtractor. d) 4-bit carry look ahead adder e) 4-bit comparator
2. 1Write a Verilog HDL program in behavioral model for a) 8:1 multiplexer 2
1b) 3:8 decoder c) 8:3 encoder d) 8 bit parity generator and checker
3. 1Write a Verilog HDL program in Hierarchical structural model for a) 16:1 3
2multiplexer realization using 4:1 multiplexer b) 3:8 decoder realization
through 2:4 decoder c) 8-bit comparator using 4-bit comparators and
additional logic
4. 1Write a Verilog HDL program in behavioral model for D,T and JK flip 4
3flops, shift registers and counters
5. 1Write a Verilog HDL program in structural and behavioral models for a) 6
48 bit asynchronous up-down counter b) 8 bit synchronous up-down
counter
6. 1Write a Verilog HDL program for 4-bit sequence detector through Moore 8
5state machines.
7. 1Write a Verilog HDL program for 4-bit sequence detector through Mealy 9
6state machines
PART-B- 10
8 Transistor Level implementation of CMOS circuits using VLSI CAD tool
9Basic Logic Gates: Inverter, NAND and NOR 11
9
9
8. Half Adder and Full Adder 13
9. 2:1 Multiplexer and 4:1 Multiplexer using 2:1 Multiplexer 22
10. one bit comparator and four-bit magnitude comparator using one bit
comparator
11. Implement the Layout of CMOS Inverter.
12. Implement the Layout of CMOS NAND.
ii
LORDS INSTITUTE OF ENGINEERINANTECHNOLOGY
DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING
To be part of universal human quest for development and progress by contributing high
caliber, ethical and socially responsible engineers who meet the global challenge of building modern
society in harmony with nature.
3
LORDS INSTITUTE OF ENGINEERING AND TECHNOLOGY
• DM2: To impart training to students for good designing skills and exposure to Research &
Development and innovations by using modern tools in frontier areas of Engineering and Project
Management
5.PROGRAM OUTCOMES
PO2: Problem analysis: Identify, formulate, research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural sciences,
and engineering sciences.
PO3: Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate consideration
for
the public health and safety, and the cultural, societal, and environmental considerations.
PO6: The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
PO7: Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need for
sustainable development.
PO8: Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
PO9: Individual and team work: Function effectively as an individual, and as a member or leader
in diverse teams, and in multidisciplinary settings.
PO 12: Life-long learning: Recognize the need for, and have the preparation and ability to engage
in independent and life-long learning in the broadest context of technological change.
PSO1: Work as Software Engineers for providing solutions to real world problems usingStructured,
Object-Oriented Programming languages and open-source software.
PSO2: Function as Systems Engineer, Software Analyst and Tester for IT and ITeS.
5
HDL(HARDWARE DESCRIPTION LANGUAGE)
HDL -> Hardware description language allows us to specify the components that makeup
instead of having to use a pictorial representation like a block or logic diagram. Every
component is defined by its input and output part logic function. It performs and timing
characteristics such as delay and clocking. An entire digital system can described in text
format using prescribed set of roles and keywords (reserved words).
6
VLSI DESIGN FLOW
7
STEPS FOR VIVADO DESIGN SUIT
8
4. Click on [Next] again.
9
6. Right click in “Hierarchy”.
10
8. Select ‘Verilog Module’.
11
10. Give the input and output parameters. Then click on [Next].
12
12. Write the Verilog code.
13
14. Run ‘Check Syntax’ to check the syntax of the written verilog code.
15. Go to ‘Simulation’ in view pane. Then right click again in ‘Hierarchy’ and
select ‘NewSource...’.
14
16. Select ‘Verilog Test Fixture’ and give a name to the file. Then click on [Next].
17. Select the Verilog code module that was created before and click on [Next].
15
18. Enter the ‘Test Bench Code’.
16
2. Run the ‘Behavioral Check Syntax’ and ‘Simulate Behavioral Model’.
17
Using Synthesis Settings
1. From the Flow Navigator click Settings, then select Synthesis, or select Flow >
Settings > Synthesis Settings. The Settings dialog box opens, as shown in the following
figure
2. Under the Constraints section of the Settings dialog box, select the Default Constraint Set
as the active constraint set; a set of files containing design constraints captured in Xilinx
design constraints (XDC) files that you can apply to your design. The two types of design
constraints are: °
• Physical constraints: These constraints define pin placement, and absolute, or relative,
placement of cells such as block RAMs, LUTs, Flip-Flops, and device configuration
settings.
• Timing constraints: These constraints define the frequency requirements for the design.
Without timing constraints, the Vivado Design Suite optimizes the design solely for wire
18
• See this link to the Vivado Design Suite User Guide: Using Constraints (UG903) [Ref 12]
• New runs use the selected constraint set, and the Vivado synthesis targets this constraint
3. From the Options area: Select a Strategy from the drop-down menu where you can view and
select a predefined synthesis strategy to use for the synthesis run. There are different preconfigured
You can also define your own strategy. When you select a synthesis strategy, available Vivado
strategy displays in the dialog box. You can override synthesis strategy settings by changing the
For a list of all the strategies and their respective settings, see the -directive option in the
following list, and see Table 1-2 to see a matrix of strategy default settings.
- none: Instructs the synthesis tool to never flatten the hierarchy. The output of synthesis has
19
- full: Instructs the tool to fully flatten the hierarchy leaving only the top level.
- rebuilt: When set, rebuilt allows the synthesis tool to flatten the hierarchy, perform
synthesis, and then rebuild the hierarchy based on the original RTL. This value allows the
QoR benefit of cross-boundary optimizations, with a final hierarchy that is similar to the
• -gated_clock_conversion: Turns on and off the ability of the synthesis tool to convert the
• The use of gated clock conversion also requires the use of an RTL attribute to work. See
• -bufg: Controls how many BUFGs the tool infers in the design. The Vivado design tools
use this option when other BUFGs in the design netlists are not visible to the synthesis
process.
The tool infers up to the amount specified, and tracks how many BUFGs are instantiated in the
RTL. For example, if the -bufg option is set to 12 and there are three BUFGs instantiated in the
° -directive: Replaces the -effort level option. When specified, this option runs Vivado synthesis
with different optimizations. See Table 1-2 for a list of all strategies and settings. Values are:
- Runtime Optimized: Performs fewer timing optimizations and eliminates some RTL optimizations
- Area Optimized high: Performs general area optimizations including forcing ternary adder
implementation, applying new thresholds for use of carry chain in comparators, and implementing
area-optimized multiplexers.
- Area Optimized medium: Performs general area optimizations including changing the threshold for
control set optimizations, forcing ternary adder implementation, lowering multiplier threshold of
20
inference into DSP blocks, moving shift register into BRAM, applying lower thresholds for use of
- AlternateRoutability: Set of algorithms to improve route-ability (less use of MUXFs and CARRYs)
- AreaMapLargeShiftRegToBRAM: Detects large shift registers and implements them using dedicated
block RAM.
- FewerCarryChains: Higher operand size threshold to use LUTs instead of the carry chain.
- LogicCompaction: Arranges CARRY chains and LUTs in such a way that it makes the logic more
compact using fewer SLICES. This could have a negative effect on timing QoR.
- PerformanceOptimized: Performs general timing optimizations including logic level reduction at the
expense of area.
- PowerOptimized_high: Performs general timing optimizations including logic level increase at the
expense of area.
-retiming: For non-Versal devices only: For controlling retiming in Versal, please the -no_retiming
option. This Boolean option provides an option improve circuit performance for intra-clock sequential
paths by automatically moving registers (register balancing) across combinatorial gates or LUTs. It
maintains the original behavior and latency of the circuit and does not require changes to the RTL
21
EXPERIMENT-1
module mux4x1(
input [3:0] in,
input [1:0] sel,
output reg out);
endmodule
module mux2x1 (
input wire a,
input wire b,
input wire sel,
output reg out
);
always @* begin
case (sel)
1'b0: out = a;
1'b1: out = b;
endcase
end
endmodule
//testbench
module mux4x1_tb;
// Inputs reg
22
[3:0] in;
reg [1:0] sel;
// Outputs
wire out;
// Test stimulus
initial begin
$monitor("in=%b, sel=%b, out=%b", in, sel, out);
// Initialize inputs
in = 4'b1010;
sel = 2'b00;
#10; // Wait for some time
sel = 2'b01;
#10; // Wait for some time
sel = 2'b10;
#10; // Wait for some time
sel = 2'b11;
#10; // Wait for some time
$finish;
end
endmodule
23
Output Waveforms:
Rtl Schematic :
//Mux 16x1
// level 1
mux4x1 dut1(i[3:0],select[1:0],y1);
mux4x1 dut2(i[7:4],select[1:0],y2);
mux4x1 dut3(i[11:8],select[1:0],y3);
mux4x1 dut4(i[15:12],select[1:0],y4);
//level 2
mux4x1 dut5({y4,y3,y2,y1},select[3:2],y);
24
endmodule
//Mux 4x1
module mux4x1(input [3:0]i,
input [1:0]select,
output reg y);
always@*
case(select)
2'b00: y=i[0];
2'b01: y=i[1];
2'b10: y=i[2];
2'b11: y=i[3];
endcase
endmodule
//Testbench
module test_mux16x1;
// Inputs
reg [15:0] data_in;
reg [3:0] select;
// Outputs
wire out;
endmodule
Output Waveform:
module full_adder(a,b,cin,sum,co);
input a,b,cin;
output sum,co;
assign sum=a^b^cin;
assign co=(a&b)|(a&cin)|(cin&b);
endmodule
input [3:0]a;
input [3:0]b;
input cin;
wire [2:0]c;
full_adder u0 (a[0],b[0],cin,sum[0],c[0]);
full_adder u1 (a[1],b[1],c[0],sum[1],c[1]);
full_adder u2 (a[2],b[2],c[1],sum[2],c[2]);
full_adder u3 (a[3],b[3],c[2],sum[3],cout);
endmodule
module TestModule;
// Inputs
reg [3:0] a;
reg [3:0] b;
reg cin;
// Outputs
wire [3:0] sum;
wire cout;
// Initialize Inputs
initial begin
$monitor("a=%0d b=%0d cin=%b sum=%0d cout=%b",a,b,cin,sum,cout);
a = 0;
b = 0;
cin = 0;
27
// Wait 100 ns for global reset to finish
#100;
a = 5;
b = 6;
cin = 1;
// Wait 100 ns for global reset to finish
#100;
end
endmodule
Output Waveform:
RTL Schematic:
28
1.4 -to-8 decoder using 2-to-4 decoder
module decoder2x4_enable(input [1:0] sel, input enable, output reg [3:0] out);always @(sel,
enable) begin
if (enable) begincase(sel)
2'b00: out = 4'b0001;
2'b01: out = 4'b0010;
2'b10: out = 4'b0100;
2'b11: out = 4'b1000;
endcase end else
begin
out = 4'b0000;end
end endmodule
//3 to 8 decoder
module decoder3x8(input [2:0]sel, output [7:0]out);
endmodule
//Testbench
module testbench();
// Declare the inputs and outputs for the testbenchreg [2:0] sel;
wire [7:0] out;
sel = 0;
#10;
sel = 1;
#10;
sel = 2;
#10;
29
sel = 3;
#10;
sel = 4;
#10;
sel = 5;
#10;
sel = 6;
#10;
sel = 7;
#10;
$finish;end
endmodule
Output Waveform:
30
EXPERIMENT-2
module full_adder_tb;reg a,
b, cin;
wire sum, cout;
// Instantiate the Full-Adder modulefull_adder dut(a,
b, cin, sum, cout);
// Apply stimulus
initial begin
$monitor("%b %b %b | %b %b", a, b, cin, sum, cout);
$display("A B Cin | Sum Cout");
$display(" ---------------------- ");
a = 0; b = 0; cin = 0; #10;
a = 0; b = 0; cin = 1; #10;
a = 0; b = 1; cin = 0; #10;
a = 0; b = 1; cin = 1; #10;
a = 1; b = 0; cin = 0; #10;
a = 1; b = 0; cin = 1; #10;
a = 1; b = 1; cin = 0; #10;
a = 1; b = 1; cin = 1; #10;
$finish; end
endmodule
Waveforms
32
2.1 Mux 8x1 using case
statement:module mux8x1 (
input [7:0] in,
input [2:0] sel,
output reg out
);
always @* begin
case (sel)
3'b000: out = in[0];
3'b001: out = in[1];
3'b010: out = in[2];
3'b011: out = in[3];
3'b100: out = in[4];
3'b101: out = in[5];
3'b110: out = in[6];
3'b111: out = in[7];
endcase
end
endmodule
module mux8x1_tb;reg
[7:0] in;
reg [2:0] sel;
wire out;
integer i;
mux8x1 dut (
.in(in),
.sel(sel),
.out(out)
);
initial begin
in=8'b1010_1010;
33
$monitor($time,"Input: in=%b, sel=%b,Output: out=%b",in,sel,out);for(i=0;i<=7;i=i+1)
begin
sel=i;
#10;
end
#10;
$stop; end
endmodule
Waveforms:
34
2.2 Encoder 8-to-3 using case statement:
//8 to 3 encoder:
module encoder8x3 ( input [7:0] in, output reg [2:0] out);
always @* begin
case (in)
8'b00000001: out = 3'b000;
8'b00000010: out = 3'b001;
8'b00000100: out = 3'b010;
8'b00001000: out = 3'b011;
8'b00010000: out = 3'b100;
8'b00100000: out = 3'b101;
8'b01000000: out = 3'b110;
8'b10000000: out = 3'b111;
default: out = 3'b000; // Default output when none of the inputs match
endcase
end
endmodule
//testbench:
module encoder8x3_tb;
reg [7:0] in;
wire [2:0] out;
integer i;
encoder8x3 dut ( .in(in), .out(out) );
initial begin
$monitor($time, "Input: in=%b, Output: out=%b",in, out);
for(i=0;i<=7;i=i+1) begin
if(i==0)
in= 8'b0000_0001;
else
in=in<<1'b1;
#10;
end
#10;
$stop;
end
endmodule
Waveforms
35
2.3 Decoder 3-to-8 using case statement:
//decoder 3 to 8
module decoder3x8 ( input [2:0] in, output reg [7:0] out);
always @* begin
case (in)
3'b000: out = 8'b00000001;
3'b001: out = 8'b00000010;
3'b010: out = 8'b00000100;
3'b011: out = 8'b00001000;
3'b100: out = 8'b00010000;
3'b101: out = 8'b00100000;
3'b110: out = 8'b01000000;
3'b111: out = 8'b10000000;
default: out = 8'b00000000; // Default output when none of the inputs match
endcase
end
endmodule
//Testbench
module decoder3x8_tb;
reg [2:0] in;
wire [7:0] out;
integer i;
36
decoder3x8 dut ( .in(in), .out(out) );
initial begin
$monitor($time,"Input: in=%b, Output: out=%b",in, out);
for(i=0;i<=7;i=i+1) begin
in=i;
#10;
end
#10;
$stop;
end
endmodule
Waveforms
2.1 write a verilog hdl code and its testbench for half-adder:
// Half-Adder Module
module half_adder(input a, b, output sum, carry);xor(sum, a, b);
and(carry, a, b);
endmodule
37
// Apply stimulus
initial begin
$monitor("%b %b | %b %b", a, b, sum, carry);
$display("A B | Sum Carry");
$display(" -------------------------");
a = 0; b = 0; #10;
a = 0; b = 1; #10;
a = 1; b = 0; #10;
a = 1; b = 1; #10;
$finish;
end
endmodule
Output Waveforms:
RTL Schematic:
38
2.2 write a verilog hdl code and its testbench for 1-bit full-adder:
// Full-Adder Module
module full_adder(input a, b, cin, output sum, cout);wire s1, c1, c2;
xor(s1, a, b);
xor(sum, s1, cin);
and(c1, a, b);
and(c2, s1, cin);
or(cout, c1, c2);
endmodule
// Apply stimulus
initial begin
$monitor("%b %b %b | %b %b", a, b, cin, sum, cout);
$display("A B Cin | Sum Cout");
$display(" --------------------------- ");
a = 0; b = 0; cin = 0; #10;
a = 0; b = 0; cin = 1; #10;
a = 0; b = 1; cin = 0; #10;
a = 0; b = 1; cin = 1; #10;
a = 1; b = 0; cin = 0; #10;
a = 1; b = 0; cin = 1; #10;
39
a = 1; b = 1; cin = 0; #10;
a = 1; b = 1; cin = 1; #10;
$finish;
end
endmodule
Waveforms:
RTL Schematic:
40
not(n1, a);
xor(diff, a, b);
and(bout, n1, b);
endmodule
// Apply stimulus
initial begin
$monitor("%b %b | %b %b", a, b, diff, bout);
$display("A B | Diff Bout");
$display(" -------------------------");
a = 0; b = 0; #10;
a = 0; b = 1; #10;
a = 1; b = 0; #10;
a = 1; b = 1; #10;
$finish;
end
endmodule
41
Waveforms:
RTL Schematic:
42
2.4 write a verilog hdl code and its testbench for 1-bit full-subtractor:
// Full-Subtractor Module
module full_subtractor(input a, b, bin, output diff, bout);xor(diff,a,b,bin);
xnor(y1,a,b);
and(y2,bin,y1);
not(y3,a);
and(y4,y3,b);
or(bout,y2,y4);
endmodule
// Testbench for Full-Subtractormodule
full_subtractor_tb;
reg a, b, bin; wire
diff, bout;
// Apply stimulus
initial begin
$monitor("%b %b %b | %b %b", a, b, bin, diff, bout);
$display("A B Bin | Diff Bout");
$display(" -------------------------------- ");
a = 0; b = 0; bin = 0; #10;
a = 0; b = 0; bin = 1; #10;
a = 0; b = 1; bin = 0; #10;
43
a = 0; b = 1; bin = 1; #10;
a = 1; b = 0; bin = 0; #10;
a = 1; b = 0; bin = 1; #10;
a = 1; b = 1; bin = 0; #10;
a = 1; b = 1; bin = 1; #10;
$finish;
end
endmodule
Output:
RTL Schematic:
44
2.5 write a verilog hdl code and its testbench for MUX 2x1
and(w1, a, ~sel);
and(w2, b, sel);
or(y, w1, w2);
endmodule
// Apply stimulus
initial begin
$monitor("%b %b %b | %b", a, b, sel, y);
$display("A B Sel | Y");
$display(" --------------- ");
a = 0; b = 0; sel = 0; #10;
a = 0; b = 0; sel = 1; #10;
a = 0; b = 1; sel = 0; #10;
a = 0; b = 1; sel = 1; #10;
a = 1; b = 0; sel = 0; #10;
a = 1; b = 0; sel = 1; #10;
45
a = 1; b = 1; sel = 0; #10;
a = 1; b = 1; sel = 1; #10;
$finish;
end
endmodule
Output waveforms:
RTL Schematic:
46
// 4-to-2 Encoder Module
module encoder_4to2(input [3:0] data, output [1:0] y);
or(y[0],data[1],data[3]);
or(y[1],data[2],data[3]);
endmodule
//Testbench module
module encoder_4to2_tb;
reg [3:0] data;
wire [1:0] y;
// Apply stimulus
initial begin
$monitor("%b | %b", data, y);
$display("Data | Y");
$display(" ----------- ");
data = 4'b0000; #10;
data = 4'b0001; #10;
data = 4'b0010; #10;
data = 4'b0011; #10;
data = 4'b0100; #10;
data = 4'b0101; #10;
data = 4'b0110; #10;
data = 4'b0111; #10;
data = 4'b1000; #10;
data = 4'b1001; #10;
data = 4'b1010; #10;
data = 4'b1011; #10;
data = 4'b1100; #10;
data = 4'b1101; #10;
47
data = 4'b1110; #10;
data = 4'b1111; #10;
$finish;
end
endmodule
endmodule
48
// Instantiate the 2-to-4 Decoder moduledecoder_2to4
dut(data, y);
// Apply stimulus
initial begin
$monitor("%b | %b", data, y);
$display("Data | Y");
$display(" -------------------- ");
data = 2'b00; #10; data =
2'b01; #10; data = 2'b10;
#10;data = 2'b11; #10;
$finish;
end
endmodule
Out Waveforms:
49
RTL Schematic
// Testbench
for 1x2 Demultiplexermodule demux_1x2_tb;
reg a, sel;
wire y0, y1;
// Apply stimulus
initial begin
$monitor("%b %b | %b %b", a, sel, y0, y1);
50
$display("A Sel | Y0 Y1");
$display(" ---------------------- ");
a = 0; sel = 0; #10;
a = 0; sel = 1; #10;
a = 1; sel = 0; #10;
a = 1; sel = 1; #10;
$finish;
end
endmodule
// Apply stimulus
initial begin
$monitor("%b %b | %b %b %b", a, b, equal, greater, less);
$display("A B | Equal Greater Less");
51
$display(" ----------------------- ");
a = 0; b = 0; #10;
a = 0; b = 1; #10;
a = 1; b = 0; #10;
a = 1; b = 1; #10;
$finish;
end
endmodule
// Half-Adder module
module half_adder(input a, b, output sum, carry);assign sum = a ^ b;
assign carry = a & b;
endmodule
// Apply stimulus
initial begin
$monitor("%b %b | %b %b", a, b, sum, carry);
$display("A B | Sum Carry");
$display(" -------------------------");
a = 0; b = 0; #10;
a = 0; b = 1; #10;
a = 1; b = 0; #10;
52
a = 1; b = 1; #10;
$finish;
end
endmodule
// Apply stimulus
initial begin
$monitor("%b %b %b | %b %b", a, b, cin, sum, cout);
$display("A B Cin | Sum Cout");
$display(" --------------------------- ");
a = 0; b = 0; cin = 0; #10;
a = 0; b = 0; cin = 1; #10;
a = 0; b = 1; cin = 0; #10;
a = 0; b = 1; cin = 1; #10;
a = 1; b = 0; cin = 0; #10;
a = 1; b = 0; cin = 1; #10;
a = 1; b = 1; cin = 0; #10;
53
a = 1; b = 1; cin = 1; #10;
$finish;
end
endmodule
//Half_subtractor module
module half_subtractor(input a, b, output diff, borrow);assign diff = a ^ b;
assign borrow = (~a) & b;
endmodule
// Apply stimulus
initial begin
$monitor("%b %b | %b %b", a, b, diff, bout);
$display("A B | Diff Bout");
$display(" -------------------------");
a = 0; b = 0; #10;
a = 0; b = 1; #10;
a = 1; b = 0; #10;
a = 1; b = 1; #10;
$finish;
end
endmodule
54
//1-bit full-subtractor module
// Apply stimulus
initial begin
$monitor("%b %b %b | %b %b", a, b, bin, diff, bout);
$display("A B Bin | Diff Bout");
$display(" -------------------- ");
a = 0; b = 0; bin = 0; #10;
a = 0; b = 0; bin = 1; #10;
a = 0; b = 1; bin = 0; #10;
a = 0; b = 1; bin = 1; #10;
a = 1; b = 0; bin = 0; #10;
a = 1; b = 0; bin = 1; #10;
a = 1; b = 1; bin = 0; #10;
a = 1; b = 1; bin = 1; #10;
$finish;
end
endmodule
55
//MUX 2x1 module
module mux_2x1(input d0, d1, sel, output y);
assign y = sel ? d1 : d0;
endmodule
// Apply stimulus
initial begin
$monitor("%b %b %b | %b", a, b, sel, y);
$display("A B Sel | Y");
$display(" ---------- ");
a = 0; b = 0; sel = 0; #10;
a = 0; b = 0; sel = 1; #10;
a = 0; b = 1; sel = 0; #10;
a = 0; b = 1; sel = 1; #10;
a = 1; b = 0; sel = 0; #10;
a = 1; b = 0; sel = 1; #10;
a = 1; b = 1; sel = 0; #10;
a = 1; b = 1; sel = 1; #10;
$finish;
end
endmodule
56
//encoder_4to2 module
module encoder_4to2(input [3:0] data, output [1:0] y);assign y[0] =
data[1] | data[3] ;
assign y[1] = data[2] | data[3] ;endmodule
// Apply stimulus
initial begin
$monitor("%b | %b", data, y);
$display("Data | Y");
$display(" --------------------- ");
data = 4'b0000; #10; data =
4'b0001; #10;data = 4'b0010;
#10;data = 4'b0011; #10;data
= 4'b0100; #10; data =
4'b0101; #10;
57
data = 4'b0110; #10;data = 4'b0111; #10;data =
4'b1000; #10; data = 4'b1001; #10; data =
4'b1010; #10; data = 4'b1011; #10; data =
4'b1100; #10; data = 4'b1101; #10; data =
4'b1110; #10;data = 4'b1111; #10;
$finish; end endmodule
//decoder_2to4 module
module decoder_2to4(input [1:0] sel, output [3:0] y);assign y[0] = (~sel[0] & ~sel[1]);
assign y[1] = (sel[0] & ~sel[1]);
assign y[2] = (~sel[0] & sel[1]) ;
assign y[3] = (sel[0] & sel[1]) ;endmodule
wire [3:0] y;
58
// Instantiate the 1x2 Demultiplexer moduledemux_1x2 dut(a, sel, y0, y1);
//comparator_1bit module
module comparator_1bit(input a, b, output less, equal, greater);assign less = (a < b) ? 1'b1 : 1'b0;
assign equal = (a == b) ? 1'b1 : 1'b0;assign greater = (a > b) ? 1'b1
: 1'b0; endmodule
// Testbench for 1-bit Comparatormodule comparator_1bit_tb;
reg a, b;
wire equal, greater, less;
59
EXPERIMENT-4
always@(posedge clk)begin
if(rst) count<=0; else if(ud)
count<=count+1'b1;else
count<=count-1'b1;end
endmodule
reg clk,rst;
reg ud; // ud=1 for up count; ud=0 for down countwire [3:0]count;
counter_updown4bit dut(clk,rst,ud,count);initial
begin
clk<=0;rst<=0; end
always #5 clk<=~clk;initial
begin
$monitor("clk %b,rst %b,ud %b,count %0d",clk,rst,ud,count);@(posedge clk); rst<=1;
@(posedge clk); rst<=0;repeat(20)
begin
@(posedge clk); ud<=1;end
repeat(20)begin
@(posedge clk); ud<=0;end
$stop;end
endmodule
Waveforms:
60
EXPERIMENT-4
SEQUENTIAL CIRCUITS
AIM:
Write a Verilog HDL program in behavioral model for D,T and JK flip flops, shift registers and
counters.
APPARATUS:
1. Computer system
2. VIVADO software tool
SR-FF:
LOGIC SYMBOL:
SR-FF q
CLK
RST
S qb
R
Truth Table:
0 0 0 0 0 1
0 0 0 1 1 0
0 0 1 X 0 1
0 1 0 X 1 0
0 1 1 X Indeterminate
state
Behavioral Description:
end
Output Waveform:
Behavioral Description:
64
Verilog code for D-Flip Flop
65
Truth Table
Behavioral Description:
`timescale 1ns / 1ps
module jkff( input clk, input rst, input j, input k,
output reg q, output reg qb );
reg[1:0] jk;
always@(posedge clk)
begin if(rst==1'b1)
q=1'b0;
else begin jk={j,k};
case (jk)
2'b00: q=q;
2'b01: q=1'b0;
2'b10: q=1'b1;
2'b11:
q=~q;
default: q=1'bx;
endcase
end
qb=~q;
end
66
endmodule
67
Verilog code for D-Flip Flop
Verilog Code
69
EXPERIMENT-5
Verilog HDL program in structural and behavioral models for a) 8 bit asynchronous up-
down counter
module async_up_down_counter_structural(
input wire clk,
input wire rst,
input wire up,
input wire down,
output reg [7:0] out
);
endmodule
Behavioral
module async_up_down_counter_behavioral(
input wire clk,
input wire rst,
input wire up,
input wire down,
output reg [7:0] out
);
endmodule
module sync_up_down_counter_structural(
input wire clk,
input wire rst,
input wire up,
input wire down,
output reg [7:0] out
);
endmodule
module sync_up_down_counter_behavioral(
input wire clk,
input wire rst,
input wire up,
input wire down,
output reg [7:0] out
71
);
endmodule
Wave forms
72
EXPERIMENT-6
To write the Verilog code for 4-bit sequence detector through Mealy state
machines and obtain the simulation, synthesis results using Xilinx ISE tool.
APPARATUS:
3. Computer system
4. VIVADO software tool
BLOCK DIAGRAM
Verilog code
reg y;
reg [1:0] Prstate, Next_state;
always @(Prstate or x)
begin
case (Prstate)
s0: if (x)
begin
Next_state = s1;
y = 1'b0;
end
else
begin
Next_state = s0;
y = 1'b0;
end
s1: if (x)
begin
Next_state = s3;
y = 1'b0;
end
else
begin
Next_state = s0;
y = 1'b1;
end
s2: if (~x)
begin
Next_state = s0;
y = 1'b1;
end
else
begin
Next_state = s2;
y = 1'b0;
end
s3: if (x)
begin
Next_state = s2;
y = 1'b0;
end
else
begin
Next_state = s0;
y = 1'b1;
end
endcase
end
endmodule
TEST BENCH:
74
module FSM_Mealy_tb_v;
reg x;
reg clock;
reg reset;
wire y;
FSM_Mealy uut(
.y(y),
.x(x),
.clock(clock),
.reset(reset)
);
initial begin
x = 0; clock = 0; reset = 1;
#10 x = 0; reset = 0;
#10 x = 1;
#10 x = 0;
#10 x = 1;
#10 x = 1;
#10 x = 0; #100;
end
always #5 clock = ~clock;
endmodule
Waveform:
75
EXPERIMENT-7
Moore state machines
AIM:
To write the Verilog code for 4-bit sequence detector through Moore state
machines and obtain thesimulation, synthesis results using VIVADO tool.
APPARATUS:
1. Computer system
2. VIVADO software tool
BLOCK DIAGRAM
A Moore state machine in Verilog differs from a Mealy state machine in that its outputs depend
only on the current state. Here's an example of a Moore state machine that detects the '1011'
sequence.
Verilog Code:
module moore_sequence_detector (
76
);
// State enumeration
S1,
S2,
S3,
} state_t;
if (reset) begin
sequence_detected <= 0;
end
end
always @* begin
77
next_state = state;
case (state)
S0: begin
if (data_in) begin
next_state = S1;
next_state = S0;
end
end
S1: begin
if (data_in) begin
next_state = S2;
next_state = S0;
end
end
S2: begin
if (data_in) begin
next_state = S3;
next_state = S0;
end
end
78
S3: begin
if (!data_in) begin
next_state = S0;
next_state = S4;
end
end
S4: begin
79