0% found this document useful (0 votes)
67 views

01 Verilog PDF

Hardware description languages (HDLs) allow designers to describe digital logic using textual code rather than schematics. HDLs like Verilog are easier to edit than schematics but still require thinking like schematics. Verilog is used to both specify logic for synthesis into circuits and simulate designs. It includes both structural constructs that directly map to hardware and behavioral constructs that are more abstract. This lecture focuses on synthesizable Verilog constructs for combinational logic.

Uploaded by

nikhil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

01 Verilog PDF

Hardware description languages (HDLs) allow designers to describe digital logic using textual code rather than schematics. HDLs like Verilog are easier to edit than schematics but still require thinking like schematics. Verilog is used to both specify logic for synthesis into circuits and simulate designs. It includes both structural constructs that directly map to hardware and behavioral constructs that are more abstract. This lecture focuses on synthesizable Verilog constructs for combinational logic.

Uploaded by

nikhil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Hardware Description Languages (HDLs)

• Textural representation of a digital logic design


• Easier to edit and revise than schematics
CSE372 • However, you still need to think in terms of schematics (pictures)
Digital Systems Organization and Design
• HDLs are not “programming languages”
Lab • No, really. Even if they look like it, they are not.
• One of the most difficult conceptual leaps of this course
Prof. Milo Martin
• Similar development chain
• Compiler: source code ! assembly code ! binary machine code
Unit 1: Synthesizable Verilog
• Synthesis tool: HDL source ! gate-level specification ! hardware

CSE 372 (Martin): Synthesizable Verilog 1 CSE 372 (Martin): Synthesizable Verilog 2

Hardware Description Languages (HDLs) Verilog HDL


• Write “code” to describe hardware • Verilog
• Specify wires, gates, modules
• One of two commonly-used HDLs
• Also hierarchical
• Verilog is a (surprisingly) big language
• Pro: easier to edit and create; Con: more abstract • Lots of features for synthesis and simulation of hardware
module mux2to1(S, A, B, O);
input S, A, B; • We’re going to learn a focused subset of Verilog
output O; S • Focus on synthesizable constructs
wire S_, AnS_, BnS; • Focus on avoiding subtle synthesis errors
A
O • Use as an educational tool
not (S_, S); B • Initially restrict some features to “build up” primitives
and (AnS_, A, S_); • Rule: if you haven’t seen it in lecture, you can’t use it
and (BnS, B, S); • Ask me if you have any questions
or (O, AnS_, BnS);
endmodule
CSE 372 (Martin): Synthesizable Verilog 3 CSE 372 (Martin): Synthesizable Verilog 4
HDL History Two Roles of HDL and Related Tools
• 1970s: First HDLs • #1: Specifying digital logic
• Late 1970s: VHDL • Specify the logic that appears in final design
• VHDL = VHSIC HDL = Very High Speed Integrated Circuit HDL • Either
• VHDL inspired by programming languages of the day (Ada) • Translated automatically (called synthesis) or
• 1980s: • Optimized manually (automatically checked for equivalence)
• Verilog first introduced
• Verilog inspired by the C programming language
• #2: Simulating and testing a design
• VHDL standardized
• High-speed simulation is crucial for large designs
• 1990s:
• Many HDL interpreters optimized for speed
• Verilog standardized (Verilog-1995 standard)
• Testbench: code to test design, but not part of final design
• 2000s:
• Continued evolution (Verilog-2001 standard)
• Both VHDL and Verilog evolving, still in use today

CSE 372 (Martin): Synthesizable Verilog 5 CSE 372 (Martin): Synthesizable Verilog 6

Synthesis vs Simulation Structural vs Behavioral HDL Constructs


• HDLs have features for both synthesis and simulation • Structural constructs specify actual hardware structures
• E.g., simulation-only operations for error messages, reading files • Low-level, direct correspondence to hardware
• Obviously, these can be simulated, but not synthesized into circuits • Primitive gates (e.g., and, or, not)
• Also has constructs such as for-loops, while-loops, etc. • Hierarchical structures via modules
• These are either un-synthesizable or (worse) synthesize poorly • Analogous to programming software in assembly
• Behavioral constructs specify an operation on bits
• Trends: a moving target • High-level, more abstract
• Good: better synthesis tools for higher-level constructs • Specified via equations, e.g., out = (a & b) | c
• Bad: harder than ever to know what is synthesizable or not • Statements, e.g., if-then-else
• Analogous to programming software in C
• Not all behavioral constructs are synthesizable
• Even higher-level, synthesize poorly or not at all (e.g., loops)
• Perhaps analogous to programming in Perl, Python, Matlab, SQL

CSE 372 (Martin): Synthesizable Verilog 7 CSE 372 (Martin): Synthesizable Verilog 8
Verilog Structural vs Behavioral Example Recall: Two Types of Digital Circuits
Structural
module mux2to1(S, A, B, Out); • Combinational Logic
input S, A, B; S • Logic without state variables
• Examples: adders, multiplexers, decoders, encoders
output Out; A
wire S_, AnS_, BnS; Out • No clock involved
not (S_, S); B
and (AnS_, A, S_); • Sequential Logic
• Logic with state variables
and (BnS, B, S);
• State variables: latches, flip-flops, registers, memories
or (Out, AnS_, BnS);
• Clocked
endmodule
• State machines, multi-cycle arithmetic, processors
Behavioral
module mux2to1(S, A, B, Out);
input S, A, B; • Today’s lecture: Verilog for specifying combinational logic
output Out; • Sequential logic will be covered later
assign Out = (~S & A) | (S & B); • Focus on structural constructs with limited behavioral ones
endmodule
CSE 372 (Martin): Synthesizable Verilog 9 CSE 372 (Martin): Synthesizable Verilog 10

Verilog Structural Primitives Three Module Components


• Gate-level • Interface specification
• One-output boolean operators: and, or, xor, nand, nor, xnor module mux2to1(S, A, B, O);
• E.g., C = A+B input S, A, B;
or (C, A, B); output O;
• E.g., C= A+B+D
• Can also have inout: bidirectional wire (we will not need)
or (C, A, B, D);
• One-input operators: not, buf • Alternative: Verilog 2001 interface specification
• E.g., A = not Z module mux2to1(input S, A, B, output O);
not (A, Z); • Declarations
• E.g., A = not Z, B = not Z • Internal wires, i.e., “local” variables
not (A, B, Z);
• Wires also known as “nets” or “signals”
• Buf just replicates signals (can increase drive strength) wire S_, AnS_, BnS;
• Transistor-level primitives too
• Implementation: primitive and module instantiations
• Will not use
and (AnS_, A, S_);

CSE 372 (Martin): Synthesizable Verilog 11 CSE 372 (Martin): Synthesizable Verilog 12
Verilog Module Example Hierarchical Verilog Example
• Build up more complex modules using simpler modules
module mux2to1(S, A, B, O); • Example: 4-bit wide mux from four 1-bit muxes
input S, A, B; • Again, just “drawing” boxes and wires
output O; S
module mux2to1_4(Sel, A, B, O);
wire S_, AnS_, BnS;
A input [3:0] A;
O
input [3:0] B;
not (S_, S); B input Sel;
and (AnS_, A, S_); output [3:0] O;
and (BnS, B, S);
or (O, AnS_, BnS); mux2to1 mux0 (Sel, A[0], B[0], O[0]);
endmodule mux2to1 mux1 (Sel, A[1], B[1], O[1]);
mux2to1 mux2 (Sel, A[2], B[2], O[2]);
mux2to1 mux3 (Sel, A[3], B[3], O[3]);
endmodule
CSE 372 (Martin): Synthesizable Verilog 13 CSE 372 (Martin): Synthesizable Verilog 14

Connections by Name Vectors of Wires


• Can (should) specify module connections by name • Wire vectors:
• Helps keep the bugs away wire [7:0] W1; // 8 bits, w1[7] is MSB
• Example wire [0:7] W2; // 8 bits, w2[0] is MSB
mux2to1 mux0 (.S(Sel), .A(A[0]), .B(B[0]), .O(O[0])); • Also called “arrays” or “busses”
• Also, order doesn’t matter
mux2to1 mux1 (.A(A[1]), .B(B[1]), .O(O[1]), .S(Sel)); • Operations
• Bit select: W1[3]
• Range select: W1[3:2]
• Concatenate: {<expr>[,<expr>]*}
vec = {x, y, z};
{carry, sum} = vec[0:1];
• e.g., swap high and low-order bytes of 16-bit vector
wire [15:0] w1, w2;
assign w2 = {w1[7:0], w1[15:8]}

