Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Fall, 2005
Outline
1
What is a Hardware Description
Language ?
2
What is Verilog HDL ?
3
Levels of Abstraction for Verilog
HDL & VHDL
VHDL
System
Behavioral
Behavioral
Level
Level Verilog
Algorithm
RTL
RTL RTL Synthesizable RTL Code
Level
Level
Logic
Gate
Gate
Level
Level vital
Gate
4
Behavioral and RTL (1/2)
¾ Behavioral model
• The function of the logic is modeled using high level
language constructs, such as @, while, wait, if/else
and case
¾ RTL model
• Based on clock
• RTL model must be accurate at the boundary of every
clocked elements
• RTL level is appropriate for synthesis, so designers use RTL
to mean the synthesizable subset of behavioral Verilog
¾ Testbenchs, or test fixtures, are typically modeled at
the behavioral level.
¾ All behavior constructs are legal for testbenchs.
5
Overall Structure
Top module
Outline
6
Module Definition
¾ Value set
• 0 – logic zero or false condition
• 1 – logic one or true condition
• x – unknown or don’t care logic value
• z – high-impedance state
¾ Number representation
• <number>
• ‘<base><number>
• <width>’<base><number>
• base – b, d, o ,h
7
Data Types (1/2)
¾ Signal nets
• wire, tri
¾ Wired nets
• wand, wor, triand, trior
• trireg
• tri0, tri1
¾ Supply nets
• supply0, supply1
¾ Registers
• reg
¾ Memories
• array of register variables
¾ Integers (32-bit)
• integer
¾ Time (64-bit)
• time
¾ Real numbers
• real
¾ Parameters
• parameter
8
Operators (1/2)
¾ Arithmetic operators
• +, 1, *, /, %
¾ Relational operators (0, 1, x)
• <, >, <=, >=
¾ Equality operators (0, 1)
• ==, !=, ===, !==
¾ Logical operators (0, 1)
• &&, ||, !
¾ Bit-wise operators
• ~, &, |, ^, ~^, ^~
Operators (2/2)
9
Module Connectivity
¾ Order list module top (…);
…
wire [7:0] data, result;
inv INV0 (data, result);
endmodule
Parameters
10
Overriding the Values of
Parameters (1/2)
Defparam
Defparam Statement
Statement
<<example>>
parameter p1 = 8,
real_constant = 2.039,
x_word = 16’bx,
file = “/usr1/jdough/design/mem_file.dat”;
...
endmodule
module test;
...
mod I1 (.in1(in1), .in2(in2), .out(out));
defparam
I1.p1 = 6;
I1.file = “../my_mem.dat”;
endmodule
Module
Module Instance
Instance Parameter
Parameter Override
Override
<<example>>
parameter p1 = 8,
real_constant = 2.039,
x_word = 16’bx,
file = “/usr1/jdough/design/mem_file.dat”;
endmodule
module top;
...
mod #(5, 3.0, 16’bx, “../my_mem.dat”)
I1 (.in1(in1), .in2(in2), .out(out));
...
endmodule
11
Register Arrays
Memory Addressing
<<example>>
module mems;
12
Behavioral Modeling
¾ Event-driven procedures
• always, initial
¾ Sequential blocks
• Begin...end
¾ Parallel blocks
• Fork...join
Procedure blocks
initial always
13
Procedure Timing Control
Edge-Sensitive Timing
input clk;
input [2:0] a, b;
output [3:0] out;
reg [3:0] out, sum;
always @ (a or b) // when any change occurs on a or b
#5 sum = a + b;
always @ (negedge clk) // at every negative edge of clk
out = sum;
endmodule
Example
Example
14
Conditional Statements
<<example>>
if (<expression>) if (index>0)
<statement_or_null> if (rega > regb)
result = rega;
else
result = regb;
<<example>>
case/casez/casex (<epression>)
<case_item>
endcase
15
Nonblocking Procedural
Assignment
<<example>> Non-blocking
Non-blocking Assignment
Assignment
parameter BIT_SIZE = 4
Continuous Assignments
/* Better !!! */
wire out;
assign out = a & b; //explicit
16
Functions and Tasks (1/2)
¾ Task
• Is typically used to perform debugging operations, or to
behaviorally describe hardware
• Can contain timing controls (#, @, wait)
• Can have input, output, and inout arguments
• Can enable other tasks or functions
¾ Function
• Is typically used to perform a computation, or to represent
combinational logic
• Cannot contain any delays; functional happen in zero
simulation time
• Has only input arguments and returns a single value through
the function name
• Can enable other functions, but not tasks
17
Verilog Tasks
<<example>>
endmodule
Verilog Functions
<<example>>
endmodule
18
Special Language Token
Text Substitution
`define D_NOT #1
`define D_AND #2
`define D_OR #1
19
Text Inclusion
Timescale
¾ `timescale compiler directive declares the time
unit and precision
• `timescale <time_unit>/<time_precision>
`timescale 1ns/100ps
20
Outline
Testbench
Design to verify
Simple
Simple test
test bench
bench
Testbench
Design to verify
Sophisticated
Sophisticated test
test bench
bench
21
Test Fixture
module testfixture;
// instantiate modules
mux2to1 test_mux (.a(a),
.b(b),
.sel(sel),
.out(out));
// apply stimulus
// display results
endmodule
《example》
22
Test Fixture ― Describing Stimulus
module testfixture;
// data type declaration
reg a, b, sel;
wire out;
// instantiate modules
mux2to1 test_mux (.a(a),
.b(b),
.sel(sel),
.out(out));
// apply stimulus
initial
begin
a = 0; b = 1; sel = 0;
#5 b = 0;
#5 b = 1; sel = 0;
#5 a = 1;
$finish;
end
// display results
Initial
$monitor($time,,”out = %b a = %b b = %b sel = %b”, out, a, b, sel);
endmodule
2005 VLSI Training Course - 45 -
23
Dumping Signals
//Dump signals in top.u1 and top.u2, and in all their subscopes of them, two level down
$dumpvars (3, top.u2, top.u1)
initial
begin
$dumpfile(“verilog_dump.vcd”);
$dumpvars(0, testfixture);
end
¾ $readmemb
$readmemb(“file_name”,<memory_name>);
$readmemb(“file_name”,<memory_name>,<start_addr>);
$readmemb(“file_name”,<memory_name>,<start_addr>,<finish_addr>);
¾ $readmemh
$readmemh(“file_name”,<memory_name>);
$readmemh(“file_name”,<memory_name>,<start_addr>);
$readmemh(“file_name”,<memory_name>,<start_addr>,<finish_addr>);
24
File Input (2/2)
...
0000_0000
0110_0001 0011_0010
// addresses 3-255 are not defined
@100 //hex
1111_1100
/* addresses 257-1022 are not defined */ 11111100 256
@3FF
1110_0010
...
11100010 1023
0 7
$fclose(MCD1);
Example
Example
2005 VLSI Training Course - 50 -
25
File Output (2/2)
<<example>>
...
integer message, broadcast, cpu_chann, alu_chann;
initial
begin
cpu_chann = $fopen(“cpu.dat”); if (!cpu_chann) $finish;
alu_chann = $fopen(“alu.dat”); if (!alu_chann) $finish;
// channel to both cpu.dat and alu.dat
message = cpu_chann | alu_chann;
// channel to both files, standard out, verilog.log
broadcast = 1 | message;
end
In Line Stimulus
¾ Variable can be listed only when their values change
¾ Complex timing relationship are easy to define
¾ A test bench can become very large for complex
tests module inline_tb;
initial
fork
data_bus = 8’h00;
addr = 8’h3f;
#10 data_bus = 8’h45;
#15 addr = 8’hf0;
#40 data_bus = 8’h0f;
#60 $finish;
join
endmodule
Example
Example
2005 VLSI Training Course - 52 -
26
Stimulus From Loops
¾ The same set of stimulus variables are modified in
every iteration
¾ Timing relationships are regular in nature
¾ Code is compact
module loop_tb;
reg clk;
reg [7:0] stimulus;
wire [7:0] results;
integer i;
initial
begin
clk = 1’b1;
for (i = 0; i < 256; i = i + 1)
@ (negedge clk) stimulus = i;
#20 finish;
end
endmodule
2005 VLSI Training Course - 53 - Example
Example
initial
begin
// load array with values
#20 stimulus = stim_array[0];
#30 stimulus = stim_array[15]; // in line
#20 stimulus = stim_array[1];
for (i = 14; i > 1; i = i - 1) // from loop
#50 stimulus = stim_array[i];
#30 finish;
end
endmodule
Example
Example
2005 VLSI Training Course - 54 -
27
Stimulus From Vector
module read_file_tb;
initial
begin // Vectors are loaded
$readmemb(“vec.txt”, stim);
for (i = 0; i < num_vecs; i = i + 1)
#50 data_bus = stim[i];
end
endmodule
Example
Example
Outline
28
Simulation Algorithms
29
Invoking Verilog-XL
¾ Syntax
• verilog [verilog-xl_operations] design_files
¾ No command line options
• verilog mux.v test.v
¾ Using the -c command-line option to check the
syntax and connectivity of your design without
actually simulating
• verilog –c mux.v test.v
¾ Using the -f command-line option to specify a file
that contains command-line arguments
• verilog –f run.f run.f
mux.v
test.v
-c
2005 VLSI Training Course - 59 -
Outline
30
Delay Calculators
31
SDF Annotator
$sdf_annotate(“sdf_file”,[module_instance,
“config_file”, “log_file”, “mtm_spec”,
“scale_factors”, “scale_type”]);
Example
Example
module top;
...
cpu u1 (...);
fpu u2 (...);
dma u3 (...);
...
initial
begin
$sdf_annotate(“sdffiles/cpu.sdf”,m1,,”logfiles/cpu_sdf.log”);
$sdf_annotate(“sdffiles/fpu.sdf”,m1,,”logfiles/fpu_sdf.log”);
$sdf_annotate(“sdffiles/dma.sdf”,m1,,”logfiles/dma_sdf.log”);
end
...
endmodule
Example
Example
32
Reference
33