SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (20)

Verilog Test Bench
Verilog Test BenchVerilog Test Bench
Verilog Test Bench
Dr.YNM
 
UNSIGNED AND SIGNED BINARY DIVISION
UNSIGNED AND SIGNED BINARY DIVISIONUNSIGNED AND SIGNED BINARY DIVISION
UNSIGNED AND SIGNED BINARY DIVISION
ciyamala kushbu
 
Combinational Logic Circuits
Combinational Logic CircuitsCombinational Logic Circuits
Combinational Logic Circuits
Prof. Swapnil V. Kaware
 
MICROCONTROLLER 8051- Architecture & Pin Configuration
MICROCONTROLLER 8051- Architecture & Pin Configuration MICROCONTROLLER 8051- Architecture & Pin Configuration
MICROCONTROLLER 8051- Architecture & Pin Configuration
AKHIL MADANKAR
 
USART
USARTUSART
USART
Islam Samir
 
Verilog VHDL code Multiplexer and De Multiplexer
Verilog VHDL code Multiplexer and De Multiplexer Verilog VHDL code Multiplexer and De Multiplexer
Verilog VHDL code Multiplexer and De Multiplexer
Bharti Airtel Ltd.
 
Flipflop
FlipflopFlipflop
Flipflop
sohamdodia27
 
Verilog code for decoder
Verilog code for decoderVerilog code for decoder
Verilog code for decoder
Rakesh kumar jha
 
Chapter 5 counter
Chapter 5 counterChapter 5 counter
Chapter 5 counter
CT Sabariah Salihin
 
Challenges in Using UVM at SoC Level
Challenges in Using UVM at SoC LevelChallenges in Using UVM at SoC Level
Challenges in Using UVM at SoC Level
DVClub
 
Lowpass RC circuit
Lowpass RC circuitLowpass RC circuit
Lowpass RC circuit
m balaji
 
Verilog lab manual (ECAD and VLSI Lab)
Verilog lab manual (ECAD and VLSI Lab)Verilog lab manual (ECAD and VLSI Lab)
Verilog lab manual (ECAD and VLSI Lab)
Dr. Swaminathan Kathirvel
 
AMBA 3 APB Protocol
AMBA 3 APB ProtocolAMBA 3 APB Protocol
AMBA 3 APB Protocol
Swetha GSM
 
Lecture 2 verilog
Lecture 2   verilogLecture 2   verilog
Lecture 2 verilog
venravi10
 
Verilog HDL CODES
Verilog HDL CODES Verilog HDL CODES
Verilog HDL CODES
OmkarDarekar6
 
Electromagnetic.pdf
Electromagnetic.pdfElectromagnetic.pdf
Electromagnetic.pdf
ManishKumawat77
 
Pcm transmitter and receiver
Pcm transmitter and receiverPcm transmitter and receiver
Pcm transmitter and receiver
BPrabhaPalani
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutine
milandhara
 
8085 interrupts
8085 interrupts8085 interrupts
8085 interrupts
deval patel
 
DIgital clock using verilog
DIgital clock using verilog DIgital clock using verilog
DIgital clock using verilog
Abhishek Sainkar
 
Verilog Test Bench
Verilog Test BenchVerilog Test Bench
Verilog Test Bench
Dr.YNM
 
UNSIGNED AND SIGNED BINARY DIVISION
UNSIGNED AND SIGNED BINARY DIVISIONUNSIGNED AND SIGNED BINARY DIVISION
UNSIGNED AND SIGNED BINARY DIVISION
ciyamala kushbu
 
MICROCONTROLLER 8051- Architecture & Pin Configuration
MICROCONTROLLER 8051- Architecture & Pin Configuration MICROCONTROLLER 8051- Architecture & Pin Configuration
MICROCONTROLLER 8051- Architecture & Pin Configuration
AKHIL MADANKAR
 
Verilog VHDL code Multiplexer and De Multiplexer
Verilog VHDL code Multiplexer and De Multiplexer Verilog VHDL code Multiplexer and De Multiplexer
Verilog VHDL code Multiplexer and De Multiplexer
Bharti Airtel Ltd.
 
Challenges in Using UVM at SoC Level
Challenges in Using UVM at SoC LevelChallenges in Using UVM at SoC Level
Challenges in Using UVM at SoC Level
DVClub
 
Lowpass RC circuit
Lowpass RC circuitLowpass RC circuit
Lowpass RC circuit
m balaji
 
AMBA 3 APB Protocol
AMBA 3 APB ProtocolAMBA 3 APB Protocol
AMBA 3 APB Protocol
Swetha GSM
 
Lecture 2 verilog
Lecture 2   verilogLecture 2   verilog
Lecture 2 verilog
venravi10
 
Pcm transmitter and receiver
Pcm transmitter and receiverPcm transmitter and receiver
Pcm transmitter and receiver
BPrabhaPalani
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutine
milandhara
 
DIgital clock using verilog
DIgital clock using verilog DIgital clock using verilog
DIgital clock using verilog
Abhishek Sainkar
 

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

Switch level modeling
Switch level modelingSwitch level modeling
Switch level modeling
Devi Pradeep Podugu
 
Q 1
Q 1Q 1
Q 1
rahulbarde420
 
gate level modeling
gate level modelinggate level modeling
gate level modeling
VandanaBR2
 
Simulation example with matlab
Simulation example with matlabSimulation example with matlab
Simulation example with matlab
Roma Larumbia
 
mod-4.pptx
mod-4.pptxmod-4.pptx
mod-4.pptx
MaheshRgk
 
Unit 3 - Styles of Modeling-1 for resource management techniques
Unit 3 - Styles of Modeling-1 for resource management techniquesUnit 3 - Styles of Modeling-1 for resource management techniques
Unit 3 - Styles of Modeling-1 for resource management techniques
MrFanatic1
 
Loopback.vhd
Loopback.vhdLoopback.vhd
Loopback.vhd
sachindb9
 
Per unit systems in power systems
Per unit systems in power systemsPer unit systems in power systems
Per unit systems in power systems
anoopeluvathingal
 
gate level modeling
gate level modelinggate level modeling
gate level modeling
VandanaBR2
 
Simulation example with matlab
Simulation example with matlabSimulation example with matlab
Simulation example with matlab
Roma Larumbia
 
Unit 3 - Styles of Modeling-1 for resource management techniques
Unit 3 - Styles of Modeling-1 for resource management techniquesUnit 3 - Styles of Modeling-1 for resource management techniques
Unit 3 - Styles of Modeling-1 for resource management techniques
MrFanatic1
 
Loopback.vhd
Loopback.vhdLoopback.vhd
Loopback.vhd
sachindb9
 
Per unit systems in power systems
Per unit systems in power systemsPer unit systems in power systems
Per unit systems in power systems
anoopeluvathingal
 
Ad

Recently uploaded (20)

2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
DED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedungDED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedung
nabilarizqifadhilah1
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
Reflections on Morality, Philosophy, and History
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
Guru Nanak Technical Institutions
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Journal of Soft Computing in Civil Engineering
 
Working with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to ImplementationWorking with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to Implementation
Alabama Transportation Assistance Program
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
DED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedungDED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedung
nabilarizqifadhilah1
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Ad

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
  翻译: