Professional Documents
Culture Documents
• Identifiers
• identifier names consist of letters, digits,
_ (underscore), and $ character
• first character in an identifier name must be
a letter or _ (underscore)
• keywords (reserved identifiers) are lower case
• Comments
• // for single line comments
• /* and */ for multi-line comments
EE 310 1
Language Elements
• Format
• Verilog HDL is case sensitive
• Verilog HDL is free format
• statements are terminated with ;
• System Tasks
• built-in system tasks begin with $ character
• $display(“Hi”); (displays the specified message)
• $time (returns the current simulation time)
EE 310 2
Compiler Directives
• `define (text substitution)
`define MAX_BUS_SIZE 32
reg [`MAX_BUS_SIZE - 1 : 0] AddReg;
EE 310 3
Value Set (Four Valued Logic)
• 0 (logic 0)
• 1 (logic 1)
• Z (high impedance)
• a wire that is not driven to 0 or 1, e.g. tristate driver
• X (unknown)
• the simulator cannot decide the value, e.g. initial state
of flip-flops
• a wire that is being driven to 0 and 1 simultaneously
EE 310 4
Parameters
• Integer
• parameter size = 32;
• Real
• parameter pi = 3.14;
EE 310 5
Integers
• Simple Decimal Format
• A negative number is represented in two’s complement form
• 6 00000000000000000000000000000110
• -6 11111111111111111111111111111010
• Base Format
• [size] ’ [signed] base value
• size (decimal specification of number of bits)
• signed (if specified, the number is signed)
• s or S → sign
• base (arithmetic base of the number)
• d or D → decimal
• h or H → hexadecimal
• b or B → binary
• value (value in the specified base)
EE 310 6
Integers
6’d6 000110
6’b010_111 010111
if (select == 00)
6’h3A 111010 e = 2;
else if (select == 01)
-6’h3A 000110
e = 4;
else if (select == 10)
6’sh3A 111010 e = 6;
-6’sh3A 000110 else
e = 8;
3’b10011 011
3’sb10011 011
EE 310 7
Data Types
• Net
• it represents a physical connection.
• its default value is Z.
• if a net is not declared in a Verilog model, by default,
it is implicitly declared as a 1-bit net.
• it cannot be assigned in an initial or always block.
• Examples
wire Start, Ready;
wire [2:0] Addr;
wire signed [7:0] pwdata, prdata;
EE 310 8
Data Types
• Variable
• it represents an abstract data storage.
• its default value is X.
• Reg Variable
• Syntax reg [signed] [msb : lsb] reg1, reg2, …, regN;
• Examples
reg Cnt; reg [3:0] Sat;
reg signed [3:0] rsp;
• Integer Variable
• it contains a 32-bit signed integer.
• Syntax integer integer1, integer2, …, integerN;
• Examples
integer a, b;
EE 310 9
Data Types
EE 310 10
Data Types
reg [31:0] a, b, c, d;
a = 58 * 58; // 3364
reg [3:0] a; b = 6’h3A * 6’h3A; // 3364
reg signed [3:0] a_signed;
reg signed [3:0] b_signed; c = -6 * -6; // 36
reg signed [7:0] c; d = 6’sh3A * 6’sh3A; // 36
reg signed [7:0] d;
EE 310 11
Data Types
• Array
EE 310 12
Data Types
• Array
EE 310 13
Expressions
EE 310 14
Operands
• Parameter (constant)
EE 310 15
Operands
• Part Select
EE 310 16
Operators
• Arithmetic ( +, -, *, /, %, ** )
• Relational ( <, <=, >, >= )
• Equality ( ==, !=, ===, !== )
• Logical ( !, &&, || )
• Bitwise ( ~, &, |, ^, ~^ )
• Reduction ( &, ~&, |, ~|, ^, ~^ )
• Logical Shift ( >>, << )
• Arithmetic Shift ( >>>, <<< )
• Conditional ( ? : )
• Concatenation ( {,} )
• Replication ( {integer{ }} )
EE 310 17
Operator Precedence
and Associativity
In the following table, operators are listed from the highest
precedence (top row) to the lowest precedence (bottom row).
Operators in the same row have identical precedence.
EE 310 18
Operator Precedence Table
EE 310 19
Operator Precedence Table
EE 310 20
Operators
• Arithmetic Operators ( +, -, *, /, %, ** )
• if any bit of an operand is an x or z, the result is x.
• Examples
7/4 (result = 1)
7 % 4 (result = 3)
2 ** 10 (result = 1024)
EE 310 21
Operators
EE 310 22
Operators
EE 310 23
Operators
• Bitwise Operators ( ~, &, |, ^, ~^ )
• they operate bit-by-bit on corresponding bits of
the vector operands and produce vector result.
• Examples
A = 4’b0110; B = 4’b0100;
A | B (result = 0110)
A ^ B (result = 0010)
EE 310 24
Operators
• Logical Shift Operators ( >>, << )
• they shift the left operand by the right operand number of times.
• Examples
reg [7:0] Qreg, Preg;
Qreg = 8’h17;
Preg = Qreg >> 2; (result = 00000101)
Preg = Qreg << 2; (result = 01011100)