Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

What are the fundamentals of Verilog programs?-Part 1

By Ashutosh Bhatt March 17, 2025

In the previous tutorial, we covered the fundamentals of Verilog, VLSI design flow, and various Verilog modeling styles, including modules and data types. Now, it’s time to dive into the Verilog programming.

In this tutorial, we’ll present basic Verilog programs for popular digital circuits. Before we begin, it’s worth reviewing the prerequisites for Verilog programming, which is in the introduction to digital electronics and digital circuit design. It’s important to ensure adequate knowledge of Boolean algebra, logic gates, combinational, sequential logic circuits, etc. So, be sure to refresh your digital electronics fundamentals.

Below, we explore the Verilog program for a given digital circuit using two to three modeling styles. This approach will help you gain a comprehensive understanding of how to write Verilog programs using various modeling techniques.

The half-adder

Gate level modeling:

module half_adder(a,b,s,c);
  input a,b;
  output s,c;
         xor(s,a,b);
         and(c,a,b);
endmodule

Dataflow modeling:

module half_adder(a,b,s,c);
  input a,b;
  output s,c;
         assign s = a ^ b;
         assign c = a & b;
endmodule

Behavior modeling:

 module half_adder(a,b,s,c);
  input a,b;
  output s,c;
  always @(a, b)
   begin
                c = a & b;
               s = a ^ b;
   end
 endmodule

D-Morgan’s theorems

Gate-level modeling:

module d_morgan(a,b,op1,op2,op3,op4);
input a,b;
output op1,op2,op3,op4;
wire a_bar,b_bar;
not(a_bar,a);
not(b_bar,b);
nand(op1,a,b);
or(op2,a_bar,b_bar);
nor(op3,a,b);
and(op4,a_bar,b_bar);
endmodule 

Dataflow modeling: 

module d_morgan(a,b,op1,op2,op3,op4);
input a,b;
output op1,op2,op3,op4;

assign op1 = ~(a & b);
assign op2 = ~a | ~b;
assign op3 = ~(a | b);
assign op4 = ~a & ~b;
endmodule

Behavior modeling:

module d_morgan(a,b,op1,op2,op3,op4);
input a,b;
output op1,op2,op3,op4;
always @(a, b)
begin
op1 = ~(a & b);
op2 = ~a | ~b;
op3 = ~(a | b);
op4 = ~a & ~b;
end
endmodule 

The parity generator

Gate-level modeling:

module parity(p,d);
output p;
input [3:0] d;
wire t1,t2;
xor(t1,d[0],d[1]);
xor(t2,d[2],t1);
xor(p,d[3],t2);
endmodule

Dataflow modeling:

module parity(p,d);
output p;
input [3:0] d;
wire t1,t2;
assign t1 = d[0] ^ d[1];
assign t2 = d[2] ^ t1;
assign p = d[3] ^ t2;
endmodule

Behavioral modeling:

module parity(p,d);
output p;
input [3:0] d;
wire t1,t2;
always @(d)
begin
t1 = d[0] ^ d[1];
t2 = d[2] ^ t1;
p = d[3] ^ t2;

end
endmodule

The 2 to 4 decoder

Gate-level modeling:

module decoder_2to4(a0,a1,d0,d1,d2,d3 );
input a0,a1;
output d0,d1,d2,d3;
wire a0_bar,a1_bar;
not(a0_bar,a0);
not(a1_bar,a1);
and(d0,a0_bar,a1_bar)
and(d1,a0,a1_bar)
and(d2,a0_bar,a1),
and(d3,a0,a1);
endmodule

Dataflow modeling:

module decoder_2to4(a0,a1,d0,d1,d2,d3 );
input a0,a1;
output d0,d1,d2,d3;
assign d0 = ~a0 & ~a1;
assign d1 = a0 & ~a1;
assign d2 = ~a0 & a1;
assign d3 = a0 & a1;
endmodule

Behavioral modeling:

module decoder_2to4(a0,a1,d0,d1,d2,d3 );
input a0,a1;
output d0,d1,d2,d3;
always @(a0,a1)
begin
d0 = ~a0 & ~a1;
d1 = a0 & ~a1;
d2 = ~a0 & a1;
d3 = a0 & a1;
end
endmodule

The SR flip flop

Dataflow modeling:

module srff_gate(q, qbar, s, r, clk);
input s,r,clk;
output q, qbar;
wire nand1_out;
wire nand2_out;
nand (nand1_out,clk,s);
nand (nand2_out,clk,r);
nand (q,nand1_out,qbar);
nand (qbar,nand2_out,q);
endmodule

Behavioral modeling:

module srff_behave(s,r,clk, q, qbar);
input s,r,clk;
output reg q, qbar;
always@(posedge clk)
begin

if(s == 1)
begin
q = 1;
qbar = 0;
end
else if(r == 1)
begin
q = 0;
qbar =1;
end
else if(s == 0 & r == 0)
begin
q <= q;
qbar <= qbar;
end
end
endmodule

After reviewing these programs, you should be familiar with Verilog programming and its different modeling styles. In the next tutorial, we’ll begin exploring the simulation and verification of Verilog programs for digital circuits.

 

You may also like:


  • What is Verilog, its features, and design flow?- Part 2

  • How to design a digital circuit using VHDL

  • What are the top tools for developing embedded software?

  • What is electronic design automation?

  • Introduction to VHDL & Verilog – DE Part 9

Filed Under: Tutorials
Tagged With: circuits, modeling, tutorial, verilog
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Reducing "shoot-through" in offline Full Bridge SMPS?
  • High Side current sensing
  • How to simulate power electronics converter in PSpice?
  • Voltage mode pushpull is a nonsense SMPS?
  • Layout IRN reduction in Comparator

RSS Electro-Tech-Online.com Discussions

  • Back to the old BASIC days
  • Parts required for a personal project
  • PIC KIT 3 not able to program dsPIC
  • Failure of polypropylene motor-run capacitors
  • Siemens large industrial PLC parts

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe