0% found this document useful (0 votes)
10 views27 pages

Introduction To Verilog (Combinational Logic)

The document introduces Verilog, a hardware description language (HDL) used for digital logic design, focusing on combinational logic. It covers key concepts such as synthesis, mapping, modules, continuous assignments, and procedural assignments, along with practical examples of Verilog code. Additionally, it highlights common pitfalls in Verilog coding, such as incomplete specifications and priority logic issues.

Uploaded by

prnjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views27 pages

Introduction To Verilog (Combinational Logic)

The document introduces Verilog, a hardware description language (HDL) used for digital logic design, focusing on combinational logic. It covers key concepts such as synthesis, mapping, modules, continuous assignments, and procedural assignments, along with practical examples of Verilog code. Additionally, it highlights common pitfalls in Verilog coding, such as incomplete specifications and priority logic issues.

Uploaded by

prnjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Introduction to Verilog

(Combinational Logic)

Acknowledgements : Anantha Chandrakasan, Rex Min

Verilog References:
• Samir Palnitkar, Verilog HDL, Pearson Education (2nd edition).
• Donald Thomas, Philip Moorby, The Verilog Hardware Description Language, Fifth
Edition, Kluwer Academic Publishers.
• J. Bhasker, Verilog HDL Synthesis (A Practical Primer), Star Galaxy Publishing

6.111 Fall 2004 Lecture 4, Slide 1


Synthesis and HDLs

„ Hardware description language (HDL) is a convenient, device-


independent representation of digital logic

Verilog
input a,b;
output sum;
assign sum <= {1b’0, a} + {1b’0, b};

Compilation and „ HDL description is compiled


Synthesis into a netlist
Netlist „ Synthesis optimizes the logic
g1 "and" n1 n2 n5
g2 "and" n3 n4 n6
g3 "or" n5 n6 n7
„ Mapping targets a specific
Mapping hardware platform

FPGA PAL ASIC


(Custom ICs)

6.111 Fall 2004 Lecture 4, Slide 2


The FPGA: A Conceptual View

„ An FPGA is like an electronic breadboard that is wired together


by an automated synthesis tool
„ Built-in components are called macros

32
32
+ SUM DQ
32

sel

counter
interconnect
a
b F(a,b,c,d)
c LUT G(a,b,c,d)
d

ADR
R/W
RAM DATA

(for everything else)

6.111 Fall 2004 Lecture 4, Slide 3


Synthesis and Mapping for FPGAs

„ Infer macros: choose the FPGA macros that efficiently


implement various parts of the HDL code

... “This section of code looks


always @ (posedge clk) like a counter. My FPGA has
begin some of those...”
count <= count + 1; counter
end
...
HDL Code Inferred Macro

„ Place-and-route: with area and/or speed in mind, choose


the needed macros by location and route the interconnect
M M M M M M M

M M M M M M M “This design only uses 10% of


the FPGA. Let’s use the macros
M M M M M M M
in one corner to minimize the
M M M M M M M distance between blocks.”
M M M M M M M

6.111 Fall 2004 Lecture 4, Slide 4


Verilog: The Module

„ Verilog designs consist of


a 1
interconnected modules.
out
„ A module can be an element or b 0 outbar
collection of lower level design blocks.
sel
„ A simple module with combinational
logic might look like this: Out = sel ● a + sel ● b
2-to-1 multiplexer with inverted output
module mux_2_to_1(a, b, out, Declare and name a module; list its
outbar, sel); ports. Don’t forget that semicolon.

// This is 2:1 multiplexor Comment starts with //


Verilog skips from // to end of the line
input a, b, sel; Specify each port as input, output,
output out, outbar; or inout

Express the module’s behavior.


assign out = sel ? a : b;
Each statement executes in
assign outbar = ~out; parallel; order does not matter.

endmodule Conclude the module code.


6.111 Fall 2004 Lecture 4, Slide 5
Continuous (Dataflow) Assignment

