S1 Introduction To Verilog
S1 Introduction To Verilog
Jaewoong Sim
Electrical and Computer Engineering
Seoul National University
• Let’s briefly look at Verilog HDL J
• What’s important?
• Wire vs Reg
• Continuous Assignment vs Procedural Assignment vs Procedural Continuous Assignment
• Procedural Assignment
} Blocking vs Non-Blocking Assignment
2
Two common hardware description languages
Verilog
• Developed by Gateway Design Automation in 1984; Gateway was acquired by
Cadence in 1990
• Became an IEEE Standard (IEEE 1364) in 1995
} Verilog-95, Verilog 2001, Verilog 2005 (minor revision), SystemVerilog (Industry)
3
• Look at other Verilog code examples
• Do write code a lot!
• HDLBits
• https://hdlbits.01xz.net/wiki/Main_Page
• Highly recommend solving the problem sets there
• Created by my former colleague Henry Wong
} His Stanford seminar about x86 on FPGA is also interesting!
https://www.youtube.com/watch?v=vhHR6fNHyG8
a
y
b
Module
5
module name
input/output ports
endmodule
Format
module module_name (module interface list);
[list of interface ports]
...
[net and variable declarations]
Items enclosed in square
...
brackets are optional
[functional specification of module]
...
endmodule
6
• The following two Verilog codes are functionally identical
• Follow the ANSI-C style from now on! (easier to read & reduces typos!)
endmodule endmodule
9
Lexical Conventions
10
• Verilog uses almost the same lexical conventions as C language
• White space: blank space (\b), tabs (\t), and new line (\n)
• Comments
• // ç used for single line comments
• /* ….*/ ç used for multi-line comments
11
Number Representation
13
Number Representation
14
Number Representation - Examples
Integer Stored as
1 00000000000000000000000000000001
8'hAA 10101010
6'b10_0011 100011
'hF 00000000000000000000000000001111
6'hCA 001010
6'hA 001010
16'bZ zzzzzzzzzzzzzzzz
8'bx xxxxxxxx
15
Number Representation – Negative Values
module signed_number;
reg [31:0] a;
initial begin
a = 14'h1234;
$display ("Current Value of a = %h", a);
a = -14'h1234;
$display ("Current Value of a = %h", a);
a = 32'hDEAD_BEEF;
$display ("Current Value of a = %h", a);
a = -32'hDEAD_BEEF;
$display ("Current Value of a = %h", a);
#10 $finish;
end
endmodule
16
• Other data types
• String: “Back to School and Have a Nice Semester”
• Real number: 3.4, 294.872, 1.44E(or e)9
} Decimal and scientific notations accepted
} At least one digit on each side of the decimal point
¨ .2 (illegal)
17
Data Types & Ports
18
• 0 and 1 represent logic values low and high, respectively
• z indicates the high-impedance condition of a node or net
• x indicates an unknown value of a net or node
Value Meaning
0 Logic 0, false condition
1 Logic 1, true condition
x Unknown logic value
z High impedance
19
• Nets mean any hardware connection points (cannot store value!)
• Variables represent any data storage elements
Nets Variables
wire supply0 reg
tri supply1 integer
wand tri0 real
wor tri1 time
triand trireg realtime
trior
• Variables
• Can be referenced anywhere in a module
• Can be assigned value only within a procedural statement, task, or function
• Cannot be an input or inout port in a module
21
• There are three forms of assignments in Verliog
• Continuous Assignments
• Drive a value into a net
• Used to model combinational logic
• (Explicit) Use assign keyword
} wire temp;
assign temp = a | b;
• (Implicit) combine declaration and assignment
} wire temp = a | b;
• Procedural Assignments
• Assign values to variables declared as regs in always blocks/tasks/functions
• Usually used to model registers and FSMs
Module
net
24
module half_adder (x, y, s, c);
input x, y;
output s, c;
// -- half adder body-- //
// instantiate primitive gates
xor xor1 (s, x, y); Can only be connected by using positional association
and and1 (c, x, y);
endmodule
Instance name is optional
module full_adder (x, y, cin, s, cout);
input x, y, cin;
output s, cout;
wire s1,c1,c2; // outputs of both half adders
// -- full adder body-- // Connecting by using positional association
// instantiate the half adder
half_adder ha_1 (x, y, s1, c1); Connecting by using named association
half_adder ha_2 (.x(cin), .y(s1), .s(s), .c(c2));
or (cout, c1, c2);
Instance name is necessary
endmodule
25
Similar to the ones in
C language
26
Module Modeling Levels
• Structural Modeling
• Dataflow Modeling
• Behavioral Modeling
27
• Structural Level
• Gate level description of the circuit
• Connect built-in primitives, user-defined primitives, or other modules using
wires
• Describe a hierarchy
• Dataflow Level
• Describe hardware in terms of dataflow from inputs to outputs
• Use operators (+, -, &, |, …) that act on operands
• Use continuous assignment statements (keyword assign)
• Behavioral Level
• Typically used for sequential logic (but it can be used for combinational logic)
• Use procedural statements (keyword always)
• The target in procedural assignment statements must be a reg type
28
• Mixed Level
• Mix use of above three modeling levels
• Commonly used in modeling large designs
29
// gate-level description of half adder
module half_adder(input x,
• Half Adder: S = X⨁Y, C = XY input y,
output s,
X Y SUM CARRY output c);
1 1 0 1 endmodule
pre-defined in Verilog
// gate-level description of full adder
• Full Adder module full_adder(input x,
input y,
input c_in,
Full Adder output sum,
output c_out);
c_in sum // internal nodes
Half // outputs of both half adders
Adder wire s1, c1, c2;
x s1 c2
c_out // full adder body
Half
// instantiate the half adder
y Adder c1 half_adder ha_1 (x, y, s1, c1);
half_adder ha_2 (c_in, s1, sum, c2);
or (c_out, c1, c2);
endmodule 30
• combinational logic
module full_adder_dataflow(input x,
input y,
input c_in,
output sum,
output c_out);
// specify the function of a full adder
assign #5 {c_out, sum} = x + y + c_in;
continuous assignment endmodule
continuously driven
by something
Full Adder
c_in sum
Half
x s1 Adder c2 - Use operators to compute sum and c_out
c_out
Half - Use continuous assignment to drive
y Adder c1 values onto nets
31
• sequential & combinational logic
always block
Whenever the event in the sensitivity list occurs,
the always block is activated
module full_adder_behavioral(input x,
input y,
input c_in,
output reg sum,
output reg c_out);
32
module full_adder_mixed_style(input x,
input y,
input c_in,
output sum,
output reg c_out);
wire s1, c1, c2;
// structural modeling of HA 1.
xor xor_ha1(s1, x, y);
and and_ha1(c1, x, y);
// dataflow modeling of HA 2.
assign sum = c_in ^ s1;
assign c2 = c_in & s1;
33
Verilog Simulation
34
• Use a separate testbench for simulation
Stimulus Block
Stimulus
Module Under Test
Response
Top-Level Block
Stimulus
Response
35
• $display displays values of variables, string, or expressions
• $display(ep1, ep2, …, epn);
• ep1, ep2, …, epn: quoted strings, variables, expressions
36
• Time scale compiler directive:
`timescale time_unit / time_precision
37
Clock Generation
• [delay] statement;
module tb;
reg clk;
// clock generation
// starts at time 0ns and loops after every 5ns
always #5 clk = ~clk;
38
/**
* SNU ECE Digital Systems Design
* encoder4to2.v: encoder4to2 module
*
* 4 to 2 encoder: (Y3, Y2, Y1, Y0) => (A1, A0)
*
* (1, 0, 0, 0) => 3
* (0, 1, 0, 0) => 2
/** * (0, 0, 1, 0) => 1
* SNU ECE Digital Systems Design * (0, 0, 0, 1) => 0
* encoder4to2_tb.v: encoder4to2 testbench * everything else => 0
*/
*/
// note that the default port type is 'wire'
// compiler directive module encoder4to2(
// time unit: 1ns & precision: 100ps input Y0,
input Y1,
`timescale 1ns / 100ps input Y2,
input Y3,
module encoder4to2_tb; output reg [1:0] A
reg [3:0] din; );
…
wire [1:0] dout;
endmodule
// instantiate the encoder4to2 module
// look at how to map internal & external ports
encoder4to2 encoder(
.Y0(din[0]), .Y1(din[1]), .Y2(din[2]), .Y3(din[3]), .A(dout)
);
initial begin
$monitor("Time = %0t din = %0b dout = %0b", $realtime, din, dout);
#5 din = 4'b1000; // assign din to b1000 at time 5ns
#10 din = 4'b0100; // assign din to b0100 at time 15ns
#20 din = 4'b0010; // assign din to b0010 at time 35ns
#15 din = 4'b0001; // assign din to b0001 at time 50ns
#1 din = 4'b1000; // assign din to b0001 at time 51ns
40
Computer Architecture
Supplements: Introduction to Verilog
Jaewoong Sim
Electrical and Computer Engineering
Seoul National University