Verilog Coding PDF
Verilog Coding PDF
Multiplexer Built with Always Multiplexer Built with Always Mux with Continuous Assignment
module mux(f, a, b, sel); module mux(f, a, b, sel); module mux(f, a, b, sel);
output f; Modules may output f; output f;
input a, b, sel; contain one or more input a, b, sel; A reg behaves like input a, b, sel;
reg f; always blocks reg f; memory: holds its value
until imperatively assign f = sel ? a : b;
always @(a or b or sel) Sensitivity list always @(a or b or sel) LHS is always set to
if (sel) f = a; if (sel) f = a; assigned otherwise endmodule the value on the RHS
else f = b; contains signals else f = b;
whose change Body of an always block Any change on the right
endmodule endmodule
makes the block contains traditional causes reevaluation
a a a
execute imperative code
f f f
b b b
sel sel sel
Mux with User-Defined Primitive How Are Simulators Used? Structural Modeling
primitive mux(f, a, b, sel);
output f; Testbench generates stimulus and checks response When Verilog was first developed (1984) most logic
input a, b, sel; Behavior defined using simulators operated on netlists
Coupled to model of the system
table a truth table that
Pair is run simultaneously Netlist: list of gates and how they’re connected
1?0 : 1; includes “don’t cares”
0?0 : 0; This is a less pessimistic than A natural representation of a digital logic circuit
?11 : 1;
?01 : 0; others: when a & b match, sel is Stimulus Not the most convenient way to express test benches
11? : 1; ignored; others produce X
00? : 0; Testbench System Model
endtable a
endprimitive
Response
Result checker
f
b
sel
Behavioral Modeling How Verilog Is Used Two Main Components of Verilog:
Behavioral
A much easier way to write testbenches Virtually every ASIC is designed using either Verilog or
VHDL (a similar language) Concurrent, event-triggered processes (behavioral)
Also good for more abstract models of circuits
Behavioral modeling with some structural elements Initial and Always blocks
• Easier to write
“Synthesis subset” can be translated using Synopsys’ Imperative code that can perform standard data
• Simulates faster
Design Compiler or others into a netlist manipulation tasks (assignment, if-then, case)
More flexible Design written in Verilog Processes run until they delay for a period of time or wait
Provides sequencing Simulated to death to check functionality for a triggering event
Verilog succeeded in part because it allowed both the Synthesized (netlist generated)
model and the testbench to be described together
Static timing analysis to check timing
Two Main Components of Verilog: Two Main Data Types: Nets Two Main Data Types: Regs
Structural
Nets represent connections between things Regs represent data storage
Structure (Plumbing) Do not hold their value Behave exactly like memory in a computer
Verilog program build from modules with I/O interfaces Take their value from a driver such as a gate or other Hold their value until explicitly assigned in an initial or
Modules may contain instances of other modules module always block
Modules contain local signals, etc. Cannot be assigned in an initial or always block Never connected to something
Module configuration is static and all run concurrently Can be used to model latches, flip-flops, etc., but do not
correspond exactly
Actually shared variables with all their attendant problems
Non-blocking Assignments Nonblocking Can Behave Oddly Nonblocking Looks Like Latches
This version does work: Nonblocking rule: A sequence of nonblocking assignments don’t RHS of nonblocking taken from latches
RHS evaluated communicate
reg d1, d2, d3, d4; RHS of blocking taken from wires
when assignment a = 1; a <= 1; a = 1;
runs
always @(posedge clk) d2 <= d1;
b = a;
c = b;
b <= a;
c <= b;
b = a;
c = b;
“ 1
a b
c ”
always @(posedge clk) d3 <= d2;
Blocking assignment: Nonblocking assignment: 1
always @(posedge clk) d4 <= d3; a=b=c=1 a=1 a
a <= 1;
• Behaviors resulting from shared-variable-like behavior • Modules contain initial and always blocks
of regs is not synthesized:
Based on discrete-event simulation semantics
always @(posedge clk) a = 1;
• Concurrent processes with sensitivity lists
New value of a may be seen by other @(posedge clk)
statements in simulation, never in synthesis • Scheduler runs parts of these processes in response
to changes
Modeling Tools Language Features Language Uses
Nets (wires) for modeling interconnection Event-driven simulation
Switch-level primitives: CMOS transistors as switches that
move around charge • Non state-holding • Event queue containing things to do at particular
• Values set continuously simulated times
Gate-level primitives: Boolean logic gates
Regs for behavioral modeling
User-defined primitives: Gates and sequential elements • Evaluate and update events
• Behave exactly like memory for imperative modeling
defined with truth tables • Compiled-code event-driven simulation for speed
• Do not always correspond to memory elements in
Continuous assignment: Modeling combinational logic Logic synthesis
synthesized netlist
with expressions
Blocking vs. nonblocking assignment • Translating Verilog (structural and behavioral) into
Initial and always blocks: Procedural modeling of behavior netlists
• Blocking behaves like normal “C-like” assignment
• Nonblocking delays update, modeling synchronous
• Register inference: whether output is always updated
behavior • Logic optimization for cleaning up the result
In Conclusion In Conclusion
Verilog is a deeply flawed language Verilog is widely used because it solves a problem
• Nondeterministic • Good simulation speed that continues to improve
• Often weird behavior due to discrete-event semantics • Designers use a well-behaved subset of the language
• Vaguely defined synthesis subset • Makes a reasonable specification language for logic
synthesis
• Many possible sources of simulation/synthesis
mismatch • Logic synthesis one of the great design automation
success stories