Unit 1
Unit 1
6
11
FPGA Design Flow
• Design Entry
– In schematic, VHDL or Verilog.
• Implementation
– Placement & Routing
– Bitstream generation
– Analyze timing, view layout, simulation, etc.
• Download
– Directly to Xilinx hardware devices with unlimited
reconfigurations.
12
Students working in VLSI Domain
Dikita Chauhan –San Jose State University, working at Intel
Corporation, California
Rohan Jagtap – Portland State University, working at
Synopsys, Oregon
Raviraj Kokil - Portland State University, working at Intel
Corporation, Oregon
Sonia Patwardhan - San Jose State University, working at
Intel Corporation
Aakash Thakare – Arizona State University, working at Intel
Corporation
Chetan Udawant - San Jose State University, working at Intel
Corporation
Aditya Kshirsagar - Penn State University, working at Intel
Corporation
Sanmati Jain - Working at Intel Corporation, India
IITs offering M. Tech in VLSI
IIT, Madras – SHAKTI Processor - Open-source
Processor Development Ecosystem- developing
family of 6 processors of RISC architecture:
https://shakti.org.in/
IIT, Bombay
IIT, Delhi
IIT, Kharagpur
NITs
Manipal University
MIT-WPU
Hardware Description Language -
Introduction
Boolean Expressions:
These are specified in Verilog HDL with a
continuous assignment statement consisting of the
keyword assign followed by a Boolean Expression.
The earlier circuit can be specified using the
statement:
assign x = (A&B)|~C)
Verilog – Module (9)
not n1 (Anot,A),
not n2 (Bnot,B),
not n3 (Enot,E);
nand n4 (D[0],Anot,Bnot,Enot),
nand n5 (D[1],Anot,B,Enot),
nand n6 (D[2],A,Bnot,Enot),
nand n7 (D[3],A,B,Enot);
endmodule
Three-State Gates
Three-State Gates
Three-state gates have a control input that can place the
gate into a high-impedance state. (symbolized by z in
HDL).
The bufif1 gate behaves like a normal buffer if
control=1. The output goes to a high-impedance state z
when control=0.
bufif0 gate behaves in a similar way except that the
high-impedance state occurs when control=1
Two not gates operate in a similar manner except that the
o/p is the complement of the input when the gate is not in a
high impedance state.
The gates are instantiated with the statement
◼ gate name (output, input, control);
Three-State Gates
The procedural assignment statements inside the always block are executed
every time there is a change in any of the variable listed after the @ symbol.
(Note that there is no “;” at the end of always statement)
//Behavioral description of 2-to-1-line
multiplexer
module mux2x1_bh(A,B,select,OUT);
input A,B,select;
output OUT;
reg OUT;
always @(select or A or B)
if (select == 1) OUT = A;
else OUT = B;
endmodule
Writing a Test Bench
The stimulus model generates inputs for the design module by declaring
identifiers TA and TB as reg data type, and checks the output of the
design unit with the wire identifier TC. The local identifiers are then used
to instantiate the design module under test.
Writing a Test-Bench (5)
The response to the stimulus generated by the initial and
always blocks will appear at the output of the simulator as
timing diagrams.
It is also possible to display numerical outputs using
Verilog system tasks.
◼ $display – display one-time value of variables or
strings with end-of-line return,
◼ $write – same $display but without going to next line.
//D flip-flop
module D_FF (Q,D,CLK);
output Q;
input D,CLK;
reg Q;
always @(posedge CLK)
Q <= D;
endmodule
D Flip Flop
//D flip-flop with set and reset
module D_FF1 (Q,D,CLK, set, reset);
output Q;
input D,CLK,set,reset;
reg Q;
always @(posedge CLK)
begin
if (set ==1) Q <=1;
else if (reset ==1) Q<=0;
else Q <= D;
end
endmodule
Relational Operators-
Returns true or false ( 1 or 0)
Operator Description
a == b a equal to b
//Dataflow description of a
4-bit comparator.
module magcomp
(A,B,ALTB,AGTB,AEQB);
input [3:0] A,B;
output ALTB,AGTB,AEQB;
assign ALTB = (A < B);
assign AGTB = (A > B);
assign AEQB = (A == B);
endmodule
Arithmetic Operators
+ (addition) : A + B
- (subtraction) : A-B
* (multiplication) : A*B
/ (division) : A/B
% (modulo) : A % B
4-bit Full Adder
Dataflow Modeling (4)
4-to-1 line
multiplexer
Behavioral Modeling (4)
1. Net
Continuously driven
Cannot be used to store a value
2. Register
Retains the last value assigned
Often used to represent storage elements but
sometimes can refer to combinational cct
1. Net data type
Blocking assignment:
integer a,b,c;
Initial
begin
a=10; b=20; c=15;
a=b+c; a = 35
b=a+5; b = 40
c= a-b; c= -5
end
Example
Non-Blocking assignment:
integer a,b,c;
Initial
begin
a=10; b=20; c=15;
a<=b+c; a = 35
b<=a+5; b = 15
c<= a-b; c = -10
end
Blocking assignment
module blocking (clk,a,c);
input clk, a;
output c;
reg c,b;
always @ (posedge clk )
begin
b = a;
c = b;
end
endmodule
Non-Blocking assignment
module blocking (clk,a,c);
input clk, a;
output c;
reg c,b;
always @ (posedge clk )
begin
b< = a;
c< = b;
end
endmodule
Sequential System Design - Finite State Machine
– (Any sequential circuit that passes through finite number of states is a
FSM.)
Types of FSM
1. Mealy machine – Output depends on present state as well
as primary inputs
Mealy Machine
Sequential System Design (2)
1. Obtain the state diagram from the statement of the
problem.
2. From step 1, obtain state table.
3. Assign binary codes to the states.
4. Derive the flip-flop input equations from the next-state
entries in the encoded state table.
5. Derive output equations from the output entries in the
state table.
6. Simplify the flip-flop input and output equations.
7. Draw the logic diagram with D flip-flops and
combinational gates, as specified by the flip-flop I/O
equations.
FSM
Parameters
Mealy Machine
Sequential Circuit (2)
Moore Machine
FSM
BCD Counter
Flip-Flops and Latches
The D-latch is transparent and responds to a change in data
input with a change in output as long as control input is
enabled.
It has two inputs, D and control, and one output Q. Since Q
is evaluated in a procedural statement it must be declared
as reg type.
Latches respond to input signals so the two inputs are
listed without edge qualifiers in the event control
expression following the @ symbol in the always
statement.
There is one blocking procedural assignment statement and
it specifies the transfer of input D to output Q if control is
true.
Flip-Flops and Latches
module D_latch(Q,D,control);
output Q;
input D,control;
reg Q;
always @(control or D)
if(control) Q = D; //Same as: if(control=1)
endmodule
Flip-Flops and Latches
//D flip-flop //D flip-flop with asynchronous reset.
module D_FF (Q,D,CLK); module DFF (Q,D,CLK,RST);
output Q; output Q;
input D,CLK,RST;
input D,CLK;
reg Q;
reg Q;
always @(posedge CLK or negedge RST)
always @(posedge CLK)
if (~RST) Q = 1'b0; // Same as: if (RST = 0)
Q = D; else Q = D;
endmodule endmodule
Course : VLSI Design
TY BTech ECE
Manisha Ingle
Courtesy
Hardware modeling using verilog, IIT Kharagpur
Prof. Indranil Sengupta
Course Title : VLSI Design
Course Code : ECE3008B
Course Category : Professional Core
Course Contents:
• Verilog HDL Design: Study of Verilog HDL, Design using Verilog basic gates,
arithmetic circuits, basic combination circuits, sequential circuit, flip flops,
memories, shift registers, FSMs using Verilog HDL.
• VLSI Testing and Analysis: Types of fault, Need of Design for Testability
(DFT), Testability, Fault models, Path sensitizing, Test pattern generation,
Built-in Self-Test, JTAG & Boundary scan.
Laboratory Exercises / Practicals:
PART: A
• To write Verilog HDL code, simulate with test bench, synthesize,
implement on PLD
• 4:1 Mux,
• D/T-Flip-flop
• Half adder and Full Adder
• 2-bit comparator
PART: B
• To prepare and simulate CMOS layout and schematic in selected
technology Inverter,
• NAND, NOR gates
• Half Adder
• 2:1 multiplexer using logic gates and transmission gates.
Once you have carried out sufficient analysis and simulation to find
out, that your design is meeting your requirements in terms of power
consumption and delay, you can send it for fabrication.
HDLs
1. Verilog
2. VHDL
3. Some of the popular languages are like SystemC,
SystemVerilog and so on,
• reg X
• X= A + B will get assigned to X.
• This assignment will not be done using an assign statement; not continuous
assignment because, in an assign statement the left hand side must be a net
type variable, it cannot be a register type variable.
• A register type variable - actually storing a value, it is supposed to hold the value
till you use it again.
• Register is different from a net which is continuously driven and cannot hold any
value
• The net variable immediately changes, it cannot hold it, but this does not mean
that a register variable will always map to a hardware register.
Register data type - reg
• Variable x and y : by default 1-bit
• reg [15:0] bus - a variable bus, 16 bits, 0 is the least significant bit, 15
is the most significant bit.
• A reg type variable , when used in an arithmetic expression, it is
treated as an unsigned number without any sign.
• When hardware required is register based design like counter, shift
register, etc. then, use reg always.
• Two kinds of assignments: ( = , <= )
- A reg type variable A
- for these kind of assignments the left hand side must be a reg type
variable
Main body of the counter description:
• a statement called always : always at positive edge of the clock
(@posedge clk).
• Whenever there is a positive edge of the clock coming, check , if the
reset signal is one , i.e high or not, if it is high, initialize count value 32’b0
• Count is initialized to 0.
• If reset =1, count is 0, else count = count + 1;
• Expression with equal to (=) sign : - Only a reg type value in the left hand
side .
• Hence output is defined as count and reg also as count.
Example with register
Integer data type:
• More general purpose type,
• Can be used to store any arbitrarily integer values mainly for counting some
situations like loop counting.
• reg may not be very convenient , because the count value may sometimes
become negative, in reg, a variable declared of type reg is always considered to
be an unsigned quantity, it cannot have a negative value,
• Integer data type another feature is that it cannot have a size defined to it
(default size is 32-bits)
• Synthesis tool try to determine the size by carrying out some data flow analysis
wherever possible
(10-2 )
Content of register
No. 5
Similar to #define in C
Code becomes more readable
If inputs arbitrary coming on
t1 and t2 , what should be
the out of f ?
AND gate Flexibility on no of inputs
Primitive gates
• Buf (buffer) – to isolate signals, the logic value does not change, but
the signal values are restored,
• tri-state version of buffers :
• Bufif1: a buffer selected by a control signal ctrl.
– Control is one then out will be equal to in, if control is 0 then
output will be tri-state, it will be in the high impedance state
• bufif0: Polarity of the control is negative.
• notif1 and notif0: Output will be in bar and there will be a control,
this is notif1 and notif0. These tristate control gates are also
available.
time scale directive: `timescale 10ns/1ns
10ns : Basic unit of time.
#5 (5 units of gate delay) : 5*10ns= 50ns
1ns: precision of simulation. it specifies the precision with which we
round off the delays during simulation;
This simulation will be accurate up to 1 nanosecond
Valid values are 1,10,100 only not 1,2,3 etc.
Examples showing reg declaration generates wire and latch in two
level circuits
Tilda symbol
Reduction operator
{cout,sum} is concatenation: 2, 8-bit nos. are added and result is 9-bit
MSB in cout and 8-bit in sum
Verilog Modeling
MUX16_1
`timescale 1ns / 1ps
module testmux16();
reg[15:0] in;
0 in=xxxx, sel=x, out=x
reg[3:0] sel; 5 in=3f0a, sel=0, out=0
wire out; 10 in=3f0a, sel=1, out=1
Mux_161 M0(.in(in),.sel(sel),.out(out)); 15 in=3f0a, sel=6, out=0
initial 20 in=3f0a, sel=c, out=1
begin
$monitor($time,"in=%h,sel=%h,out=%b
", in,sel,out);
#5 in=16'h3f0a; sel=4'h0;
#5 sel=4'h1;
#5 sel=4'h6;
#5 sel=4'hc;
#5
$finish;
end
endmodule
16:1 Mux structural Modeling
reg
net
Vector with non constant index , synthesizer tool generates a mux
No mux is generated
Decoder or DMUX
En=0, Q=Q remembering the previous
state synthesizer generates
latch
Always- insied TB or in ckt descreption
Conditional statement: a==0 is not specified, synthesis tool generates and gate
and latch.
PROCEDURAL ASSIGNMENT (EXAMPLES)
• Wire
• Flip-flop (an edge-triggered storage element)
• Latch (a level-sensitive storage element)
• A variable in verilog can either be of net data type or register datatype
• Variable of net datatype generates H/W as wire.
• Variable of register datatype maps either to a wire or a storage element(FF or a
latch) depending on the context under which the variable is assigned a value.
• A register variable retains its value through the entire simulation run, thus inferring
memory
Data Values and Signal Strengths
• Verilog supports this 4 logic values 0, 1, x and z.
– 0 is logic 0, 1 is logic 1, x is unknown which is not initialized it
can be 0, it can be 1, but z is high impedance state.
• When initialized, all unconnected nets, the wires they are
initialized to z.
• If variables are declared of type register, registers are
initialized to x.
• If 2 signals are connected together, and if first signal is
stronger, then the final output will be dominated by the
stronger signal that is the concept of signal strength.
• In Verilog, there are 8 signal strengths as shown in table.
Simulation Features
• Simulation only features: make sense only during
simulation.
• During synthesis, those features does not mean
anything and the synthesis tool will be simply ignoring
them.
• Whenever we instantiate a gate, we specify some
delays of these gates, the synthesis tool ignore that.
• It is only for simulation purposes.
• Time is something that relates to the time of
simulation, it is never used during synthesis, such
variables will be ignored during synthesis.
Verilog always block
• always block ( for behavior level )
• assign ( for RT level )
• Thumb Rule for always block in combinatorial block In order to create
Verilog code that can generate synthesizable circuit, all inputs to the
hardware must appear in the sensitivity list. If it does not, then a latch
will result in place of a combinatorial logic.
• Combinational logic with always block:
• Output signal called z of type reg gets updated whenever one of the
signals in the sensitivity list changes its value.
Combinational ckt.
A ternary operator used to decide which input should be assigned to the output
Sequential logic with always
• Always block is mainly used to implement sequential logic
which uses memory elements like flipflops that can hold the
values.
• DFF: with asynchronous active-low reset
DFF: with synchronous active-low reset
initial Block
• A set of verilog statements are usually executed sequentially in a
simulation
• Statements are placed inside procedural block
• Procedural blocks in Verilog- initial and always
• First block delay of 20 units, second block delay of 50 units total, last
block delay is 60 units.
• Total simulation time 60 units.
• $finish- tells the simulator to terminate the current simulation
Case Statement
• Case statement checks if the given expression matches one of the other
expressions in the list and branches accordingly
• Typically used to implement a Mux.
• If-else construct may not be suitable if there are may conditions to be
checked and would synthesize into a priority encoder instead of mux.
• Verilog case statement starts with case keyword and ends with the
endcase keyword.
• A block of multiple statements must be grouped and be within begin and
end.
Mux using case
Task
• Tasks used in all programming languages, generally known as
procedures or subroutines.
• The lines of code are enclosed in task....end task brackets.
• Data is passed to the task, the processing done, and the result
returned.
• They have to be specifically called, with data ins and outs.
• Included in the main body of code, they can be called many
times, reducing code repetition.
• Tasks are defined in the module in which they are used.
• It is possible to define a task in a separate file and use the
compile directive 'include to include the task in the file which
instantiates the task.
Task
• tasks can include timing delays, like posedge, negedge, # delay
and wait.
• tasks can have any number of inputs and outputs.
• The variables declared within the task are local to that task.
• The order of declaration within the task defines how the
variables passed to the task by the caller are used.
• Tasks can take, drive and source global variables, when no local
variables are used.
• When local variables are used, basically output is assigned only
at the end of task execution.
• Tasks can call another task or function.
• Tasks can be used for modeling both combinational and
sequential logic.
• A task must be specifically called with a statement, it cannot be
used within an expression as a function can.
Syntax
• A task begins with keyword task and ends with keyword endtask
• Inputs and outputs are declared after the keyword task.
• Local variables are declared after input and output declaration.
module simple_task();
task convert;
input [7:0] temp_in;
output [7:0] temp_out;
begin
temp_out = (9/5) *( temp_in + 32)
end
endtask
endmodule
Task using Global Variables
module task_global();
reg [7:0] temp_out;
reg [7:0] temp_in;
task convert;
begin
temp_out = (9/5) *( temp_in + 32);
end
endtask
endmodule
Calling a Task
Task stored in a separate file.
Advantage of coding a task in a separate file, is that it can be used in multiple
modules.
module simple_function();
function myfunction;
input a, b, c, d;
begin
myfunction = ((a+b) + (c-d));
end
endfunction
endmodule
Calling a Function
module function_calling(a, b, c, d, e, f);
input a, b, c, d, e ;
output f;
wire f;
`include "myfunction.v"
assign f = (myfunction (a,b,c,d)) ? e :0;
endmodule
FSM and Types of FSM
FSM
• FSM are the heart of any digital design;
• A counter is a simple form of FSM.
• State machine Types
• Moore State Machine: outputs are only a function of the present state,
• Mealy State Machine: one or more of the outputs are a function of the
present state and one or more of the inputs.
Modeling State machines
• One thing that need to be kept in mind when coding
FSM is that combinational logic and sequence logic
should be in two different always blocks.
• In the above two figures, next state logic is always the
combinational logic.
• State Registers and Output logic are sequential logic.
• It is very important that any asynchronous signal to the
next state logic be synchronized before being fed to the
FSM.
• Always try to keep FSM in a separate Verilog file.
• Using constants declaration like parameter or `define
to define states of the FSM makes code more readable
and easy to manage.
Combinational design in synchronous circuit
• The output of these combinational designs can depend on
states only, or on the states along with external inputs.
• The former is known as Moore design and latter is known
as Mealy design
• Since, the sequential designs are sensitive to edge of the
clock, therefore the glitches can occur only at the edge of
the clock.
• Hence, the glitches at the edge can be removed by sending
the output signal through the D flip flop, as shown in Fig.
moore_regular_template2.v
next state logic : state_next
module moore_regular_template2 // This is combinational of the sequential design,
#( parameter // which contains the logic for next-state
param1 : <value>, // include all signals and input in sensitive-list except
param2 : <value> state_next
) // next state logic : state_next
( // This is combinational of the sequential design,
input wire clk, reset, // which contains the logic for next-state
input wire [<size>] input1, input2, ..., // include all signals and input in sensitive-list except
output reg [<size>] output1, output2 state_next
); always @(input1, input2, state_reg) begin
state_next = state_reg; // default state_next
localparam [<size_state>] // for 4 states : size_state = 1:0 case (state_reg)
s0 = 0, s0 : begin
s1 = 1, output1 = <value>;
s2 = 2, output2 = <value>;
... ; ...
reg[<size_state>] state_reg, state_next; if (<condition>) begin // if (input1 = 2'b01) then
// state register : state_reg state_next = s1;
// This process contains sequential part and all the D-FF are end
// included in this process. Hence, only 'clk' and 'reset' are else if (<condition>) begin // add all the required
// required for this process. conditionstion
always @(posedge clk, posedge reset) begin state_next = ...;
if (reset) begin end
state_reg <= s1; else begin // remain in current state
end state_next = s0;
else begin end
state_reg <= state_next; end
end s1 : begin
end output1 = <value>;
output2 = <value>;
• / optional D-FF to remove glitches
• always @(posedge clk, posedge reset)
• begin
• if (reset) begin
• new_output1 <= ... ;
• new_output2 <= ... ;
• end
• else begin
• new_output1 <= output1;
• new_output2 <= output2;
• end
• end
• endmodule
Lexical Conventions
• The basic lexical conventions used by Verilog HDL are similar to those in the C
programming language.
• Verilog contains a stream of tokens.
• Tokens can be comments, delimiters, numbers, strings, identifiers, and keywords.
• Verilog HDL is a case-sensitive language.
• All keywords are in lowercase.
1. Whitespace
• Blank spaces (\b) , tabs (\t) and newlines (\n) comprise the whitespace.
• Whitespace is ignored by Verilog except when it separates tokens.
• Whitespace is not ignored in strings.
2. Comments
• Comments can be inserted in the code for readability and
documentation.
• Two ways to write comments.
• A one-line comment starts with "//". Verilog skips from that point
to the end of line.
• A multiple-line comment starts with "/*" and ends with "*/".
• Multiple-line comments cannot be nested.
• However, one-line comments can be embedded in multiple-line
comments.
• a = b && c; // This is a one-line comment
• /* This is a multiple line
comment */
• /* This is /* an illegal */ comment */
• /* This is //a legal comment */
3 Operators
• Operators are of three types: unary, binary, and
ternary.
• Unary operators precede the operand.
• Binary operators appear between two operands.
• Ternary operators have two separate operators
that separate three operands.
• a = ~ b; // ~ is a unary operator. b is the operand
• a = b && c; // && is a binary operator. b and c are
operands
• a = b ? c : d; // ?: is a ternary operator. b, c and d
are operands
4 Number Specification
• There are two types of number specification in Verilog: sized and
unsized.
• Sized numbers are represented as <size> '<base format> <number>.
• <size> is written only in decimal and specifies the number of bits in
the number.
• Legal base formats are decimal ('d or 'D), hexadecimal ('h or 'H),
binary ('b or 'B) and octal ('o or 'O). The number is specified as
consecutive digits from 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b,c, d, e, f.
• Only a subset of these digits is legal for a particular base.
• Uppercase letters are legal for number specification.
• 4'b1111 // This is a 4-bit binary number
• 12'habc // This is a 12-bit hexadecimal number
• 16'd255 // This is a 16-bit decimal number.
Unsized numbers
• Numbers that are specified without a <base format> specification are decimal
numbers by default.
• Numbers that are written without a <size> specification have a default
number of bits that is simulator- and machine-specific (must be at least 32).
• 23456 // This is a 32-bit decimal number by default
• 'hc3 // This is a 32-bit hexadecimal number
• 'o21 // This is a 32-bit octal number
X or Z values
• Verilog has two symbols for unknown and high impedance values.
• These values are very important for modeling real circuits. An unknown value
is denoted by an x.
• A high impedance value is denoted by z.
• 12'h13x // This is a 12-bit hex number; 4 least significant bits unknown
• 6'hx // This is a 6-bit hex number
• 32'bz // This is a 32-bit high impedance number
Negative numbers
• Negative numbers can be specified by putting a minus sign before the size for
a constant number.
• Size constants are always positive. It is illegal to have a minus sign between
<base format> and <number>.
• An optional signed specifier can be added for signed arithmetic.
• -6'd3 // 8-bit negative number stored as 2's complement of 3
• -6'sd3 // Used for performing signed integer math
• 4'd-2 // Illegal specification
• Underscore characters and question marks
• An underscore character "_" is allowed anywhere in a number except the first
character.
• Underscore characters are allowed only to improve readability of numbers and
are ignored by Verilog.
• A question mark "?" is the Verilog HDL alternative for z in the context of
numbers. The ?
• 12'b1111_0000_1010 // Use of underline characters for readability
• 4'b10?? // Equivalent of a 4'b10zz
5 Strings
• A string is a sequence of characters that are enclosed by double quotes.
• The restriction on a string is that it must be contained on a single line, that is,
without a carriage return.
• It cannot be on multiple lines. Strings are treated as a sequence of one-byte
ASCII values.
• "Hello Verilog World" // is a string
• "a / b" // is a string
6 Identifiers and Keywords
• Keywords are special identifiers reserved to define the language constructs.
• Keywords are in lowercase.
• Keywords, System Tasks, and Compiler Directives.
• Identifiers are names given to objects so that they can be referenced in the
design.
• Identifiers are made up of alphanumeric characters, the underscore ( _ ), or
the dollar sign ( $ ).
• They cannot start with a digit or a $ sign (The $ sign as the first character is
reserved for system tasks).
• reg value; // reg is a keyword; value is an identifier
• input clk; // input is a keyword, clk is an identifier
System Tasks and Compiler Directives
• Two special concepts used in Verilog: system tasks and compiler directives.
1. System Tasks
• Verilog provides standard system tasks for certain routine operations.
• All system tasks appear in the form $<keyword>.
• Operations such as displaying on the screen, monitoring , values of nets,
stopping, and finishing are done by system tasks.
Most useful system tasks:
1. Displaying information
• $display is the main system task for displaying values of variables or strings or
expressions.
Usage: $display(p1, p2, p3,....., pn);
• p1, p2, p3,..., pn can be quoted strings or variables or expressions.
• The format of $display is very similar to printf in C.
• A $display inserts a newline at the end of the string by default.
• A $display without any arguments produces a newline.
Example 3-3 $display Task
• //Display the string in quotes
$display("Hello Verilog World");
-- Hello Verilog World
• //Display value of current simulation time 230
$display($time);
-- 230
• //Display value of 41-bit virtual address 1fe0000001c at time 200
reg [0:40] virtual_addr;
$display("At time %d virtual address is %h", $time, virtual_addr);
-- At time 200 virtual address is 1fe0000001c
• //Display value of port_id 5 in binary
reg [4:0] port_id;
$display("ID of the port is %b", port_id);
-- ID of the port is 00101
Monitoring information
• Verilog provides a mechanism to monitor a signal when its
value changes.
• This facility is provided by the $monitor task.
Usage: $monitor(p1,p2,p3,....,pn);
• The parameters p1, p2, ... , pn can be variables, signal names,
or quoted strings.
• A format similar to the $display task is used in the $monitor
task.
• $monitor continuously monitors the values of the variables or
signals specified in the parameter list and displays all
parameters in the list whenever the value of any one variable
or signal changes.
• Unlike $display, $monitor needs to be invoked only once.
• Only one monitoring list can be active at a time.
Monitor Statement
//Monitor time and value of the signals clock and reset
//Clock toggles every 5 time units and reset goes down at 10
time units
initial
begin
$monitor($time," Value of signals clock = %b reset = %b",
clock,reset);
end
• Partial output of the monitor statement:
-- 0 Value of signals clock = 0 reset = 1
-- 5 Value of signals clock = 1 reset = 1
-- 10 Value of signals clock = 0 reset = 0
• The task $stop is provided to stop during a simulation.
Usage: $stop;
• The $finish task terminates the simulation.
Usage: $finish;
Compiler Directives
• Compiler directives are provided in Verilog.
• All compiler directives are defined by using the `<keyword>
construct.
• Two most useful compiler directives.
• `define
• The `define directive is used to define text macros in Verilog.
• The Verilog compiler substitutes the text of the macro
wherever it encounters a `<macro_name>.
• This is similar to the #define construct in C.
• The defined constants or text macros are used in the Verilog
code by preceding them with a ` (back tick).
`define Directive
• //define a text macro that defines default word size
• //Used as 'WORD_SIZE in the code
'define WORD_SIZE 32
• //define an alias. A $stop will be substituted wherever 'S appears
'define S $stop;
• //define a frequently used text string
'define WORD_REG reg [31:0]
• // you can then define a 32-bit register as 'WORD_REG reg32;
`include
• The `include directive allows you to include entire contents of a Verilog source file in
another Verilog file during compilation.
• This works similarly to the #include in the C programming language.
• This directive is typically used to include header files, which typically contain global or
commonly used definitions.
`include Directive
• // Include the file header.v, which contains declarations in the
• // main verilog file design.v.
• 'include header.v
• ...
• ...
• <Verilog code in file design.v>
• (-) Means no input/colour (Moore machine)
• To represent three states we need 2-bits (binary encoding)
• State 11 is invalid
`timescale 1ns / 1ps
module cyclic_lamp (clk, light); s1: begin
input clk; light <= yellow;
state<= s2;
output reg [0:2] light;
end
parameter s0=0, s1=1, s2=2; s2: begin
parameter red=3'b100, light <= red;
green=3'b010, yellow=3'b001; state<= s0;
reg [0:1] state; end
default : begin
always @(posedge clk)
light <= red;
case (state) <=
state<= s0;
s0: begin end
light <= green; endcase
state<= s1; endmodule
end
• Nonblocking assignments (<=) are used
• Synthesis tool generates 2FFs for states because it has to remember it and
3FFs for light.
• This is because non blocking assignments are triggered by clock for both
state and light,
Optimized Code
`timescale 1ns / 1ps
// blocking assignment
module cyclic_lamp(clk,light); always @(state)
input clk; case (state)
output reg [0:2] light; s0: light = red;
parameter s0=0,s1=1,s2=2; s1: light = green;
s2: light = yellow;
parameter red=3'b100,
default : light<= red;
green=3'b010,
endcase
yellow=3'b001;
endmodule
reg [0:1] state;
Two always blocks.
always @(posedge clk)
• First always block (non blocking assignments):
case (state) only the state change happens as soon as
s0: state <= s1; positive edge of the clock comes
s1: state <= s2; (S0,S1,S2 Sequential Circuit)
s2: state <= s0; • Second always block (blocking assignment) :
triggering is not by clock, but by state.
default : state<= s0;
• whenever state changes synthesis tool can
endcase actually find out that what will be the output just
from the state. So, it will be a combinational
circuit.
`timescale 1ns / 1ps
module Test_light();
reg clk;
wire [0:2] light;
cyclic_lamp L0(clk, light);
always #5 clk=~clk;
initial
begin
clk=1'b0;
$monitor($time,"RGY: %b", light);
#100 $finish;
end
endmodule
• n-bit word
• Parity - whether the number of 1s in that word is odd or even.
• Odd- it is odd parity
• Even - even parity.
`timescale 1ns / 1ps
always @(even_odd)
module Moore_parity(x,clk,z); case (even_odd)
input x,clk; EVEN: z=0;
output reg z; ODD: z=1;
reg even_odd; endcase
parameter EVEN=0, ODD=1; endmodule