Lecture 2 Verilog PDF
Lecture 2 Verilog PDF
Lecture 2 Verilog PDF
Disclaimer: This course was prepared, in its entirety, by Adam Teman. Many materials were copied from sources freely available on the internet. When possible, these sources have been cited;
however, some references may have been cited incorrectly or overlooked. If you feel that a picture, graph, or code example has been copied from you and either needs to be cited or removed,
please feel free to email adam.teman@biu.ac.il and I will address this as soon as possible.
What is a hardware description language?
• HDL is NOT another programming language:
• Textual representation of Hardware constructs
• All statements are executed in parallel
• Code ordering is flexible.
Example:
• a=1;b=2;c=a+b c==3
• c=a+b;a=1;b=2 c==3
• Execution of code is triggered by Events
• Sensitivity lists are used to define when a code section is executed
• Different simulators may yield different results
• => Coding style is required
2
Abstraction levels
• Three coding styles:
• Structural code (GTL (Gate Level), Netlist)
• RTL (Register Transfer Level)
• Behavioral (Testbench)
3
1 2 3 4 5
Introduction Verilog Simple FSM Coding Style
Syntax Examples Implementation for RTL
Verilog Syntax
Basic Constructs
• Primitives:
• not, and, or, etc.
or(out, in1, in2);
• Signals:
• 4 states: 0,1,X,Z
• Wires: do not keep states
• Registers: keep states (i.e., outputs)
• Can represent buses or group of signals
wire in1,in2;
reg out;
wire [7:0] data;
reg [31:0] mem [0:7]; //width (bits)=32, depth (words)=8
5
Basic Constructs
• Operators:
• Similar to primitives
• &, |, ~, &&, ||, etc. out = in1 | in2;
• Constants:
• The format is: W’Bval
• Examples:
• 1’b0 – single bit binary 0 (or decimal 0)
• 4’b0011 - 4 bit binary 0011 (or decimal 3)
• 8’hff = 8 bit hexadecimal ff (or decimal 255)
• 8’d255= 8 bit decimal 255
6
Procedural Blocks
• Initial block
• Will be executed only once, at first time initial begin
a = 1’b0;
the unit is called (Only in testbench) b = 1’b0;
end
• Always block
• Statements will be evaluated when always @(posedge clock)
a change in sensitivity list occurs if (!nreset)
q <= #1 1’b0;
• Example1 - sync reset, else
rising edge triggered flop: q <= #1 d;
8
Assignments
Verilog has three types of assignments: assign muxout = (sel&in1) | (~sel&in0);
• Continuous assignment assign muxout = sel ? in1 : in0;
always @*
case (sel)
2’b00: out = in[0];
2’b01: out = in[1]; Module
2’b10: out = in[2]; Body
2’b11: out = in[3];
default: out = 4’bx;
endcase
endmodule
10
System Tasks
• System tasks are used to provide interface to simulation data
• Identified by a $name syntax
• Printing tasks:
• $display, $strobe: Print once the statement is executed
• $monitor: Print every time there is a change in one of the parameters
• All take the “c” style printf format
$display(“At %t Value of out is %b\n”,$time,out);
• Waveform tasks:
• $shm_open - Opens a dump file $shm_open(“testbench.db”,1);
$shm_probe(“AS”) // dump all signals
• $shm_probe() – lists signals to probe
11
1 2 3 4 5
Introduction Verilog Simple FSM Coding Style
Syntax Examples Implementation for RTL
Simple Examples
Hello World
• Your first Verilog module:
module main;
initial
begin
$display(“Hello world!”);
$finish;
end
endmodule
13
Combinatorial Logic
• Three ways to make a Mux
• Using an Assign Statement: • Using a Case statement:
wire out; reg out;
assign out = sel ? a : b; always @ (a or b or sel)
begin
case (sel)
• Using an Always Block: 1’b0: out=b;
1’b1: out=a;
reg out; endcase
always @ (a or b or sel) end
if (sel)
out=a;
else
out=b;
14
Sequential Logic
• A simple D-Flip Flop: • Be careful not to infer latches!!!:
reg q; reg q;
always @(posedge clk) always @(en)
q<= #1 d; if (en)
q<= #1 d;
15
Arithmetic
• Verilog supports standard arithmetic operators:
• +, -, *, << (shift left), >> (shift right), etc.
• Be careful about division… (not synthesizable!)
• Concatenate signals with the {,} operator assign a = 4’b1100;
assign b = 4’b1010;
• But… assign c = {a,b}; //c=8’b11001010
16
reg vs. wire
• Oh no… Don’t go there!
• A reg is not necessarily an actual register, but rather a “driving signal”… (huh?)
• This is truly the most ridiculous thing in Verilog…
• But, the compiler will complain, so here is what you have to remember:
1. Inside always blocks (both sequential and combinational) only reg can be used as LHS.
2. For an assign statement, only wire can be used as LHS.
3. Inside an initial block (Testbench) only reg can be used on the LHS.
4. The output of an instantiated module can only connect to a wire.
5. Inputs of a module cannot be a reg.
initial
begin //begins executing at time 0
clk = 0;
end
18
1 2 3 4 5
Introduction Verilog Simple FSM Coding Style
Syntax Examples Implementation for RTL
19
FSM Example
• A 4-bit counter module sm
#(parameter COUNTER_WIDTH = 4)
• Receives 4 inputs: (clk,rst_n,act,up_dwn_n,count,ovflw);
• clk – the system clock input clk;
• rst_n – an active low reset input rst_n;
• act – the activate signal input act;
• up_dwn_n – count up (positive) input up_dwn_n;
or count down (negative) output [COUNTER_WIDTH-1:0] count;
output reg
• Outputs 2 signals: reg ovflw;
• count: the current counted value reg [COUNTER_WIDTH-1:0] count;
• ovflw: an overflow signal reg [3:0] state, next_state;
20
FSM Example act==1
up_dwn_n==1
25
Testbench Example • Define a clock:
always
• Set initial values, value monitoring #5 clk = ~clk;
and reset sequence: • Set stimuli:
initial begin initial begin
clk = 1'b1; // @100, Start counting up
rst_n = 1'b0; // Activate reset // until overflow
act = 1'b0; #100 act = 1'b1;
up_dwn_n = 1'b1; up_dwn_n = 1'b1;
$shm_open("testbench.db",1); // Open waveform file // Reset (10 cycles pulse)
$shm_probe("AS"); // Dump all signals to file #1000 rst_n = 1'b0;
// Monitor changes act = 1'b0;
$monitor("%t: rst_n=%b act=%b up_dwn_n=%b count=%d #100 rst_n = 1'b1;
ovflw=%b\n",$time,rst_n,act,up_dwn_n,count,ovflw); // Do a count-up to 4 and
// After 100 time steps, release reset // then count-down to ovflw
#100 rst_n = 1'b1; #100 act = 1'b1;
end up_dwn_n = 1'b1;
#40 up_dwn_n = 1'b0;
end
endmodule
26
1 2 3 4 5
Introduction Verilog Simple FSM Coding Style
Syntax Examples Implementation for RTL
always @(sel or a or b or c or d)
case (sel)
2‘b00: out = a;
• The same will happen if an output is not 2‘b01: out = b;
assigned in all branches of an if-else 2‘b10: out = d;
endcase
block. endmodule
31
Stick with one reset type
• The purpose of reset is to bring your design into a well-known state.
• It is desirable to have every flip-flop resettable, whether or not required.
• We usually use asynchronous reset: always @(posedge clk or negedge rst_)
if (!rst_)
state <= idle;
else
state <= next_state;
33
Write readble code
• Always use indentation!!!
• You will lose points if you turn in ugly code!
• Naming Conventions
• Really useful!
• There is no “one right answer”, but two recommended styles are:
• NetFPGA VerilogCodingGuidelines
https://github.com/NetFPGA/netfpga/wiki/VerilogCodingGuidelines
• ETH-Zurich VHDL naming conventions (with emacs highlighting!):
https://www.dz.ee.ethz.ch/en/information/hdl-help/vhdl-naming-conventions.html
34
Some helpful documents and references
• Chris Fletcher “Verilog: wire vs. reg”
• Greg Tumbush “Signed Arithmetic in Verilog 2001 – Opportunities and Hazards”
• NetFPGA wiki “VerilogCodingGuidelines”
• MIT 6.111 Lectures http://web.mit.edu/6.111/www/f2007/
• Stuart Sutherland, Don Mills “Standard Gotchas: Subtleties in the Verilog and
SystemVerilog Standards That Every Engineer Should Know”
35