Lecture 2
Lecture 2
Verilog HDL
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 2
Abstraction
levels
• Three coding styles:
• Structural code (GTL (Gate Level), Netlist)
• RTL (Register Transfer Level)
• Behavioral (Testbench)
• DUT (Device Under Test)
• Represents Hardware
• Usually RTL or GTL
• Testbench
• Represents System
• Usually Behavioral
• Using higher order languages (“e”/SystemVerilog)
3
1 2 3 4 5
Introduction Verilog Simple FSM Coding Style
Syntax Example Implementation for RTL
s
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
signalswire 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
• &, |, ~, &&, ||,
out = in1 | in2;
etc.
• 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) 6
• 8’d255= 8 bit decimal 255
Procedural Blocks
initial begin
• Initial block a = 1’b0;
• Will be executed only once, at first timeb = 1’b0;
• the unit is called (Only in testbench) end
always @(posedge clock)
if (!nreset)
• Always block q <= 1’b0;
Statements will be evaluated else
q <= d;
when
a change in sensitivity list always @(posedge clock or negedge
occurs nreset) if (!nreset)
• Example1 - sync reset, q <= 1’b0;
else if
rising edge triggered (load_enable) q
7
flop: <= d;
Procedural Blocks
• There are two types of always
always @(posedge clock or negedge
blocks nreset)
if (!
• Sequential nreset) q
• Asserted by a clock in the <= 1’b0;
else if
sensitivity list. (load_enable) q
• Translates into flip-flops/latches. <= d;
always @(a or b or c)
• Combinational out = a & b & c;
• Describes purely combinational
logic, and therefore, the
sensitivity list always @(*)
has (non-clock) signals. out = a & b & c;
11
1 2 3 4 5
Introduction Verilog Simple FSM Coding Style
Syntax Example Implementation for RTL
s
Simple
Examples
Hello World
• Your first Verilog
module:
module
main;
initial
begin
$disp
lay(“
Hello
world
!”);
$finish
; end
endmodule
13
Combinatorial Logic
• Three ways to make a Mux
• Using an assign • Using a case
Statement:
wire out; statement:
reg out;
assign out = sel ? a : b; always @ (a or b or
sel) begin
case (sel)
• Using an always 1’b0: out=b;
Block: 1’b1:
reg out; out=a;
always @ (a or b or endcas
sel) if (sel) e end
out=a;
else
out=b;
14
Sequential Logic
• A simple D-Flip • Be careful not to infer
Flop: latches!!!:
reg q; reg q;
always @(posedge always
clk) q<= d; @(en)
if (en)
q<=
• An asynch reset D-Flip d;
Flop:
reg q;
always @(posedge clk or negedge
reset_)
if
(~reset_)
q<= 0;
else
q<= d;
15
Arithmetic
• Verilog supports standard arithmetic operators:
• +, -, *, << (shift left), >> (shift right), etc.
• Be careful about division… (not synthesizable!)
• Concatenate signals with the {,} operatorassign a = 4’b1100;
assign b = 4’b1010;
assign c = {a,b};
• But… //c=8’b11001010
• By default, Verilog treats all vectors as unsigned binary
• numbers.
To do signed (two’s complement) wire signed [9:0] a,b;
operations, declare the reg/wire as wire signed [19:0] result =
a*b;
signed:
• To make a constant signed, add an s:
10’sh37C
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
reg r; of a module cannot
reg r;be a reg. module m1 module m2
always initia (out) output (in) input
@* l out; in; reg in;
r = a begi endmodule endmodule
& b;w;
wire n
assign w = a & b; r = reg r;
1’b0; #1 m1
17
r = m1_instance(.
Testbench constructs
• Create a
clock: `define CLK_PERIOD 10
initial
begin //begins executing at
time 0 clk = 0;
end
18
1 2 3 4 5
Introduction Verilog Simple FSM Coding Style
Syntax Example Implementation for RTL
s
Verilog FSM
Implementation
A simple 4-bit counter example
19
FSM Example
• A 4-bit counter module sm
#(parameter COUNTER_WIDTH = 4)
• Receives 4 inputs: (clk,rst_n,act,up_dwn_n,count,ovf
• clk – the system clock lw);
• rst_n – an active low reset input clk;
• act – the activate signal input rst_n;
• up_dwn_n – count up input act;
(positive) input
or count down up_dwn_n;
(negative) output [COUNTER_WIDTH-1:0]
count; reg [COUNTER_WIDTH-1:0]
• Outputs 2 signals: count; output ovflw;
• count: the current counted reg ovflw;
value reg [3:0] state, next_state;
• ovflw: an overflow signal
20
FSM
Example act==1
up_dwn_n==
1
• Draw the state act==1
up_dwn_n==
machine: 1
Coun
t
reset
act== Up
0 act== act==1
count
up_dwn_n== up_dwn_n==
IDL 1
act== 1 ++ 0 Overflo
0 E act== w
0 ovflw=
Count 1
act==1
up_dwn_n== Down
0 count-
-
always@*
case (state)
3
• And do not put logic on your reset 1’b1011: if (b || reset )
next_state =Adam
idle; Teman, 20 1
Parameterize your
design
• “Pretty code” is code that is completely parametrized
• Two approaches to parameterization:
• Compiler directives: `define, `include, and `ifdef
• put all `define statements in external define files.
• Parameters or localparam
• parameters can be overridden through instantiation
• localparam is better for constants, such as FSM encoding