module mux_2_to_1(a, b, out,


outbar, sel); a 1
input a, b, sel; out
output out, outbar; b 0 outbar
assign out = sel ? a : b;
assign outbar = ~out;
sel
endmodule

„ Continuous assignments use the assign keyword


„ A simple and natural way to represent combinational logic
„ Conceptually, the right-hand expression is continuously evaluated as a function of
arbitrarily-changing inputs…just like dataflow
„ The target of a continuous assignment is a net driven by combinational logic
„ Left side of the assignment must be a scalar or vector net or a concatenation of scalar
and vector nets. It can’t be a scalar or vector register (discussed later). Right side can be
register or nets
„ Dataflow operators are fairly low-level:
† Conditional operator: (conditional_expression) ? (value-if-true) : (value-if-false);
† Boolean logic: ~, &, |, ^
† Arithmetic: +, -, *
„ Nested conditional operator (4:1 mux)
† assign out = s1 ? (s0 ? i3 : i2) : (s0? i1 : i0);

6.111 Fall 2004 Lecture 4, Slide 6


MAX+plusII: Simulator, Synthesis,
Mapping
„ Must be synthesizable Verilog files
„ Step by step instructions on the course WEB site
Create *.v file (module name same as file name)

Select area and set inputs through overwrite or insert menu (under edit)
Glitch
6.111 Fall 2004 Lecture 4, Slide 7
Gate Level Description

module muxgate (a, b, out, outbar, sel);


input a, b, sel;
output out, outbar; a
wire out1, out2, selb; out1
sel
and a1 (out1, a, sel); out
not i1 (selb, sel);
outbar
and a2 (out2, b , selb); selb
out2
or o1 (out, out1, out2); b
assign outbar = ~out;
endmodule

„ Verilog supports basic logic gates as primitives


† and, nand, or, nor, xor, xnor, not, buf
† can be extended to multiple inputs: e.g., nand nand3in (out, in1, in2,in3);
† bufif1 and bufif0 are tri-state buffers

„ Net represents connections between hardware elements. Nets are


declared with the keyword wire.

6.111 Fall 2004 Lecture 4, Slide 8


Procedural Assignment with always
„ Procedural assignment allows an alternative, often higher-level, behavioral
description of combinational logic
„ Two structured procedure statements: initial and always
„ Supports richer, C-like control structures such as if, for, while,case
module mux_2_to_1(a, b, out,
outbar, sel);
input a, b, sel;
Exactly the same as before.
output out, outbar;
Anything assigned in an always
reg out, outbar; block must also be declared as
type reg (next slide)
Conceptually, the always block
always @ (a or b or sel) runs once whenever a signal in the
sensitivity list changes value
begin
if (sel) out = a; Statements within the always
else out = b;
block are executed sequentially.
outbar = ~out; Order matters!

end Surround multiple statements in a


single always block with begin/end.
endmodule

6.111 Fall 2004 Lecture 4, Slide 9


Verilog Registers

„ In digital design, registers represent memory elements (we


will study these in the next few lectures)
„ Digital registers need a clock to operate and update their
state on certain phase or edge
„ Registers in Verilog should not be confused with hardware
registers
„ In Verilog, the term register (reg) simply means a variable
that can hold a value
„ Verilog registers don’t need a clock and don’t need to be
driven like a net. Values of registers can be changed
anytime in a simulation by assigning a new value to the
register

6.111 Fall 2004 Lecture 4, Slide 10


Mix-and-Match Assignments

„ Procedural and continuous assignments can (and often do) co-exist


within a module
„ Procedural assignments update the value of reg. The value will remain
unchanged till another procedural assignment updates the variable.
This is the main difference with continuous assignments in which the
right hand expression is constantly placed on the left-side

module mux_2_to_1(a, b, out, a 1


outbar, sel);
input a, b, sel; out
output out, outbar; b 0 outbar
reg out;

always @ (a or b or sel) sel


begin
if (sel) out = a; procedural
else out = b; description
end

assign outbar = ~out; continuous


description
endmodule

6.111 Fall 2004 Lecture 4, Slide 11


