Appendix - Verilog
Appendix - Verilog
( CE_118 )
• Data Types
• Dataflow modeling
• Continuous assignment
• Procedural assignment
How to represent Hardware?
• Classical design method
- Schematic
- Hand-draw or machine-draw
• Computer-based language method
- Hardware Description Language (HDL): Verilog, VHDL
- Fast, popularly used to design complex circuit with large and very
large scale
4
Module
• Module is the basic building block in Verilog
- Can be an element or a collection of lower-level design blocks
5
Module
Example: 4-bit Ripple Carry Counter
Module
Module
6
Design Hierarchy
Module
Module description
module module name ( port name, port name,…);
module_port declaration
module
data type declaration
logic description part
A part of a chip, or
endmodule the whole chip
A module definition
7
Lexical convention
• Operator
• White space
• Comment
• Number
• String
• Identifier
• Keyword
Lexical convention
• Verilog is a case-sensitive language.
• Whitespace: \b blank, \t tab, \n new line
• Comment: /*..*/ or //……..
• Operator: unary, binary, ternary
a = ~ b; // ~ is a unary operator. b is the operand
a = b && c; // && is a binary operator. b and c are operands
a = b ? c : d; // ?: is a ternary operator. b, c and d are operands
9
Lexical convention
• Number
- Two forms to express numbers:
* 37 : 32 bit decimal 37, or
* <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
10
Lexical convention
• Strings are stored as a sequence of 8 bit ASCII values
• Strings are variables of reg type
11
Lexical convention
• Identifier:
– specified by a letter or underscore followed by more letter or
digits, or signs ($, _).
– The first character must NOT be a digit or $; it can be a letter or
an underscore
– Identifiers are case sensitive.
– Identifier can up to 1024 characters
– Example:
• shiftreg_a
• busa_index
• error_condition
• merge_ab
• _bus3
• n$657
Data Types
• Value set:
– 0 - represents a logic zero, or false condition
– 1 - represents a logic one, or true condition
– x - represents an unknown logic value
– z - represents a high-impedance state
• Net
• Variable
• Vector
• Array
• Memory
• Parameter
Data types
2 main groups: Net and Variable
• Net represents physical connections between structural entities,
such as gates.
- Net does not store a value, except “trireg” net
- Default value is z (trireg default value is x)
- Net types:
Ex:
- wire w1, w2; //declare 2 wires
- wand w;
14
Data types
• Variable: hold a value until another value assigned to it
- Used in behavioral description (procedural assignment)
reg : unsigned integer variables of varying bit width
integer : 32-bit signed integer
time : 64-bit unsigned integer
real : signed floating-point
realtime: store time as a real value
- reg, time, integer (default value is x),
real , realtime (default value is 0)
15
Data Types
• Registers
– an abstraction of a data storage element
– keyword for the register data type is reg.
– A register stores a value from one assignment to the next.
– The default initialization value for a reg data type is the unknown
value, x.
– Example:
reg reset;
initial begin
reset = 1’b1;
#100 reset = 1’b0;
end
Data types
17
Data types
• Array declaration for a net or variable that is either scalar or
vector
• Parameters
– Verilog parameters do not belong to either the net or variable
group, they are constants
– Syntax:
• <parameter_declaration> = parameter <list_of_assignments>;
– Example:
parameter msb = 7; // defines msb as a constant value 7
parameter e = 25, f = 9;// defines two constant numbers
parameter average_delay = (r + f) / 2;
parameter byte_size = 8, byte_mask = byte_size - 1;
parameter r = 5.7; //declares r as a ’ real’ parameter
Expression, operator, operand
• expression = operators + operands
– Operators
– Operands
Expression: Operands
21
Expression: Operators
Arithmetic + - * / % **
Logical ! && ||
Logical equality == !=
Case equality === !===
Bitwise ~ & | ^ ^~ (or ~^)
Relational < > >= <=
Unary reduction & ~& | ~| ^ ^~ (or ~^)
Shift << >> <<< >>>
Concatenation {}
Replication {{}}
Condition ?: Ref. book for detail of
each operator!
Unary + - 22
Level of modeling
• Structural
- Explicit structure of the circuit
- How a module is composed as an interconnection of more primitive
modules or components
- E.g. Each logic gate initially instantiated and connected to others
24
Structural model
• Structural Models are built from gate primitives,
switches, and other modules
• Describe the logic circuit using logic gates
25
Structural model
• Primitive gates:
12 primitive logic gates predefined in the Verilog HDL
Advantanges:
✓ Gates provide a much closer one-to-one mapping
between the actual circuit and the model.
✓There is no continuous assignment equivalent to the
bidirectional transfer gate. 26
Structural model
Example:
t1
// Define a 1-bit full adder
module fulladd(sum, c_out, a, b, c_in);
t2
// I/O port declarations
output sum, c_out;
input a, b, c_in;
// Internal nets
wire s1, c1, c2;
t3 // Instantiate logic gate primitives
xor (s1, a, b);
Full adder 1-bit and (c1, a, b);
xor (sum, s1, c_in);
and (c2, s1, c_in);
xor (c_out, c2, c1);
endmodule
27
Behavioral model
• What is Behavioral model ?
– More like a procedure in a programming language, but NOT.
28
Behavioral model
• Why behavioral model?
– Good for more abstract models of circuits
– Easier to write
– More flexible
– Provide sequencing
– A much easier way to write testbenches
– Allow both model and the testbench to be described together
29
Assignments
• The continuous assignment
– assigns values to nets
• The procedural assignment
– assigns values to registers
Continuous assignment
• Always active
• Delay value: control time when the net is assigned value
assign #10 out = in1 & in2; //delay of performing computation,
//only used by simulator, not synthesis
31
Continuous assignment
Examples:
wire out = in1 & in2; //scalar net
//implicit continuous assignment, declared only once
assign addr[15:0] = addr1_bits[15:0] ^ addr2_bits[15:0]; //vector net
assign {c_out, sum[3:0]} = a[3:0] + b[3:0] + c_in; //concatenation
33
Procedural Assignment
• Assign value to variable data types
Left-hand side Right-hand side
- reg, integer, time, real, realtime Net, variable, function
- Bit-select, part-select of reg, integer, time call (Any expression that
- Memory word evaluates a value)
- Concatenation of any of the above
35
Procedural assignment
• 2 types of procedural assignment:
blocking and non-blocking procedural assignment
-> specify different flow in a sequential block
36
Procedural assignment
• Example
37
Initial & Always
• Two basic components of behavioral
modeling: initial, always construct.
38
Initial
• Variable and Net can be initialized when they are declared
module adder (sum, co, a, b, ci);
40
Procedural timing control
41
Procedural timing control
• Examples:
//delay-based timing control
#10 rega = a+b;
reg a = #10 a+b; //intra-assignment delay
//wait control
wait (!enable) #10 a = b;
42
Procedural timing control
sensitive list
end end
always creates By adding “@ ( )” to always statement: means
an infinite loop “wait for change of signals listed in the sensitivity
list” and when the change takes place, sequential
block ( from begin to end ) is executed.
43
Procedural timing control
description timing
always @ ( posedge clk ) when signal clk rises
45