Verilog Tutorial1
Verilog Tutorial1
Example: A Computer
Functionality: Perform user defined computations I/O Ports: Keyboard, Mouse, Monitor, Printer
Example
module HalfAdder (A, B, Sum Carry); input A, B; output Sum, Carry; assign Sum = A ^ B; //^ denotes XOR assign Carry = A & B; // & denotes AND endmodule
Number
decimal, hex, octal, binary unsized decimal form size base form include underlines, +,-
String
" Enclose between quotes on a single line"
4
Strings are limited to 1024 chars First char of identifier must not be a digit Keywords: See text.
Description Styles Structural: Logic is described in terms of Verilog gate primitives Example: not n1(sel_n, sel); and a1(sel_b, b, sel_b); and a2(sel_a, a, sel); or o1(out, sel_b, sel_a); b sel
n1 a1
sel_b
sel_n
a
a2 sel_a
o1
out
Description Styles (cont.) Dataflow: Specify output signals in terms of input signals Example: assign out = (sel & a) | (~sel & b); b sel
sel_b sel_n
out
sel_a
a
7
Description Styles (cont.) Behavioral: Algorithmically specify the behavior of the design
Example: if (select == 0) begin out = b; end else if (select == 1) begin out = a; end
a b
Black Box
out
10
Dataflow Modeling (cont.) Example: `timescale 1ns/100ps module HalfAdder (A, B, Sum, Carry); input A, B; output Sum, Carry; assign #3 Sum = A ^ B; assign #6 Carry = A & B; endmodule
12
13
Sensitivity List
14
Behavioral Modeling (cont.) always statement : Sequential Block Sequential Block: All statements within the block are executed sequentially When is it executed?
Occurrence of an event in the sensitivity list Event: Change in the logical value
Intra-Assignment Delay
Example:
16
initial Statement : Executes only once always Statement : Executes in a loop Example:
initial begin Sum = 0; Carry = 0; end always @(A or B) begin Sum = A ^ B; Carry = A & B; end
17
Event Control
Event Control Edge Triggered Event Control Level Triggered Event Control Edge Triggered Event Control @ (posedge CLK) //Positive Edge of CLK Curr_State = Next_state;
Repeat Loop
Example:
repeat (Count) sum = sum + 5;
If condition is a x or z it is treated as 0
19
If condition is a x or z it is treated as 0
For Loop
Example:
for (Count = 0; Count < 10; Count = Count + 1) begin sum = sum + 5; end
20
procedural_statement Example:
if (Clk) Q = 0; else Q = D;
21
Example 2: case (3b101 << 2) 3b100: A = B + C; 4b0100: A = B C; 5b10100: A = B / C; //This statement is executed endcase
22
23
Net Types: wire, tri, wor, trior, wand, triand, supply0, supply1
24
Example wire Reset; // A 1-bit wire wire [6:0] Clear; // A 7-bit wire
25
Restrictions on Data Types Data Flow and Structural Modeling Can use only wire data type Cannot use reg data type Behavioral Modeling Can use only reg data type (within initial and always
constructs) Cannot use wire data type
26
Example
reg [ 0 : 3 ] mem [ 0 : 63 ]; // An array of 64 4-bit registers reg mem [ 0 : 4 ]; // An array of 5 1-bit registers
27
Compiler Directives `define (Similar to #define in C) used to define global parameter Example:
`define BUS_WIDTH 16 reg [ `BUS_WIDTH - 1 : 0 ] System_Bus;
29
Time
$time: gives the simulation
30
31
32
Empty Port Connections If an input port of an instantiated module is empty, the port is set to a value of z (high impedance).
module child_mod(In1, In2, Out1, Out2) input In1; input In2; output Out1; output Out2;
//behavior relating In1 and In2 to Out1 endmodule
module parent_mod(.)
child_mod mod(A, ,Y1, Y2); //Empty Input endmodule
If an output port of an instantiated module is left empty, the port is considered to be unused.
module parent_mod(.) child_mod mod(A, B, Y1, ); //Empty Output endmodule
33
Test Bench
`timescale 1ns/100ps module Top; reg PA, PB; wire PSum, PCarry; HalfAdder G1(PA, PB, PSum, PCarry); initial begin: LABEL reg [2:0] i; for (i=0; i<4; i=i+1) begin {PA, PB} = i; #5 $display (PA=%b PB=%b PSum=%b PCarry=%b, PA, PB, PSum, PCarry); end // for end // initial endmodule Design Module
Test Bench
Apply Inputs
Observe Outputs
34
Test Bench - Generating Stimulus Example: A sequence of values initial begin Clock = 0; #50 Clock = 1; #30 Clock = 0; #20 Clock = 1; end
35
Test Bench - Generating Clock Repetitive Signals (clock) Clock A Simple Solution:
wire Clock; assign #10 Clock = ~ Clock
Caution:
Initial value of Clock (wire data type) = z ~z = x and ~x = x
36