The case Statement

„ case and if may be used interchangeably to implement


conditional execution within always blocks
„ case is easier to read than a long string of if...else statements

module mux_2_to_1(a, b, out, module mux_2_to_1(a, b, out,


outbar, sel); outbar, sel);
input a, b, sel; input a, b, sel;
output out, outbar; output out, outbar;
reg out; reg out;

always @ (a or b or sel) always @ (a or b or sel)


begin begin
if (sel) out = a; case (sel)
else out = b; 1’b1: out = a;
end 1’b0: out = b;
endcase
assign outbar = ~out; end

endmodule assign outbar = ~out;

endmodule

Note: Number specification notation: <size>’<base><number>


(4’b1010 if a 4-bit binary value, 16’h6cda is a 16 bit hex number, and 8’d40 is an 8-bit decimal value)
6.111 Fall 2004 Lecture 4, Slide 12
The Power of Verilog: n-bit Signals

„ Multi-bit signals and buses are easy in Verilog.


„ 2-to-1 multiplexer with 8-bit operands:
module mux_2_to_1(a, b, out,
outbar, sel);
input[7:0] a, b;
8
input sel;
output[7:0] out, outbar; a 1 8
reg[7:0] out; out
always @ (a or b or sel) b 0 outbar
begin 8 8
if (sel) out = a;
else out = b; sel
end
assign outbar = ~out;
endmodule

Concatenate signals using the { } operator


assign {b[7:0],b[15:8]} = {a[15:8],a[7:0]};
effects a byte swap

6.111 Fall 2004 Lecture 4, Slide 13


The Power of Verilog: Integer Arithmetic

„ Verilog’s built-in arithmetic makes a 32-bit adder easy:


module add32(a, b, sum);
input[31:0] a,b;
output[31:0] sum;
assign sum = a + b;
endmodule

„ A 32-bit adder with carry-in and carry-out:


module add32_carry(a, b, cin, sum, cout);
input[31:0] a,b;
input cin;
output[31:0] sum;
output cout;
assign {cout, sum} = a + b + cin;
endmodule

6.111 Fall 2004 Lecture 4, Slide 14


Dangers of Verilog: Incomplete Specification

Goal: Proposed Verilog Code:


module maybe_mux_3to1(a, b, c,
sel, out);
input [1:0] sel;
a 00 input a,b,c;
output out;
b 01 out reg out;
c 10
always @(a or b or c or sel)
2
begin
case (sel)
sel 2'b00: out = a;
2'b01: out = b;
2'b10: out = c;
3-to-1 MUX
endcase
(‘11’ input is a don’t-care)
end
endmodule

Is this a 3-to-1 multiplexer?

6.111 Fall 2004 Lecture 4, Slide 15


Incomplete Specification Infers Latches

module maybe_mux_3to1(a, b, c, Synthesized Result:


sel, out);
input [1:0] sel;
input a,b,c;
output out; a
reg out; 00
b 01 D Q out
always @(a or b or c or sel)
begin c 10
case (sel) G
2'b00: out = a; 2
2'b01: out = b;
2'b10: out = c; sel
endcase
end sel[1]
endmodule sel[0]

if out is not assigned


„ Latch memory “latches”
during any pass through old data when G=0 (we
the always block, then the will discuss latches later)
previous value must be „ In practice, we almost
retained! never intend this

6.111 Fall 2004 Lecture 4, Slide 16


Avoiding Incomplete Specification

always @(a or b or c or sel)


begin
„ Precede all conditionals out = 1’bx;
case (sel)
with a default assignment 2'b00: out = a;
for all signals assigned 2'b01: out = b;
within them… 2'b10: out = c;
endcase
end
endmodule

always @(a or b or c or sel)


begin „ …or, fully specify all
case (sel) branches of conditionals and
2'b00: out = a;
2'b01: out = b; assign all signals from all
2'b10: out = c; branches
default: out = 1’bx;
endcase † For each if, include else
end † For each case, include default
endmodule