CSE 372 (Martin): Synthesizable Verilog 15 CSE 372 (Martin): Synthesizable Verilog 16
Wire and Vector Assignment Operators
• Wire assignment: “continuous assignment” • Operators similar to C or Java
• Connect combinational logic block or other wire to wire input • On wires:
• Order of statements not important, executed totally in parallel • & (and), | (or), ~ (not), ^ (xor)
• When right-hand-side changes, it is re-evaluated and re-assigned • On vectors:
• Designated by the keyword assign • &, |, ~, ^ (bit-wise operation on all wires in vector)
wire c; • E.g., assign vec1 = vec2 & vec3;
assign c = a | b; • &, |, ^ (reduction on the vector)
wire c = a | b; // same thing • E.g., assign wire1 = | vec1;
• Even ==, != (comparisons) +, -, * (arithmetic), <<, >> (shifts)
• But you can’t use these, yet. Can you guess why?
• Note: use with care, assume unsigned numbers
• Verilog 2001: signed vs unsigned vectors, >>> operator
• Can be arbitrarily nested: (a & ~b) | c

CSE 372 (Martin): Synthesizable Verilog 17 CSE 372 (Martin): Synthesizable Verilog 18

Conditional Operator Miscellaneous


• Verilog supports the ?: conditional operator • Operators and expressions can be used with modules
• Almost never useful in C (in my opinion) • !mux2to1 mux0 (cond1 & cond2, a, b, out);
• Much more useful in Verilog
• C/Java style comments
• // comment until end of line
• Examples: • /* comment between markers */
assign out = S ? B : A;
• All variable names are case sensitive
assign out = sel == 2'b00 ? a :
sel == 2'b01 ? b :
• Constants:
sel == 2'b10 ? c :
• assign x = 3’b011
sel == 2'b11 ? d : 1'b0;
• The “3” is the number of bits
• The “b” means “binary” - “h” for hex, “d” for decimal
• What do these do? • The “011” are the digits (in binary in this case)

CSE 372 (Martin): Synthesizable Verilog 19 CSE 372 (Martin): Synthesizable Verilog 20
Arrays of Modules Parameters
• Verilog also supports arrays of module instances • Allow per-instantiation module parameters
• Well, at least some Verilog tools • Use “parameter” statement
• Support for this feature varies • modname #(10, 20, 30) instname(in1, out1);
• Example:
module mux2to1_4(Sel, A, B, O);
input [3:0] A; module mux2to1_N(Sel, A, B, O);
input [3:0] B; parameter N = 1
input Sel; input [N-1:0] A;
output [3:0] O; input [N-1:0] B;
input Sel;
mux2to1 mux0[3:0] (Sel, A, B, O); output [N-1:0] O;
endmodule mux2to1 mux0[N-1:0] (Sel, A, B, O);
endmodule

Mux2to1_N #(4) mux1 (S, in1, in2, out)
CSE 372 (Martin): Synthesizable Verilog 21 CSE 372 (Martin): Synthesizable Verilog 22

Last Multiplexer Example Verilog Pre-Processor


• Using conditional operator • Like the C pre-processor
• But uses ` (back-tick) instead of #
module mux2to1_N(Sel, A, B, Out); • Constants: `define
parameter N = 1 • No parameterized macros
input [N-1:0] A; • Use ` before expanding constant macro
input [N-1:0] B; `define letter_A 8’h41
input Sel; wire w = `letter_A;
output [N-1:0] Out;
• Conditional compilation: `ifdef, `endif
assign Out = Sel ? B : A
• File inclusion: `include
endmodule

• Parameter vs `define
• Parameter only for “per instance” constants
• `define for “global” constants

