Verilog Tutorial Fa16
Verilog Tutorial Fa16
ELE/COS 475
P R I NCE TON UN I V ERSITY
PAU L JACKS ON
FA L L 2 0 1 5
2 / 39
Purposes of HDL
3 / 39
Purposes of HDL
• Initially was created to simplify design simulation and verification
4 / 39
Purposes of HDL
• Initially was created to simplify design simulation and verification
5 / 39
Purposes of HDL
• Initially was created to simplify design simulation and verification
6 / 39
Verilog HDL
7 / 39
Poll #1
8 / 39
Poll #1
9 / 39
Poll #1
10 / 39
Verilog paradigm
Design consists of hierarchical modules connected on
the same level of hierarchy
11 / 39
Differences from programming languages
Verilog C/C++
Variable declaration
wire [7:0] sum; int sum;
Procedural blocks
if (a == 8’b0) begin if (a == 0) {
… …
end }
else begin else {
… …
end }
12 / 39
Differences from programming languages
Verilog C/C++
For statement
wire [N-1:0] a;
generate
for (i=0; i<N; i=i+1) for (i=0; i<N; i++) {
begin a[i] = b + c;
assign a[i] = b & c; }
end
endgenerate
a[0] = b+c;
a[1] = b+c;
a[2] = b+c; Describes
Describes
… Sequence of
Hardware
a[N-2] = b+c; Instructions
a[N-1] = b+c;
13 / 39
Verilog
Syntax
14 / 39
Syntax
• Logic values:
‘0’, ‘1’
‘x’ – unknown value (‘0’, ‘1’ or ‘z’)
‘z’ – high-impedance
• Literals <width>’<base><number>
Binary: 1’b0, 4’b1100, 8’b1001_1101
Decimal: 8’d7
Hexadecimal: 32’hBAC007
• Integer
int i; - 32 bit signed value;
15 / 39
Syntax
• Wires
wire single_wire;
wire [7:0] one_dimensional_bus_1; or
wire [0:7] one_dimensional_bus_2;
wire [31:0] two_dimensional_bus [7:0];
• Slicing
wire [31:0] bus; - declare 4 byte bus
bus[7:0] – lowest byte of bus
• Concatenation
{bus[7:0], bus[15:8], bus[23:16], bus[31:24]}
• Replication
{8{4’b1010}}; - replicate binary value 4’b1010 8 times
16 / 39
Syntax
17 / 39
Syntax
• Operators
Operator Type Symbols Example
Bitwise ~ & | ^ 4’b1010 & 4’b0100
Arithmetic + - * / ** % 4’b1110 – 1
Relational > < >= <= == != === !=== 4’d5 < 4’d3
18 / 39
Syntax
• Basic Verilog statements: always block
19 / 39
Syntax
• Blocking VS non-blocking assignments
= - blocking
<= - non blocking, used only in always blocks
d =? a d =? a
20 / 39
Syntax
Blocking Non Blocking
21 / 39
Syntax
• Assignment statement (block)
wire any_val;
wire in_val_1, in_val_2, in_val_3;
assign any_val = in_val_1 | in_val_2 | in_val_3;
Important notes:
all assign statements are executed in parallel
LHS of assign block is always a wire
Describes only combinational logic
22 / 39
Syntax
23 / 39
Syntax
• Conditional statements Important notes:
if <condition> begin
<statement1>
Used in always, initial
end
[else if <condition> begin blocks
<statement2> A latch will be
end] generated instead of
[Else begin a flip-flop if there is
<statement3>
no else statement
end]
24 / 39
Syntax
• Case statement Important notes:
case (addr):
0: out = a; Used in always, initial
1: out = b; blocks
2: out = c; A latch will be
default: begin end generated instead of
endcase a flip-flop if case is
not full and there is
• casez: treats z as don’t care no default statement
casex: treats x and z as don’t care
Used in simulation
25 / 39
Syntax
• Conditional (ternary) operator
26 / 39
Common styles
Style 1 Style 2
Combinational logic
reg empty;
wire empty;
always @(*) begin
assign empty = (cnt==0) & ~wr_val; empty = (cnt==0) & ~wr_val;
end
Sequential logic
reg state, state_next;
reg [ST_W-1:0] state;
always @(*) begin
always @(posedge clk) begin if (val_1_in)
if (~rst_n) state_next = ACCEPT_ST;
state <= {ST_W{1’b0}}; else if (val_2_in)
else state_next = WAIT_ST;
state <= val_1_in ? ACCEPT_ST : else
val_2_in ? WAIT_ST : state_next = state;
state ; End
end
always @(posedge clk) begin
if (~rst_n)
state <= {ST_W{1’b0}};
else
state <= state_next;
end
27 / 39
Syntax
• Module structure • Module instantiation
module <module name> ( <module name> <instance name> (
input clk, .clk (rst_n ),
input rst_n, .rst_n (rst_n ),
// Register declaration
reg [3:0] position;
// Wire declaration
wire [3:0] position_next;
// Sequential logic
// Combinational logic
endmodule
28 / 39
Syntax
• Parametrization
Verilog has preprocessor – can use defines
`include <defines.h>
module example(
input clk,
input rst_n,
endmodule
29 / 39
Syntax
• Parametrization
Parameters • Overwriting parameters
module example #( example exmpl_inst #
parameter WIDTH = 16 .WIDTH (32)
) )
( (
input clk, .clk (clk ),
input rst_n, .rst_n (rst_n ),
input [`WIDTH-1:0] data_in,
output reg [`WIDTH-1:0] data_out .data_in (data_in ),
); .data_out (data_out )
);
...
endmodule
30 / 39
Verilog
• Parametrization
Local parameters
`define WIDTH 16 Notes:
module example ( Parameters can be
input clk,
input rst_n,
overwritten from outside,
localparms can not
input [`WIDTH-1:0] cmd
);
localparam INIT_ST = 0;
localparam WAIT_DATA_ST = 1;
localparam WAIT_CMD_ST = 2;
...
endmodule
31 / 39
Non-synthesizable Verilog &
Other topics
32 / 39
Verilog
• Nonsynthesizable part of Verilog is used to:
• Simulate delays
• Monitor signal values
• Model external signals
• Stop/continue simulation
• Simulation time precision
`timescale 1ns/1ps
module tb_top();
...
endmodule
33 / 39
Verilog
• Generate system clock
initial begin
forever
clk = #10 ~clk;
end
• Dump waveforms*
initial begin
$dumpfile("waves.vcd");
$dumpvars(0, fpga_top.cmp_top.mc_top.pkt_trans_dp_wide);
$dumpvars(0, fpga_top.cmp_top.mem_io_splitter);
end
*for loop may be required to dump multidimensional buses
• Finish simulation
always @(posedge clk)
if ($time >= MAX_SIM_TIME)
$finish;
34 / 39
Verilog
… and many others:
Tasks
Functions
While, for loops
$reset, $stop
$random
$fopen, $fdisplay, $fwrite
35 / 39
Python Preprocessor for Verilog
PyHP preprocessor can run Python code to generate Verilog
code
Simplifies verbose Verilog code
Can use Python functions and/or loops to dynamically
generate Verilog
Useful for
Connecting large modules together
Filling out a complex control signal table
Instantiating code that is difficult to create using a
generate statement
36 / 39
Python Preprocessor for Verilog
37 / 39
Job Scheduling on Adroit
• Logging into Adroit logs into the head node
• The head node should be used for small tasks like file
editing, compilation, etc.
• Simulations should not run on the head node
38 / 39
Job Scheduling on Adroit
• sinfo – displays cluster resources
• Squeue – displays job queue
Reference: http://www.ceci-hpc.be/slurm_tutorial.html
39 / 39
Job Scheduling on Adroit
• A submission script is used to define what commands
are needed in order to run the job
• srun indicates a command to be run
• Submission scripts also contain information about the
job itself
• Use SBATCH parameters
(http://slurm.schedmd.com/sbatch.html)
#!/bin/bash
#
#SBATCH --job-name=pjjtest // Job name
#SBATCH --output=result.txt // Log file for console output
#SBATCH --ntasks=1 // Number of tasks (>1 when parallelizing tasks)
#SBATCH --time=10:00 // Allocated time to complete job
#SBATCH --mem-per-cpu=100 // Memory allocation per cpu(MB)
Reference: http://www.ceci-hpc.be/slurm_tutorial.html
41 / 39
Design verification
Make sure that the module is doing what it supposed to do
• For labs:
• Check all functions, performed by a module
• Check corner case
• Explain why those test cases are corner cases
42 / 39
Common Errors
• No clock signal
• Wrong reset level
• If-else, case statements without default case
• No initial values for registers (which do require it)
• Blocking assignment (=) in @(posedge clk) blocks
• Describing a combinational logic instead of flip-flop
• Width mismatch
• Counter overlow/undeflow (do no assume all counter to
be power of 2, especially in parametrized modules)
• Deadlock on val/rdy interface (circular dependence)
43 / 39
Design guidelines
• Use version control
44 / 39
Have more questions?
45 / 39