Verilog Intro
Verilog Intro
Verilog Intro
Langaue (HDL)
Verilog
1
5 March 2020
Outline
n Verilog
¡ Structural constructs
¡ Describing combinational circuits
n Additional references
¡ “Starter’s Guide to Verilog 2001” by Michael
Ciletti copies for borrowing in hardware lab
¡ ASIC-WORLD: http://www.asic-
world.com/verilog/index.html
¡ https://sutherland-
hdl.com/pdfs/verilog_2001_ref_guide.pdf 2
Combinational design
n Step 1: Understand the problem
¡ Identify the inputs and outputs
¡ Draw a truth table
n Step 2: Simplify the logic
¡ Draw a K-map
¡ Write a simplified Boolean expression
n SOP or POS
n Use don’t cares
n Step 3: Implement the design
¡ Logic gates and/or
¡ Verilog
3
Ways of specifying circuits
n Schematics
¡ Structural description
¡ Describe circuit as interconnected elements
n Build complex circuits using hierarchy
¡ Large circuits are unreadable
4
Verilog versus VHDL
5
Simulation and synthesis
n Simulation
¡ Models what a circuit does
n Multiply is “*”, ignoring implementation options
¡ Allows you to test design options
¡ “Execute” a design to verify correctness
n Synthesis
¡ Converts your code to a "netlist"
n Can simulate synthesized design
¡ Tools map your netlist to hardware
6
Simulation and synthesis
Gate or
HDL
Synthesis Transistor
Description
Description
Physical
Simulation Simulation
Implementation
Functional/
Functional Real
Timing
Validation Chip!
Validation
8
Specifying circuits in Verilog
“Structural” “Behavioral”
wire E; wire E; reg E, X, Y;
and g1(E,A,B); assign E = A & B; always @ (A or B or C)
not g2(Y,C); assign Y = ~C; begin
or g3(X,E,Y); assign X = E | Y; E = A & B;
Y = ~C;
X = E | Y;
end 9
Data types
n Values on a wire
¡ 0, 1, x (unknown or conflict), z (tristate or
unconnected)
n Vectors
¡ A[3:0] vector of 4 bits: A[3], A[2], A[1], A[0]
n Unsigned integer value
n Indices must be constants
10
Manipulating vectors
11
Data types that do NOT exist
n Structures
n Pointers
n Objects
n Recursive types
12
Numbers
n Format: <sign><size><base><number>
n 14
¡ Decimal number
n –4’b11
¡ 4-bit 2’s complement binary of 0011 (is 1101)
n 12’b0000_0100_0110
¡ 12 bit binary number (_ is ignored)
n 12’h046
¡ 3-digit (12-bit) hexadecimal number
13
Numbers are unsigned
14
Operators
15
Two abstraction mechanisms
n Modules
¡ More structural, but also behavioral
¡ Heavily used in 370 and “real” Verilog
code
n Functions
¡ More behavioral
¡ Used to some extent in “real” Verilog, but
not much in 370
16
Basic building blocks: modules
17
Basic building blocks: modules
n Instanced into a design n Name can’t begin with a
¡ Never called number
n Use wires for connections n Names are case sensitive
n Modules execute in parallel n Keywords are in lowercase
n Gate declarations (and, or, n and, or, not are keywords
etc) n Illegal to nest module
¡ List outputs first definitions
¡ Inputs second n // for comments
18
Modules are circuit components
AND2
n Module has ports A OR2
E
g1
¡ External connections B 1 g3 X
3
¡ A,B,C,X,Y in example
NOT
n Port types C Y
¡ input 2 g2
¡ output
¡ inout (tristate) // previous example as a
// Boolean expression
n Use assign statements for
module simple2 (X,Y,A,B,C);
Boolean expressions
input A,B,C;
¡ and Û & output X,Y;
¡ or Û | assign X = (A&B)|~C;
¡ not Û ~ assign Y = ~C;
endmodule
19
Structural Verilog
NOT AND2
module xor_gate (out,a,b); abar
a t1
input a,b; 4 and1
inva b 6
output out; OR2
20
Behavioral Verilog
21
Behavioral 4-bit adder
module add4 (SUM, OVER, A, B); Buses are implicitly connected—
input [3:0] A; If you write BUS[3:2], BUS[1:0],
input [3:0] B;
output [3:0] SUM; they become part of BUS[3:0]
output OVER;
assign {OVER, SUM[3:0]} = A[3:0] + B[3:0];
endmodule
“[3:0] A” is a 4-wire bus labeled “A”
Bit 3 is the MSB
Bit 0 is the LSB
24
Example: 4-bit comparator
module Compare1 (Equal, Alarger, Blarger, A, B);
input A, B;
output Equal, Alarger, Blarger;
assign Equal = (A & B) | (~A & ~B);
assign Alarger = (A & ~B);
assign Blarger = (~A & B);
endmodule
25
4-bit comparator
// Make a 4-bit comparator from 4 1-bit comparators
26
Functions
28
Assignments
n Blocking assignments (Q = A)
¡ Variable is assigned immediately
n New value is used by subsequent statements
n Non-blocking assignments (Q <= A)
¡ Variable is assigned after all scheduled
statements are executed
n Value to be assigned is computed but saved for later
parallel assignment
¡ Usual use: Register assignment
n Registers simultaneously take new values after the
clock edge
29
Blocking vs. non-blocking
n Example: Swap
always @(posedge CLK) always @(posedge CLK)
begin begin
temp = B; A <= B;
B = A; B <= A;
A = temp; end
end
30
Verilog tips
n Do not write C-code
¡ Think hardware, not algorithms
n Verilog is inherently parallel
n Compilers don’t map algorithms to circuits
well
31