6.111 Fall 2004 Lecture 4, Slide 17


Dangers of Verilog: Priority Logic

Goal: Proposed Verilog Code:

4-to-2 Binary Encoder module binary_encoder(i, e);


input [3:0] i;
0 I3 output [1:0] e;
1 reg e;
I2 E1 1
0 I1 E0 0 always @(i)
0 I0 begin
if (i[0]) e = 2’b00;
else if (i[1]) e = 2’b01;
I3 I2 I1 I0 E1 E0 else if (i[2]) e = 2’b10;
else if (i[3]) e = 2’b11;
0001 00
else e = 2’bxx;
0010 01 end
0100 10 endmodule
1000 11
all others XX
What is the resulting circuit?

6.111 Fall 2004 Lecture 4, Slide 18


Priority Logic

Intent: if more than one input is Code: if i[0] is 1, the result is 00


1, the result is a don’t-care. regardless of the other inputs.
i[0] takes the highest priority.
I3 I2 I1 I0 E1 E0
0001 00 if (i[0]) e = 2’b00;
else if (i[1]) e = 2’b01;
0010 01 else if (i[2]) e = 2’b10;
0100 10 else if (i[3]) e = 2’b11;
1000 11 else e = 2’bxx;
all others XX end

Inferred 2’b11 2’b10 2’b01 2’b00


1 1 1 1
Result: e[1:0]
2’bxx 0 0 0 0

i[3] i[2] i[1] i[0]

„ if-else and case statements are interpreted very literally!


Beware of unintended priority logic.

6.111 Fall 2004 Lecture 4, Slide 19


Avoiding (Unintended) Priority Logic

„ Make sure that if-else and case statements are parallel


† If mutually exclusive conditions are chosen for each branch...
† ...then synthesis tool can generate a simpler circuit that evaluates
the branches in parallel

Parallel Code: Minimized Result:


module binary_encoder(i, e);
input [3:0] i;
output [1:0] e;
I3
reg e; E0
always @(i)
begin I1
E1
if (i == 4’b0001) e = 2’b00; I0
else if (i == 4’b0010) e = 2’b01;
else if (i == 4’b0100) e = 2’b10;
else if (i == 4’b1000) e = 2’b11;
else e = 2’bxx;
end
endmodule

6.111 Fall 2004 Lecture 4, Slide 20


Interconnecting Modules

„ Modularity is essential to the success of large designs


