Ss6.Verilog Comb Logic
Ss6.Verilog Comb Logic
[http://vlsiarch.eecs.harvard.edu/accelerators/die-photo-analysis]
Slide Set #6 2
Learning Objectives
After this slide set you should be able to:
1. Write Verilog for combinational logic
2. Write Verilog for comb. logic testbenches
3. List important style guidelines that help avoid
common errors when working with
combinational logic
Slide Set #6 3
Scaling logic design
Slide Set #6 6
Text: Dally §1.5
What is Verilog?
• It is NOT a programming language!
• It is a hardware description language (HDL)
• Why do we need a new method to describe
hardware?
– What is wrong with schematics?
• With Verilog, you can specify hardware in two ways:
– Structurally: what the hardware looks like (like schematics)
– Behaviourally: what the hardware does
• But the real power of Verilog is that it lets you
combine structural and behavioural descriptions in
the same design!
Slide Set #6 7
Tools and design flow
There are many things that we can describe, but can not build
Slide Set #6 10
Verilog Modules
• The basic construct for modeling a digital system in
Verilog is called a module. Each hardware block is
described in one module.
• Modules have
– Module declaration
– Input and output declarations
– Internal signal declarations
– Logic definition; describes either what the block does
and/or what it is composed of
• Assign statements
• Case statements
• Module instantiations
Slide Set #6 11
Running example…
• Input: 3 bits
• Output: 1 if more inputs are 1 than are 0
abc out
a
000 0 ba
001 0 c 00 01 11 10 a
b
010 0 0 0 1 0
0
0 1 3 2
b v c
011 1 out
f
0 1 1 1
c
100 0
1
4 5 7 6
101 1 b c
110 1 a c a b
v v
111 1
Slide Set #6 12
Structure module name ports
a
module Majority(input a, input b, input c, output out);
b n1
n2 f out
endmodule
ports specify direction
n3
c
endmodule
bus width (here 3 bits: 2, 1, 0)
instance name
modules to
instantiate*
*actually, not and and are Verilog primitives, but you can instantiate them like modules
Slide Set #6 14
Text: Dally §3.6
Modules + instance hierarchy = structure
instance of instance of
a 7404 module a 7400 module
test inputs
and outputs
wires inside
our design
time
Slide Set #6 16
Text: Dally §3.6
Primer on Verilog Syntax
Slide Set #6 17
File types
.v is extension for Verilog
.sv is extension for SystemVerilog <- use this
Slide Set #6 18
Boolean Expressions in Verilog
• Verilog uses &, |, and ~ to represent AND, OR,
and NOT, respectively.
• Verilog uses ^ for exclusive-OR.
Slide Set #6 19
Text: Dally §3.6
Continuous assignment
We can instantiate logic and connect it together
with a type of statement called a continuous
assignment. As the name suggests, it is always
active (always happening):
Slide Set #6 20
Example: Verilog description of an AND gate (older K&R style):
assign c = a & b;
n2 fout
n3
c
assign n1 ==an1
out & b
| ; | n3 ;
n2
assign n1 = a &
n2 b
c ;
assign n2 = b
n3 a & c ;
assign n3 ==bn1
out & c
| n2
; | n3 ;
endmodule
Slide Set #6 23
Text: Dally §3.6
Majority Circuit in Verilog
a
b n1
n2 fout
n3
c
wire n1 = a & b ;
wire n2 = a & c ;
wire n3 = b & c ;
assign out = n1 | n2 | n3 ;
endmodule
Slide Set #6 24
Text: Dally §3.6
Majority Circuit in Verilog
a
b
fout
Slide Set #6 25
Text: Dally §3.6
So a recipe for specifying combinational logic in VHDL:
This will work, and you can use it. But, there are more
efficient and easier ways to do it.
Slide Set #6 28
Describing Buses in Verilog
The size of a bus and the element numbers are assigned when the signal is
defined:
Slide Set #6 29
Cool things™, continued…
3. You can access individual elements (“wires”):
wire STATUS;
wire [15:0] MAIN_BUS;
…
assign STATUS = MAIN_BUS[15];
Slide Set #6 30
Examples of Copying Busses
// Example 1
wire [7:0] out_bus;
wire [7:0] in_bus;
…
assign out_bus = in_bus;
// Example 2
wire [7:0] out_bus;
wire [15:8] in_bus;
…
assign out_bus = in_bus; // OK: out_bus(7) connected to in_bus(15)
// Example 3
wire [7:0] out_bus;
wire [0:7] in_bus;
…
assign out_bus = in_bus; // OK: out_bus(7) connected to in_bus(0)
// Example 4
wire [7:0] out_bus;
wire [15:0] in_bus;
…
assign out_bus = in_bus; // Same as assign out_bus[7:0] = in_bus[7:0];
Slide Set #6 31
Examples of Copying Busses
// Example 5
module bus_test(a,b);
input [7:0] a;
output [7:0] b;
assign t = a;
assign b = t;
endmodule
Moral? Verilog does not consider it an error if you use a signal without
declaring it first! (Verilog treats signals without declarations like a wire).
Slide Set #6 32
Cool things™, continued…
5. Concatenate short vectors to produce longer ones using { }
Slide Set #6 33
Cool things™, continued…
6. You can make copies of one or more bits using the replication operator, {k{n}}
We could have used “wire” instead of “reg” – but we will use reg because
assignments to memory are made in an “always” block. Inside an always block
you could write:
Slide Set #6 35
Text: Dally §3.6
Busses and Booleans
wire [3:0] result = A & B;
X(3)
A[0]
Z(3)
result[0]
Y(3)
B[0]
X(2)
A[1]
4
B[1] Z(2)
result[1]
Y(2) XA
Z
result
X(1)
A[2] YB 4
Z(1)
result[2] 4
Y(1)
B[2]
X(0)
A[3]
Z(0)
result[3]
Y(0)
B[3]
Slide Set #6 36
Cool things™, continued…
9. You can OR or AND together bits using:
Slide Set #6 37
Thermostat Revisited
[http://www.clipartpanda.com/categories/cute-house-clipart]
[http://blogs.plos.org/obesitypanacea/2011/02/25/turn-down-your-thermostat-to-lose-weight-suggests-new-study-dripping-with-sarcasm/]
Slide Set #6 38
Design Step
presetTemp
fanOn
Thermostat
currentTemp
Slide Set #6 39
Example, Verilog for Thermostat
wire fanOn ; // don’t really need this line as outputs are “wire” by default
assign fanOn = (currentTemp > presetTemp) ;
endmodule
Slide Set #6 40
Text: Dally §1.5
Example: Majority Gate
val
3 =
3'b111
=
3'b110
out
=
3'b101
=
3'b011
Slide Set #6 41
Verilog “define”
Very similar to “#define” in C:
Example:
No Semicolon!
`define Sa 2'b00
Slide Set #5 42
Verilog Conditional Operator
Examples:
If “in” is 1, then value of (in ? `Sb : `Sa) is `Sb
If “in” is 0, then value of (in ? `Sb : `Sa) is `Sa
Slide Set #5 43
Verilog Description of a Multiplexer
A0
2'b00 1
2
b
next_state_reset
2
A1
next_state 0
2
reset
select
=
3'b110
0
1 1
=
3'b101
0 0
1 1
=
3'b011
Slide Set #6 45
The Always Block
Two important things to know:
Slide Set #6 46
The always block allows us to describe the function or behaviour
of a circuit or subcircuit without describing the actual hardware
But we are going to take this MUCH further and describe circuits
at a much higher level.
Slide Set #6 47
How can we describe behaviour?
If we could write an English description, that would be nice
- but English gives too much room for ambiguity
Slide Set #6 48
Always Block Syntax and Semantics
NOTE: The “description” inside the always block may have little resemblance to the
actual hardware implementation. However, provided you follow certain rules
(described later) the hardware will implement the same behavior.
Slide Set #6 49
A Great (But Silly) Harry Potter Analogy
Hardware
[Source: http://www.davidbordwell.net/blog/wp-content/uploads/HPatPoA-paintings.jpg]
Software
[Source: http://ichef.bbci.co.uk/news/640/cpsprodpb/EDED/production/_83890906_83890905.jpg]
Slide Set #6 50
What goes inside an Always Block?
• For CPEN 211: “if”, “case”, “casex” statements! DO
NOT USE LOOPS IN ALWAYS BLOCKS.
• Use “begin” and “end” to group statements (like {} in C)
• Signals modified in an always block MUST be declared
as “reg” instead of “wire”. (This is a syntax rule, says
nothing about the hardware that will be synthesized.)
• Always block outputs modified using “=” or “<=” (will
talk about difference a bit later).
• Can read either “wire” or “reg” inside an always block
• Do NOT use assign statements inside an always block.
Slide Set #6 51
If Statement
if (<condition expression>)
<statement>
[ else <statement> ]
NOTES:
1. Need to wrap <statement> in begin/end if you
want multiple statements.
2. When describing combinational logic using if
statement need to include “else” (Why? See
synthesis rules later in slide set).
Slide Set #6 52
Case Statement
case (<selector>)
{ <label list> : <statement> }+
[ default: <statement> ]
endcase
NOTES:
1. Need to wrap <statement> part in begin/end pair if you want
multiple statements.
2. When describing combinational logic with case statement need
to include default (Why? See synthesis rules later in slide set).
Slide Set #6 53
Example: Days in Month Function
month days
DaysInMonth
Slide Set #6 54
Example, Days in Month Function
Slide Set #6 55
Example: Days in Month Function
module DaysInMonth(month,
Reg definesdays)
a signal ;set in an
always
input [3:0] month ; block.// Itmonth
does NOTof define
the year 1 = Jan, 12 = Dec
a register.
output [4:0] days ; // number of days in month
Error (10043): Verilog HDL unsupported feature error at <file name>(<line number>):
Procedural Continuous Assignment to register is not supported
Do NOT update a “wire” inside an always block (output of always block must be reg)
57
Writing a truth table in Verilog
Example: Prime number function
No dcba q
Truth table: 0 0000 0
1 0001 1
2 0010 1
3 0011 1
4 0100 0
5 0101 1
6 0110 0
7 0111 1
8 1000 0
9 1001 0
10 1010 0
11 1011 1
12 1100 0
13 1101 1
14 1110 0
15 1111 0
Slide Set #6 58
Text: Dally §7.1.2
4-bit Prime Number Function in
Verilog Code – Using case
module prime(in, isprime) ;
input [3:0] in ; // 4-bit input
output isprime ; // true if input is prime
reg isprime ;
always @(in) begin
case(in)
1,2,3,5,7,11,13: isprime = 1'b1 ;
default: isprime = 1'b0 ;
endcase
end
endmodule
Slide Set #6 59
Text: Dally §7.1.2
Q: When do you need begin / end?
If you want more than one statement to be “evaluated”
based upon same condition, then you group those
statements in ”begin” and “end”:
We do NOT need
always @(sel or A or B)
“begin” here because
if (sel == 1'b0) “if/else” is a single
begin We DO need “begin/end” statement
Y = A; here because we want
Z = B; two statements “Y=A;”
end and “Z=B;” to be
evaluated if sel == 1’b0.
else
begin
Y = B;
Z = A;
end
Slide Set #6 60
Q: When do you need begin / end?
Example of using begin/end inside case to allow
multiple statements for a given case label:
always @(*)
case(in)
4’b0000: begin
out1 = 1'b1;
out2 = |in2;
end
4'b0001: begin
out1 = 1'b0;
out2 = 1'bx;
end
…
Slide Set #6 61
always_comb
• Using sensitivity list or @(*) is error prone as
“always” is too powerful (too general).
• SystemVerilog introduced versions that specify
design intent: always_comb, always_ff
• For combinational logic use “always_comb”
always_comb
case(month)
2: days = 5'd28 ;
4,6,9,11: days = 5'd30 ;
default: days = 5'd31 ;
endcase
Slide Set #6 62
Multiple updates
always_comb begin
Y = 2'b11;
if (A == 1'b1) begin
Y = 2'b00;
end
end
Slide Set #6 63
Beware the Ambiguous ELSE!
always_comb begin always_comb begin
Y = 2'b00; Y = 2'b00;
if (A == 1'b0) if (A == 1'b0)
if (B == 1'b1) if (B == 1'b1)
Y = 2'b01; Y = 2'b01;
else else
Y = 2'b10; Y = 2'b10;
end end
always_comb begin
Y = 2'b11;
if (A == 1'b1) begin
if (B == 1'b1)
Y = 2'b00;
end else
Y = 2'b01;
end
Slide Set #6 65
Combinational Logic Building Blocks
in Verilog
Slide Set #6 66
Verilog implementation of a 2:4 decoder
a1 a0 b3 b2 b1 b0
Decoder
0 0 0 0 0 1
0 1 0 0 1 0 a b
1 0 0 1 0 0 n2 m
4
1 1 1 0 0 0
n
m≤2
“X << Y“ means a[1]
a[0]
shift X to the
left by Y bit
module Dec24(a, b) ; positions. b[3]
endmodule
b[0]
Slide Set #6 67
Text: Dally §8.2
Encoder
a3 a2 a1 a0 b1 b0
a3
a2
a1
a0
0 0 0 1 0 0
0 0 1 0 0 1 b1
0 1 0 0 1 0
1 0 0 0 1 1
b0
b0 = a3 ∨ a1
// 4:2 encoder
b1 = a3 ∨ a2
module Enc42(a, b) ;
input [3:0] a ;
output [1:0] b ;
assign b = {a[3] | a[2], a[3] | a[1]} ;
endmodule
Slide Set #6 68
Text: Dally §8.4
1-bit, 2-input One-Hot Select Mux
Symbol: Implementation with
AND and OR gates:
a0
Mux b a0
a1
s[0]
b
a1
s s[1]
2
a1[0]
b[0]
a1[1]
a2[0]
a0 a2[1]
2
a1 b
Mux
2 2
a2
b[1]
2
s s[0]
3 s[1]
s[2]
Slide Set #6 71
Verilog Syntax Module Instantiation
Slide Set #6 72
Module Instantiation
Instantiate
TwoInArray
9
win
cout
module TwoInArray(ain, bin, cout) ; bin
9
TwoInArray
9
Select3
block oout
cout b out
9 9
2x bin
don't lose
c
Empty
9
…
TwoInArray winx(oin, xin, win) ; first open square
73
TwoInArray blockx(xin, oin, block) ;
Consider a REALLY simple example
Consider the following example:
Slide Set #6 74
Instantiation Syntax
Slide Set #6 75
module NAND_GATE(A,B,Z);
A Z
input A, B ; A Z
output Z ; B
assign Z = ~(A & B);
NAND_GATE INV_GATE
endmodule
module OtherModule;
wire [7:0] x;
wire [7:0] y;
Bad U0(x,y);
In ModelSim, no compile error, but you do get a warning when starting simulation:
** Warning: (vsim-3015) <file1>(<line1>): [PCDPC] - Port size (1) does not
match connection size (8) for port 'b'. The port definition is at:
<filename2>(<line2>).
Slide Set #6 79
Module Instantiation “LEGO Analogy”
Verilog can be viewed as a description for how to build hardware. You may
want to think of module instantiation, assign statements, always blocks in
Verilog as instructions saying how to build a LEGO toy (picture above).
After you build a LEGO toy, you can play with it, which is similar in this
analogy to changing slide switches and/or pressing push buttons on your
DE1-SoC after downloading compiled design (e.g., LEGO wheels turning).
Slide Set #6 80
[figure source: http://lego.brickinstructions.com/lego_instructions/set/9324/Micro_Building_Set]
Implicit vs. Named Port Association
In the NAND+NOT gate example, order to connect wires was implicit
means that IN1 is connected to the first port in the module definition
and IN2 is connected to the second port in the module definition, etc.
Slide Set #6 82
Example of Named Association
module Dec24(a, b) ;
input [1:0] a ;
output [3:0] b ;
wire [3:0] b = 1 << a ;
endmodule
NO210, INV110 are modules
Using “Synopsys” above decoder Verilog synthesizes to: defining NOR and NOT gates in
the “technology library” or
module Dec_Synthesized ( in, out ); “cell library” provided by the
input [1:0] in; chip manufacturing company.
output [3:0] out;
wire n2, n3;
NO210 U2 ( .A(n2), .B(n3), .Y(out[3]) );
NO210 U3 ( .A(in[0]), .B(n2), .Y(out[2]) );
NO210 U4 ( .A(in[1]), .B(n3), .Y(out[1]) );
NO210 U5 ( .A(in[0]), .B(in[1]), .Y(out[0]) );
IV110 U6 ( .A(in[1]), .Y(n2) );
IV110 U7 ( .A(in[0]), .Y(n3) );
endmodule
Slide Set #6 83
Module Parameters
Another silly analogy…
When buying a car typically need to pick a
color… color doesn’t change once you pick
up your car (not without a lot of work).
Slide Set #2 84
Module Parameters
• Often want to change width of signals when instantiating a module
in different parts of our design (or in different designs) and often
the rest of the design should change in a predictable way.
• Verilog parameter syntax allows us to specify properties of a
module that stay fixed for any given module instance:
Decoder
input [n-1:0] a ;
output [m-1:0] b ; a b
n m
wire [m-1:0] b = 1 << a ;
endmodule n
m≤2
3:8
3 8 b [7 ]
b [5 ]
b [3 ] i sp rime
b [2 ]
b [1 ]
s always_comb begin
3k case(s)
3'b001: b = a0 ;
3'b010: b = a1 ;
3'b100: b = a2 ;
default: b = {k{1'bx}} ;
endcase
end
endmodule
Slide Set #6 88
Parameterized Multiplexer (v2)
// 4-input, k-bit mux with one-hot select
module Mux4(a3, a2, a1, a0, s, b) ;
parameter k = 1 ;
input [k-1:0] a0, a1, a2, a3 ; // inputs
input [3:0] s ; // one-hot select
output[k-1:0] b ;
wire [k-1:0] b = ({k{s[0]}} & a0) |
({k{s[1]}} & a1) |
({k{s[2]}} & a2) |
({k{s[3]}} & a3) ;
endmodule
f h
# 0001 00
k=1 k=2
# 0010 01
# 0100 10
# 1000 11
Slide Set #6 89
// 4:1 multiplexer with binary select (arbitrary width)
module Mux4b(a3,a2, a1, a0, sb, b) ;
parameter k = 1 ;
input [k-1:0] a0, a1, a2, a3 ; // inputs
input [1:0] sb ; // binary select
output[k-1:0] b ;
wire [k-1:0] t0, t1 ;
t1
Slide Set #6 90
Text: Dally §8.3
Always Block Synthesis Rules for
Combinational Logic
Slide Set #6 91
Most important few slides in this course!
Why?
Slide Set #6 92
VHDL Code
Verilog
Slide Set #6 93
VHDL Code
Verilog
Slide Set #6 94
“Synthesizable” Verilog
Slide Set #6 95
But what sort of Verilog is Synthesizable?
In general, it depends a bit on the tools.
In the next three slides, I am going to explain rules to ensure your Verilog
for combinational logic is synthesizable by all tools.
Slide Set #6 96
Type 1: Purely Combinational: All outputs are a function
only of the present inputs (outputs do not depend upon
previous inputs).
Slide Set #6 97
For purely combinational always blocks:
Forgetting Rule 2 will cost you HOURS or even DAYS of extra debugging
due to “inferred latches” (see HDL Tools Tutorial). If you use
always_comb, you’ll get an error if you forget Rule 2.
Slide Set #6 98
This would not be synthesizable This would not be synthesizable
(violates Rule 1): (violates Rule 2):
Use RTL viewer for labs and while studying for midterm.
Helps you develop better intuition for Verilog; catch
design errors. Slide Set #6 100
IMPORTANT: If you write Verilog that connects
output of combinational logic back to input (possibly
through some other combinational logic block) you’ll
get warnings in Quartus about “combinational loops”
and/or inferred latches. ModelSim will not give you
any warnings and may (or may not) hit an “iteration
limit” error. Synthesize with Quartus and check
warnings!
D-flip-flop rules:
– at the rising edge of clk, D copied to Q
– Q retains value until next rising edge of clk
module dff(input clk, input D, output Q);
variable to retain value reg r;
(you can think of this as the reg) assign Q = r; rising edge of clk
always_ff @(posedge clk) begin
block executed whenever r <= D;
nonblocking assignment
event posedge clk happens end
endmodule: dff for register updates
(more on this a bit later)
initial begin
<sequence of statements in which order matters; delays allowed>
end
- Initial blocks are not synthesizable – use ONLY for test bench ”scripts”
- Each initial block describes a sequence of operations
- Delays allowed
Syntax: #<number of time units> ;
Example:
#10;
The line above means the enclosing initial block should wait
10 “time units” before executing the next statement.
in
Test Script DUT
(process) out (majority)
[http://www.servicenoodle.com/lighthouse-auto-sales-and-repair-p-552966]
1
B
0
0 5 10 15 20 25 30 35 40 45 50
Time
1
// generate input patterns here B
initial begin 0
Inputs
✔ Hardware defined
✔ by Verilog
Outputs
statement (e.g.,
✔ always block)
module pb_top (
input not_LEFT_pushbutton,
input not_RIGHT_pushbutton,
input [3:0] A,
input [3:0] B,
output reg [3:0] result );
...
always @* begin
case( {LEFT_pushbutton, RIGHT_pushbutton} )
2’b01: result = ADDed_result;
2’b10: result = ANDed_result;
2’b11: result = ADDed_result; // Right push button takes precedence
endcase
end
Slide Set #6 125
Possibility (b) Input Looks Wrong
Inputs
✔ Hardware defined
by Verilog
statement (e.g.,
Outputs
✔ always block)
module NAND_GATE(A,B,Z); A Z
input A, B ;
A Z
B
output Z ;
assign Z = (A & B); NAND_GATE INV_GATE
endmodule
module INV_GATE(A,Z);
input A;
output Z;
assign Z = ~A;
endmodule