Verilog
Verilog
Jorge Ramírez
Corp Application Engineer
SystemVerilog is a superset of
Verilog-2005, with many new
features and capabilities to aid
design-verification and design-
modeling
Synopsys University Courseware
Copyright © 2011 Synopsys, Inc. All rights reserved.
Developed by: Jorge Ramirez
Types of modeling
• Behavioral
– Models describe what a module rst Counter
cnt [0:3]
does. clk
If (rst)
cnt = 0;
else
• <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 © 2011 Synopsys, Inc. All rights reserved.
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
endmodule
Synopsys University Courseware
Copyright © 2011 Synopsys, Inc. All rights reserved.
Developed by: Jorge Ramirez
Data storage and Verilog arrays
Counter
module cter (input rst, clock, jmp,
input [7:0] jump,
output reg [7:0] count
);
always@(posedge clock)
begin
if (rst) count = 8’h00;
else if (jmp) count = jump + count;
else count = count + 8’h1;
end
endmodule
top.v TOP
comp.v COMP
mux.v
MUX
Synopsys University Courseware
Copyright © 2011 Synopsys, Inc. All rights reserved.
Developed by: Jorge Ramirez
Module declaration
• 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 //…
endmodule
Synopsys University Courseware
Copyright © 2011 Synopsys, Inc. All rights reserved.
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,
output reg Cout
ANSI C style );
//module internals
endmodule
Synopsys University Courseware
Copyright © 2011 Synopsys, Inc. All rights reserved.
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> #(<param list>)
<instance name> (<port list>);
Where:
<module name> Module to be instantiated
<param list> Parameters values passed to the instance
<instance name> Identifies the instance of the module
<port list> Port list connection
Synopsys University Courseware
Copyright © 2011 Synopsys, Inc. All rights reserved.
Developed by: Jorge Ramirez
Port list connections
• Ports
– Provide the interface by which a module can
communicate with the environment
– Port declarations (input, output, inout)
input output
net inout
net
/* replicate a 3 times,
{{}} b = {3{a}}
equivalent to {a,a,a} */
Synopsys University Courseware
Copyright © 2011 Synopsys, Inc. All rights reserved.
Developed by: Jorge Ramirez
Operators precedence
Operator precedence
Unary, Multiply, Divide, +,-,!,~ Highest
Modulus *, / %
+, -
Add, subtract, shift
<<. >>
<, < =, >, >=
Relational
=, ==. !=
Equality
===, !==
&, ~&
^, ^~
Reduction
|, ~|
Logical
&&
||
Conditional ?: Lowest
Synopsys University Courseware
Copyright © 2011 Synopsys, Inc. All rights reserved.
Developed by: Jorge Ramirez
Outline
• Lexical elements
• Data type representation
• Structures and Hierarchy
• Operators
• Assignments
• Control statements
• Task and functions
• Generate blocks
Synopsys University Courseware
Copyright © 2011 Synopsys, Inc. All rights reserved.
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
– Continuous assignments, and procedural blocks
are concurrent within a module
module AND (input A, B, output C);
wire w;
NAND U1 (A, B, w);
NAND U2 (w, w, C);
endmodule
Synopsys University Courseware
Copyright © 2011 Synopsys, Inc. All rights reserved.
Developed by: Jorge Ramirez
Continuous assignments
• Continuous assignments imply that whenever
any change on the RHS of the assignment
occurs, it is evaluated and assigned to the LHS
• Continuous assignments always implement
combinational logic
• Continuous assignments drive wire variables
wire A;
assign A = (B|C)&D;
Synopsys University Courseware
Copyright © 2011 Synopsys, Inc. All rights reserved.
Developed by: Jorge Ramirez
Continuous assignments
• Implicit continuous assignment
– Continuous assignment can be placed when the
net is declared
wire A = i1 & i2;
• Implicit net declaration (not recommended)
– If a signal name is used to the left of a continuous
assignment, a implicit net declaration will be
inferred
wire i1, i2;
assign A = i1 & i2;
Synopsys University Courseware
Copyright © 2011 Synopsys, Inc. All rights reserved.
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
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 © 2011 Synopsys, Inc. All rights reserved.
Developed by: Jorge Ramirez
Outline
• Lexical elements
• Data type representation
• Structures and Hierarchy
• Operators
• Assignments
• Control statements
• Task and functions
• Generate blocks
Synopsys University Courseware
Copyright © 2011 Synopsys, Inc. All rights reserved.
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 © 2011 Synopsys, Inc. All rights reserved.
Developed by: Jorge Ramirez
Generate loop
Synthesis Simulation
Unknown value
Don’t care
Assigning “X” to a wire or reg is Be aware, when
highly encouraged for synthesis: assigning X’s they may
it specifies a don’t care propagate throughout
condition, letting the synthesis your design under
tool do further optimization
simulation
initial
begin
clk = 0;
forever #5 clk = ~clk;
end
SUB2.2 endmodule
wire data;
Clocked Storage
Elements
Sn Sn+1
Clock
DCQ DCQ
U U
Y= Sn-1 Sn Sn+1
Time
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)
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 © 2011 Synopsys, Inc. All rights reserved.
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 © 2011 Synopsys, Inc. All rights reserved.
Developed by: Jorge Ramirez
HDL FSM Implementation
The Fundamentals of Efficient Synthesizable
Finite State Machine.
Clifford E. Cummings
http://www.sunburst-
design.com/papers/CummingsICU2
002_FSMFundamentals.pdf
3. Added “full” and 1 case (1’b1) //ambit synthesis case full, parallel
state[IDLE] : if (in1) next[BBUSY] = 1’b1;
3