SlideShare a Scribd company logo
2
Most read
5
Most read
13
Most read
1.Fulladder
----------------------------------------------
`timescale 1ns/1ps
module fulladder(a,b,cin,s,cout);
input a,b,cin;
output s,cout;
//wire w1,w2,w3,w4;
//xor(w1,a,b);
//xor(s,w1,cin);
//and(w2,a,b);
//and(w3,b,cin);
//and(w4,cin,a);
//or(cout,w1,w2,w3);
wire w1,w2,w3;
xor(s,a,b,cin);
and(w1,a,b);
and(w2,b,cin);
and(w3,cin,a);
or(cout,w1,w2,w3);
endmodule
--------------------------
testbench code:-
-----------------------------------------
`timescale 1ns/1ps
module test_fulladder();
reg a,b,cin;
wire s,cout;
fulladder DUT(.a(a),.b(b),.cin(cin),.s(s),.cout(cou
t));
initial begin
$monitor("a=%b b=%b cin=%b s=%b cout=%b",a,b,c
in,s,cout);
end
initial begin
a=1'b0;
b=1'b0;
cin=1'b0;
#6
a=1'b1;
b=1'b0;
cin=1'b0;
#6
a=1'b0;
b=1'b1;
cin=1'b1;
#6
a=1'b0;
b=1'b1;
cin=1'b0;
#6
a=1'b1;
b=1'b1;
cin=1'b1;
end
endmodule
---------------------------------------
2.Fulladder using half adders:
------------------------------
`timescale 1ns/1ps
module ha(a,b,s,c);
input a,b;
output reg s,c;
always@(*)
begin
s=a^b;
c=a&b;
end
endmodule
module fulladd(a,b,cin,s,cout);
input a,b,cin;
output s,cout;
wire s1,c1,c2;
ha h1(a,b,s1,c1);
ha h2(s1,cin,s,c2);
or(cout,c2,c1);
endmodule
test bench code:
----------------------
`timescale 1ns/1ps
module test();
reg a,b,cin;
wire s,cout;
fulladd h(a,b,cin,s,cout);
initial begin
$monitor("a=%b b=%b cin=%b s=%b cout=%b ",a,b,cin,
s,cout);
end
initial begin
a=1'b0;
b=1'b0;
cin=1'b1;
#5
a=1'b1;
b=1'b0;
cin=1'b1;
#5
a=1'b1;
b=1'b1;
cin=1'b1;
end
endmodule
-------------------------------
3.Ripple Carry Adder:-
---------------------------------------------------
-----
`timescale 1ns/1ps
module fa(a,b,cin,s,cout);
input a,b,cin;
output s,cout;
reg s,cout;
always@(a or b or cin)
begin
s=(a^b)^cin;
cout=(a&b)+(b&cin)+(a&cin);
end
endmodule
module rca(x,y,cin,s,cout);
input cin;
input[3:0]x,y;
output [3:0]s;
output cout;
wire[3:1]c;
fa stage0(x[0],y[0],cin,s[0],c[1]);
fa stage1(x[1],y[1],c[1],s[1],c[2]);
fa stage2(x[2],y[2],c[2],s[2],c[3]);
fa stage3(x[3],y[3],c[3],s[3],cout);
endmodule
~
---------------------------------------------------
---
testbench code:
`timescale 1ns/1ps
module test_rca();
reg cin;
reg[3:0]x,y;
wire cout;
wire[3:0]s;
rca pujari(x,y,cin,s,cout);
initial begin
$display("THE INPUT AND OUTPUTS ARE:");
$monitor("x=%b y=%b s=%b cout=%b",x,y,cin,s,cout
);
end
initial begin
cin=1'b0;
x=4'b0000;
y=4'b1010;
#5
x=4'b1111;
y=4'b1010;
#5
x=4'b0011;
y=4'b1110;
#5
x=4'b0001;
y=4'b0011;
end
endmodule
---------------------------------------------------
-------------
4.16x1 MUX using 4x1 MUX
-------------------------------------
`timescale 1ns/1ps
module mux16(w,s16,f);
input[0:15]w;
input[3:0]s16;
output f;
reg f;
always@(w or s16)
case(s16[3:2])
0:mux4(w[0:3],s16[1:0],f);
1:mux4(w[4:7],s16[1:0],f);
2:mux4(w[8:11],s16[1:0],f);
3:mux4(w[12:15],s16[1:0],f);
endcase
task mux4;
input [0:3]x;
input [1:0]s4;
output g;
reg g;
case(s4)
0:g=x[0];
1:g=x[1];
2:g=x[2];
3:g=x[3];
endcase
endtask
endmodule
---------------------------------------------------
----------
Test_bench code:
`timescale 1ns/1ps
module test();
reg [0:15]w;
reg[3:0]s16;
wire f;
mux16 shobhan(w,s16,f);
initial begin
$monitor("w=%b s16=%b f=%b",w,s16,f);
end
initial begin
w=16'b1011111110110001;
s16=4'b1100;
#9
w=16'b1010110110110001;
s16=4'b1101;
#9
w=16'b111110110110001;
s16=4'b0011;
#9
w=16'b101010110110111;
s16=4'b1111;
end
endmodule
---------------------------------------------
5.Ripple Carry Adder:-
---------------------------------------------------
-----
`timescale 1ns/1ps
module fa(a,b,cin,s,cout);
input a,b,cin;
output s,cout;
reg s,cout;
always@(a or b or cin)
begin
s=(a^b)^cin;
cout=(a&b)+(b&cin)+(a&cin);
end
endmodule
module rca(x,y,cin,s,cout);
input cin;
input[3:0]x,y;
output [3:0]s;
output cout;
wire[3:1]c;
fa stage0(x[0],y[0],cin,s[0],c[1]);
fa stage1(x[1],y[1],c[1],s[1],c[2]);
fa stage2(x[2],y[2],c[2],s[2],c[3]);
fa stage3(x[3],y[3],c[3],s[3],cout);
endmodule
~
---------------------------------------------------
---
testbench code:
`timescale 1ns/1ps
module test_rca();
reg cin;
reg[3:0]x,y;
wire cout;
wire[3:0]s;
rca pujari(x,y,cin,s,cout);
initial begin
$display("THE INPUT AND OUTPUTS ARE:");
$monitor("x=%b y=%b s=%b cout=%b",x,y,cin,s,cout
);
end
initial begin
cin=1'b0;
x=4'b0000;
y=4'b1010;
#5
x=4'b1111;
y=4'b1010;
#5
x=4'b0011;
y=4'b1110;
#5
x=4'b0001;
y=4'b0011;
end
endmodule
---------------------------------------------------
-------------
6.1).decoder:-
`timescale 1ns/1ps
module encoder(w,en,y);
input[1:0]w;
input en;
output [3:0]y;
wire p0,p1;
reg [3:0]y;
not(p0,w[0]);
not(p1,w[1]);
always@(w or en)
begin
if(en)
case(w)
2’b00: y<= 4’b1110;
2’b01: y<= 4’b1101;
2’b10: y<= 4’b1011;
2’b11: y<= 4’b0111;
endcase;
else
y=0;
end
endmodule
---------------------------------------------------
----------------
test bench:-
`timescale 1ns/1ps
module test();
reg [0:1]w;
reg en;
wire [0:3]y;
encoder shobhan(w,en,y);
initial begin
$monitor("w=%b en=%b y=%b",w,en,y);
end
initial begin
w=2'b00;
en=1'b1;
#5
w=2'b01;
en=1'b1;
#5
w=2'b10;
en=1'b1;
#5
w=2'b11;
en=1'b1;
#5
w=2'b10;
en=1'b0;
#5
w=2'b11;
en=1'b0;
end
endmodule
---------------------------------------------------
--------------
2).4x1 mux using 2x1
---------------------------------------------------
--------------
`timescale 1ns/1ps
module mux2(c,s,z);
input[0:3]c;
input[1:0]s;
output z;
reg z;
always@(c or s)
case(s[1])
0:mux(c[0:1],s[0],z);
1:mux(c[2:3],s[0],z);
endcase
task mux;
input[0:1]x;
input l;
output g;
reg g;
case(l)
0:g=x[0];
1:g=x[1];
endcase
endtask
endmodule
---------------------------------------------------
--------------
testbench code:-
`timescale 1ns/1ps
module test();
reg [0:3]i;
reg [1:0]s;
wire y;
mux2 shobhan(i,s,y);
initial begin
$monitor("i=%b s=%b y=%b",i,s,y);
end
initial begin
i=4'b1000;
s=2'b10;
#5
i=4'b1011;
s=2'b00;
#5
i=4'b1111;
s=2'b01;
#5
i=4'b1010;
s=2'b11;
end
endmodule
---------------------------------------------------
----------
8.Mealy State Machine:-
---------------------------------------------------
--------
`timescale 1ns/1ps
module mealy(i,z,clk,rst);
input i,clk,rst;
output z;
reg z;
reg[1:0]state,nextstate;
parameter s0=2'b00;
parameter s1=2'b01;
parameter s2=2'b10;
always@(posedge clk,posedge rst)
if(rst)
begin
state=s0;
z=0;
end
else begin
state=nextstate;
z=0;
end
always@(*)
case(state)
2'b00:
begin
if(i)
begin nextstate=s0;z=0;end
else
begin nextstate=s1;z=0;end
end
2'b01:begin
if(i) begin nextstate=s2;z=0;end
else begin nextstate=s1;z=0;end
end
2'b10:begin
if(i) begin nextstate=s0;z=0;end
else begin nextstate=s1;z=1;end
end
default :begin nextstate=s0;z=0;end
endcase
endmodule
-------------------------------------------------
`timescale 1ns/1ps
module test();
reg clk, rst, i;
wire z;
reg[15:0] seq;
integer j;
moore shobhan(i,z,clk,rst);
initial
begin`
clk = 0;
rst = 1;
seq = 16'b0101_1101_1101_0010;
#5 rst = 0;
for( j = 0; j <= 15; j = j + 1)
begin
i = seq[j];
#2 clk = 1;
#2 clk = 0;
$display("State =",shobhan.state,"Input = ",i,",Out
put= ", z);
end
test2;
end
task test2;
for( j = 0; j <= 15; j = j + 1)
begin
i = $random % 2;
#2 clk = 1;
#2 clk = 0;
$display("State =",shobhan.state,"Input =",i,",Out
put= ",z);
end
endtask
endmodule
---------------------------------------------------
--
9.Counter
---------------------------------------------------
--
module counter(
input clk,rst,enable,
output reg [3:0]counter_output
);
always@ (posedge clk)
begin
if( rst | counter_output==4'b1001)
counter_output <= 4'b0000;
else if(enable)
counter_output <= counter_output + 1;
else
counter_output <= 0;
end
endmodule
always
#50 clk= ~ clk;
initial begin
clk=0;
// Initialize Inputs
rst = 0;
enable = 0;
#100;
rst=0;
enable=1;
#100;
rst=0;
enable=1;
// Wait 100 ns for global reset to finish
#100;
end
endmodule

More Related Content

PDF
Communication Protocols (UART, SPI,I2C)
PDF
Verilog HDL- 2
PPT
PPTX
AVR programming - BASICS
PPT
Crash course in verilog
PDF
Verilog Tasks & Functions
PPTX
APB2SPI.pptx
PPT
Bidirectional Bus Modelling
Communication Protocols (UART, SPI,I2C)
Verilog HDL- 2
AVR programming - BASICS
Crash course in verilog
Verilog Tasks & Functions
APB2SPI.pptx
Bidirectional Bus Modelling

What's hot (20)

PDF
VERILOG CODE FOR Adder
PPT
system verilog
PPTX
Presentation1.pptx
PPT
Verilog Lecture4 2014
PPTX
Advanced Pipelining in ARM Processors.pptx
PPTX
Hardware View of Intel 8051
PPTX
Setup and hold time violation in flip-flops
PPTX
Linux I2C
DOCX
301378156 design-of-sram-in-verilog
PDF
VHDL- gate level modelling
PPT
L08 power amplifier (class a)
PDF
Session 9 advance_verification_features
PPT
Basics of Verilog.ppt
PDF
Verilog VHDL code Multiplexer and De Multiplexer
PPTX
Part-2: Mastering microcontroller with embedded driver development
DOC
All VLSI programs
PPTX
Presentasi E.M. Terapan (Horn Antenna)
PPTX
FULL CUSTOM, STANDARD CELLS - VLSI Design Styles.pptx
PPTX
timer counter (1).pptx
PPTX
I2C Protocol
VERILOG CODE FOR Adder
system verilog
Presentation1.pptx
Verilog Lecture4 2014
Advanced Pipelining in ARM Processors.pptx
Hardware View of Intel 8051
Setup and hold time violation in flip-flops
Linux I2C
301378156 design-of-sram-in-verilog
VHDL- gate level modelling
L08 power amplifier (class a)
Session 9 advance_verification_features
Basics of Verilog.ppt
Verilog VHDL code Multiplexer and De Multiplexer
Part-2: Mastering microcontroller with embedded driver development
All VLSI programs
Presentasi E.M. Terapan (Horn Antenna)
FULL CUSTOM, STANDARD CELLS - VLSI Design Styles.pptx
timer counter (1).pptx
I2C Protocol
Ad

Similar to Verilog codes and testbench codes for basic digital electronic circuits. (20)

DOCX
verilog code
DOCX
Vend with ff
PPTX
Sequential statements in Verilog (2).pptx
PPT
Verilogforlab
PDF
Digital Systems Design Using Verilog 1st Edition Roth Solutions Manual
PPTX
verilog_tutorial1.pptx
PPTX
Rishabh-Verilog_Examples_Nov_2019_VGS.pptx
PDF
Task i
PDF
Digital Systems Design Using Verilog 1st Edition Roth Solutions Manual
DOCX
Alu all shift register
PDF
Verilog_Overview.pdf
PPT
Structural_Modelling Design DetailsExplaination
DOCX
VERILOG CODE
PDF
An Example MIPS
PDF
Day2 Verilog HDL Basic
PDF
論文紹介 Hyperkernel: Push-Button Verification of an OS Kernel (SOSP’17)
PPTX
mod-4.pptx
PPT
PPTX
gate level modeling
PPT
Unit 3 - Styles of Modeling-1 for resource management techniques
verilog code
Vend with ff
Sequential statements in Verilog (2).pptx
Verilogforlab
Digital Systems Design Using Verilog 1st Edition Roth Solutions Manual
verilog_tutorial1.pptx
Rishabh-Verilog_Examples_Nov_2019_VGS.pptx
Task i
Digital Systems Design Using Verilog 1st Edition Roth Solutions Manual
Alu all shift register
Verilog_Overview.pdf
Structural_Modelling Design DetailsExplaination
VERILOG CODE
An Example MIPS
Day2 Verilog HDL Basic
論文紹介 Hyperkernel: Push-Button Verification of an OS Kernel (SOSP’17)
mod-4.pptx
gate level modeling
Unit 3 - Styles of Modeling-1 for resource management techniques
Ad

Recently uploaded (20)

PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
web development for engineering and engineering
PPTX
Internet of Things (IOT) - A guide to understanding
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
CH1 Production IntroductoryConcepts.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
web development for engineering and engineering
Internet of Things (IOT) - A guide to understanding
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
R24 SURVEYING LAB MANUAL for civil enggi
Model Code of Practice - Construction Work - 21102022 .pdf
Automation-in-Manufacturing-Chapter-Introduction.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...

Verilog codes and testbench codes for basic digital electronic circuits.

  • 1. 1.Fulladder ---------------------------------------------- `timescale 1ns/1ps module fulladder(a,b,cin,s,cout); input a,b,cin; output s,cout; //wire w1,w2,w3,w4; //xor(w1,a,b); //xor(s,w1,cin); //and(w2,a,b); //and(w3,b,cin); //and(w4,cin,a); //or(cout,w1,w2,w3); wire w1,w2,w3; xor(s,a,b,cin); and(w1,a,b); and(w2,b,cin); and(w3,cin,a); or(cout,w1,w2,w3); endmodule -------------------------- testbench code:- ----------------------------------------- `timescale 1ns/1ps module test_fulladder(); reg a,b,cin; wire s,cout; fulladder DUT(.a(a),.b(b),.cin(cin),.s(s),.cout(cou t)); initial begin $monitor("a=%b b=%b cin=%b s=%b cout=%b",a,b,c in,s,cout); end initial begin a=1'b0; b=1'b0; cin=1'b0; #6 a=1'b1; b=1'b0;
  • 2. cin=1'b0; #6 a=1'b0; b=1'b1; cin=1'b1; #6 a=1'b0; b=1'b1; cin=1'b0; #6 a=1'b1; b=1'b1; cin=1'b1; end endmodule --------------------------------------- 2.Fulladder using half adders: ------------------------------ `timescale 1ns/1ps module ha(a,b,s,c); input a,b; output reg s,c; always@(*) begin s=a^b; c=a&b; end endmodule module fulladd(a,b,cin,s,cout); input a,b,cin; output s,cout; wire s1,c1,c2; ha h1(a,b,s1,c1); ha h2(s1,cin,s,c2); or(cout,c2,c1); endmodule
  • 3. test bench code: ---------------------- `timescale 1ns/1ps module test(); reg a,b,cin; wire s,cout; fulladd h(a,b,cin,s,cout); initial begin $monitor("a=%b b=%b cin=%b s=%b cout=%b ",a,b,cin, s,cout); end initial begin a=1'b0; b=1'b0; cin=1'b1; #5 a=1'b1; b=1'b0; cin=1'b1; #5 a=1'b1; b=1'b1; cin=1'b1; end endmodule ------------------------------- 3.Ripple Carry Adder:- --------------------------------------------------- ----- `timescale 1ns/1ps module fa(a,b,cin,s,cout); input a,b,cin; output s,cout; reg s,cout; always@(a or b or cin) begin s=(a^b)^cin; cout=(a&b)+(b&cin)+(a&cin);
  • 4. end endmodule module rca(x,y,cin,s,cout); input cin; input[3:0]x,y; output [3:0]s; output cout; wire[3:1]c; fa stage0(x[0],y[0],cin,s[0],c[1]); fa stage1(x[1],y[1],c[1],s[1],c[2]); fa stage2(x[2],y[2],c[2],s[2],c[3]); fa stage3(x[3],y[3],c[3],s[3],cout); endmodule ~ --------------------------------------------------- --- testbench code: `timescale 1ns/1ps module test_rca(); reg cin; reg[3:0]x,y; wire cout; wire[3:0]s; rca pujari(x,y,cin,s,cout); initial begin $display("THE INPUT AND OUTPUTS ARE:"); $monitor("x=%b y=%b s=%b cout=%b",x,y,cin,s,cout ); end initial begin cin=1'b0; x=4'b0000; y=4'b1010; #5 x=4'b1111; y=4'b1010; #5 x=4'b0011; y=4'b1110;
  • 5. #5 x=4'b0001; y=4'b0011; end endmodule --------------------------------------------------- ------------- 4.16x1 MUX using 4x1 MUX ------------------------------------- `timescale 1ns/1ps module mux16(w,s16,f); input[0:15]w; input[3:0]s16; output f; reg f; always@(w or s16) case(s16[3:2]) 0:mux4(w[0:3],s16[1:0],f); 1:mux4(w[4:7],s16[1:0],f); 2:mux4(w[8:11],s16[1:0],f); 3:mux4(w[12:15],s16[1:0],f); endcase task mux4; input [0:3]x; input [1:0]s4; output g; reg g; case(s4) 0:g=x[0]; 1:g=x[1]; 2:g=x[2]; 3:g=x[3]; endcase endtask endmodule --------------------------------------------------- ----------
  • 6. Test_bench code: `timescale 1ns/1ps module test(); reg [0:15]w; reg[3:0]s16; wire f; mux16 shobhan(w,s16,f); initial begin $monitor("w=%b s16=%b f=%b",w,s16,f); end initial begin w=16'b1011111110110001; s16=4'b1100; #9 w=16'b1010110110110001; s16=4'b1101; #9 w=16'b111110110110001; s16=4'b0011; #9 w=16'b101010110110111; s16=4'b1111; end endmodule --------------------------------------------- 5.Ripple Carry Adder:- --------------------------------------------------- ----- `timescale 1ns/1ps module fa(a,b,cin,s,cout); input a,b,cin; output s,cout; reg s,cout; always@(a or b or cin) begin s=(a^b)^cin; cout=(a&b)+(b&cin)+(a&cin);
  • 7. end endmodule module rca(x,y,cin,s,cout); input cin; input[3:0]x,y; output [3:0]s; output cout; wire[3:1]c; fa stage0(x[0],y[0],cin,s[0],c[1]); fa stage1(x[1],y[1],c[1],s[1],c[2]); fa stage2(x[2],y[2],c[2],s[2],c[3]); fa stage3(x[3],y[3],c[3],s[3],cout); endmodule ~ --------------------------------------------------- --- testbench code: `timescale 1ns/1ps module test_rca(); reg cin; reg[3:0]x,y; wire cout; wire[3:0]s; rca pujari(x,y,cin,s,cout); initial begin $display("THE INPUT AND OUTPUTS ARE:"); $monitor("x=%b y=%b s=%b cout=%b",x,y,cin,s,cout ); end initial begin cin=1'b0; x=4'b0000; y=4'b1010; #5 x=4'b1111; y=4'b1010; #5 x=4'b0011; y=4'b1110;
  • 8. #5 x=4'b0001; y=4'b0011; end endmodule --------------------------------------------------- ------------- 6.1).decoder:- `timescale 1ns/1ps module encoder(w,en,y); input[1:0]w; input en; output [3:0]y; wire p0,p1; reg [3:0]y; not(p0,w[0]); not(p1,w[1]); always@(w or en) begin if(en) case(w) 2’b00: y<= 4’b1110; 2’b01: y<= 4’b1101; 2’b10: y<= 4’b1011; 2’b11: y<= 4’b0111; endcase; else y=0; end endmodule ---------------------------------------------------
  • 9. ---------------- test bench:- `timescale 1ns/1ps module test(); reg [0:1]w; reg en; wire [0:3]y; encoder shobhan(w,en,y); initial begin $monitor("w=%b en=%b y=%b",w,en,y); end initial begin w=2'b00; en=1'b1; #5 w=2'b01; en=1'b1; #5 w=2'b10; en=1'b1; #5 w=2'b11; en=1'b1; #5 w=2'b10; en=1'b0; #5 w=2'b11; en=1'b0; end endmodule --------------------------------------------------- -------------- 2).4x1 mux using 2x1 --------------------------------------------------- -------------- `timescale 1ns/1ps module mux2(c,s,z); input[0:3]c; input[1:0]s;
  • 10. output z; reg z; always@(c or s) case(s[1]) 0:mux(c[0:1],s[0],z); 1:mux(c[2:3],s[0],z); endcase task mux; input[0:1]x; input l; output g; reg g; case(l) 0:g=x[0]; 1:g=x[1]; endcase endtask endmodule --------------------------------------------------- -------------- testbench code:- `timescale 1ns/1ps module test(); reg [0:3]i; reg [1:0]s; wire y; mux2 shobhan(i,s,y); initial begin $monitor("i=%b s=%b y=%b",i,s,y); end initial begin i=4'b1000; s=2'b10; #5 i=4'b1011; s=2'b00; #5
  • 11. i=4'b1111; s=2'b01; #5 i=4'b1010; s=2'b11; end endmodule --------------------------------------------------- ---------- 8.Mealy State Machine:- --------------------------------------------------- -------- `timescale 1ns/1ps module mealy(i,z,clk,rst); input i,clk,rst; output z; reg z; reg[1:0]state,nextstate; parameter s0=2'b00; parameter s1=2'b01; parameter s2=2'b10; always@(posedge clk,posedge rst) if(rst) begin state=s0; z=0; end else begin state=nextstate; z=0; end always@(*) case(state) 2'b00: begin if(i) begin nextstate=s0;z=0;end else
  • 12. begin nextstate=s1;z=0;end end 2'b01:begin if(i) begin nextstate=s2;z=0;end else begin nextstate=s1;z=0;end end 2'b10:begin if(i) begin nextstate=s0;z=0;end else begin nextstate=s1;z=1;end end default :begin nextstate=s0;z=0;end endcase endmodule ------------------------------------------------- `timescale 1ns/1ps module test(); reg clk, rst, i; wire z; reg[15:0] seq; integer j; moore shobhan(i,z,clk,rst); initial begin` clk = 0; rst = 1; seq = 16'b0101_1101_1101_0010; #5 rst = 0; for( j = 0; j <= 15; j = j + 1) begin i = seq[j]; #2 clk = 1; #2 clk = 0; $display("State =",shobhan.state,"Input = ",i,",Out put= ", z); end test2; end task test2; for( j = 0; j <= 15; j = j + 1) begin i = $random % 2;
  • 13. #2 clk = 1; #2 clk = 0; $display("State =",shobhan.state,"Input =",i,",Out put= ",z); end endtask endmodule --------------------------------------------------- -- 9.Counter --------------------------------------------------- -- module counter( input clk,rst,enable, output reg [3:0]counter_output ); always@ (posedge clk) begin if( rst | counter_output==4'b1001) counter_output <= 4'b0000; else if(enable) counter_output <= counter_output + 1; else counter_output <= 0; end endmodule always #50 clk= ~ clk; initial begin clk=0; // Initialize Inputs rst = 0; enable = 0; #100; rst=0; enable=1; #100;
  • 14. rst=0; enable=1; // Wait 100 ns for global reset to finish #100; end endmodule