01 Verilog PDF
01 Verilog PDF
CSE 372 (Martin): Synthesizable Verilog 1 CSE 372 (Martin): Synthesizable Verilog 2
CSE 372 (Martin): Synthesizable Verilog 5 CSE 372 (Martin): Synthesizable Verilog 6
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
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
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
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
• 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
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
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
CSE 372 (Martin): Synthesizable Verilog 35 CSE 372 (Martin): Synthesizable Verilog 36
Clocks Signals
• Clocks signals are not normal signals
• 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!