„ A Verilog module may contain submodules that are “wired together”
„ High-level primitives enable direct synthesis of behavioral descriptions (functions such
as additions, subtractions, shifts (<< and >>), etc.

Example: A 32-bit ALU Function Table


A[31:0] B[31:0]
F2 F1 F0 Function
0 0 0 A+B
32’d1 32’d1
F[0] 0 0 1 A+1
0 1 0 1
0 1 0 A-B
F[2:0] 0 1 1 A-1
+ - *
1 0 X A*B
00 01 10
F[2:1]

R[31:0]

6.111 Fall 2004 Lecture 4, Slide 21


Module Definitions

2-to-1 MUX 3-to-1 MUX


module mux32three(i0,i1,i2,sel,out);
module mux32two(i0,i1,sel,out);
input [31:0] i0,i1,i2;
input [31:0] i0,i1; input [1:0] sel;
input sel; output [31:0] out;
output [31:0] out; reg [31:0] out;
assign out = sel ? i1 : i0; always @ (i0 or i1 or i2 or sel)
begin
endmodule case (sel)
2’b00: out = i0;
2’b01: out = i1;
2’b10: out = i2;
default: out = 32’bx;
endcase
end
endmodule

32-bit Adder 32-bit Subtracter 16-bit Multiplier


module mul16(i0,i1,prod);
module add32(i0,i1,sum); module sub32(i0,i1,diff); input [15:0] i0,i1;
input [31:0] i0,i1; input [31:0] i0,i1; output [31:0] prod;
output [31:0] sum; output [31:0] diff;
// this is a magnitude multiplier
assign sum = i0 + i1; assign diff = i0 - i1; // signed arithmetic later
assign prod = i0 * i1;
endmodule endmodule
endmodule

6.111 Fall 2004 Lecture 4, Slide 22


Top-Level ALU Declaration

„ Given submodules: A[31:0] B[31:0]


alu
module mux32two(i0,i1,sel,out);
module mux32three(i0,i1,i2,sel,out);
32’d1 32’d1
module add32(i0,i1,sum); F[0]
0 1 0 1
module sub32(i0,i1,diff);
module mul16(i0,i1,prod); F[2:0]
+ - *
„ Declaration of the ALU Module:
module alu(a, b, f, r); 00 01 10
F[2:1]
input [31:0] a, b;
input [2:0] f;
output [31:0] r; R[31:0]
wire [31:0] addmux_out, submux_out;
wire [31:0] add_out, sub_out, mul_out; intermediate output nodes

mux32two adder_mux(b, 32'd1, f[0], addmux_out);


mux32two sub_mux(b, 32'd1, f[0], submux_out);
add32 our_adder(a, addmux_out, add_out);
sub32 our_subtracter(a, submux_out, sub_out);
mul16 our_multiplier(a[15:0], b[15:0], mul_out);
mux32three output_mux(add_out, sub_out, mul_out, f[2:1], r);

endmodule module (unique) corresponding


names instance wires/regs in
names module alu

6.111 Fall 2004 Lecture 4, Slide 23


Simulation

addition subtraction multiplier

6.111 Fall 2004 Lecture 4, Slide 24


More on Module Interconnection

„ Explicit port naming allows port mappings in arbitrary


order: better scaling for large, evolving designs
Given Submodule Declaration:
module mux32three(i0,i1,i2,sel,out);

Module Instantiation with Ordered Ports:


mux32three output_mux(add_out, sub_out, mul_out, f[2:1], r);

Module Instantiation with Named Ports:


mux32three output_mux(.sel(f[2:1]), .out(r), .i0(add_out),
.i1(sub_out), .i2(mul_out));

corresponding
submodule’s wire/reg in
port name outer module

„ Built-in Verilog gate primitives may be instantiated as well


† Instantiations may omit instance name and must be ordered:
buf(out1,out2,...,outN, in); and(in1,in2,...inN,out);

6.111 Fall 2004 Lecture 4, Slide 25


Useful Boolean Operators
„ Bitwise operators perform bit-sliced operations on vectors
† ~(4’b0101) = {~0,~1,~0,~1} = 4’b1010
† 4’b0101 & 4’b0011 = 4’b0001

„ Logical operators return one-bit (true/false) results


† !(4’b0101) = ~1 = 1’b0
„ Reduction operators act on each bit of a single input vector
† &(4’b0101) = 0 & 1 & 0 & 1 = 1’b0
„ Comparison operators perform a Boolean test on two arguments
Bitwise Logical Reduction Comparison
~a NOT !a NOT &a AND a<b
a>b Relational
a&b AND a && b AND ~& NAND a <= b
a >= b
a|b OR a || b OR | OR
a == b [in]equality
a^b XOR ~| NOR a != b returns x when x
^ XOR or z in bits. Else
a ~^ b XNOR returns 0 or 1
a === b case
Note distinction between ~a and !a
a !== b [in]equality
returns 0 or 1
based on bit by bit
comparison
6.111 Fall 2004 Lecture 4, Slide 26
Summary

„ Multiple levels of description: behavior, dataflow, logic and


switch (not used in 6.111)
„ Gate level is typically not used as it requires working out
the interconnects
„ Continuous assignment using assign allows specifying
dataflow structures
„ Procedural Assignment using always allows efficient
behavioral description. Must carefully specify the
sensitivity list
„ Incomplete specification of case or if statements can
result in non-combinational logic
„ Verilog registers (reg) is not to be confused with a
hardware memory element
„ Modular design approach to manage complexity

6.111 Fall 2004 Lecture 4, Slide 27

You might also like