Verilog - Chapter2 - Fundamental Concepts
Verilog - Chapter2 - Fundamental Concepts
3
Lexical conventions
Comments :
- Two forms to introduce comments:
1. //……….//
2. /*…….....*/
Ex:
module FlipFlop (din, clk, qout);
input din, clk;
output qout;
reg qout;
// At the rising edge of clk, qout <= din //
/* At the rising edge of clk, qout <= din */
always @ (posedge clk) begin
qout <= #8 din;
endmodule
4
Lexical conventions
Numbers :
- Two forms to express numbers:
1. 37 32 bit decimal 37
2. <size>’<base_format><number>
Ex:
10’hFA 10 bits hexadecimal number FA (00_1111_1010)
1’b0 1 bit binary number 0 (0)
6’d30 6 bits decimal number (011110), decimal 30
15’o10752 15 bits octal number (001,000,111,101,010),
decimal 4586
4’b0 is equal to 4’b0000
4’b1 is equal to 4’b0001
4’bz is equal to 4’bzzzz
4’bx is equal to 4’bxxxx
-8 ‘d 6 The two’s complement of 6, held in 8 bits
5
Lexical conventions
Strings :
-A string is a sequence of characters enclosed by double quotes and all contained on
a single line. Verilog treats strings used as operands in expressions and assignments as
a sequence of eight-bit ASCII values, with one eight-bit ASCII value representing one
character.
Ex:
“Hello world”
String variable declaration:
- To store the string “Hello world” requires a register 8*11, or 88 bits wide:
reg [8*11:1] stringvar;
initial
begin
stringvar = “Hello world”;
end
6
Lexical conventions
Strings manipulation:
- It can be manipulated with strings.
Ex:
module string_test;
reg [8*11:1] stringvar;
initial
begin
stringvar = “Hello”;
$display(“%s is stored as %h”,stringvar,stringvar);
stringvar = {stringvar,” world”};
$display(“%s is stored as %h”,stringvar,stringvar);
end
endmodule
7
Lexical conventions
8
Lexical conventions
Keywords :
- Keywords are used to define the language constructs. There are a lot of
keywords in Verilog HDL. (Refer to Verilog books)
- All keywords are defined in lower case
- Do not use keywords as user’s definition.
Examples :
module, endmodule fork, join
input, output, inout specific, endspecific
reg, integer, real, time timescale
not, and, nand, or, nor, xor include
parameter undef
begin, end nmos, pmos,… 9
Lexical conventions
System tasks and functions :
– They are considered part of the Verilog HDL. These system tasks and
functions are divided into some categories as follows:
+ Display tasks : $display, $monitor, $strobe, $writ, $dumpfile,
$dumpvars…
+ File I/O tasks : $fclose, $fdisplay, $swrite, $fread, $sdf_annotate,
$readmemb, $readmemh…
+ Simulation control tasks: $finish, $stop
+ Math functions: $ln, $log10, $exp, $sqrt, $sin, $cos, $asin, $acos…
Ex:
$display (“Hello world”);
$finish;
10
Lexical conventions
11
Lexical conventions
Compiler directives
– The scope of a compiler directive extends from the point where it is processed,
across all files processed, to the point where another compiler directive supersedes
it or the processing completes.
– There are some common compiler directives:
+ `celldefine
+ `endcelldefine
+ `define
+ `else
+ `elsif
+ `ifdef
+ `endif
+ `include
+…
12
Lexical conventions
Compiler directives
– Example
13
Lexical conventions
14
Data types
Value set :
- Four basic values:
1. 0 – represents a logic zero, logic low, ground or false condition.
2. 1 – represents a logic one, logic high, power or true condition.
3. x – represents an unknown logic value.
4. z – represents a high-impedance, unconnected, tri-state.
‘0’
‘X’
‘1’ ‘Z’
0
15
Data types
Nets data types
- Nets data types present the physical connections between devices. A net does
not store a value, it must be driven by a gate or continuous assignment. If a net
variable has no driver, then it has a high-impedance value (z).
- Net data type can be declared by following keywords : wire, wand, wor,
supply0, supply1, …
- Cannot be assigned in an initial or always block
• Net data type represent physical connections between structural entities.
• A net must be driven by a driver, such as a gate or a continuous assignment.
• Verilog automatically propagates new values onto a net when the drivers
change value.
Ex:
- wire w1, w2; // declares 2 wires
- wand w;
16
Data types
Net data types (cont’d)
17
Data types
Registers
- Registers present abstract storage elements. Registers are data types that hold the
assigned values until a new value assigned to it. A new value can be assigned to
registers only by using procedural assignments.
- Register data type can be declared by using “reg” keyword.
– Don’t confuse reg assignments with the combinational continuous assign statement!
(more soon)
– Reg should only be used with always blocks (sequential logic)
Ex:
- reg a; // a scalar register
– reg[3:0] v; // a 4-bit vector register made up of (from most to least significant) v[3],
v[2], v[1] and v[0]
– reg [1:4] b; // a 4-bit vector register
– reg signed [0:3] signed_reg; // 4-bit signed register with a range of -8 to +7
– reg signed [0:3] signed_mem [99:0] // 100 words with a range of -8 to +7
18
Data types
Register (cont’d)
– A variables used in behavioral description
– A storage device or a temporary variable
– Types of register:
• reg : unsigned integer variables of varying bit width
• integer : 32-bit signed integer
• real : signed floating-point
• time : 64-bit unsigned integer
19
Data types
Variable declarations
– Declaring a net
wire [<range>] <net_name> [<net_name>*];
Range is specified as [MSb:LSb]. Default is one bit wide
– Declaring a register
reg [<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 [7:0] vreg; // 8-bit register
reg [7:0] memory [0:1023]; a 1 KB memory
20
Port and Data types
Correct data types for ports
Module
net
inout
net
21
Data types
Example
Module : MM
B
A D Q
module MM (Q,A,CLK) ;
output Q ; CLK CLK
input A,CLK;
wire B
reg Q ;
assign B = ! A;
always @ (negedge CLK)
Q <= B;
endmodule
22
Data types
Signed objects:
Ex:
wire signed [3:0] signed_wire; // range -8 <-> +7
reg signed [3:0] signed_reg; // range -8 <-> +7
reg signed [3:0] signed_mem [99:0] // 100 words range -8 <-> +7
- A signed value will not cross hierarchical boundaries. If you want a signed
value in other modules in a hierarchy, you must declare them in each of the
modules where signed arithmetic is necessary.
23
Data types
Scalar and Vector:
– A net or reg declaration without a <range> specification is one bit wide; that is,
it is scalar.Multiple bit net and reg data types are declared by specifying a
<range>, and are known as vectors.
Ex:
wire w1, w2; // declares 2 scalar nets
reg a; // declares a scalar register
wire [3:0] addr; // declare a vector net
reg[3:0] v; // declare a vector register
- Vector can be declared at [high# : low#] or [low# : high#], but the left number in
the squared brackets is always the most significant bit of the vector
- Vectors can be declared only for nets and reg data types. (Vector declaration for
integer, real, realtime, and time data types are illegal.)
24
Data types
Parameters:
- Parameters are constants. There are two types of parameters: module parameters and
specify parameters.
- Parameters are not variables, they are used for “per instance” constants.
- For global constants, using ‘define …
+ Module parameters:
– parameter msb = 7; // defines msb as a constant value 7
– parameter e = 25, f = 9; // defines two constant numbers
– parameter r = 5.7; // declares r as a real parameter
– parameter byte_size = 8,
– parameter average_delay = (r + f) / 2;
– parameter signed [3:0] mux_selector = 0;
– parameter real r1 = 3.5e17;
25
Verilog in Hardware Design
Verilog design
Gate primitives and/or other modules Like the programming language but not
Structural model Behavioral model
27
END
28