Introduction To Verilog
Introduction To Verilog
SystemVerilog is a superset of
Verilog-2005, with many new
features and capabilities to aid
design-verification and design-
modeling
Synopsys University Courseware
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
Types of modeling
• Behavioral
– Models describe what a module rst Counter
If (rst)
does. clk
cnt = 0;
else
cnt = cnt+1;
cnt [0:3]
• <size>:
– number of bits (optional)
• <base format>:
– It is a single character ' followed by one of the following
characters b, d, o and h, which stand for binary, decimal,
octal and hex, respectively.
• <number>
– Contains digits which are legal for the <base format>
– ‘_’ underscore can be use for readability
Synopsys University Courseware
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
Number representation
• Negative numbers are store as 2’s
complement
• Extended number
– If MSB is 0, X or Z number is extended to fill MSBs
with 0, X, Z respectively
3’b01=3’b001 3’bx1=3’bxx1 3’bz=3’bzz
• 2 declaration flavors:
Inside a module In module header
module test (... I/O’s ...); module test
parameter ASIZE = 32, BSIZE =16; #(parameter ASIZE = 32, BSIZE =16)
//... (... I/O’s ...);
reg [ASIZE -1:0] Abus, Zbus; //...
wire [BSIZE-1:0] Bwire; reg [ASIZE -1:0] Abus, Zbus;
//... wire [BSIZE-1:0] Bwire;
endmodule //...
Synopsys University Courseware
endmodule
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
Example
module Adder (A, B, Cin, S,
A[N-1:0]
Cout, Clk); Adder S[N-1:0]
parameter N=8; B[N-1:0]
N-bits
input [N-1:0]A, B;
(8 by default) Cout
input Cin; Cin
reg-ouputs
input Clk;
output [N-1:0] S;
output Cout;
reg [N-1:0] S; Clk
reg Cout;
//module internals module Adder #(parameter N=8)
endmodule (input [N-1:0]A, B,
input Cin,
input Clk,
output reg [N-1:0] S,
ANSI C style output reg Cout
);
//module internals
endmodule
Synopsys University Courseware
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
Structures and Hierarchy
• Instance of a module
– Instantiation is the process of “calling” a module
– Create objects from a module template
<module name> #(<parameter list>)
<instance name> (<port list>);
Where:
<module name> Module to be instantiated
<parameter list> Parameters values passed to the instance
<instance name> Identifies the instance of the module
<port list> Port list connection
Synopsys University Courseware
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
Structures and Hierarchy
• Ports list connections
– Provide the interface by which a module can
communicate with the environment
– Port declarations (input, output, inout)
input output
net inout
net
Synopsys University Courseware
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
Port connections/Parameter overwrite
• Named connection
– Explicitly linking the 2 names for each side of the
connection
my_mod #(.W(1), .N(4)) U1 (.in1(a), .in2(b),
.out(c));
• Order connection
– Expression shall be listed in the same order as the
port declaration
my_mod #(1,4) U2 (a, b, c);
Synopsys University Courseware
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
Example
module TOP (input t1, t2, t3, output t4, t5, t6);
wire w1, w2, w3;
ALU #(.S(2)) U1 (.a1(t1), .a2(t2), .a3(t3), a4(w1), a5(w2), a6(w3));
MEM U2 (w1, w2, w3, t4, t5, t6);
endmodule
module ALU (input a1, a2, a3, output a4, a5, a6);
TOP
parameter S=4; U1
reg rsl;
wire sig; ALU S1
FIFO S1 (.f1(rsl), .f3(sig)); FIFO
//...
endmodule
U2
module FIFO #(parameter F=2) MEM
(input f1, output reg f2);
//...
endmodule
/* replicate a 3 times,
{{}} b = {3{a}}
equivalent to {a,a,a} */
Synopsys University Courseware
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
Operators precedence
+,-,!,~ Highest
Unary, Multiply, Divide, Modulus
*, / %
+, -
Add, subtract, shift
<<. >>
Operator precedence
<, < =, >, >=
Relational
=, ==. !=
Equality
===, !==
&, ~&
^, ^~
Reduction
|, ~|
Logical
&&
||
Conditional ?: Lowest
Synopsys University Courseware
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
Outline
• Lexical elements
• Data type representation
• Structures and Hierarchy
• Operators
• Blocks and Assignments
• Control statements
• Task and functions
• Generate blocks
Synopsys University Courseware
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
Concurrent blocks
• Blocks of code with no well-defined order relative
to one another
– Module instance is the most important concurrent
block module AND (input A, B, output C);
wire w;
NAND U1 (A, B, w);
NAND U2 (w, w, C);
endmodule
wire A;
assign A = (B|C)&D;
Synopsys University Courseware
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
Example
A module NAND (A, B, C);
C input A, B;
output C;
B // Continuous assignments
assign C = ~(A&B);
endmodule
R G
B B
A D Q D Q C A D Q
CLK CLK
case (<expression>)
<alternative 1> : <statement 1>;
<alternative 2> : <statement 2>;
default : <default statement>;
endcase
for (<loop var init>; <loop var reentry expr>; <loop var update>)
<statement>;
always @*
while(delay)
// Multiple statement groups with begin-end
begin
ldlang = oldldlang;
delay = delay – 1;
end
always @*
repeat(`BIT-WIDTH)
begin
if (a[0]) out = b + out;
a = a << 1;
end
• Data Sharing
– Functions and task could be declare as automatic
– A static function retains the value of all it's internal
variables between calls. An automatic function re-
initializes them each call
Synopsys University Courseware
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
Outline
• Lexical elements
• Data type representation
• Structures and Hierarchy
• Operators
• Blocks and Assignments
• Control statements
• Task and functions
• Generate blocks
Synopsys University Courseware
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
Generate blocks
• Allow to generate Verilog code dynamically at
elaboration time
– Facilitated the parameterized model generation
– Required the keywords generate – endgenerate
– Generate instantiations can be
• Module instantiations
• Continuous assignments
• initial / always blocks
– Typically used are generate loop and conditional
generate
Synopsys University Courseware
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
Generate loop
Synthesis Simulation
Unknown value
Don’t care
Assigning “X” to a wire or Be aware, when
reg is highly encouraged for assigning X’s they may
synthesis: it specifies a don’t propagate throughout
care condition, letting the your design under
synthesis tool do further simulation
optimization
Synopsys University Courseware
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
VERILOG TEST BENCH
initial
begin
clk = 0;
forever #5 clk = ~clk;
end
Clocked Storage
Elements
Sn Sn+1
Clock
DCQ DCQ
U U
Y= Sn-1 Sn Sn+1
Time
Synopsys University Courseware
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
Finite-State machine
Critical Path
chain of gates in
Clocked Storage
the longest Elements
(slowed) path
thought the logic Present State: Sn
Clock
Next State Sn+1
Sn+1 = f (Sn, X)
http://www.sunburst-
design.com/papers/CummingsICU2
002_FSMFundamentals.pdf
7. Added “full” and 5 case (1’b1) //ambit synthesis case full, parallel
state[IDLE] : if (in1) next[BBUSY] = 1’b1;
7
Level to Pulse
L P
Converter
Whenever input L goes from low to high... ...output P produces a single pulse, one clock period wide.
CLK
Synopsys University Courseware
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
FSM Implementation
State Diagram (Moore implementation)
For S0+:
L\S1S0 00 01 11 10
0 0 0 0 X
1 1 1 1 X
Speed
Area Power
Binary Gray
Synopsys University Courseware
Copyright © 2012 Synopsys, Inc. All rights reserved.
Verilog HDL
Developed By: Jorge Ramirez
REFERENCES