Verilog Lec s03
Verilog Lec s03
Overview
Simulation and Synthesis Modules and Primitives Styles Structural Descriptions Language Conventions Data Types Delay Behavioral Constructs Compiler Directives Simulation and Testbenches
2
1/28/2001
Modules
The Module Concept
Basic design unit Modules are:
Declared Instantiated
1/28/2001
Syntax (continued)
::= module_item_declaration | parameter_override | continuous_assign | gate_instantiation | udp_instantiation | module_instantiation | specify_block | initial_construct | always_construct
module_item_declaration ::= parameter_declaration | input_declaration | output_declaration | inout_declaration | net_declaration | reg_declaration | integer_declaration | real_declaration | time_declaration | realtime_declaration | event_declaration | task_declaration | function_declaration parameter_override ::= defparam list_of_parameter_assignments udp declaration
1/28/2001 6
Module Declaration
Annotated Example
/* module_keyword module_identifier (list of ports) */ module C_2_4_decoder_with_enable (A, E_n, D) ; input [1:0] A ; // input_declaration input E_n ; // input_declaration output [3:0] D ; // output_declaration assign D = {4{~E_n}} & ((A == 2'b00) ? 4'b0001 : (A == 2'b01) ? 4'b0010 : (A == 2'b10) ? 4'b0100 : (A == 2'b11) ? 4'b1000 : 4'bxxxx) ; // continuous_assign endmodule
1/28/2001 7
Module Declaration
Identifiers - must not be keywords! Ports
First example of signals Scalar: e. g., E_n Vector: e. g., A[1:0], A[0:1], D[3:0], and D[0:3] Range is MSB to LSB Can refer to partial ranges - D[2:1] Type: defined by keywords
input output inout (bi-directional)
1/28/2001
Module Instantiation
Example
module C_4_16_decoder_with_enable (A, E_n, D) ;
input [3:0] A ; input E_n ; output [15:0] D ; wire [3:0] S; wire [3:0] S_n; C_2_4_decoder_with_enable DE (A[3:2], not N0 (S_n, S); C_2_4_decoder_with_enable D0 (A[1:0], C_2_4_decoder_with_enable D1 (A[1:0], C_2_4_decoder_with_enable D2 (A[1:0], C_2_4_decoder_with_enable D3 (A[1:0], endmodule
1/28/2001 9
E_n, S);
Module Instantiation
More Examples
Single module instantiation for five module instances
C_2_4_decoder_with_enable DE (A[3:2], D0 (A[1:0], D1 (A[1:0], D2 (A[1:0], D3 (A[1:0], E_n, S), S_n[0], D[3:0]), S_n[1], D[7:4]), S_n[2], D[11:8]), S_n[3], D[15:12]);
Named_port connection
C_2_4_decoder_with_enable DE (.E_n (E_n), .A (A[3:2]) .D (S)); // Note order in list no longer important (E_n and A interchanged).
1/28/2001 11
Primitives
Gate Level
and, nand or, nor xor, xnor buf , not bufif0, bufif1, notif0, notif1 (three-state)
Switch Level
*mos where * is n, p, c, rn, rp, rc; pullup, pulldown; *tran+ where * is (null), r and + (null), if0, if1 with both * and + not (null)
1/28/2001 12
Primitives
No declaration; can only be instantiated All output ports appear in list before any input ports Optional drive strength, delay, name of instance Example: and N25 (Z, A, B, C); //instance name Example: and #10 (Z, A, B, X); // delay (X, C, D, E); //delay /*Usually better to provide instance name for debugging.*/ Example: or N30 N41 (SET, Q1, AB, N5), (N25, ABC, R1);
1/28/2001
13
Styles
Structural - instantiation of primitives and modules RTL/Dataflow - continuous assignments Behavioral - procedural assignments
1/28/2001
14
15
endmodule
1/28/2001
16
reg S, CO;
always@(A or B or CI) //; begin S <= A ^ B ^ CI; // procedural assignment CO <= A & B | A & CI | B & CI; // procedural assignment end endmodule
1/28/2001 17
Structural Descriptions
Textual description of schematic Form of netlist Connections Hierarchy Arrays of instances Hierarchy established by instantiation of modules and primitives within modules
18
1/28/2001
Connections
By position association
module C_2_4_decoder_with_enable (A, E_n, D); C_4_16_decoder_with_enable DX (X[3:2], W_n, word); A = X[3:2], E_n = W_n, D = word
By name association
module C_2_4_decoder_with_enable (A, E_n, D); C_2_4_decoder_with_enable DX (.E_n(W_n), .A(X[3:2]), .D(word)); A = X[3:2], E_n = W_n, D = word
1/28/2001 19
Connections
Empty Port Connections
module C_2_4_decoder_with_enable (A, E_n, D); C_2_4_decoder_with_enable DX (X[3:2], , word); E_n is at high-impedance state (z) C_2_4_decoder_with_enable DX (X[3:2], W_n ,); Outputs D[3:0] unused.
1/28/2001
20
Arrays of Instances
{ , } is concatenate Example
module add_array (A, B, CIN, S, COUT) ; input [7:0] A, B ; input CIN ; output [7:0] S ; output COUT ;
endmodule
21
Language Conventions
Case-sensitivity
Verilog is case-sensitive. Some simulators are case-insensitive Advice: - Dont use case-sensitive feature! Keywords are lower case
Different names must be used for different items within the same scope Identifier alphabet:
Upper and lower case alphabeticals decimal digits underscore
1/28/2001
22
Language Conventions
Maximum of 1024 characters in identifier First character not a digit Statement terminated by ; Free format within statement except for within quotes Comments:
All characters after // in a line are treated as a comment Multi-line comments begin with /* and end with */
Compiler directives begin with // synopsys Built-in system tasks or functions begin with $ Strings enclosed in double quotes and must be on a single line
1/28/2001 23
Logic Values
Verilog signal values
0 - Logical 0 or FALSE 1 - Logical 1 or TRUE x, X - Unknown logic value z, Z - High impedance condition
Also may have associated strength for switch level modeling of MOS devices
7 signal strengths plus 3 charge strengths
1/28/2001 24
Number Representation
Format: <size><base_format><number>
<size> - decimal specification of number of bits
default is unsized and machine-dependent but at least 32 bits
Number Representation
Examples:
6b010_111 8'b0110 8b1110 4'bx01 16'H3AB 24 5'O36 16'Hx 8'hz gives gives gives gives gives gives gives gives gives 010111 00000110 00001110 xx01 0000001110101011 00011000 11100 xxxxxxxxxxxxxxxx zzzzzzzz
26
1/28/2001
Variables
Nets
Used for structural connectivity
Registers
Abstraction of storage (May or may not be real physical storage)
Properties of Both
Informally called signals May be either scalar or vector
1/28/2001 27
1/28/2001
Vectored - multiple-bit net treated as a single object cannot reference individual bits or part-select Scalared - bits can be referenced individually or be part selected
1/28/2001
1/28/2001
31
1/28/2001
32
time - stores time 64-bit unsigned real - stores values as real num realtime - stores time values as real numbers
1/28/2001 33
Register Assignment
A register may be assigned value only within: a procedural statement a user-defined sequential primitive a task, or a function. A reg object may never by assigned value by: a primitive gate output or a continuous assignment
1/28/2001 34
Register Examples
reg a, b, c; reg [15:0] counter, shift_reg; integer sum, difference;
1/28/2001
35
Strings
No explicit data type Must be stored in reg (or array) reg [255:0] buffer; //stores 32 characters
1/28/2001
36
Constants
Declaration of parameters
parameter A = 2b00, B = 2b01, C = 2b10; parameter regsize = 8;
reg [regsize - 1:0]; /* illustrates use of parameter regsize */
1/28/2001
37
Operators
Arithmetic (binary: +, -,*,/,%*); (unary: +, -) Bitwise (~, &,|,^,~^,^~) Reduction (&,~&,|,~|,^,~^,^~) Logical (!,&&,||,==,!=,===,!==) Relational (<,<=,>,>=) Shift (>>,<<) Conditional ? : Concatenation and Replications {,} {int{ }}
38
Verilog fills in smaller-width operands by using zero extension. Final or intermediate result width may increase expression width
1/28/2001
39
op x where op is ~
Bitwise negation Bit width = width(x)
1/28/2001 41
{x, , y}
Concatenation Bit width = width(x) + + width(y)
{x{y, , z}}
Replication Bit width = x * (width(y) + + width(z))
1/28/2001
43
Relational
If any bit is x or z, result is x.
Logical
== and != If any bit is x or z, result is x. === and !== All bits including x and z values must match for equality
1/28/2001 44
Reduction
Defined by tables as for bitwise operators.
Shifts
z changed to x. Vacated positions zero filled.
Conditional
If conditional expression is ambiguous (e.g., x or z), both expressions are evaluated and bitwise combined as follows: f(1,1) = 1, f(0,0) = 0, otherwise x.
1/28/2001
45
1/28/2001
46
Types
Gate Delay (Inertial Delay) Net Delay (Transport Delay) Module Path Delay
1/28/2001
47
C
C
1/28/2001
falling delay
Delay_value3 - constant_mintypmax_expression -
1/28/2001
51
Different timescales can be used for different sequences of modules The smallest time precision determines the precision of the simulation. Will ignore time issues for system tasks/functions
1/28/2001 53
1/28/2001
55
1/28/2001
56
Behavioral Constructs
Concurrent communicating behaviors => processes same as behaviors Two constructs
initial - one-time sequential activity flow - not synthesizable but good for testbenches Always - cyclic (repetitive) sequential activity flow
Use procedural statements that assign only register variables (with one exception)
1/28/2001
57
1/28/2001
58
Always:
always begin F1 = 0, F2 = 0; # 2 F1 = 1; # 4 F2 = 0; # 2 F1 = 1; # 4; end
60
Procedural Assignments
Types
= blocking assignment assign = continuous assignment <= non-blocking assignment
1/28/2001
force release
to register or net variable dynamic binding to target register or net variable
1/28/2001
63
1/28/2001
64
1/28/2001
65
1/28/2001
67
@ (posedge clk) Q = D;
end
1/28/2001
71
Example:
always begin a = b; c = d; wait (advance);
end
1/28/2001 72
Blocking Assignments
Identified by = Sequence of blocking assignments executes sequentially Example:
always @(posedge clk) begin b = 0; c = 0; b = a + a; c = b + a; d = c + a; end
1/28/2001 73
Non-Blocking Assignments
Identified by <= Sequence of non-blocking assignments executes concurrently Example 1:
always @(posedge clk) begin
1/28/2001
Example:
always @(posedge clk) begin b = 0; c = 0; b = a + a; // uses a at posedge clock #5 c = b + a; // uses a at posedge clock + 5 d = c + a; // uses a at posedge clock + 5 end /*c = 2 a(at posedge clock)+ a(at posedge clock + 5) d = 2 a(at posedge clock) + 2 a(at posedge clock + 5)*/
1/28/2001
75
Example:
always @(posedge clk) begin b = 0; c = 0; b = a + a; // uses a at posedge clock c = #5 b + a; // uses a at posedge clock d = c + a; // uses a at posedge clock + 5 end /* c = 3 a(at posedge clock) d = 3a (at posedge clock)+ a (at posedge clock + 5)*/
1/28/2001
76
Example:
always @(posedge clk) begin b <= 0; c <= 0; b <= a + a; // uses a at posedge clock #5 c <= b + a; // uses b and a at posedge clock + 5 d <= c + a; // uses a at posedge clock + 5 end /*c = b(at posedge clock + 5) + a(at posedge clock + 5) d = c(at posedge clock + 5) + a (at posedge clock +5) */
1/28/2001 77
Example:
always @(posedge clk) begin b <= 0; c <= 0; b <= a + a; // uses a at posedge clock c <= #5 b + a; // uses a and b at posedge clock d <= c + a; // uses a and c at posedge clock end /* Calculates *c(posedge clock + 5) = b(at posedge clock) + a(at posedge clock); d(posedge clock) = c(at posedge clock) + a (at posedge clock) */
1/28/2001 78
1/28/2001
79
1/28/2001
Activity Control
Overview
Constructs for Activity Control
Conditional operator case statement if else statement Loops : repeat, for, while, forever disable statement fork join statement
Conditional Operator
?: Same as for use in continuous assignment statement for net types except applied to register types Example:
always@(posedge clock)
Q <= S ? A : B //combined DFF and 2-to-1 MUX
1/28/2001
83
1/28/2001
84
case Statement
Requires complete bitwise match over all four values so expression and case item expression must have same bit length Example: always@(state, x) begin
reg[1:0] state; case (state) 2b00: next_state <= s1; 2b01: next_state <= s2; 2b10: if x next_state <= s0; else next_state <= s1; end default next_state = 1bxx; endcase end
1/28/2001
85
casex Statement
Requires bitwise match over all but positions containing x or z; executes first match encountered if multiple matches. Example:
always@(code) begin casex (code) 2b0x: control <= 8b00100110; //same for 2b0z 2b10: control <= 8b11000010; 2b11: control <= 8b00111101; default control <= 8bxxxxxxxx; endcase end
1/28/2001 86
casez Statement
Requires bitwise match over all but positions containing z or ? (? is explicit dont care); executes first match encountered if multiple matches. Example:
reg [1:0] code; always@(code) begin casez (code) 2b0z: control <= 8b00100110; 2b1?: control <= 8b11000010; default control <= 8bxxxxxxxx; endcase end
1/28/2001 87
88
91
Tasks (FIO)
Declared within a module Referenced only by a behavior within the module Parameters passed to task as inputs and inouts and from task as outputs or inouts Local variables can be declared Recursion not supported although nesting permitted (nested copies of variables use same storage) See Fig. 7.43 p. 226 of [5]for rules
1/28/2001
93
Tasks (FIO)
Syntax task_declaration ::= task task_identifier {task_item_declaration} statement or null endtask
1/28/2001
94
Task Example
task leading_1; input [7:0] data_word; output [2:0] position; reg [7:0] temp; reg [2:0] position; begin temp = data_word; position = 3'b111; while (!temp[7]) @(posedge clock) //* begin temp = temp << 1; position = position - 1; end end endtask //* This may not work unclear contradictory statements in FPGA Express documentation.
1/28/2001
95
Functions (FIO)
Implement combinational behavior No timing controls or tasks which implies no while May call other functions with no recursion Reference in an expression, e.g. RHS No output or inout allowed Implicit register having name and range of function
1/28/2001
96
Functions (FIO)
Syntax:
function_declaration ::= function [range or type] function_identifier; function_call ::= function_identifier (expression {, expression}) Example:
position = leading_1(data_val);
1/28/2001 97
Function Example
function [2:0] leading_1; input [7:0] data_word; reg [7:0] temp; begin temp = data_word; leading_1 = 3'b111; while (!temp[7]) begin temp = temp << 1; leading_1 = leading_1 - 1; end end endfunction Is the above code synthesizable? No
1/28/2001 98
1/28/2001
99
1/28/2001
100
FF
1/28/2001
101
Output Logic
FF
1/28/2001
102
State register - Combinational next state and output logic - Output register
Output Register Inputs Outputs
FF
FF
1/28/2001
103
Output Logic
FF
1/28/2001
104
start_s
start = 1
fill_s
full = 1
wash_s spin = 1
empty = 0 timeout = 1
drain_s
/timeset = 1 drain = 1
105
1/28/2001
parameter start_s = 3'b000, fill_s = 3'b001, wash_s = 3'b010, drain_s = 3'b011, wring_s = 3'b100;
always@(posedge clk or posedge reset) begin if (reset) state <= start_s; else if (clk) state <= next_state; end 1/28/2001
1/28/2001
107
Verilog - state register - next state logic and output logic (FIO)
module control_el1 (reset, clk, start, full, empty, timeout, drain, spin, timeset, water); //state register - next state logic and output logic input reset, clk, start, full, empty, timeout; output drain, spin, timeset, water; reg drain, spin, timeset, water; reg [2:0] state, next_state; parameter start_s = 3'b000, fill_s = 3'b001, wash_s = 3'b010, drain_s = 3'b011, wring_s = 3'b100; always@(posedge clk or posedge reset) begin if (reset) state <= start_s; else if (clk) state <= next_state; end always@(state or start or full or empty or timeout) begin case (state) start_s: if (start) next_state <= fill_s; else next_state <= start_s; fill_s: begin if (full) begin next_state <= wash_s; timeset <= 1'b1; end else next_state <= fill_s; end wash_s: begin if (timeout) next_state <= drain_s; else next_state <= wash_s; end
1/28/2001
108
Verilog - state register - next state logic and output logic (continued) (FIO)
drain_s: begin if (empty) next_state <= wring_s; else next_state <= drain_s; end wring_s: begin if (timeout) next_state <= start_s; else next_state <= wring_s; end default next_state <= start_s; endcase end always@(state or full or empty) drain <= 1'b0; spin <= 1'b0; timeset <= 1'b0; water <= 1'b0; // sets outputs to default value - in the following, // only output changes to 1 are specified case (state) start_s: ; fill_s: begin water <= 1'b1; if (full) timeset <= 1'b1; end wash_s: spin <= 1b1; drain_s: begin drain <= 1'b1; if (empty) timeset <= 1'b1; end wring_s: begin spin <= 1b1; drain <= 1b1; endcase end endmodule
1/28/2001
109
Verilog - State register - Combinational next state and output logic - Output register (FIO) Same as state and output register - state and output logic Same as combined state and output logic and registers Both state and outputs are from flip-flops and synchronized with the clock.
1/28/2001
110
Verilog - State register - Combinational next state and output logic - Output register (FIO)
If delay of the output for one clock cycle acceptable, then same output logic can feed output flip-flop inputs as originally feed combinational outputs Suppose outputs are to obey specifications on a clock cycle specific basis, i. e., are not delayed Then the output flip-flop D-input functions must be defined one cycle earlier than the normal combinational output.
1/28/2001 111
Verilog - State register - Combinational next state and output logic - Output register (FIO)
How is this done? Example:
A X=1 Z=0
C M=1
Z=1 N=1
Verilog - State register - Combinational next state and output logic - Output register (FIO)
module control_er1 (reset, clk, start, full, empty, timeout, drain, spin, timeset, water); //state register - combined next state and output logic - output register input reset, clk, start, full, empty, timeout; output drain, spin, timeset, water; reg drain, spin, timeset, water; reg [2:0] state, next_state; parameter start_s = 3'b000, fill_s = 3'b001, wash_s = 3'b010, drain_s = 3'b011, wring_s = 3'b100; always@(posedge clk or posedge reset) begin drain <= 1'b0; spin <= 1'b0; timeset <= 1'b0; water <= 1'b0; // sets outputs to default value - in the following, // only output changes to 1 are specified if (reset) state <= start_s; else if (clk) case (state) start_s: if (start) begin state <= fill_s; water <= 1b1; end else state<= start_s; fill_s: if (full) begin state <= wash_s; spin <= 1b1; end else begin state <= fill_s; water <= 1'b1; timeset <= 1b1; end
1/28/2001
113
Verilog - State register - Combinational next state and output logic - Output register (continued)(FIO)
wash_s: if (timeout) begin state <= drain_s; drain <= 1b1; end else spin <= 1'b1; drain_s: begin drain <= 1b1; if (empty) begin state <= wring_s; spin <= 1b1; end else begin state <= drain_s; drain <= 1'b1; timeset <= 1'b1; end end 1/28/2001 if (timeout) state <= start_s; else begin state <= wring_s; spin <= 1'b1; drain <=1b1; end default next_state <= start_s; endcase end endmodule wring_s:
114
Verilog - State register - Combinational next state and output logic - Output register (continued)(FIO) How is (Mealy) timeset handled?
Timeset is not used while in states fill_s and drain_s. Time value is fixed during last cycle before conditions to leave these states, full = 1 and empty = 1, respectively, occur. Can hammer timeset every clock cycle until condition to leave these states states satisfied. End result in terms of loading the time value is the same as for original design
Implicit Model
More abstract representation Restricted to structures in which a given state can be entered from only one other state! Yields simpler code Description of reset behavior more complex Ciletti examples not good illustrations [5] For novice, good route to disaster!
1/28/2001
116
Compiler Directives
Useful for controlling what is synthesized and the resulting logic Warning: Not recognized by other compilers therefore reduce code portability Examples:
// synopsys translate_off Code here describes something that is not to be synthesized such at a simulation testbench can contain non-synthesizable constructs such as delays) // synopsys translate_on
1/28/2001 117
UUT Module
Stimulus
1/28/2001
Response
120
Testbench Approach
Use Verilog module to produce testing environment including stimulus generation and/or response monitoring
Stimulus UUT Module Response
Testbench Module
1/28/2001 121
1/28/2001
125
References
1. IEEE, 1364-1995 IEEE Standard Description Language Based on the Verilog(TM) Hardware Description Language. 2. Synopsys, FPGA Compiler II/FPGA Express: Verilog HDL Reference Manual, Version 1999.05, May 1999. 3. Thomas, D. E., and P. R. Moorby, The Verilog Hardware Description Language, 4th Ed., Kluwer Academic Publishers, 1998. 4. Smith, D. R., and P. D. Franzon, Verilog Styles for Synthesis of Digital Systems, Prentice Hall, 2000. 5. Ciletti, Michael D., Modeling, Synthesis, and Rapid Prototyping, Prentice Hall, 1999. 6. Palnitkar, Samir, Verilog HDL: A Guide to Design and Synthesis, Sunsoft Press, 1996.
1/28/2001 126