CSE 372 (Martin): Synthesizable Verilog 23 CSE 372 (Martin): Synthesizable Verilog 24
Common Errors Additional Verilog Resources
• Tools are from a less gentle time • Elements of Logic Design Style by Shing Kong, 2001
• More like C, less like Java • Dos, do-nots, tips
• Assume that you mean what you say • http://www.cis.upenn.edu/~milom/elements-of-logic-design-style/
• Common errors:
• Not assigning a wire a value • Verilog HDL Synthesis: A Practical Primer
• Assigning a wire a value more than once • By J. Bhasker, 1998
• Implicit wire declarations (default to type “wire”) • To the point (<200 pages)
• Disable by adding the following to the file:
• `default_nettype none • Advanced Digital Design with the Verilog HDL
• Does not work with ModelSim • By Michael D. Ciletti, 2003
• !Avoid names such as: • Verilog plus lots of digital logic design (~1000 pages)
• clock, clk, power, pwr, ground, gnd, vdd, vcc, init, reset, rst
• Some of these are “special” and will silently cause errors • Verilog tutorial on CD from “Computer Org. and Design”
CSE 372 (Martin): Synthesizable Verilog 25 CSE 372 (Martin): Synthesizable Verilog 26

Lab 1 - ALU (Arithmetic/Logical Unit)


• Task: design an ALU for a P37X CPU

CSE372 • Ten operations:


Digital Systems Organization and Design •

Addition, subtraction
Multiplication
Lab • And, or, not, xor
• Shift left, logical shift right, arithmetic shift right

Prof. Milo Martin • The different adder implementations


• Ripple-carry
Unit 1: Synthesizable Verilog (continued) • Two carry-select adders

• Pay close attention in CSE371 lecture this week!

CSE 372 (Martin): Synthesizable Verilog 27 CSE 372 (Martin): Synthesizable Verilog 28
Aside: Honors Points Aside: Due Dates and Late Days
• Goals: • Normal due dates
• Make the labs accessible to all • Lab demos are on Fridays
• Challenge those that want more • Lab write-ups are due on Mondays (at start of class)
• So, I’m trying something different
• Again, experimental
• I’ll give you two “late credits” for the semester
• Labs will have two types of “points” • Used for emergencies, sickness, travel, etc.
• “Normal” - standard labs
• Otherwise, no late assignments accepted
• “Honors” - above and beyond
• Normal points
• Get all the normal points -> A- in the class • Impact of using a “late credit”
• Honors points • Demo moved from Friday to Monday
• Will distinguish the A- from A and A+ • Lab write-up moved from Monday to Wednesday (in TA lab hours)
• May bump others a third of a letter grade • No “honors points” for these late assignments
• Examples: fast adders (lab 1), advanced pipelines
CSE 372 (Martin): Synthesizable Verilog 29 CSE 372 (Martin): Synthesizable Verilog 30

Repeated Signals FYI: Non-binary Hardware Values


• Last time we discussed vector concatenation • A hardware signal can have four values
assign vec = {x, y, z}; 0, 1
X: don’t know, don’t care
• Can also repeat a signal n times Z: high-impedance (no current flowing)
assign vec = {16{x}}; // 16 copies of x • Uses for “x”
• Example uses (what does this do?): • Tells synthesis tool you don’t care
wire [7:0] out; • Synthesis tool makes the most convenient circuit (fast, small)
• Use with care, leads to synthesis dependent operation
wire [3:0] A;
• Uses for “z”
assign out = {{4{0}}, A[3:0]}; • Tri-state devices drive a zero, one, or nothing (z)
• What about this? • Many tri-states drive the same wire, all but one must be “z”
assign out = {{4{A[3]}}, A[3:0]}; • Makes some circuits very fast
• Example: multiplexer
• Why Verilog allows multiple assignments to same wire.

CSE 372 (Martin): Synthesizable Verilog 31 CSE 372 (Martin): Synthesizable Verilog 32
Simulation Levels of Simulation
• Used to test and debug our designs • Functional (or Behavioral) Simulation
• Graphical output via waveforms • Simulates Verilog abstractly
• No timing information, can’t detect timing “bugs”
• Post-synthesis Timing Simulation
• Simulating devices generated via synthesis
• Gates, transistors, FPGA logical units (LUTs)
• No interconnect delay
• Not all internal signals may still exist
• Synthesis might have optimized or changed the design
• Slower
• Layout Timing Simulation
• After synthesis, the tool “places and routes” the logic blocks
• Includes all sources of delay
• Even slower

CSE 372 (Martin): Synthesizable Verilog 33 CSE 372 (Martin): Synthesizable Verilog 34

Sequential Logic in Verilog Designing Sequential Logic


• How do we specify state-holding constructs in Verilog? • CSE372 design rule: separate comb. logic from sequential
state elements
module dff (Clock, D, WE, Reset, Q); • Not enforced by Verilog, but a very good idea
input Clock, D, WE, Reset; • Possible exceptions: counters, shift registers
output Q;
• We’ll give you a 1-bit flip-flop module (see previous slide)
reg Q;
always @(posedge Clock) • Edge-triggered, not a latch
begin • Use it to build a n-bit register
if (Reset) • Example use: state machine
Q = 1'b0;
Clock
else if (WE)
State Combinational Output
Q = D;
Register Current Logic
end
State
endmodule Next State

CSE 372 (Martin): Synthesizable Verilog 35 CSE 372 (Martin): Synthesizable Verilog 36
Clocks Signals
• Clocks signals are not normal signals

• Travel on dedicated “clock” wires


• Reach all parts of the chip
• Special “low-skew” routing

• Ramifications:
• Never do logic operations on the clocks
• If you want to add a “write enable” to a flip-flop:
• Use a mux to route the old value back into it
• Do not just “and” the write-enable signal with the clock!

• Messing with the clock can cause a errors


• Often can only be found using timing simulation

CSE 372 (Martin): Synthesizable Verilog 37

You might also like