Verilog Tutorial
Verilog Tutorial
Verilog HDL
Verilog HDL
Though Verilog is C like in syntax but has distinct character and interpretation. A programmer must set his perception right before coding in Verilog. He must visualize hardware in his mind while structuring Verilog modules consisting of procedural blocks and assignments.
Design Specification
HW
SW
Functional Verification
System Integration
Synthesis
Layout
C++ Implementation
C++ Implementation
Transactor
Checker
Transactor
TRANSLATOR
Transactor Coverage
Checker
Transactor
Coverage TLM Driver Monitor
TLM
DUT
RTL
Design Components
Verilog Standards
- 1995: IEEE Standard 1364-1995 (Verilog 95) - 2002: IEEE Standard 1364-2001 (Verilog 2001) - 2003: IEEE Standard 1364-2001 revision C - 2005: IEEE Standard 1364-2005 (Verilog 2005) 1364-2005 IEEE Standard for Verilog Hardware Description Language - 2005: IEEE Standard 1800-2005 (SystemVerilog) 1800-2005 IEEE Standard for System Verilog: Unified Hardware Design, Specification and Verification Language
Modules
The Module Concept
The module is the basic building block in Verilog Modules are:
Declared Instantiated
Verilog-95
Verilog-2001
Module Template
module FA (<port declaration>); . . . . . . endmodule module FA( input a, input b, input c_in, output sum, output c_out); assign {c_out, sum} = a+b+c_in; endmodule
(a)
(b)
Hierarchical Design
Verilog code contains a top-level module and zero or more instantiated modules The top-level module is not instantiated anywhere Several copies of a lower-level module may exist Each copy stores its own values of regs/wires Ports are used for interconnections to instantiated modules Order of ports on first line of module definition determine order for connections of instantiated module Order of listing inputs, outputs, and inouts on the following lines is not important
(a)
(b)
a[1]
b[1]
a[2]
b[2]
1 cin
fa0
fa1
fa2
cout
FA
FA
FA
carry[1] sum[2]
sum 3
(a)
(b)
Design Partitioning
8 mem AES Port A/B Encryption & Round Key Local mem circuit clk
NRZ-IF
serial-data
ASP AES
aes_done
FEC-IF
serial clk
FEC Encoding
Read
Write
Read Port A
Write words to
Port A
Port B
Port B
Port B
Port A
Port B
FIFO AES 8
FEC FIFO
Framer FIFO
fec_done
D/A Interface
X
jn e
GMSK Modulator
frm_bit_valid
Framer
Framer IF
Selection logic
Tx_out
mod_bit_stream
frm_bit_stream
Module 1
Module 2
Cloud 1
Cloud 2
Cloud 3
Module 3
glue logic
Module 2
Synthesis guideline
A bad design where time critical and non critical logic are placed in the same module
Module 1
Critical Logic
Non-Critical Logic
Timing
Area
Synthesis guideline A good design places critical logic and non critical logic in separate modules
Module 1 Module 2
Critical Logic
Non-Critical Logic
Timing
Area
VERILOG SYNTEXT
0 1 x z
one, logic high, or power unknown high impedance, unconnected, or tri-state port
Data Types
Nets
Nets are physical connections between components Nets always show the logic value of the driving components Many types of nets, we use wire in RTL
Registers
Implicit storage unless variable of this type is modified it retains previously assigned value Does not necessarily imply a hardware register Register type is denoted by reg
Variable Declaration
Declaring a net, signed or unsigned wire [<signed>] [<range>] <net_name> [<net_name>*]; Range is specified as [MSb:LSb]. Default is one bit wide Declaring a register reg [<signed>] [<range>] <reg_name> [<reg_name>*]; Declaring memory reg [<range>] <memory_name> [<start_addr> : <end_addr>]; Examples reg r; // 1-bit reg variable wire w1, w2; // 2 1-bit wire variable reg signed [7:0] vreg; // 8-bit sign register reg [7:0] memory [0:1023]; //a 1 KB memory
Constants
decimal (default)
13, d13
binary
4b1101
octal
4o15
hexadecimal
4hd
Levels of Abstractions
Switch Level: The lowest level of abstraction is the switch or transistor Level Modeling. Gate Level: Synthesis tools compile high level code and generate code at gate level. Dataflow Level: The level of abstraction higher than the gate level. Behavioral Level: In more complex digital designs, priority is given to the performance and behavior algorithm.
in1
out1
sel
out
in2 out2
module mux (out, in1, in2, sel); output out; input in1, in2, sel; wire out1, out2, sel_n; and #5 a1(out1, in1, sel_n); and #5 a2(out2, in2, sel); or #5 o1(out, out1, out2); not n1(sel_n, sel); endmodule (b)
(a)
Dataflow Modeling
Expressions, operands and operators form the basis of dataflow modeling.
Operators
/ ~| % ^ ** ~^ ^~
Unary reduction
&
~&
~|
^,
~^
--
Logical
&&
||
==
===
!=
!==
==
===
< >>
> <<
<=
>=
Arithmetic shift
>>>
<<<
?: {} {{ }}
Arithmetic Operators
Operator Type Arithmetic Operator Symbol Operation Performed
Multiply
/ +
% **
Divide Add
Subtract Modulus Power
Conditional Operator
Operator Type
Operator Symbol
Operation Performed
Conditional
?:
Conditional
Conditional Operator
out = sel ? a : b;
This statement is equivalent to following decision logic.
Concatenation
{}
Concatenation
Replication
{{}}
Replication
MSB
LSB
13 bits
Relational Operator
Relational Operator
Operator Symbol
> < >=
Operation Performed
Greater than Less than Greater than or equal
<=
Reduction Operators
Operator Type Reduction & ~& | ~| ^ Operator Symbol Operation performed Reduction and Reduction nand Reduction or Reduction nor Reduction xor
^~ or ~^
Reduction xnor
Operator Symbol
~ & ~& | ~| ^ ^~ or ~^
Operation Performed
Bitwise negation Bitwise AND Bitwise NAND Bitwise OR Bitwise NOR Bitwise XOR Bitwise XNOR
Equality Operators
Operator Type Equality Operator Symbol == != === !== Operation performed Equality Inequality Case Equality Case Inequality
Logical Operators
Operator Type
Logical
Operator Symbol
! || &&
Operation Performed
Logical Negation Logical Or Logical AND
Shift Operators
Operator Type
Logic Shift
Operator Symbol
>> <<
Operation Performed
Unsigned Right Shift Unsigned Left Shift Signed Right Shift
Arithmetic Shift
>>>
<<<
Example
Shift an unsigned reg A = 6b101111 by 2 B = A >> 2; drops 2 LSBs and appends two zeros at MSBs position, thus B = 6b001011
Example
Arithmetic shift right a wire A= 6b101111 by 2 B = A >>> 2; This operation will drop 2 LSBs and appends the sign bit to 2 MSBs locations. Thus B is 6b111011.
Example
Apply & reduction operator on a 4-bit number A=4b1011 assign out = &A; This operation is equivalent to performing a bitwise & operation on all the bits of A i.e.
output
co;
endmodule
module mux2_1(in1, in2, sel, out); input in1, in2, sel; output out;
module stimulus;
initial begin
endmodule
IN1 = 1; IN2 = 0; SEL = 0; #5 #5 end initial $monitor($time, ": IN1=%b, IN2=%b, SEL=%b, OUT=%b\n", IN1, IN2, SEL, OUT); endmodule SEL = 1; IN1 = 0;
Behavioral Modeling
High level language constructs are used
for loop if else while etc
end
end
initial Block
This block starts with initial keyword This is non synthesizable Non RTL This block is used only in stimulus All initial blocks execute concurrently in arbitrary order They execute until they come to a #delay operator Then they suspend, putting themselves in the event list delay time units in the future At delay units, they resume executing where they left off.
Procedural assignments
Blocking assignment =
Regular assignment inside procedural block Assignment takes place immediately LHS must be a register
always begin A=B, B=B
A=B B=A
end
Procedural assignments
Nonblocking assignment <=
Compute RHS Assignment takes place at end of block LHS must be a register
always begin Swap A and B
A <= B B <= A
end
reg sum, carry; always @ (x or y) begin sum = x^y; carry = x&y; end
reg sum, carry; always @ (x, y) begin sum = x^y; carry = x&y; end
reg sum, carry; always @ (*) begin sum = x^y; carry = x&y; end
(a)
(b)
(c)
$time A built-in variable that represents simulated time a unitless integer $display($time, a=%d, a); # Time Control #<number> statement statement is not executed until <number> time units have passed control is released so that other processes can execute used in test code used to model propagation delay in combinational logic
# Time Control
# Time Control
Delay parameter for a built-in logic gate wire c; xor #2 x2(c, a, b);
@ Time Control
@(expression) @(expression or expression or ) @(posedge onebit) @(negedge onebit) do not execute statement until event occurs @(clk) is same as @(posedge clk or negedge clk)
reg sum_reg, carry_reg; always @ (posedge clk) begin sum_reg <= x^y; carry_reg <= x&y; end
Event Control @
Delay execution until event occurs
@ Time Control
Used to model combinational logic behaviorally
always @ (a, b) // equivalent is (a or b) c = a^b;
Combinational cloud
Combinational cloud
Combinational cloud
Combinational cloud
Register
Combinational cloud
Combinational cloud
(a)
(b)
A Feedback Register
1 Logic acc
1 Logic
rst_n
Initial // All the initializations should be in the initial block begin clk = 0; # 5 rst_n = 0; // pull it low
// resetting inside the module always @ (posedge clk or negedge rst_n) begin If (! rst_n) // If rst_n=0 reg a <= 4b0; else a_reg <= a; end
acc.v
Conditional statements
If, If-else
case
Could also use casez (treats z as dont cares ) and casex ( treats z and x as dont cares)
Loop Statements
Repeat
While
For
module2 wire
wire wire
inout
Port Definitions
Input Ports
Always wire
Output Ports
wire if dataflow modeling constructs are used reg if behavioral modeling I.e. assignment is made in a procedural block
Inout Ports
always wire
Simulation Control
$finish Specifies when simulation ends $stop Suspends the simulation and enters interactive mode $display Prints output using format similar to C and creates a new line $monitor Similar to $display but active all the time
Prints its string when one of the listed values changes Only one monitor can be active at any time Prints at the end of current simulation time Display is like printf( ) $monitor ( $time, A=%d, B=%d, CIN=%b, SUM=%d, COUT=%d, A, B, CIN, COUT ); $display ( $time, A=%d, B=%d, CIN=%b, SUM=%d, COUT=%d, A, B, CIN, COUT );
$monitor
(a)
(b)
(a) Verilog code to infer a register with synchronous active low reset (b) Verilog code to infer a register with synchronous active high reset
// register with synchronous active low reset always @ (posedge clk) begin If (! rst_n) r_reg <= 4b0; else r_reg <= data; end endmodule // register with synchronous active high reset always @ (posedge clk) begin If (rst) r_reg <= 4b0; else r_reg <= data; end endmodule
(a)
(b)
Avoid Latches
input [1:0] sel; reg [1:0] out_a, out_b;
always @ (*) begin out_a = 2b00; out_b = 2b00; if (sel=2b00) begin out_a = 2b01; out_b = 2b10; end else out_a = 2b01; end
always @* begin out_a = 2b00; out_b = 2b00; if (sel==2b00) begin out_a = 2b01; out_b = 2b10; end else if (sel == 2b01) out_a = 2b01; end
$stop;
endmodule
clk rst_n
Value 0
Instantiation of the module for adding 8-bit inputs in1 and in2
module stimulus; reg [7:0] in1, in2; wire [7:0] sum_byte; reg c_in; wire c_out; adder #8 add_byte (in1, in2, c_in, sum_byte, c_out); . . endmodule
Parameterized code
module adder #(parameter SIZE1 = 4, SIZE2=6) (input [SIZE1-1: 0] a, Input [SIZE2-1: 0] b, output [SIZE2-1: 0] sum, input c_in, output c_out);
Parameter
Parameters are not variables
they are constants
Macros
`define DIFFERENCE 6b011001 The use of the define tag is shown here. if (ctrl == `DIFFERENCE)
Preprocessing commands
`ifdef G723 $display (G723 execution); `else $display (other codec execution); `endif
Verilog Task
module RCA( input [3:0] a, b, input c_in, output reg c_out, output reg [3:0] sum ); reg carry[4:0]; integer i; task FA( input in1, in2, carry_in, output reg out, carry_out); {carry_out, out} = in1 + in2 + carry_in; endtask always@* begin carry[0]=c_in; for(i=0; i<4; i=i+1) begin FA(a[i], b[i], carry[i], sum[i], carry[i+1]); end c_out = carry[4]; end endmodule
Verilog Function
module MUX4to1( input [3:0] in, input [1:0] sel, output out); wire out1, out2; function MUX2to1; input in1, in2; input select; assign MUX2to1 = select ? in2:in1; endfunction assign out1 = MUX2to1(in[0], in[1], sel[0]); assign out2 = MUX2to1(in[2], in[3], sel[0]); assign out = MUX2to1(out1, out2, sel[1]); endmodule
module testFunction; reg [3:0] IN; reg [1:0] SEL; wire OUT;
MUX4to1 mux(IN, SEL, OUT); initial begin IN = 1; SEL = 0; #5 IN = 7; SEL = 0; #5 IN = 2; SEL=1; #5 IN = 4; SEL = 2; #5 IN = 8; SEL = 3; end initial $monitor($time, " %b %b %b\n", IN, SEL, OUT); endmodule