Lect 7: Verilog Behavioral model for Absolute BeginnersDr.YNM
The document discusses Verilog behavioral modeling, which provides the highest level of abstraction for designing complex systems through procedural statements. It describes two main procedural blocks - the always block and initial block. The always block uses procedural assignments to continuously update variables, while the initial block executes stimulus code once at startup for simulation testing. Several procedural statements like if/else, case, and forever are also covered.
Verilog Tutorial - Verilog HDL Tutorial with ExamplesE2MATRIX
E2MATRIX Research Lab
Opp Phagwara Bus Stand, Backside Axis Bank,
Parmar Complex, Phagwara Punjab (India).
Contact : +91 9041262727
web: www.e2matrix.com -- email: support@e2matrix.com
Simulation tools typically accept full set of Verilog language constructs
Some language constructs and their use in a Verilog description make simulation efficient and are ignored by synthesis tools
Synthesis tools typically accept only a subset of the full Verilog language constructs
In this presentation, Verilog language constructs not supported in Synopsys FPGA Express are in red italics
There are other restrictions not detailed here, see [2].
The Module Concept
Basic design unit
Modules are:
Declared
Instantiated
Modules declarations cannot be nested
Hardware description languages (HDLs) allow designers to describe digital systems at different levels of abstraction in a textual format. The two most commonly used HDLs are Verilog and VHDL. Verilog is commonly used in the US, while VHDL is more popular in Europe. HDLs enable simulation of designs before fabrication to verify functionality. Digital designs can be modeled at the gate level, data flow level, or behavioral level in Verilog. Verilog code consists of a design module and test bench module to stimulate inputs and observe outputs.
This document provides an introduction to Verilog, a hardware description language (HDL). It describes the main purposes of HDLs as allowing designers to describe circuits at both the algorithmic and gate levels, enabling simulation and synthesis. The document then discusses some Verilog basics, including modules as building blocks, ports, parameters, variables, instantiation, and structural vs procedural code. It provides examples of module declarations and typical module components.
This document discusses blocking and non-blocking assignments in Verilog. Blocking assignments execute sequentially, while non-blocking assignments are scheduled for the end of the simulation cycle. The document provides examples of always blocks and initial blocks using both blocking and non-blocking assignments. It demonstrates that blocking assignments update values immediately, while non-blocking assignments update values at the end of the block.
This document provides information about Verilog, a hardware description language used for designing digital circuits. It discusses what Verilog is, why it is used, how it was developed, its structure and syntax. Key points covered include:
- Verilog is a hardware description language used for designing digital circuits at different levels of abstraction.
- It allows designers to describe designs behaviorally or at lower levels like gate and switch levels.
- Verilog provides a software platform for designers to express their designs using behavioral constructs before being synthesized into hardware.
- It was introduced in 1985 and became an open standard in 1990 to promote broader adoption.
- The document reviews Verilog syntax, variables, data types,
Lect 7: Verilog Behavioral model for Absolute BeginnersDr.YNM
The document discusses Verilog behavioral modeling, which provides the highest level of abstraction for designing complex systems through procedural statements. It describes two main procedural blocks - the always block and initial block. The always block uses procedural assignments to continuously update variables, while the initial block executes stimulus code once at startup for simulation testing. Several procedural statements like if/else, case, and forever are also covered.
Verilog Tutorial - Verilog HDL Tutorial with ExamplesE2MATRIX
E2MATRIX Research Lab
Opp Phagwara Bus Stand, Backside Axis Bank,
Parmar Complex, Phagwara Punjab (India).
Contact : +91 9041262727
web: www.e2matrix.com -- email: support@e2matrix.com
Simulation tools typically accept full set of Verilog language constructs
Some language constructs and their use in a Verilog description make simulation efficient and are ignored by synthesis tools
Synthesis tools typically accept only a subset of the full Verilog language constructs
In this presentation, Verilog language constructs not supported in Synopsys FPGA Express are in red italics
There are other restrictions not detailed here, see [2].
The Module Concept
Basic design unit
Modules are:
Declared
Instantiated
Modules declarations cannot be nested
Hardware description languages (HDLs) allow designers to describe digital systems at different levels of abstraction in a textual format. The two most commonly used HDLs are Verilog and VHDL. Verilog is commonly used in the US, while VHDL is more popular in Europe. HDLs enable simulation of designs before fabrication to verify functionality. Digital designs can be modeled at the gate level, data flow level, or behavioral level in Verilog. Verilog code consists of a design module and test bench module to stimulate inputs and observe outputs.
This document provides an introduction to Verilog, a hardware description language (HDL). It describes the main purposes of HDLs as allowing designers to describe circuits at both the algorithmic and gate levels, enabling simulation and synthesis. The document then discusses some Verilog basics, including modules as building blocks, ports, parameters, variables, instantiation, and structural vs procedural code. It provides examples of module declarations and typical module components.
This document discusses blocking and non-blocking assignments in Verilog. Blocking assignments execute sequentially, while non-blocking assignments are scheduled for the end of the simulation cycle. The document provides examples of always blocks and initial blocks using both blocking and non-blocking assignments. It demonstrates that blocking assignments update values immediately, while non-blocking assignments update values at the end of the block.
This document provides information about Verilog, a hardware description language used for designing digital circuits. It discusses what Verilog is, why it is used, how it was developed, its structure and syntax. Key points covered include:
- Verilog is a hardware description language used for designing digital circuits at different levels of abstraction.
- It allows designers to describe designs behaviorally or at lower levels like gate and switch levels.
- Verilog provides a software platform for designers to express their designs using behavioral constructs before being synthesized into hardware.
- It was introduced in 1985 and became an open standard in 1990 to promote broader adoption.
- The document reviews Verilog syntax, variables, data types,
This document describes gate level modeling in Verilog. It discusses gate types like AND, OR, and NOT gates that can be used as primitives. It describes how to instantiate gates and provides examples of instantiating gates like NAND and AND gates. It also describes structural modeling of circuits like a 2-input multiplexer, full adder, D latch, and master-slave JK flip-flop using gate level primitives.
The document contains Verilog code for half adders and full adders. It provides two implementations for each: a half adder is implemented using XOR and AND gates to calculate the sum and carry outputs from two input bits, and a full adder uses additional gates to calculate the sum and carry from three input bits.
This document discusses behavioral modeling in VHDL. It covers different VHDL design styles including behavioral, dataflow, and structural. Behavioral modeling uses sequential statements inside processes to model functionality. Key concepts covered include processes with and without sensitivity lists, concurrent vs sequential execution, if/case statements, loops, and wait statements. An example of a behavioral model for a full adder is presented using two processes.
This document discusses Verilog data types. There are two main groups: nets and variables. Nets like wire are used for connections and have a default value of Z. Variables like reg store values and have a default of X. Important net types are wire, tri, and trireg; wire is used for single-driver nets while tri is for multiple drivers. Variables include reg for storage in procedural blocks, integer for integers, and real for real numbers.
This document provides an overview of System Verilog concepts including simulation and synthesis, modules and primitives, styles, data types, operators, and more. Key points covered include the module concept as the basic design unit, module declaration syntax, module instantiation, different styles like structural, RTL/dataflow and behavioral, data types for nets and registers, number representation formats, and basic Verilog operators. The document serves as a tutorial introduction to essential System Verilog language constructs.
The document contains Verilog code for half adders and full adders. It provides two implementations for each: a half adder is implemented using either XOR and AND gates, or XOR and AND modules; a full adder is implemented using XOR, AND and OR gates arranged in a specific way to calculate the sum and carry outputs, or using XOR, AND and OR modules and a wire to decompose the calculation into steps.
Delays in Verilog allow modeling of timing aspects like propagation delays. There are different types of delays depending on the design approach - gate level modeling uses rise, fall, and turn-off delays while dataflow modeling uses assignment delays on nets. Behavioral modeling supports regular delays before assignments, intra-assignment delays after the equals sign, and zero delays to ensure last execution. Sequential and parallel blocks also control statement ordering.
This document discusses switch level modeling in Verilog. It describes different types of transistor switches that can be used as primitives in Verilog, including nmos, pmos, rnmos, rpmos, and cmos switches. It also covers bidirectional switches like tran, tranif1, and examples of how to use the switches to model basic logic gates and memory cells like a RAM cell. Time delays can be specified for switches. Switch level modeling allows designing circuits using transistors directly in Verilog.
Task and Function is the basic component of a programming language. Even on hardware Verification , those task and function is used. Task ans function provides a short way to repeatedly use the same block of code many times, This presentation gives you the basic information about Task and Function in Verilog. For more information on this, kindly contact us.
The document describes the design and simulation of basic logic gates and a 2-to-4 decoder using Verilog HDL. It includes the block diagrams, truth tables, and Verilog code for AND, OR, NAND, NOR, XOR, XNOR and NOT gates. Testbenches are provided to simulate and verify the gate designs. The 2-to-4 decoder section provides the block diagram, theory of operation, and Verilog code using dataflow, behavioral and structural modeling styles. A testbench is also included to simulate the 2-to-4 decoder design.
Those slides describe digital design using Verilog HDL,
starting with Design methodologies for any digital circuit then difference between s/w (C/C++) and H/w (Verilog) and the most important constructs that let us start hardware design using Verilog HDL.
Tasks and functions allow designers to abstract commonly used Verilog code into reusable routines. Tasks can contain timing constructs and pass multiple values through input, output, and inout arguments. Functions must not contain timing constructs and return a single value. Tasks are similar to subroutines while functions are similar to functions in other languages like FORTRAN. Automatic tasks make tasks re-entrant to avoid issues with concurrent calls operating on shared variables.
The document discusses different data types in Verilog including nets, registers, vectors, real numbers, parameters, arrays, and integers. It explains that nets like wire are used to model physical connections and do not store values while registers like reg can store values. Vectors allow declaring registers and nets with multiple bits. Parameters represent symbolic constant values. Arrays allow declaring registers with multiple elements. Integers are for non-hardware variables like loop indices.
This document provides an introduction to Verilog HDL including:
- An overview of Verilog keywords, data types, abstraction levels, and design methodology.
- Details on the history of Verilog including its development over time and transitions to newer standards.
- Explanations of key Verilog concepts like modules, ports, instantiation, stimuli, and lexical conventions.
Modules are the basic building blocks, ports define module interfaces, and instantiation replicates modules. Stimuli provide test inputs and lexical conventions cover syntax rules.
This document describes the design and implementation of a universal shift register (USR) in Verilog. It includes:
1) A block diagram and description of a USR that can perform shift left, shift right, and parallel load operations using D flip-flops and 4-to-1 multiplexers.
2) The Verilog code for the USR module using D flip-flop and 4-to-1 multiplexer submodules.
3) The test bench and simulation results verifying the USR functionality.
This document provides an overview of hardware description language (HDL) and VHDL. It begins with an introduction to HDLs and why they are needed to model digital hardware. It then presents an example VHDL code for an even parity detector circuit to demonstrate basic VHDL concepts like entities, architectures, signals, and concurrent statements. Finally, it discusses how VHDL fits into the digital design flow from coding to simulation to synthesis.
System Verilog introduces several new control flow constructs compared to Verilog, including unique if, priority if, foreach loops, and enhanced for loops. It also adds tasks and functions with arguments that can be passed by value, reference, or name. System Verilog defines two types of blocks - sequential blocks that execute statements sequentially and parallel blocks like fork-join that execute statements concurrently. It introduces various timing controls like delays, events, and wait statements.
The document discusses modules and ports in Verilog. It describes that a module defines distinct parts including module name, port list, port declarations, and optional parameters. Ports provide the interface for a module to communicate with its environment. There are two methods for connecting ports to external signals - by ordered list where signals must appear in the same order as ports, and by name where the order does not matter as long as port names match. Hierarchical names provide unique names for every identifier by denoting the design hierarchy with identifiers separated by periods.
Hardware Description Language (HDL) is used to describe digital systems in a textual format similar to a programming language. HDL represents both the structure and behavior of hardware at different levels of abstraction. It can be used for documentation, simulation to verify design functionality, and synthesis to automate hardware design processes. The two most common HDLs are VHDL and Verilog.
This document discusses behavioral Verilog coding constructs including always and initial blocks, blocking vs non-blocking assignments, and if-else and case statements. It provides examples of coding flip-flops and sequential logic using these constructs. It also discusses simulator mechanics and the stratified event queue model of simulation.
This document describes gate level modeling in Verilog. It discusses gate types like AND, OR, and NOT gates that can be used as primitives. It describes how to instantiate gates and provides examples of instantiating gates like NAND and AND gates. It also describes structural modeling of circuits like a 2-input multiplexer, full adder, D latch, and master-slave JK flip-flop using gate level primitives.
The document contains Verilog code for half adders and full adders. It provides two implementations for each: a half adder is implemented using XOR and AND gates to calculate the sum and carry outputs from two input bits, and a full adder uses additional gates to calculate the sum and carry from three input bits.
This document discusses behavioral modeling in VHDL. It covers different VHDL design styles including behavioral, dataflow, and structural. Behavioral modeling uses sequential statements inside processes to model functionality. Key concepts covered include processes with and without sensitivity lists, concurrent vs sequential execution, if/case statements, loops, and wait statements. An example of a behavioral model for a full adder is presented using two processes.
This document discusses Verilog data types. There are two main groups: nets and variables. Nets like wire are used for connections and have a default value of Z. Variables like reg store values and have a default of X. Important net types are wire, tri, and trireg; wire is used for single-driver nets while tri is for multiple drivers. Variables include reg for storage in procedural blocks, integer for integers, and real for real numbers.
This document provides an overview of System Verilog concepts including simulation and synthesis, modules and primitives, styles, data types, operators, and more. Key points covered include the module concept as the basic design unit, module declaration syntax, module instantiation, different styles like structural, RTL/dataflow and behavioral, data types for nets and registers, number representation formats, and basic Verilog operators. The document serves as a tutorial introduction to essential System Verilog language constructs.
The document contains Verilog code for half adders and full adders. It provides two implementations for each: a half adder is implemented using either XOR and AND gates, or XOR and AND modules; a full adder is implemented using XOR, AND and OR gates arranged in a specific way to calculate the sum and carry outputs, or using XOR, AND and OR modules and a wire to decompose the calculation into steps.
Delays in Verilog allow modeling of timing aspects like propagation delays. There are different types of delays depending on the design approach - gate level modeling uses rise, fall, and turn-off delays while dataflow modeling uses assignment delays on nets. Behavioral modeling supports regular delays before assignments, intra-assignment delays after the equals sign, and zero delays to ensure last execution. Sequential and parallel blocks also control statement ordering.
This document discusses switch level modeling in Verilog. It describes different types of transistor switches that can be used as primitives in Verilog, including nmos, pmos, rnmos, rpmos, and cmos switches. It also covers bidirectional switches like tran, tranif1, and examples of how to use the switches to model basic logic gates and memory cells like a RAM cell. Time delays can be specified for switches. Switch level modeling allows designing circuits using transistors directly in Verilog.
Task and Function is the basic component of a programming language. Even on hardware Verification , those task and function is used. Task ans function provides a short way to repeatedly use the same block of code many times, This presentation gives you the basic information about Task and Function in Verilog. For more information on this, kindly contact us.
The document describes the design and simulation of basic logic gates and a 2-to-4 decoder using Verilog HDL. It includes the block diagrams, truth tables, and Verilog code for AND, OR, NAND, NOR, XOR, XNOR and NOT gates. Testbenches are provided to simulate and verify the gate designs. The 2-to-4 decoder section provides the block diagram, theory of operation, and Verilog code using dataflow, behavioral and structural modeling styles. A testbench is also included to simulate the 2-to-4 decoder design.
Those slides describe digital design using Verilog HDL,
starting with Design methodologies for any digital circuit then difference between s/w (C/C++) and H/w (Verilog) and the most important constructs that let us start hardware design using Verilog HDL.
Tasks and functions allow designers to abstract commonly used Verilog code into reusable routines. Tasks can contain timing constructs and pass multiple values through input, output, and inout arguments. Functions must not contain timing constructs and return a single value. Tasks are similar to subroutines while functions are similar to functions in other languages like FORTRAN. Automatic tasks make tasks re-entrant to avoid issues with concurrent calls operating on shared variables.
The document discusses different data types in Verilog including nets, registers, vectors, real numbers, parameters, arrays, and integers. It explains that nets like wire are used to model physical connections and do not store values while registers like reg can store values. Vectors allow declaring registers and nets with multiple bits. Parameters represent symbolic constant values. Arrays allow declaring registers with multiple elements. Integers are for non-hardware variables like loop indices.
This document provides an introduction to Verilog HDL including:
- An overview of Verilog keywords, data types, abstraction levels, and design methodology.
- Details on the history of Verilog including its development over time and transitions to newer standards.
- Explanations of key Verilog concepts like modules, ports, instantiation, stimuli, and lexical conventions.
Modules are the basic building blocks, ports define module interfaces, and instantiation replicates modules. Stimuli provide test inputs and lexical conventions cover syntax rules.
This document describes the design and implementation of a universal shift register (USR) in Verilog. It includes:
1) A block diagram and description of a USR that can perform shift left, shift right, and parallel load operations using D flip-flops and 4-to-1 multiplexers.
2) The Verilog code for the USR module using D flip-flop and 4-to-1 multiplexer submodules.
3) The test bench and simulation results verifying the USR functionality.
This document provides an overview of hardware description language (HDL) and VHDL. It begins with an introduction to HDLs and why they are needed to model digital hardware. It then presents an example VHDL code for an even parity detector circuit to demonstrate basic VHDL concepts like entities, architectures, signals, and concurrent statements. Finally, it discusses how VHDL fits into the digital design flow from coding to simulation to synthesis.
System Verilog introduces several new control flow constructs compared to Verilog, including unique if, priority if, foreach loops, and enhanced for loops. It also adds tasks and functions with arguments that can be passed by value, reference, or name. System Verilog defines two types of blocks - sequential blocks that execute statements sequentially and parallel blocks like fork-join that execute statements concurrently. It introduces various timing controls like delays, events, and wait statements.
The document discusses modules and ports in Verilog. It describes that a module defines distinct parts including module name, port list, port declarations, and optional parameters. Ports provide the interface for a module to communicate with its environment. There are two methods for connecting ports to external signals - by ordered list where signals must appear in the same order as ports, and by name where the order does not matter as long as port names match. Hierarchical names provide unique names for every identifier by denoting the design hierarchy with identifiers separated by periods.
Hardware Description Language (HDL) is used to describe digital systems in a textual format similar to a programming language. HDL represents both the structure and behavior of hardware at different levels of abstraction. It can be used for documentation, simulation to verify design functionality, and synthesis to automate hardware design processes. The two most common HDLs are VHDL and Verilog.
This document discusses behavioral Verilog coding constructs including always and initial blocks, blocking vs non-blocking assignments, and if-else and case statements. It provides examples of coding flip-flops and sequential logic using these constructs. It also discusses simulator mechanics and the stratified event queue model of simulation.
TLA+ and PlusCal / An engineer's perspectiveTorao Takami
This document discusses TLA+ and PlusCal for modeling and verifying concurrent systems. It provides an overview of TLA+ and PlusCal, including their syntax and how PlusCal models are translated to TLA+. It also explains how the TLC model checker is used to verify properties like safety and liveness by exploring all possible states and transitions of a TLA+ model. The document provides examples of how to specify safety properties with temporal logic operators and ensure liveness properties by using fairness assumptions to avoid infinite stuttering.
This document discusses different types of simulation for digital circuits including analog simulation using a SPICE engine, digital simulation, and event-driven simulation. It also covers testbenches, including generating stimulus, monitoring outputs, and exhaustively testing designs. Key topics covered include clocks, finite state machine testing, and force/release in testbenches.
This document discusses four types of event-based timing control in Verilog:
1. Regular event control uses @ to specify execution on signal value changes or transitions.
2. Named event control declares events to trigger execution.
3. Event OR control allows execution on changes to any signal in a sensitivity list.
4. Level-sensitive timing control uses wait to monitor conditions continuously before executing statements.
The document discusses behavioral modeling in Verilog. It describes that Verilog allows designers to describe a circuit's behavior at a high level of abstraction through behavioral modeling. There are two main procedural statements in Verilog for behavioral modeling - always and initial. The initial statement executes code once at time zero, while the always statement executes code continuously in a loop. Behavioral modeling allows evaluation of algorithms before implementing in hardware through logic gates.
This document discusses SystemVerilog assertions (SVA). It introduces SVA and explains that assertions are used to document design functionality, check design intent is met, and determine if verification tested the design. Assertions can be specified by the design or verification engineer. The document outlines the key building blocks of SVA like sequences, properties, and assertions. It provides examples of different types of assertions and how they are used. Key concepts discussed include implication, timing windows, edge detection, and repetition operators.
Verilog is a HARDWARE DESCRIPTION LANGUAGE (HDL). It is a language used for describing a digital system like a network switch or a microprocessor or a memory or a flip−flop. It means, by using a HDL we can describe any digital hardware at any level. Designs, which are described in HDL are independent of technology, very easy for designing and debugging, and are normally more useful than schematics, particularly for large circuits.
Verilog supports a design at many levels of abstraction. The major three are −
Behavioral level
Register-transfer level
Gate level
Behavioral level
This level describes a system by concurrent algorithms (Behavioural). Every algorithm is sequential, which means it consists of a set of instructions that are executed one by one. Functions, tasks and blocks are the main elements. There is no regard to the structural realization of the design. Verilog Keywords
Words that have special meaning in Verilog are called the Verilog keywords. For example, assign, case, while, wire, reg, and, or, nand, and module. They should not be used as identifiers. Verilog keywords also include compiler directives, and system tasks and functions.
Gate Level Modelling
Verilog has built-in primitives like logic gates, transmission gates and switches. These are rarely used for design work but they are used in post synthesis world for modelling of ASIC/FPGA cells.
Gate level modelling exhibits two properties −
Drive strength − The strength of the output gates is defined by drive strength. The output is strongest if there is a direct connection to the source. The strength decreases if the connection is via a conducting transistor and least when connected via a pull-up/down resistive. The drive strength is usually not specified, in which case the strengths defaults to strong1 and strong0.
Delays − If delays are not specified, then the gates do not have propagation delays; if two delays are specified, then first one represents the rise delay and the second one, fall delay; if only one delay is specified, then both, rise and fall are equal. Delays can be ignored in synthesis.
Gate Primitives
The basic logic gates using one output and many inputs are used in Verilog. GATE uses one of the keywords - and, nand, or, nor, xor, xnor for use in Verilog for N number of inputs and 1 output.
The document discusses various techniques for process synchronization and solving the critical section problem in concurrent systems. It describes producer-consumer problems and solutions using shared memory. It covers issues like race conditions that can occur. Different algorithms for solving the critical section problem are presented, including Peterson's algorithm and the Bakery algorithm. The document also discusses synchronization hardware support and low-level synchronization tools like locks, test-and-set instructions, and semaphores.
The document describes interfacing an FPGA to an LCD 16x2 display. It includes a block diagram, pin descriptions, timing diagrams, LCD initialization procedures from the datasheet, and VHDL code to implement the LCD controller on the FPGA. The VHDL code uses a state machine and ROM-based model with an 8-bit data line to generate the LCD initialization sequence and display text on the LCD. Behavioral and post-route simulations are shown to verify the design works as intended.
At the end of this lecture students should be able to;
Describe the looping structures in C programming language.
Practice the control flow of different looping structures in C programming language.
Practice the variants in control flow of different looping structures in C programming language.
Apply taught concepts for writing programs.
The document discusses control statements in C programming. It covers various selection statements like if, if-else, switch as well as iteration statements like for, while, do-while loops. Nested loops and special control statements like break, continue and goto are also explained. Operators used in control statements like relational, logical, assignment operators are defined along with examples. The different types of loops and their usages to control program flow are demonstrated.
The document discusses process synchronization and solutions to common synchronization problems. It covers topics like race conditions, critical sections, solutions to the critical section problem including Peterson's algorithm and Bakery algorithm. It also discusses hardware solutions using test-and-set and swap instructions. Finally, it discusses synchronization constructs like semaphores and how they can be used to solve classical synchronization problems like bounded buffer, dining philosophers, and readers-writers problem.
The document provides information about control structures in the C programming language. It discusses various decision making statements like if-else, ladder if-else, and switch statements. It also covers different types of loops in C - while loop, do-while loop, for loop, and nesting of loops. Examples programs are given for each control structure to demonstrate their usage. The document is intended as a study material for a computer programming course.
This document contains questions about various Verilog concepts including:
1) The difference between inter-delay and intra-delay and how they are used to model timing in always blocks.
2) Whether it is mandatory to include all inputs of a combinational circuit in the sensitivity list and the reasoning.
3) The differences between case, casex, and casez and how they handle unknown and don't care values.
3) The value of a variable after non-blocking and blocking assignments in an always block and how they execute.
The document provides an agenda for an introduction to computers lab covering repetition structures (loops), introduction to Visual Studio, and C++ programs. It discusses while, do-while, and for loops through flowcharts and C++ code examples. It also covers solutions containers, projects containers, program components in Visual Studio, and provides examples of C++ programs for various algorithms including determining if a number is positive/negative, even/odd, in a range, and baby weight classification. Exercises include finding maximum of numbers, GCD calculation, decimal to binary conversion, and factorial calculation.
The document discusses loop control structures in C++. It explains the for, while, and do-while loops and provides examples. It also covers break, continue, return, and goto statements used to control program flow in loops.
This material is to help designers who just started peddling on FPGA RTL design. This explains about RTL design guidelines for better performance and less resource utilization. We know that, timing closure in any FPGA design is very much important and many engineers spend most of their time in meeting the timings because of bad coding style. In this blog we will explore good RTL coding styles in order make our life easy and better. All the information presented in this blog is mostly taken from “sunburst design technical papers”.
Similar to Concepts of Behavioral modelling in Verilog HDL (20)
Transducers and sensors
Sensors in robotics
Tactile sensors
Proximity and range sensors
Miscellaneous sensors and sensor based system
Use of sensors in Robotics
Methods of robot programming
Leadthrough programming methods
A robot program as a path in space
Motion interpolation
WAIT, SIGNAL and DELAY commands
Branching
This document discusses robot applications in manufacturing, focusing on material transfer and machine loading/unloading applications. It describes several types of material transfer operations including pick-and-place, palletizing, and applications involving conveyors. It also discusses using robots for die casting, plastic molding, and other machine operations, describing how robots can load/unload parts and interface with production equipment. Key considerations for robot applications are also reviewed.
This document discusses artificial intelligence and its applications in robotics. It covers several topics:
1. It provides an introduction to AI and discusses how early robots had limited interaction with their environment but modern robotics incorporates AI through computer vision, sensors, and other techniques.
2. It outlines several goals of AI research including problem solving, natural language processing, expert systems, learning, computer vision, inference, and search. It also discusses knowledge representation techniques like logic, procedures, semantic networks, production systems, and frames.
3. It covers AI techniques for representing and manipulating data including knowledge representation, problem representation for problem solving using state-space and problem-reduction, and different data representation schemes.
File system and IOCS
Files and file organization
Fundamentals of file organizations
Directory structures
File protection
Interface between file system and IOCS
Allocation of disk space
Implementation of file access
The document discusses virtual memory and page replacement policies in operating systems. It begins with an overview of virtual memory basics such as using a combination of hardware and software to create an illusion of memory larger than physical RAM. It describes how processes are divided into pages that can be loaded and removed from memory as needed. The document then covers demand paging, where only needed pages are loaded, and page replacement policies including optimal, FIFO, and LRU which aim to replace pages least likely to be used soon. It concludes with contact information for the author.
Managing the memory hierarchy
Static and dynamic memory allocations
Memory allocation to a process
Reuse of memory
Contiguous and non contiguous memory allocation
Paging
Segmentation
Segmentation with paging
Characteristics and Quality Attributes of Embedded Systemanand hd
The document discusses the characteristics and quality attributes of embedded systems. It describes key characteristics of embedded systems such as being application specific, reactive and real-time in response, operating in harsh environments, being distributed, and having concerns for small size, weight and power. It then outlines important quality attributes for embedded systems including operational attributes like response, throughput, reliability, maintainability, security and safety as well as non-operational attributes.
The document discusses basic concepts in Verilog including lexical conventions, data types, and system tasks. It covers topics like comments, numbers, operators, identifiers, data types for nets, registers and vectors. Lexical conventions like whitespace, comments, operators, number representation, strings, and escaped identifiers are explained. Data types discussed include nets, registers, vectors, vector part selects, and variable vector part selects.
Definition
Embedded systems vs. General Computing Systems
Core of the Embedded System
Memory
Sensors and Actuators
Communication Interface
Embedded Firmware
Other System Components
PCB and Passive Components
Introduction
Types of end effectors
Mechanical gripper
Other types of grippers
Tools as end effectors
The Robot/End effectors interface
Considerations in gripper selection and design
Operation of an O.S
Structure of an operating system,
Operating systems with monolithic structure
Layered design of an operating system
Virtual machine operating systems
Kernel based operating systems
Fundamentals of Robotics and Machine Vision Systemanand hd
Automation and Robotics
Robotics in science Fiction
A brief history of robotics
Robot Anatomy & Work volume
Robot drive systems
Control systems and Dynamic performance
Precision of movement
End effectors
Robotic sensors,
Robot programming and work cell control
Robot applications
Efficiency, system performance and user convenience
Classes of operating systems
Batch processing system,
Multi programming systems
Time sharing systems
Real time operating systems
The document discusses the goals and operations of an operating system. The key goals of an operating system are efficient use of resources, user convenience, and preventing interference between users. The main operations of an operating system are program management, which involves executing and scheduling programs, resource management, which involves allocating and managing memory, CPU, and I/O devices, and security/protection which prevents unauthorized access to programs and data.
Introduction- e - waste – definition - sources of e-waste– hazardous substances in e-waste - effects of e-waste on environment and human health- need for e-waste management– e-waste handling rules - waste minimization techniques for managing e-waste – recycling of e-waste - disposal treatment methods of e- waste – mechanism of extraction of precious metal from leaching solution-global Scenario of E-waste – E-waste in India- case studies.
Understanding Inductive Bias in Machine LearningSUTEJAS
This presentation explores the concept of inductive bias in machine learning. It explains how algorithms come with built-in assumptions and preferences that guide the learning process. You'll learn about the different types of inductive bias and how they can impact the performance and generalizability of machine learning models.
The presentation also covers the positive and negative aspects of inductive bias, along with strategies for mitigating potential drawbacks. We'll explore examples of how bias manifests in algorithms like neural networks and decision trees.
By understanding inductive bias, you can gain valuable insights into how machine learning models work and make informed decisions when building and deploying them.
International Conference on NLP, Artificial Intelligence, Machine Learning an...gerogepatton
International Conference on NLP, Artificial Intelligence, Machine Learning and Applications (NLAIM 2024) offers a premier global platform for exchanging insights and findings in the theory, methodology, and applications of NLP, Artificial Intelligence, Machine Learning, and their applications. The conference seeks substantial contributions across all key domains of NLP, Artificial Intelligence, Machine Learning, and their practical applications, aiming to foster both theoretical advancements and real-world implementations. With a focus on facilitating collaboration between researchers and practitioners from academia and industry, the conference serves as a nexus for sharing the latest developments in the field.
Literature Review Basics and Understanding Reference Management.pptxDr Ramhari Poudyal
Three-day training on academic research focuses on analytical tools at United Technical College, supported by the University Grant Commission, Nepal. 24-26 May 2024
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTjpsjournal1
The rivalry between prominent international actors for dominance over Central Asia's hydrocarbon
reserves and the ancient silk trade route, along with China's diplomatic endeavours in the area, has been
referred to as the "New Great Game." This research centres on the power struggle, considering
geopolitical, geostrategic, and geoeconomic variables. Topics including trade, political hegemony, oil
politics, and conventional and nontraditional security are all explored and explained by the researcher.
Using Mackinder's Heartland, Spykman Rimland, and Hegemonic Stability theories, examines China's role
in Central Asia. This study adheres to the empirical epistemological method and has taken care of
objectivity. This study analyze primary and secondary research documents critically to elaborate role of
china’s geo economic outreach in central Asian countries and its future prospect. China is thriving in trade,
pipeline politics, and winning states, according to this study, thanks to important instruments like the
Shanghai Cooperation Organisation and the Belt and Road Economic Initiative. According to this study,
China is seeing significant success in commerce, pipeline politics, and gaining influence on other
governments. This success may be attributed to the effective utilisation of key tools such as the Shanghai
Cooperation Organisation and the Belt and Road Economic Initiative.
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSIJNSA Journal
The smart irrigation system represents an innovative approach to optimize water usage in agricultural and landscaping practices. The integration of cutting-edge technologies, including sensors, actuators, and data analysis, empowers this system to provide accurate monitoring and control of irrigation processes by leveraging real-time environmental conditions. The main objective of a smart irrigation system is to optimize water efficiency, minimize expenses, and foster the adoption of sustainable water management methods. This paper conducts a systematic risk assessment by exploring the key components/assets and their functionalities in the smart irrigation system. The crucial role of sensors in gathering data on soil moisture, weather patterns, and plant well-being is emphasized in this system. These sensors enable intelligent decision-making in irrigation scheduling and water distribution, leading to enhanced water efficiency and sustainable water management practices. Actuators enable automated control of irrigation devices, ensuring precise and targeted water delivery to plants. Additionally, the paper addresses the potential threat and vulnerabilities associated with smart irrigation systems. It discusses limitations of the system, such as power constraints and computational capabilities, and calculates the potential security risks. The paper suggests possible risk treatment methods for effective secure system operation. In conclusion, the paper emphasizes the significant benefits of implementing smart irrigation systems, including improved water conservation, increased crop yield, and reduced environmental impact. Additionally, based on the security analysis conducted, the paper recommends the implementation of countermeasures and security approaches to address vulnerabilities and ensure the integrity and reliability of the system. By incorporating these measures, smart irrigation technology can revolutionize water management practices in agriculture, promoting sustainability, resource efficiency, and safeguarding against potential security threats.
Comparative analysis between traditional aquaponics and reconstructed aquapon...bijceesjournal
The aquaponic system of planting is a method that does not require soil usage. It is a method that only needs water, fish, lava rocks (a substitute for soil), and plants. Aquaponic systems are sustainable and environmentally friendly. Its use not only helps to plant in small spaces but also helps reduce artificial chemical use and minimizes excess water use, as aquaponics consumes 90% less water than soil-based gardening. The study applied a descriptive and experimental design to assess and compare conventional and reconstructed aquaponic methods for reproducing tomatoes. The researchers created an observation checklist to determine the significant factors of the study. The study aims to determine the significant difference between traditional aquaponics and reconstructed aquaponics systems propagating tomatoes in terms of height, weight, girth, and number of fruits. The reconstructed aquaponics system’s higher growth yield results in a much more nourished crop than the traditional aquaponics system. It is superior in its number of fruits, height, weight, and girth measurement. Moreover, the reconstructed aquaponics system is proven to eliminate all the hindrances present in the traditional aquaponics system, which are overcrowding of fish, algae growth, pest problems, contaminated water, and dead fish.
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELgerogepatton
As digital technology becomes more deeply embedded in power systems, protecting the communication
networks of Smart Grids (SG) has emerged as a critical concern. Distributed Network Protocol 3 (DNP3)
represents a multi-tiered application layer protocol extensively utilized in Supervisory Control and Data
Acquisition (SCADA)-based smart grids to facilitate real-time data gathering and control functionalities.
Robust Intrusion Detection Systems (IDS) are necessary for early threat detection and mitigation because
of the interconnection of these networks, which makes them vulnerable to a variety of cyberattacks. To
solve this issue, this paper develops a hybrid Deep Learning (DL) model specifically designed for intrusion
detection in smart grids. The proposed approach is a combination of the Convolutional Neural Network
(CNN) and the Long-Short-Term Memory algorithms (LSTM). We employed a recent intrusion detection
dataset (DNP3), which focuses on unauthorized commands and Denial of Service (DoS) cyberattacks, to
train and test our model. The results of our experiments show that our CNN-LSTM method is much better
at finding smart grid intrusions than other deep learning algorithms used for classification. In addition,
our proposed approach improves accuracy, precision, recall, and F1 score, achieving a high detection
accuracy rate of 99.50%.
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...IJECEIAES
Climate change's impact on the planet forced the United Nations and governments to promote green energies and electric transportation. The deployments of photovoltaic (PV) and electric vehicle (EV) systems gained stronger momentum due to their numerous advantages over fossil fuel types. The advantages go beyond sustainability to reach financial support and stability. The work in this paper introduces the hybrid system between PV and EV to support industrial and commercial plants. This paper covers the theoretical framework of the proposed hybrid system including the required equation to complete the cost analysis when PV and EV are present. In addition, the proposed design diagram which sets the priorities and requirements of the system is presented. The proposed approach allows setup to advance their power stability, especially during power outages. The presented information supports researchers and plant owners to complete the necessary analysis while promoting the deployment of clean energy. The result of a case study that represents a dairy milk farmer supports the theoretical works and highlights its advanced benefits to existing plants. The short return on investment of the proposed approach supports the paper's novelty approach for the sustainable electrical system. In addition, the proposed system allows for an isolated power setup without the need for a transmission line which enhances the safety of the electrical network
1. Timing Controls
Various behavioral timing control constructs are available in Verilog. In Verilog, if there
are no timing control statements, the simulation time does not advance. Timing controls
provide a way to specify the simulation time at which procedural statements will
execute.
There are three methods of timing control: delay-based timing control, event-based
timing control, and level-sensitive timing control.
Delay-Based Timing Control
Delay-based timing control in an expression specifies the time duration between when
the statement is encountered and when it is executed.
Delays are specified by the symbol #.
Syntax for the delay-based timing control statement is shown below:
delay3 ::= # delay_value | # ( delay_value [ , delay_value [ ,delay_value ] ] )
delay2 ::= # delay_value | # ( delay_value [ , delay_value ] )
delay_value ::= unsigned_number | parameter_identifier | specparam_identifier |
mintypmax_expression
There are three types of delay control for procedural assignments: regular delay control,
intra-assignment delay control, and zero delay control.
1
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
2. Regular delay control
Regular delay control is used when a non-zero delay is specified to the left of a procedural
assignment.
// no delay control
// delay control with a number. Delay execution of y = 1 by 10 units
// Delay control with identifier. Delay of 20 units
// Delay control with expression
// Delay control with identifier. Take value of y.
/* Minimum, typical and maximum delay values. Discussed in gate-level
modeling chapter.*/
//define parameters
parameter latency = 20;
parameter delta = 2;
//define register variables
reg x, y, z, p, q;
initial
begin
x = 0;
#10 y = 1;
#latency z = 0;
#(latency + delta) p = 1;
#y x = x + 1;
#(4:5:6) q = 0;
end
2
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
3. Intra-assignment delay control
Instead of specifying delay control to the left of the assignment, it is possible to assign a
delay to the right of the assignment operator.
Such delay specification alters the flow of activity in a different manner.
reg x, y, z; //define register variables
//intra assignment delays
initial
begin
x = 0; z = 0;
y = #5 x + z;
end
/*Equivalent method with temporary variables and
regular delay control*/
initial
begin
x = 0; z = 0;
temp_xz = x + z;
#5 y = temp_xz;
end
/*Take value of x and z at the time=0,
evaluate x + z and then wait 5 time
units to assign value to y.*/
/*Take value of x + z at the current time and
store it in a temporary variable.
Even though x and z might change between 0 and 5*/
Note the difference between intra-
assignment delays and regular delays.
Regular delays defer the execution of
the entire assignment.
Intra-assignment delays compute the
righthand-side expression at the
current time and defer the assignment
of the computed value to the left-
hand-side variable.
Intra-assignment delays are like using
regular delays with a temporary
variable to store the current value of a
right-hand-side expression.
3
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
4. module blocking_nonblocking();
reg a,b,c,d;
initial begin
#10 a = 0;
#11 a = 1;
#12 a = 0;
#13 a = 1;
end
initial begin
#10 b <= 0;
#11 b <= 1;
#12 b <= 0;
#13 b <= 1;
end
initial begin
c = #10 0;
c = #11 1;
c = #12 0;
c = #13 1;
end
initial begin
d <= #10 0;
d <= #11 1;
d <= #12 0;
d <= #13 1;
end
initial begin
$monitor("TIME = %g A = %b B = %b C = %b D = %b",$time, a, b, c, d);
#50 $finish;
end
Simulator Output:
TIME = 0 A = x B = x C = x D = x
TIME = 10 A = 0 B = 0 C = 0 D = 0
TIME = 11 A = 0 B = 0 C = 0 D = 1
TIME = 12 A = 0 B = 0 C = 0 D = 0
TIME = 13 A = 0 B = 0 C = 0 D = 1
TIME = 21 A = 1 B = 1 C = 1 D = 1
TIME = 33 A = 0 B = 0 C = 0 D = 1
TIME = 46 A = 1 B = 1 C = 1 D = 1
4
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
5. module blocking_nonblocking();
reg a,b,c,d;
initial begin
#10 a = 0;
#11 a = 1;
#12 a = 0;
#13 a = 1;
end
initial begin
#10 b <= 0;
#11 b <= 1;
#12 b <= 0;
#13 b <= 1;
end
initial begin
c = #10 0;
c = #11 1;
c = #12 0;
c = #13 1;
end
initial begin
d <= #10 0;
d <= #11 1;
d <= #12 0;
d <= #13 1;
end
10 a <= 0;
21 a <= 1;
33 a<= 0;
46 a <= 1;
10 b <= 0;
21 b <= 1;
33 b<= 0;
46 b <= 1;
10 c <= 0;
21 c <= 1;
33 c<= 0;
46 c <= 1;
10 d <= 0;
11 d <= 1;
12 d <= 0;
13 d <= 1;
Simulator Output:
TIME = 0
TIME = 10
TIME = 11
TIME = 12
TIME = 13
TIME = 21
TIME = 33
TIME = 46
A = x
A = 0
A = 0
A = 0
A = 0
A = 1
A = 0
A = 1
B = x
B = 0
B = 0
B = 0
B = 0
B = 1
B = 0
B = 1
C = x
C = 0
C = 0
C = 0
C = 0
C = 1
C = 0
C = 1
D = x
D = 0
D = 1
D = 0
D = 1
D = 1
D = 1
D = 1
5
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
6. module blocking_nonblocking();
reg a,b,c,d;
initial begin
#10 a = 0;
#11 a = 1;
#12 a = 0;
#13 a = 1;
end
initial begin
#10 b <= 0;
#11 b <= 1;
#12 b <= 0;
#13 b <= 1;
end
initial begin
c = #10 0;
c = #11 1;
c = #12 0;
c = #13 1;
end
initial begin
d <= #10 0;
d <= #11 1;
d <= #12 0;
d <= #13 1;
end
initial begin
$monitor("TIME = %g A = %b B = %b C = %b D = %b",$time, a, b, c, d);
#50 $finish;
end
Simulator Output:
TIME = 0 A = x B = x C = x D = x
TIME = 10 A = 0 B = 0 C = 0 D = 0
TIME = 11 A = 0 B = 0 C = 0 D = 1
TIME = 12 A = 0 B = 0 C = 0 D = 0
TIME = 13 A = 0 B = 0 C = 0 D = 1
TIME = 21 A = 1 B = 1 C = 1 D = 1
TIME = 33 A = 0 B = 0 C = 0 D = 1
TIME = 46 A = 1 B = 1 C = 1 D = 1
6
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
7. Zero delay control
initial
begin
x = 0;
y = 0;
end
initial
begin
#0 x = 1; //zero delay control
#0 y = 1;
end
In this Example, four statements
x = 0, y = 0, x = 1, y = 1 are to be executed at
simulation time 0.
However, since x = 1 and y = 1 have #0, they
will be executed last. Thus, at the end of time
0, x will have value 1 and y will have value 1.
The order in which x = 1 and y = 1 are executed
is not deterministic.
Using #0 is not a recommended practice.
Procedural statements in different always-initial blocks may be evaluated at the same
simulation time.
The order of execution of these statements in different always-initial blocks is
nondeterministic.
Zero delay control is a method to ensure that a statement is executed last, after all other
statements in that simulation time are executed.
This is used to eliminate race conditions. However, if there are multiple zero delay
statements, the order between them is nondeterministic.
7
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
8. Event-Based Timing Control
An event is the change in the value on a register or a net. Events can be utilized to trigger
execution of a statement or a block of statements.
There are four types of event-based timing control: regular event control,
named event control,
event OR control, and
level sensitive timing control.
Regular event control:
The @ symbol is used to specify an event control.
Statements can be executed on changes in signal value or at a positive or negative
transition of the signal value.
The keyword posedge is used for a positive transition.
@(clock) q = d;
@(posedge clock) q = d;
@(negedge clock) q = d;
q = @(posedge clock) d;
//q = d is executed whenever signal clock changes value
/*q = d is executed whenever signal clock does a positive
transition ( 0 to 1,x or z, x to 1, z to 1 )*/
/*q = d is executed whenever signal clock does a negative
transition ( 1 to 0,x or z,x to 0, z to 0) */
/*d is evaluated immediately and assigned to q at the positive
edge of clock*/ 8
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
9. Named event control
Verilog provides the capability to declare an event and then trigger and recognize the
occurrence of that event.
The event does not hold any data. A named event is declared by the keyword event.
An event is triggered by the symbol ->. The triggering of the event is recognized by the
symbol @.
//This is an example of a data buffer storing data after the last packet of data has
arrived.
event received_data;
always @(posedge clock)
begin
if(last_data_packet)
->received_data;
end
//Define an event called received data
//check at each positive clock edge
//If this is the last data packet
//trigger the event received_data
always @(received_data)
data_buf = {data_pkt[0], data_pkt[1], data_pkt[2], data_pkt[3]};
//Await triggering of event received_data
//When event is triggered, store all four packets of received data in data buffer using
concatenation operator { }
9
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
10. Event OR Control
Sometimes a transition on any one of multiple signals or events can trigger the execution
of a statement or a block of statements. This is expressed as an OR of events or signals.
The list of events or signals expressed as an OR is also known as a sensitivity list.
The keyword or is used to specify multiple triggers.
//A level-sensitive latch with asynchronous reset
always @( reset or clock or d)
begin
if (reset)
q = 1'b0;
else if(clock)
q = d;
end
Sensitivity lists can also be specified using the "," (comma) operator instead of the or
operator. Comma operators can also be applied to sensitivity lists that have edge-
sensitive triggers.
//Wait for reset or clock or d to change
//if reset signal is high, set q to 0.
//if clock is high, latch input
10
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
11. //A level-sensitive latch with asynchronous reset
always @( reset, clock, d)
begin
if (reset)
q = 1'b0;
else if(clock)
q = d;
end
A positive edge triggered D flipflop with asynchronous falling reset can be modeled as
shown below
//Wait for reset or clock or d to change
//if reset signal is high, set q to 0.
//if clock is high, latch input
//Note use of comma operatoralways @(posedge clk, negedge reset)
if(!reset)
q <=0;
else
q <=d;
end
11
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
12. When the number of input variables to a combination logic block are very large,
sensitivity lists can become very cumbersome to write.
Moreover, if an input variable is missed from the sensitivity list, the block will not behave
like a combinational logic block.
To solve this problem, Verilog HDL contains two special symbols: @* and @(*).
Both symbols exhibit identical behavior.
These special symbols are sensitive to a change on any signal that may be read by the
statement group that follows this symbol.
Use of @* Operator
always @(a or b or c or d or e or f or g or h or p or m)
begin
out1 = a ? b+c : d+e;
out2 = f ? g+h : p+m;
end
//Combination logic block using the or operator
//Cumbersome to write and it is easy to miss one input to the block
always @(*)
begin
out1 = a ? b+c : d+e;
out2 = f ? g+h : p+m;
end
//Instead of the above method, use @(*) symbol
//Alternately, the @* symbol can be used
//All input variables are automatically included in the sensitivity list.
12
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
13. Level-Sensitive Timing Control:
Event control discussed earlier waited for the change of a signal value or the triggering of
an event.
The symbol @ provided edge-sensitive control.
Verilog also allows level sensitive timing control, that is, the ability to wait for a certain
condition to be true before a statement or a block of statements is executed.
The keyword wait is used for level sensitive constructs.
always
wait (count_enable) #20 count = count + 1;.
In the above example, the value of count_enable is monitored continuously.
If count_enable is 0, the statement is not entered.
If it is logical 1, the statement count = count + 1 is executed after 20 time units.
If count_enable stays at 1, count will be incremented every 20 time units
13
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
14. Conditional Statements:
Conditional statements are used for making decisions based upon certain conditions.
These conditions are used to decide whether or not a statement should be executed.
Keywords if and else are used for conditional statements.
There are three types of conditional statements:
//Type 1 conditional statement. No else statement.
//Statement executes or does not execute.
//Type 2 conditional statement. One else statement
//Either true_statement or false_statement is evaluated
//Type 3 conditional statement. Nested if-else-if.
//Choice of multiple statements. Only one is executed.
if (<expression>) true_statement ;
if (<expression>) true_statement ; else false_statement ;
if (<expression1>) true_statement1 ;
else if (<expression2>) true_statement2 ;
else if (<expression3>) true_statement3 ;
else default_statement ; 14
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
15. The <expression> is evaluated. If it is true (1 or a non-zero value), the true_statement is
executed. However, if it is false (zero) or ambiguous (x), the false_statement is executed.
The <expression> can contain any operators. Each true_statement or false_statement
can be a single statement or a block of multiple statements.
A block must be grouped, typically by using keywords begin and end. A single statement
need not be grouped.
//Type 1 statements
if(!lock) buffer = data;
if(enable) out = in;
//Type 2 statements
if (number_queued < MAX_Q_DEPTH)
begin
data_queue = data;
number_queued = number_queued + 1;
end
else
$display("Queue Full. Try again"); 15
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
16. //Type 3 statements
//Execute statements based on ALU control signal.
if (alu_control == 0)
y = x + z;
else if(alu_control == 1)
y = x - z;
else if(alu_control == 2)
y = x * z;
else
$display("Invalid ALU control signal");
16
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
17. Examples on Conditional Statements
module simple_if();
reg latch;
wire enable,din;
always @ (enable or din)
if (enable)
Begin
latch <= din;
end
endmodule
module if_else();
reg dff;
wire clk,din,reset;
always @ (posedge clk)
if (reset) begin
dff <= 0;
end
else
begin
dff <= din;
end
endmodule
17
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
18. module nested_if(counter, clk, reset, enable, up_en, down_en);
output reg [3:0] counter;
input clk, reset, enable, up_en, down_en;
always @ (posedge clk)
// If reset is asserted
/* If counter is enable and
up count is asserted */
/* If counter is enable and
down count is asserted */
// If counting is disabled
// Redundant code
if (reset == 1'b0) begin
counter <= 4'b0000;
end
else if (enable == 1'b1 && up_en == 1'b1)
begin
counter <= counter + 1'b1;
end
else if (enable == 1'b1 && down_en == 1'b1)
begin
counter <= counter - 1'b1;
end
else
begin
counter <= counter;
end
18
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
19. module stimulus();
reg [3:0] counter;
reg clk,reset,enable, up_en, down_en;
always @ (posedge clk)
initial
begin
$monitor ("@%0dns reset=%b enable=%b up=%b down=%b count=%b“, $time,
reset, enable, up_en, down_en,counter);
$display("@%0dns Driving all inputs to know state",$time);c
clk = 0;
reset = 0;
enable = 0;
up_en = 0;
down_en = 0;
#3 reset = 1;
$display("@%0dns De-Asserting reset",$time);
#4 enable = 1;
$display("@%0dns De-Asserting enable",$time);
#4 up_en = 1;
$display("@%0dns Putting counter in up count mode",$time);
#10 up_en = 0;
down_en = 1;
$display("@%0dns Putting counter in down count mode",$time);
#8 $finish;
end
always #1 clk = ~clk;
endmodule 19
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
21. module parallel_if();
reg [3:0] counter;
wire clk,reset,enable, up_en, down_en;
always @ (posedge clk)
if (reset == 1'b0)
begin
counter <= 4'b0000;
end
else
begin
if (enable == 1'b1 && up_en == 1'b1) begin
counter <= counter + 1'b1;
end
if (enable == 1'b1 && down_en == 1'b1)
begin
counter <= counter - 1'b1;
end
end
endmodule
//If reset is asserted
/* If counter is enable and
up count is mode*/
/* If counter is enable and
down count is mode*/
21
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
22. module Full_Adder_Behavioral_Verilog( input X1, X2, Cin, output S, Cout ); reg[1:0]
temp;
always @(*)
begin
temp = {1'b0,X1} + {1'b0,X2}+{1'b0,Cin};
end
assign S = temp[0];
assign Cout = temp[1];
$monitor(“%g: a=%b; B=%b; Cin=%b; S=%b; Cout=%b”, $time, X1, X2, Cin, S, Cout);
endmodule
module Testbench_Behavioral_adder();
reg A,B,Cin; wire S,Cout; //Verilog code for the structural full adder
Full_Adder_Behavioral_Verilog Behavioral_adder(.X1(A), .X2(B), .Cin(Cin), .S(S), .Cout(Cout) );
Initial
begin
A = 0; B = 0; Cin = 0;
#5; A = 0; B = 0; Cin = 1;
#5; A = 0; B = 1; Cin = 0;
#5; A = 0; B = 1; Cin = 1;
#5; A = 1; B = 0; Cin = 0;
#5; A = 1; B = 0; Cin = 1;
#5; A = 1; B = 1; Cin = 0;
#5; A = 1; B = 1; Cin = 1;
#5; end
endmodule
Stimulator output:
#0: A = 0; B = 0; Cin = 0; S=0; Cout=0
#5: A = 0; B = 0; Cin = 1; S=1; Cout=0
#10: A = 0; B = 1; Cin = 0; S=1; Cout=0
#15: A = 0; B = 1; Cin = 1; S=0; Cout=1
#20: A = 1; B = 0; Cin = 0; S=1; Cout=0
#25: A = 1; B = 0; Cin = 1; S=0; Cout=1
#30: A = 1; B = 1; Cin = 0; S=0; Cout=1
#35: A = 1; B = 1; Cin = 1; S=1; Cout=122
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
23. Multiway Branching:
case Statement
The keywords case, endcase, and default are used in the case statement.
case (expression)
alternative1: statement1;
alternative2: statement2;
alternative3: statement3;
...
...
default: default_statement;
endcase
Each of statement1, statement2 ,
default_statement can be a single statement or a
block of multiple statements.
If none of the alternatives matches, the default_statement is executed. The
default_statement is optional.
For the first alternative that matches, the corresponding statement or block is executed.
A block of multiple statements must be grouped
by keywords begin and end.
The expression is compared to the alternatives in
the order they are written.
Placing of multiple default statements in one case statement is not allowed.
The case statements can be nested. 23
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
24. //Execute statements based on the ALU control signal
reg [1:0] alu_control;
...
...
case (alu_control)
2'd0 : y = x + z;
2'd1 : y = x - z;
2'd2 : y = x * z;
default : $display("Invalid ALU control signal");
endcase
//Type 3 statements
//Execute statements based on ALU control signal.
if (alu_control == 0)
y = x + z;
else if(alu_control == 1)
y = x - z;
else if(alu_control == 2)
y = x * z;
else
$display("Invalid ALU control signal");
24
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
25. module mux4_to_1 (out, i0, i1, i2, i3, s1, s0);
// Port declarations from the I/O diagram
output out;
input i0, i1, i2, i3;
input s1, s0;
reg out;
always @(s1 or s0 or i0 or i1 or i2 or i3)
case ({s1, s0}) //Switch based on concatenation
of control signals
2'd0 : out = i0;
2'd1 : out = i1;
2'd2 : out = i2;
2'd3 : out = i3;
default: $display("Invalid control signals");
endcase
endmodule
The case statement can also act like a many-to-one multiplexer.
25
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
26. module mux4_to_1 (out, i0, i1, i2, i3, s1, s0);
// Port declarations from the I/O diagram
output out;
input i0, i1, i2, i3;
input s1, s0;
reg out;
always @(s1 or s0 or i0 or i1 or i2 or i3)
case ({s1, s0}) //Switch based on concatenation
of control signals
2'd0 : out = i0;
2'd1 : out = i1;
2'd2 : out = i2;
2'd3 : out = i3;
default: $display("Invalid control signals");
endcase
endmodule
The case statement can also act like a many-to-one multiplexer.
The case statement compares 0, 1, x,
and z values in the expression and the
alternative bit for bit.
If the expression and the alternative are
of unequal bit width, they are zero filled
to match the bit width of the widest of
the expression and the alternative.
26
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
27. module demultiplexer1_to_4 (out0, out1, out2, out3, in, s1, s0);
// Port declarations from the I/O diagram
output out0, out1, out2, out3;
reg out0, out1, out2, out3;
input in;
input s1, s0;
always @(s1 or s0 or in)
case ({s1, s0}) //Switch based on control signals
2'b00 : begin out0 = in; out1 = 1'bz; out2 = 1'bz; out3 = 1'bz; end
2'b01 : begin out0 = 1'bz; out1 = in; out2 = 1'bz; out3 = 1'bz; end
2'b10 : begin out0 = 1'bz; out1 = 1'bz; out2 = in; out3 = 1'bz; end
2'b11 : begin out0 = 1'bz; out1 = 1'bz; out2 = 1'bz; out3 = in; end
2'bx0, 2'bx1, 2'bxz, 2'bxx, 2'b0x, 2'b1x, 2'bzx : begin
out0 = 1'bx; out1 = 1'bx; out2 = 1'bx; out3 = 1'bx; end
2'bz0, 2'bz1, 2'bzz, 2'b0z, 2'b1z : begin
out0 = 1'bz; out1 = 1'bz; out2 = 1'bz; out3 = 1'bz; end
default: $display("Unspecified control signals");
endcase
endmodule
In the demultiplexer, multiple
input signal combinations such as
2'bz0, 2'bz1, 2,bzz, 2'b0z, and 2'b1z
that cause the same block to be
executed are put together with a
comma (,) symbol.
Case Statement with x and z
27
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
28. casex, casez Keywords
There are two variations of the case statement. They are denoted by keywords, casex and
casez.
• casez treats all z values in the case alternatives or the case expression as don‘t cares.
All bit positions with z can also represented by ? in that position.
• casex treats all x and z values in the case item or the case expression as don‘t cares.
The use of casex and casez allows comparison of only non-x or -z positions in the case
expression and the case alternatives.
encoding Next_state
1xxx 3
x1xx 2
xx1x 1
xxx1 0
Default 0
28
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
29. casex, casez Keywords
reg [3:0] encoding;
integer state;
casex (encoding)
4'b1xxx : next_state = 3;
4'bx1xx : next_state = 2;
4'bxx1x : next_state = 1;
4'bxxx1 : next_state = 0;
default : next_state = 0;
endcase
There are two variations of the case statement. They are denoted by keywords, casex and
casez.
• casez treats all z values in the case alternatives or the case expression as don‘t cares.
All bit positions with z can also represented by ? in that position.
• casex treats all x and z values in the case item or the case expression as don‘t cares.
The use of casex and casez allows comparison of only non-x or -z positions in the case
expression and the case alternatives.
encoding Next_state
1xxx 3
x1xx 2
xx1x 1
xxx1 0
Default 0
29
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
30. casex, casez Keywords
Thus, an input
encoding = 4'b10xz would cause next_state = 3 to be executed
encoding = 4'b11zz would cause
encoding = 4‘b0000 would cause
encoding = 4'bxz11 would cause
encoding = 4'b00xz would cause
encoding = 4'b1111 would cause
encoding = 4'bzxxz would cause
reg [3:0] encoding;
integer state;
casex (encoding)
4'b1xxx : next_state = 3;
4'bx1xx : next_state = 2;
4'bxx1x : next_state = 1;
4'bxxx1 : next_state = 0;
default : next_state = 0;
endcase
There are two variations of the case statement. They are denoted by keywords, casex and
casez.
• casez treats all z values in the case alternatives or the case expression as don‘t cares.
All bit positions with z can also represented by ? in that position.
• casex treats all x and z values in the case item or the case expression as don‘t cares.
The use of casex and casez allows comparison of only non-x or -z positions in the case
expression and the case alternatives.
next_state = 2 to be executed
next_state = 0 to be executed
next_state = 1 to be executed
next_state = 0 to be executed
next_state = 0 to be executed
next_state = 0 to be executed
30
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
31. casex, casez Keywords
reg [3:0] encoding;
integer state;
casez (encoding)
4'b1zzz : next_state = 3;
4'bz1zz : next_state = 2;
4'bzz1z : next_state = 1;
4'bzzzz : next_state = 0;
default : next_state = 0;
endcase
There are two variations of the case statement. They are denoted by keywords, casex and
casez.
• casez treats all z values in the case alternatives or the case expression as don‘t cares.
All bit positions with z can also represented by ? in that position.
• casex treats all x and z values in the case item or the case expression as don‘t cares.
The use of casex and casez allows comparison of only non-x or -z positions in the case
expression and the case alternatives.
31
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
32. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
Comparing case, casex & casez satements
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
SIMULATOR OUTPUT
?
32
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
33. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
33
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
35. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
35
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
36. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
36
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
37. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
37
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
38. Simulator Output
Driving 0
Normal : Logic 0 on sel
CASEX : Logic 0 on sel
CASEZ : Logic 0 on sel
38
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
39. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
39
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
40. Simulator Output
Driving 0
Normal : Logic 0 on sel
CASEX : Logic 0 on sel
CASEZ : Logic 0 on sel
Driving 1
40
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
41. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
41
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
42. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
42
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
43. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
43
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
44. Simulator Output
Driving 0
Normal : Logic 0 on sel
CASEX : Logic 0 on sel
CASEZ : Logic 0 on sel
Driving 1
Normal : Logic 1 on sel
CASEX : Logic 1 on sel
CASEZ : Logic 1 on sel
44
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
45. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
45
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
46. Simulator Output
Driving 0
Normal : Logic 0 on sel
CASEX : Logic 0 on sel
CASEZ : Logic 0 on sel
Driving 1
Normal : Logic 1 on sel
CASEX : Logic 1 on sel
CASEZ : Logic 1 on sel
Driving x
46
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
47. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
47
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
48. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
48
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
49. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
49
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
50. Simulator Output
Driving 0
Normal : Logic 0 on sel
CASEX : Logic 0 on sel
CASEZ : Logic 0 on sel
Driving 1
Normal : Logic 1 on sel
CASEX : Logic 1 on sel
CASEZ : Logic 1 on sel
Driving x
Normal : Logic x on sel
CASEX : Logic 0 on sel
CASEZ : Logic x on sel
50
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
51. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
51
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
52. Simulator Output
Driving 0
Normal : Logic 0 on sel
CASEX : Logic 0 on sel
CASEZ : Logic 0 on sel
Driving 1
Normal : Logic 1 on sel
CASEX : Logic 1 on sel
CASEZ : Logic 1 on sel
Driving x
Normal : Logic x on sel
CASEX : Logic 0 on sel
CASEZ : Logic x on sel
Driving z
52
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
53. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
53
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
54. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
54
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
55. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
55
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
56. Simulator Output
Driving 0
Normal : Logic 0 on sel
CASEX : Logic 0 on sel
CASEZ : Logic 0 on sel
Driving 1
Normal : Logic 1 on sel
CASEX : Logic 1 on sel
CASEZ : Logic 1 on sel
Driving x
Normal : Logic x on sel
CASEX : Logic 0 on sel
CASEZ : Logic x on sel
Driving z
Normal : Logic z on sel
CASEX : Logic 0 on sel
CASEZ : Logic 0 on sel
56
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
57. module case_compare;
reg sel;
initial begin
#1 $display ("n Driving 0");
sel = 0;
#1 $display ("n Driving 1");
sel = 1;
#1 $display ("n Driving x");
sel = 1'bx;
#1 $display ("n Driving z");
sel = 1'bz;
#1 $finish;
end
always @ (sel)
case (sel)
1'b0 : $display("Normal : Logic 0 on sel");
1'b1 : $display("Normal : Logic 1 on sel");
1'bx : $display("Normal : Logic x on sel");
1'bz : $display("Normal : Logic z on sel");
endcase
always @ (sel)
casex (sel)
1'b0 : $display("CASEX : Logic 0 on sel");
1'b1 : $display("CASEX : Logic 1 on sel");
1'bx : $display("CASEX : Logic x on sel");
1'bz : $display("CASEX : Logic z on sel");
endcase
always @ (sel)
casez (sel)
1'b0 : $display("CASEZ : Logic 0 on sel");
1'b1 : $display("CASEZ : Logic 1 on sel");
1'bx : $display("CASEZ : Logic x on sel");
1'bz : $display("CASEZ : Logic z on sel");
endcase
endmodule
57
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
58. Simulator Output with timing analysis
Driving 0
Normal : Logic 0 on sel
CASEX : Logic 0 on sel
CASEZ : Logic 0 on sel
Driving 1
Normal : Logic 1 on sel
CASEX : Logic 1 on sel
CASEZ : Logic 1 on sel
Driving x
Normal : Logic x on sel
CASEX : Logic 0 on sel
CASEZ : Logic x on sel
Driving z
Normal : Logic z on sel
CASEX : Logic 0 on sel
CASEZ : Logic 0 on sel
#1
#2
#3
#4
$finish #5 58
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
59. Loops
Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
59
•There are four types of looping statements in Verilog: while, for, repeat, and forever.
•The syntax of these loops is very similar to the syntax of loops in the C programming
language.
•All looping statements can appear only inside an initial or always block. Loops
may contain delay expressions.
While Loop:
•The keyword while is used to specify this loop.
•The while loop executes until the while expression is not true. If the loop is entered
when the while-expression is not true, the loop is not executed at all.
•Each expression can contain the operators. Any logical expression can be specified with
these operators.
•If multiple statements are to be executed in the loop, they must be grouped typically
using keywords begin and end.
60. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
60
Example1:
integer count;
initial
begin
count = 0;
while (count < 128)
begin
$display("Count = %d", count);
count = count + 1;
end
end
Example2:
'define TRUE 1'b1';
'define FALSE 1'b0;
reg [15:0] flag;
integer i; //integer to keep count
reg continue;
initial
begin
flag = 16'b 0010_0000_0000_0000;
i = 0;
continue = 'TRUE;
while((i < 16) && continue)
begin
if (flag[i])
begin
$display("Encountered a TRUE bit at element number %d", i);
continue = 'FALSE;
end
i = i + 1;
end
end
//Execute loop till count is 127.
//exit at count 128
//Multiple conditions using
operators.
61. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
61
For Loop
The keyword for is used to specify this loop. The for loop contains three parts:
• An initial condition
• A check to see if the terminating condition is true
• A procedural assignment to change value of the control variable
integer count;
initial
for ( count=0; count < 128; count = count + 1)
$display("Count = %d", count);
The initialization condition and the incrementing procedural assignment are included
in the for loop and do not need to be specified separately. Thus, the for loop provides
a more compact loop structure than the while loop.
Note, however, that the while loop is more general-purpose than the for loop. The for
loop cannot be used in place of the while loop in all situations.
62. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
62
Example:
'define MAX_STATES 32
integer state [0: 'MAX_STATES-1];
integer i;
initial
begin
for(i = 0; i < 32; i = i + 2)
state[i] = 0;
for(i = 1; i < 32; i = i + 2)
state[i] = 1;
end
//Integer array state with elements 0:31
//initialize all even locations with 0
//initialize all odd locations with 1
for loops are generally used when there is a fixed beginning and end to the loop.
If the loop is simply looping on a certain condition, it is better to use the while loop.
63. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
63
Repeat Loop
The keyword repeat is used for this loop.
The repeat construct executes the loop a fixed number of times. A repeat construct
cannot be used to loop on a general logical expression. A while loop is used for that
purpose.
A repeat construct must contain a number, which can be a constant, a variable or a signal
value. However, if the number is a variable or signal value, it is evaluated only when the
loop starts and not during the loop execution.
integer count;
initial
begin
count = 0;
repeat(128)
begin
$display("Count = %d", count);
count = count + 1;
end
end
The counter expressed with the repeat loop
64. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
64
Example:
module data_buffer(data_start, data, clock);
parameter cycles = 8;
input data_start;
input [15:0] data;
input clock;
reg [15:0] buffer [0:7];
integer i;
always @(posedge clock)
begin
if(data_start)
begin
i = 0;
repeat(cycles)
begin
@(posedge clock) buffer[i] = data;
i = i + 1;
end
end
end
endmodule
//data start signal is true
//Store data at the posedge of next 8 clock cycles
//waits till next posedge to latch data
Data buffer module example
•After it receives a data_start
signal.
•Reads data for next 8 cycles.
65. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
65
Forever loop
•The keyword forever is used to express this loop.
•The loop does not contain any expression and executes forever until the $finish task is
encountered. The loop is equivalent to a while loop with an expression that always
evaluates to true, e.g., while (1).
•A forever loop can be exited by use of the disable statement.
•A forever loop is typically used in conjunction with timing control constructs.
•If timing control constructs are not used, the Verilog simulator would execute this
statement infinitely without advancing simulation time and the rest of the design would
never be executed.
66. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
66
Example 1: Clock generation
//Use forever loop instead of always block
reg clock;
initial
begin
clock = 1'b0;
forever #10 clock = ~clock; //Clock with period of 20 units
end
Example 2: Synchronize two register values at every positive edge of clock
reg clock;
reg x, y;
initial
forever @(posedge clock) x = y
67. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
67
Sequential and Parallel Blocks:
Block statements are used to group multiple statements to act together as one.
In previous examples, we used keywords begin and end to group multiple statements.
Thus, we used sequential blocks where the statements in the block execute one after
another.
In this section we discuss the block types: sequential blocks and parallel blocks.
Block Types:
There are two types of blocks: sequential blocks and parallel blocks.
Sequential blocks:
The keywords begin and end are used to group statements into sequential blocks.
68. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
68
Sequential blocks have the following characteristics:
• The statements in a sequential block are processed in the order they are specified.
A statement is executed only after its preceding statement completes execution
• If delay or event control is specified, it is relative to the simulation time when the
previous statement in the block completed execution,
(except for nonblocking assignments with intra-assignment timing control).
reg x, y;
reg [1:0] z, w;
initial
begin
x = 1'b0;
y = 1'b1;
z = {x, y};
w = {y, x};
end
//Illustration 1: Sequential block
without delay
//Illustration 2: Sequential blocks with delay.
reg x, y;
reg [1:0] z, w;
initial
begin
x = 1'b0; //completes at simulation time 0
#5 y = 1'b1; //completes at simulation time 5
#10 z = {x, y}; //completes at simulation time 15
#20 w = {y, x}; //completes at simulation time 35
end
69. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
69
Parallel blocks:
Parallel blocks, specified by keywords fork and join, provide interesting simulation
features.
Parallel blocks have the following characteristics:
• Statements in a parallel block are executed concurrently.
• Ordering of statements is controlled by the delay or event control assigned to each
statement.
• If delay or event control is specified, it is relative to the time the block was entered.
Notice the fundamental difference between sequential and parallel blocks. All
statements in a parallel block start at the time when the block was entered. Thus, the
order in which the statements are written in the block is not important.
reg x, y;
reg [1:0] z, w;
initial
fork
x = 1'b0;
#5 y = 1'b1;
#10 z = {x, y};
#20 w = {y, x};
join
//Example 1: Parallel blocks with delay.
//completes at simulation time 0
//completes at simulation time 5
//completes at simulation time 10
//completes at simulation time 20
70. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
70
Generate Blocks:
Generate statements allow Verilog code to be generated dynamically at elaboration time
before the simulation begins.
This facilitates the creation of parameterized models. Generate statements are particularly
convenient when the same operation or module instance is repeated for multiple bits of a
vector, or when certain Verilog code is conditionally included based on parameter
definitions.
Generate statements allow control over the declaration of variables, functions, and tasks,
as well as control over instantiations.
All generate instantiations are coded with a module scope and require the keywords
generate - endgenerate
Generated instantiations can be one or more of the following types:
• Modules
• User defined primitives
• Verilog gate primitives
• Continuous assignments
• initial and always blocks
71. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
71
Generated declarations and instantiations can be conditionally instantiated into a design.
Generated variable declarations and instantiations can be multiply instantiated into a
design.
Generated instances have unique identifier names and can be referenced
hierarchically.
To support interconnection between structural elements and/or procedural blocks,
generate statements permit the following Verilog data types to be declared within
the generate scope:
• net, reg
• integer, real, time, realtime
• event
Parameter redefinition using ordered or named assignment or a defparam statement can
be declared with the generate scope.
However, a defparam statement within a generate scope is allowed to modify the value
of a parameter only in the same generate scope or within the hierarchy instantiated
within the generate scope.
72. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
72
Task and function declarations are permitted within the generate scope but not within a
generate loop. Generated tasks and functions have unique identifier names and can be
referenced hierarchically.
Connections to generated module instances are handled in the same way as with normal
module instances.
There are three methods to create generate statements:
• Generate loop
• Generate conditional
• Generate case
Generate Loop
A generate loop permits one or more of the following to be
instantiated multiple times using a for loop:
• Variable declarations
• Modules
• User defined primitives, Gate primitives
• Continuous assignments
• initial and always blocks
73. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
73
Example: Bit-wise Xor of Two N-bit Buses
bitwise_xor (out, i0, i1);
parameter N = 32;
output [N-1:0] out;
input [N-1:0] i0, i1;
genvar j;
//Generate the bit-wise Xor with a single loop
generate for (j=0; j<N; j=j+1) begin: xor_loop
xor g1 (out[j], i0[j], i1[j]);
end //end of the for loop inside the generate block
endgenerate //end of the generate block
// As an alternate style, the xor gates could be replaced by always blocks.
//reg [N-1:0] out;
//generate for (j=0; j<N; j=j+1) begin: bit
//always @(i0[j] or i1[j]) out[j] = i0[j] ^ i1[j];
//end
//endgenerate
endmodule
// Port declarations
// 32-bit bus by default
// Declare a temporary loop variable. This variable is used only in the
evaluation of generate blocks. This variable does not exist during the
simulation of a Verilog design
74. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
74
Prior to the beginning of the simulation, the simulator elaborates (unrolls) the
code in the generate blocks to create a flat representation without the generate
blocks.
The unrolled code is then simulated. Thus, generate blocks are simply a convenient way of
replacing multiple repetitive Verilog statements with a single statement inside a loop.
• genvar is a keyword used to declare variables that are used only in the evaluation
of generate block. genvars do not exist during simulation of the design.
• The value of a genvar can be defined only by a generate loop.
• Generate loops can be nested. However, two generate loops using the same
genvar as an index variable cannot be nested.
• The name xor_loop assigned to the generate loop is used for hierarchical name
referencing of the variables inside the generate loop. Therefore, the relative
hierarchical names of the xor gates will be xor_loop[0].g1, xor_loop[1].g1, .......,
xor_loop[31].g1
75. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
75
Generated Ripple Adder
module ripple_adder(co, sum, a0, a1, ci);
parameter N = 4; // 4-bit bus by default
output [N-1:0] sum;
output co;
input [N-1:0] a0, a1;
input ci;
wire [N-1:0] carry;
assign carry[0] = ci;
genvar i;
//Generate the bit-wise Xor with a single loop
generate for (i=0; i<N; i=i+1) begin: r_loop
wire t1, t2, t3;
xor g1 (t1, a0[i], a1[i]);
xor g2 (sum[i], t1, carry[i]);
and g3 (t2, a0[i], a1[i]);
and g4 (t3, t1, carry[i]);
or g5 (carry[i+1], t2, t3);
end //end of the for loop inside the generate block
endgenerate //end of the generate block
// Port declarations
//Local wire declaration
//Assign 0th bit of carry equal to carry input
76. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
76
// For the above generate loop, the following relative hierarchical instance names are
generated
// xor : r_loop[0].g1, r_loop[1].g1, r_loop[2].g1, r_loop[3].g1
r_loop[0].g2, r_loop[1].g2, r_loop[2].g2, r_loop[3].g2
// and : r_loop[0].g3, r_loop[1].g3, r_loop[2].g3, r_loop[3].g3
r_loop[0].g4, r_loop[1].g4, r_loop[2].g4, r_loop[3].g4
// or : r_loop[0].g5, r_loop[1].g5, r_loop[2].g5, r_loop[3].g5
// Generated instances are connected with the following generated nets
// Nets: r_loop[0].t1, r_loop[0].t2, r_loop[0].t3
// r_loop[1].t1, r_loop[1].t2, r_loop[1].t3
// r_loop[2].t1, r_loop[2].t2, r_loop[2].t3
// r_loop[3].t1, r_loop[3].t2, r_loop[3].t3
assign co = carry[N];
endmodule
77. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
77
Generate Conditional
A generate conditional is like an if-else-if generate construct that permits the following
Verilog constructs to be conditionally instantiated into another module based on an
expression that is deterministic at the time the design is elaborated:
• Modules
• User defined primitives, Gate primitives
• Continuous assignments
• initial and always blocks
Parametrized Multiplier using Generate Conditional
module multiplier (product, a0, a1);
parameter a0_width = 8; // 8-bit bus by default
parameter a1_width = 8; // 8-bit bus by default
localparam product_width = a0_width + a1_width;
output [product_width -1:0] product;
input [a0_width-1:0] a0;
input [a1_width-1:0] a1;
78. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
78
// Instantiate the type of multiplier conditionally.
// Depending on the value of the a0_width and a1_width
// parameters at the time of instantiation, the appropriate
// multiplier will be instantiated.
Generate
if (a0_width <8) || (a1_width < 8)
cla_multiplier #(a0_width, a1_width) m0 (product, a0, a1);
else
tree_multiplier #(a0_width, a1_width) m0 (product, a0, a1);
endgenerate //end of the generate block
endmodule
Generate Case:
A generate case permits the following Verilog constructs to be conditionally instantiated
into another module based on a select-one-of-many case construct that is deterministic at
the time the design is elaborated:
• Modules
• User defined primitives, Gate primitives
• Continuous assignments
• initial and always blocks
79. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
79
Generate Case Example
module adder(co, sum, a0, a1, ci);
parameter N = 4; // 4-bit bus by default
output [N-1:0] sum;
output co;
input [N-1:0] a0, a1;
input ci;
// Instantiate the appropriate adder based on the width of the bus.
// This is based on parameter N that can be redefined at instantiation time.
generate
case (N)
1: adder_1bit adder1(c0, sum, a0, a1, ci); //1-bit implementation
2: adder_2bit adder2(c0, sum, a0, a1, ci); //2-bit implementation
default: adder_cla #(N) adder3(c0, sum, a0, a1, ci);
endcase
endgenerate
endmodule
// Port declarations
//Special cases for 1 and 2 bit adders
// Default is N-bit carry look ahead adder
//end of the generate block
80. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
80
Examples :
4-to-1 Multiplexer
module mux4_to_1 (out, i0, i1, i2, i3, s1, s0);
output out;
input i0, i1, i2, i3;
input s1, s0;
reg out;
always @(s1 or s0 or i0 or i1 or i2 or i3)
begin
case ({s1, s0})
2'b00: out = i0;
2'b01: out = i1;
2'b10: out = i2;
2'b11: out = i3;
default: out = 1'bx;
endcase
end
endmodule
81. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
81
Behavioral 4-bit Counter Description
module counter(Q , clock, clear);
output [3:0] Q;
input clock, clear;
reg [3:0] Q;
always @( posedge clear or negedge clock)
begin
if (clear)
Q <= 4'd0;
else
Q <= Q + 1;
end
endmodule
82. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
82
The following specifications must be considered:
• The traffic signal for the main highway gets highest priority because cars are
continuously present on the main highway. Thus, the main highway signal
remains green by default.
• Occasionally, cars from the country road arrive at the traffic signal. The traffic
signal for the country road must turn green only long enough to let the cars on the
country road go.
• As soon as there are no cars on the country road, the country road traffic signal
turns yellow and then red and the traffic signal on the main highway turns green
again.
• There is a sensor to detect cars waiting on the country road. The sensor sends a
signal X as input to the controller. X = 1 if there are cars on the country road;
otherwise, X= 0.
• There are delays on transitions from S1 to S2, from S2 to S3, and from S4 to S0.
The delays must be controllable.
84. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
84
Traffic Signal Controller
`define TRUE 1'b1
`define FALSE 1'b0
`define Y2RDELAY 3 //Yellow to red delay
`define R2GDELAY 2 //Red to green delay
module sig_control
(hwy, cntry, X, clock, clear);
output [1:0] hwy, cntry;
reg [1:0] hwy, cntry;
input X;
input clock, clear;
parameter RED = 2'd0,
YELLOW = 2'd1,
GREEN = 2'd2;
//State definition HWY CNTRY
parameter S0 = 3'd0, //GREEN RED
S1 = 3'd1, //YELLOW RED
S2 = 3'd2, //RED RED
S3 = 3'd3, //RED GREEN
S4 = 3'd4; //RED YELLOW
//Delays
//I/O ports
85. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
85
//Internal state variables
reg [2:0] state;
reg [2:0] next_state;
//state changes only at positive edge of clock
always @(posedge clock)
if (clear)
state <= S0; //Controller starts in S0 state
else
state <= next_state; //State change
//Compute values of main signal and country signal
always @(state)
begin
hwy = GREEN; //Default Light Assignment for Highway light
cntry = RED; //Default Light Assignment for Country light
case(state)
S0: ; // No change, use default
S1: hwy = YELLOW;
S2: hwy = RED;
S3: begin
hwy = RED;
cntry = GREEN;
end
86. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
86
S4: begin
hwy = RED;
cntry = `YELLOW;
end
endcase
end
//State machine using case statements
always @(state or X)
begin
case (state)
S0: if(X)
next_state = S1;
else
next_state = S0;
S1: begin //delay some positive edges of clock
repeat(`Y2RDELAY) @(posedge clock) ;
next_state = S2;
end
S2: begin //delay some positive edges of clock
repeat(`R2GDELAY) @(posedge clock);
next_state = S3;
end
87. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
87
S3: if(X)
next_state = S3;
else
next_state = S4;
S4: begin //delay some positive edges of clock
repeat(`Y2RDELAY) @(posedge clock) ;
next_state = S0;
end
default: next_state = S0;
endcase
end
endmodule
88. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
88
Stimulus for Traffic Signal Controller
//Stimulus Module
module stimulus;
wire [1:0] MAIN_SIG, CNTRY_SIG;
reg CAR_ON_CNTRY_RD;
//if TRUE, indicates that there is car on
//the country road
reg CLOCK, CLEAR;
//Instantiate signal controller
sig_control SC(MAIN_SIG, CNTRY_SIG, CAR_ON_CNTRY_RD, CLOCK, CLEAR);
//Set up monitor
initial
$monitor($time, " Main Sig = %b Country Sig = %b Car_on_cntry = %b",
MAIN_SIG, CNTRY_SIG, CAR_ON_CNTRY_RD);
//Set up clock
initial
begin
CLOCK = `FALSE;
forever #5 CLOCK = ~CLOCK;
end
//control clear signal
initial
89. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
89
begin
CLEAR = `TRUE;
repeat (5) @(negedge CLOCK);
CLEAR = `FALSE;
end
//apply stimulus
initial
begin
CAR_ON_CNTRY_RD = `FALSE;
repeat(20)@(negedge CLOCK); CAR_ON_CNTRY_RD = `TRUE;
repeat(10)@(negedge CLOCK); CAR_ON_CNTRY_RD = `FALSE;
repeat(20)@(negedge CLOCK); CAR_ON_CNTRY_RD = `TRUE;
repeat(10)@(negedge CLOCK); CAR_ON_CNTRY_RD = `FALSE;
repeat(20)@(negedge CLOCK); CAR_ON_CNTRY_RD = `TRUE;
repeat(10)@(negedge CLOCK); CAR_ON_CNTRY_RD = `FALSE;
repeat(10)@(negedge CLOCK); $stop;
end
endmodule
90. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
90
References
• Samir Palnitkar, “Verilog HDL-A Guide to
Digital Design and Synthesis”, Pearson, 2003
• www.asic-world.com/verilog/vbehave3.html
91. Verilog HDL_18EC44 by Anand H D, Dr. AIT,
Bengaluru-56
91
Prof. Anand H. D.
M. Tech. (PhD.)
Assistant Professor,
Department of Electronics & Communication Engineering
Dr. Ambedkar Institute of Technology, Bengaluru-56
Email: anandhdece@dr-ait.org
Phone: 9844518832