Verilog Tut
Verilog Tut
Verilog Tut
Introduction Basics of the Verilog Language Operators Hierarchy/Modules Procedures and Assignments Timing Controls and Delay Control Statement Logic-Gate Modeling Modeling Delay Other Verilog Features Summary
2
Aleksandar Milenkovic
E-mail: Web: [email protected] http://www.ece.uah.edu/~milenka
Introduction
Introduction
endmodule
a g4 b sel
3
nsel
g1 g2
f1 g3 f2
4
Introduction
Introduction
endmodule
a g4 b sel
nsel
g1 g2
f1 g3 f2
5
a f b sel
6
Introduction
Introduction
a f b sel
7
a f b sel
8
Introduction
Introduction
Behavior defined using a truth table that includes dont cares This is a less pessimistic than others: when a & b match, sel is ignored (others produce X)
a f b sel
9
Result checker
10
Introduction
Introduction
Styles
Structural - instantiation of primitives and modules RTL/Dataflow - continuous assignments Behavioral - procedural assignments
Structural Modeling
When Verilog was first developed (1984) most logic simulators operated on netlists Netlist: list of gates and how theyre connected A natural representation of a digital logic circuit Not the most convenient way to express test benches
11
12
Introduction
Introduction
Behavioral Modeling
A much easier way to write testbenches Also good for more abstract models of circuits
Easier to write Simulates faster
More flexible Provides sequencing Verilog succeeded in part because it allowed both the model and the testbench to be described together
13
14
Introduction
Introduction
always@(A or B or CI) begin S <= A ^ B ^ CI; CO <= A & B | A & CI | B & CI; end endmodule
15
16
Introduction
Introduction
An Example: Counter
`timescale 1ns/1ns module counter; reg clock; // declare reg data type for the clock integer count; // declare integer data type for the count initial // initialize things - this executes once at start begin clock = 0; count = 0; // initialize signals #340 $finish; // finish after 340 time ticks end /* an always statement to generate the clock, only one statement follows the always so we don't need a begin and an end */ always #10 clock = ~ clock; // delay is set to half the clock cycle /* an always statement to do the counting, runs at the same time (concurrently) as the other always statement */ always begin // wait here until the clock goes from 1 to 0 @ (negedge clock); // now handle the counting if (count == 7) count = 0; else count = count + 1; $display("time = ",$time," count = ", count); end endmodule
Design written in Verilog Simulated to death to check functionality Synthesized (netlist generated) Static timing analysis to check timing
17
18
Introduction
Outline
19
Introduction Basics of the Verilog Language Operators Hierarchy/Modules Procedures and Assignments Timing Controls and Delay Control Statement Logic-Gate Modeling Modeling Delay Other Verilog Features Summary
20
Language Conventions
Case-sensitivity Verilog is case-sensitive. Some simulators are case-insensitive Advice: - Dont use case-sensitive feature! Keywords are lower case Different names must be used for different items within the same scope Identifier alphabet: Upper and lower case alphabeticals decimal digits underscore
21
22
Four-valued Logic
Verilogs nets and registers hold four-valued data 0, 1
Logical Zero, Logical One
Maximum of 1024 characters in identifier First character not a digit Statement terminated by ; Free format within statement except for within quotes Comments: All characters after // in a line are treated as a comment Multi-line comments begin with /* and end with */ Compiler directives begin with // synopsys Built-in system tasks or functions begin with $ Strings enclosed in double quotes and must be on a single line
23
z
Output of an undriven tri-state driver high-impedance value Models case where nothing is setting a wires value
x
Models when the simulator cant decide the value uninitialized or unknown logic value
o Initial state of registers o When a wire is being driven to 0 and 1 simultaneously o Output of a gate with z inputs
24
0 0 1 X Z 0 0 0 0
1 0 1 X X
X 0 X X X
Data Types
nets are further divided into several net types
wire, tri, supply0, ...
registers - stores a logic value - reg integer - supports computation time - stores time 64-bit unsigned real - stores values as real num realtime - stores time values as real numbers event an event data type
module declarations_4; wire Data; // a scalar net of type wire wire [31:0] ABus, DBus; // two 32-bit wide vector wires... // DBus[31] = left-most = most-significant bit = msb // DBus[0] = right-most = least-significant bit = lsb // Notice the size declaration precedes the names // wire [31:0] TheBus, [15:0] BigBus; // illegal reg [3:0] vector; // a 4-bit vector register reg [4:7] nibble; // msb index < lsb index integer i; initial begin i = 1; vector = 'b1010; // vector without an index nibble = vector; // this is OK too #1; $display("T=%0g",$time," vector=", vector," nibble=", nibble); #2; $display("T=%0g",$time," Bus=%b",DBus[15:0]); end assign DBus [1] = 1; // this is a bit-select assign DBus [3:0] = 'b1111; // this is a part-select // assign DBus [0:3] = 'b1111; // illegal - wrong direction endmodule
27
28
Net Types
module declarations_5; reg [31:0] VideoRam [7:0]; // a 8-word by 32-bit wide memory initial begin VideoRam[1] = 'bxz; // must specify an index for a memory VideoRam[2] = 1; VideoRam[7] = VideoRam[VideoRam[2]]; // need 2 clock cycles for this VideoRam[8] = 1; // careful! the compiler won't complain! // Verify what we entered: $display("VideoRam[0] is %b",VideoRam[0]); $display("VideoRam[1] is %b",VideoRam[1]); $display("VideoRam[2] is %b",VideoRam[2]); $display("VideoRam[7] is %b",VideoRam[7]); end endmodule
wire - connectivity only tri - same as wire, but will be 3-stated in hardware wand - multiple drivers - wired and wor - multiple drivers - wired or triand - same as wand, but 3-state trior - same as wor but 3-state supply0 - Global net GND supply1 - Global Net VCC (VDD) tri0, tri1 model resistive connections to VSS and VDD trireg like wire but associates some capacitance with the net, so it can model charge storage
30
29
Declarations: An Example
module declarations_1; wire pwr_good,pwr_on,pwr_stable; integer i; time t; event e; real r; // // // // // Explicitly declare wires 32-bit, signed (2's complement) 64-bit, unsigned, behaves like a 64-bit reg Declare an event data type Real data type of implementation defined size
Register Assignment
A register may be assigned value only within:
a a a a procedural statement user-defined sequential primitive task, or function.
// assign statement continuously drives a wire... assign pwr_stable = 1'b1; assign pwr_on = 1; // 1 or 1'b1 assign pwr_good = pwr_on & pwr_stable; initial begin $display("pwr_on=",pwr_on); i = 123.456; // There must be a digit on either side r = 123456e-3; // of the decimal point if it is present. t = 123456e-3; // Time is rounded to 1 second by default. $display("i=%0g",i," t=%6.2f",t," r=%f",r); #2 $display("TIME=%0d",$time," ON=",pwr_on, " STABLE=",pwr_stable," GOOD=",pwr_good); end endmodule # pwr_on=x # i=123 t=123.00 r=123.456000 # TIME=2 ON=1 STABLE=1 GOOD=1
Examples
reg a, b, c; reg [15:0] counter, shift_reg; integer sum, difference;
31 32
Number Representation
Format: <size><base_format><number>
<size> - decimal specification of number of bits
parameter A = 2b00, B = 2b01, C = 2b10; parameter regsize = 8; reg [regsize - 1:0]; /* illustrates use of parameter regsize */
Strings
No explicit data type Must be stored in reg (or array)
reg [255:0] buffer; parameter Tab = "\t"; parameter NewLine = "\n"; parameter BackSlash = "\\"; //stores 32 characters // tab character // newline character // back slash
33
34
Number Representation
Examples:
6b010_111 8'b0110 4'bx01 16'H3AB 24 5'O36 16'Hx 8'hz gives 010111 gives 00000110 gives xx01 gives 0000001110101011 gives 00011000 gives 11100 gives xxxxxxxxxxxxxxxx gives zzzzzzzz
Outline
Introduction Basics of the Verilog Language Operators Hierarchy/Modules Procedures and Assignments Timing Controls and Delay Control Statement Logic-Gate Modeling Modeling Delay Other Verilog Features Summary
36
35
Operators
Operators
Operators
Arithmetic (pair of operands, binary word) [binary: +, -,*,/,%*]; [unary: +, -] Bitwise (pair of operands, binary word) [~, &,|,^,~^,^~] Reduction (single operand, bit) [&,~&,|,~|,^,~^,^~] Logical (pair of operands, boolean value) [!,&&,||,==,!=,===,!==] Relational (pair of operands, boolean value) [<,<=,>,>=] Shift (single operand, binary word) [>>,<<] Conditional ? : (three operands, expression) Concatenation and Replications {,} {int{ }}
* unsupported for variables
37
Operators (contd)
Arithmetic
+ (addition), initial begin #1 Seven = 7; #1 - (subtraction), Seven = Seven * (multiplication), #1 end / (division), endmodule % (modulus) Before=7
After =0
module modulo; reg [2:0] Seven; $display("Before=", Seven); + 1; #1 $display("After =", Seven);
Bitwise
~ (negation), & (and), | (or), ^ (xor), ~^ (xnor) Reduction E.g.: &(0101) = 0 & (and), ~& (nand), | (or), ~| (nor), ^ (or), ~^ (xnor) Logical ! (negation), && (and), || (or), == (equality), != (inequality), === (case equality), !== (case inequality) === : determines whether two words match identically on a bit-by-bit basis, including bits that have values x and z
38
Operators
Operators
Operators (contd)
Relational < (lt), <= (lte), > (gt), >= (gte) Shift
<< (left shift), >> (right shift) Conditional E.g.: Y = (A==B) ? A: B wire[15:0] bus_a = drive_bus_a ? data : 16bz; Concatenation {4{a}} = {a, a, a, a}
Operators (contd)
module operators; parameter A10xz = {1'b1,1'b0,1'bx,1'bz}; // concatenation parameter A01010101 = {4{2'b01}}; // replication // arithmetic operators: +, -, *, /, and modulus % parameter A1 = (3+2) %2; // result of % takes sign of argument #1 // logical shift operators: << (left), >> (right) parameter A2 = 4 >> 1; parameter A4 = 1 << 2; // zero fill // relational operators: <, <=, >, >= initial if (1 > 2) $stop; // logical operators: ! (negation), && (and), || (or) parameter B0 = !12; parameter B1 = 1 && 2; reg [2:0] A00x; initial begin A00x = 'b111; A00x = !2'bx1; end parameter C1 = 1 || (1/0); /* this may or may not cause an error: the short-circuit behavior of && and || is undefined. An evaluation including && or || may stop when an expression is known to be true or false */ // == (logical equality), != (logical inequality) parameter Ax = (1==1'bx); parameter Bx = (1'bx!=1'bz); parameter D0 = (1==0); parameter D1 = (1==1); ...
39
40
Operators
Operators
Operators (contd)
... parameter D0 = (1==0); parameter D1 = (1==1); // === case equality, !== (case inequality) // case operators only return true or false parameter E0 = (1===1'bx); parameter E1 = 4'b01xz === 4'b01xz; parameter F1 = (4'bxxxx === 4'bxxxx); // bitwise logical: // ~ (negation), & (and), | (inclusive or), // ^ (exclusive or), ~^ or ^~ (equivalence) parameter A00 = 2'b01 & 2'b10; // unary logical reduction: // & (and), ~& (nand), | (or), ~| (nor), // ^ (xor), ~^ or ^~ (xnor) parameter G1= & 4'b1111; // conditional expression x = a ? b : c // if (a) then x = b else x = c reg H0, a, b, c; initial begin a=1; b=0; c=1; H0=a?b:c; end reg[2:0] J01x, Jxxx, J01z, J011; initial begin Jxxx = 3'bxxx; J01z = 3'b01z; J011 = 3'b011; J01x = Jxxx ? J01z : J011; end // bitwise result ....
Operators
Operators
op x where op is ~
Bitwise negation Bit width = width(x)
{x, , y}
Concatenation Bit width = width(x) + + width(y)
x op y where op is ==, !==, ===, !===, &&, ||, >, >=, <, <= or op y where op is !, &, |, ^, ~&, ~|, ~^
Logical, relational and reduction Bit width = 1
{x{y, , z}}
Replication Bit width = x * (width(y) + + width(z))
Operators
Operators
Reduction
Defined by tables as for bitwise operators.
Relational
If any bit is x or z, result is x.
Shifts
z changed to x. Vacated positions zero filled.
Logical
== and != If any bit is x or z, result is x. === and !== All bits including x and z values must match for equality
Conditional
If conditional expression is ambiguous (e.g., x or z), both expressions are evaluated and bitwise combined as follows: f(1,1) = 1, f(0,0) = 0, otherwise x.
45
46
Modules
Outline
Introduction Basics of the Verilog Language Operators Hierarchy/Modules Procedures and Assignments Timing Controls and Delay Control Statement Logic-Gate Modeling Modeling Delay Other Verilog Features Summary
47
Modules
Basic design units
Declared Instantiated
Module interface is defined using ports
Modules declarations cannot be nested Modules may contain instances of other modules Modules contain local signals, etc. Module configuration is static and all run concurrently
48
Modules
Modules
Module Declaration
Basic structure of a Verilog module:
module mymod(output1, output2, input1, input2); output output1; output [3:0] output2; input input1; input [2:0] input2; endmodule
49
50
Modules
Modules
Module Instantiation
Instances of
module mymod(y, a, b);
look like
mymod mm1(y1, a1, b1); // Connect-by-position mymod (y2, a1, b1), (y3, a2, b2); // Instance names omitted mymod mm2(.a(a2), .b(b2), .y(c2)); // Connect-by-name
51
52
Modules
Modules
53
54
Modules
Modules
Connections
Position association
C24DecoderWithEnable DE (A[3:2], E, S);
... C24DecoderWithEnable DE (A[3:2], // A = A[3:2], E = E, S = S ... E, S);
Connections (contd)
Empty Port Connections
... // E is at high impedance state (z) C24DecoderWithEnable DE (A[3:2],,S); // Outputs S[3:0] are unused C24DecoderWithEnable DE (A[3:2],E,);
Name association
C24DecoderWithEnable DE (.E(E), .A(A[3:2]), .D(S));
... C24DecoderWithEnable DE (.E (E), .A (A[3:2]) .D (S)); // Note order in list no longer important // (E and A interchanged). // A = A[3:2], E = E, D = S ...
55 56
Modules
Array of Instances
{ , } is concatenate Example
module add_array (A, B, CIN, S, COUT) ; input [7:0] A, B ; input CIN ; output [7:0] S ; output COUT ; wire [7:1] carry; full_add FA[7:0] (A,B,{carry, CIN},S,{COUT, carry}); // full_add is a module endmodule
Outline
57
Introduction Basics of the Verilog Language Operators Hierarchy/Modules Procedures and Assignments Timing Controls and Delay Control Statement Logic-Gate Modeling Modeling Delay Other Verilog Features Summary
58
Procedures
Procedures
Assignments
Continuous assignment
module holiday_1(sat, sun, weekend); input sat, sun; output weekend; assign weekend = sat | sun; // outside a procedure endmodule
Sequential block: a group of statements that appear between a begin and an end
executed sequentially considered as a statement can be nested
Procedural assignment
module holiday_2(sat, sun, weekend); input sat, sun; output weekend; reg weekend; always #1 weekend = sat | sun; // inside a procedure endmodule
module assignments // continuous assignments go here always begin // procedural assignments go here end endmodule 59 60
Procedures
Procedures
Continuous Assignments
Convenient for logical or datapath specifications
Define bus widths Continuous assignment: permanently sets the value of sum to be a+b+carryin Recomputed when a, b, or carryin changes
wire [8:0] sum; wire [7:0] a, b; wire carryin; assign sum = a + b + carryin;
61
62
Procedures
Procedures
Sequential Block
Sequential block may appear in an always or initial statement
initial begin imperative statements end Runs when simulation starts Terminates when control reaches the end (one time sequential activity flow) Good for providing stimulus (testbenches); not synthesizable always begin imperative statements end Runs when simulation starts Restarts when control reaches the end (cycle sequential activity flow) Good for modeling/specifying hardware
Procedures
Procedures
Procedural Assignment
Inside an initial or always block: sum = a + b + cin; Just like in C: RHS evaluated and assigned to LHS before next statement executes
Clk=0 Clk=1 Clk=1 Clk=0 Clk=1 Clk=1 Clk=0 Clk=1 Clk=1 Clk=0 Y=0 Y=0 Y=1 Y=1 Y=1 Y=0 Y=0 Y=0 Y=1 Y=1
>>>> T= 0 T=10 T=15 T=20 T=30 T=35 T=40 T=50 T=55 T=60
Timing Control
Outline
Introduction Basics of the Verilog Language Operators Hierarchy/Modules Procedures and Assignments Timing Control and Delay Control Statement Logic-Gate Modeling Modeling Delay Other Verilog Features Summary
67
Timing Control
Statements within a sequential block are executed in order In absence of any delay they will execute at the same simulation time the current time stamp Timing control
Delay control Event control
Delay control delays an assignment by a specified amount of time Event control delays an assignment until a specified event occur
68
Timing Control
Timing Control
Delay control
Timescale compiler directive
`timescale 1ns/10ps // Units of time are ns. Round times to 10 ps. // Allowed unit/precision values: {1 | 10 | 100, s | ms | us | ns | ps}
Event control
posedge 0 => 1, 0 => x, x => 1 negedge 1 => 0, 1 => x, x => 0
event_control ::= @ event_identifier | @ (event_expression) event_expression ::= expression | event_identifier | posedge expression | negedge expression | event_expression or event_expression
module show_event; reg clock; event event_1, event_2; // Declare two named events. always @(posedge clock) -> event_1; // Trigger event_1. always @ event_1 begin $display("Strike 1!!"); -> event_2; end // Trigger event_2. always @ event_2 begin $display("Strike 2!!"); $finish; end // Stop on detection of event_2. always #10 clock = ~ clock; // We need a clock. initial clock = 0; endmodule Strike 1!! Strike 2!!
70
Timing Control
Timing Control
71
72
Timing Control
Timing Control
Wait Statement
Suspends a procedure until a condition becomes true
there must be another concurrent procedure that alters the condition otherwise we have an infinite hold
module test_dff_wait; reg D, Clock, Reset; dff_wait u1(D, Q, Clock, Reset); initial begin D=1; Clock=0;Reset=1'b1; #15 Reset=1'b0; #20 D=0; end always #10 Clock = !Clock; initial begin $display("T Clk D Q Reset"); $monitor("%2g",$time,,Clock,,,,D,,Q,,Reset); #50 $finish; end endmodule module dff_wait(D, Q, Clock, Reset); output Q; input D, Clock, Reset; reg Q; wire D; always @(posedge Clock) if (Reset !== 1) Q = D; always begin wait (Reset == 1) Q = 0; wait (Reset !== 1); end endmodule 73 T 0 10 15 20 30 35 40 Clk 0 1 1 0 1 1 0 D 1 1 1 1 1 0 0 Q 0 0 0 0 1 1 1 Reset 1 1 0 0 0 0 0
LHS updated only after all events for the current instant have run
74
Timing Control
Timing Control
a 1
a b
Control statements
Control Statements
If statement
if (select == 1) else y = a; y = b;
Case statement
case (op) 2b00: y 2b01: y 2b10: y default: endcase = = = y a a a = + b; b; ^ b; hxxxx;
77
78
Control statements
Control statements
Fork statement and join statement allows execution of two or more parallel threads in a parallel block
module fork_1 event eat_breakfast, read_paper; initial begin fork @eat_breakfast; @read_paper; join end endmodule
80
No declaration; can only be instantiated All output ports appear in list before any input ports Optional drive strength, delay, name of instance
81
82
A Carry Primitive
primitive carry(out, a, b, c); output out; input a, b, c; table 00? : 0; 0?0 : 0; ?00 : 0; 11? : 1; 1?1 : 1; ?11 : 1; endtable endprimitive
Always have exactly one output Truth table may include dont-care (?) entries
83
84
A Sequential Primitive
Primitive dff(q, clk, data); output q; reg q; input clk, data; table // clk data q new-q (01) 0 : ? : 0; (01) 1 : ? : 1; (0x) 1 : 1 : 1; (0x) 0 : 0 : 0; (?0) ? : ? : -; ? (??) : ? : -; endtable endprimitive
Shorthand notations: - * is (??) - r is (01) - f is (10) - n is (10), (1x), (x0)
85
// // // // // //
Latch a 0 Latch a 1 Hold when Hold when Hold when Hold when
86
Modeling delay
Modeling delay
1 ns
Types
Gate Delay (Inertial Delay) Net Delay (Transport Delay) Module Path Delay
87
Modeling delay
Modeling delay
nd01 has a delay of 3 ns (assuming ns timescale) for both falling and rising delays nd02 has a triplet for the delay (min is 2.6 ns, typ is 3.0 ns, max is 3.4) nd03 has two triplets for the delay
first triplet specifies min/typ/max for rising delay second triplet specifies min/typ/max for falling delay
For primitives which can produce high-impedance output we can specify turn-off triplet
Modeling delay
Module Delay
Example: norf201 3-input nor gate from a 1.2um CMOS
module norf201(o, a1, b1); output o; input a1, b1; nor(o, a1, b1); specify // module paths (a1, b1 *> o) = (0.179:0.349:0.883, 0:084:0.169:0.466); endspecify; endmodule;
Outline
Introduction Basics of the Verilog Language Operators Hierarchy/Modules Procedures and Assignments Timing Controls and Delay Control Statement Logic-Gate Modeling Modeling Delay Other Verilog Features Summary
92
91
Altering Parameters
Use parameter
module Vector_And(Z, A, B); parameter CARDINALITY = 1; input [CARDINALITY-1:0] A, B; output [CARDINALITY-1:0] Z; wire [CARDINALITY-1:0] Z = A & B; endmodule
Define the behavior in a single always @(posedge clk) block Variations on these themes
Or using defparam
module And_Gates(OutBus, InBusA, InBusB); parameter WIDTH = 1; input [WIDTH-1:0] InBusA, InBusB; output [WIDTH-1:0] OutBus; Vector_And #(WIDTH) My_And(OutBus, InBusA, InBusB); endmodule module Super_Size; defparam And_Gates.WIDTH = 4; endmodule 93
94
Output o is declared a reg because it is assigned procedurally, not because it holds state
always @(a or b or state) case (state) 2b00: begin nextState = a ? 2b00 : 2b01; (Implies state-holding o = a & b; elements otherwise) end 2b01: begin nextState = 2b10; o = 0; end endcase
95
96
always @(a or b or state) This is a Mealy machine case (state) because the output is directly affected by any 2b00: begin change on the input nextState = a ? 2b00 : 2b01; o = a & b; end 2b01: begin nextState = 2b10; o = 0; end endcase always @(posedge clk or reset) if (reset) state <= 2b00; else state <= nextState;
Expresses Moore machine behavior: Outputs are latched Inputs only sampled at clock edges Nonblocking assignments used throughout to ensure coherency. RHS refers to values calculated in previous
end clock cycle
97
98
Writing Testbenches
module test; reg a, b, sel; mux m(y, a, b, sel);
Simulation Behavior
Scheduled using an event queue Non-preemptive, no priorities A process must explicitly request a context switch Events at a particular time unordered
Inputs to device under test Device under test $monitor is a built-in event driven printf
initial begin $monitor($time,, a = %b b=%b sel=%b y=%b, a, b, sel, y); a = 0; b= 0; sel = 0; #10 a = 1; Stimulus generated by #10 sel = 1; sequence of assignments #10 b = 1; and delays end
Scheduler runs each event at the current time, possibly scheduling more as a result
99
100
Simulation Behavior
Evaluation events compute functions of inputs Update events change outputs Split necessary for delays, nonblocking assignments, etc.
Evaluation event reads values of b and c, adds them, and schedules an update event
Concurrent processes (initial, always) run until they stop at one of the following #42
Schedule process to resume 42 time units from now
Update event writes new value of a and schedules any evaluation events that are sensitive to a change on a
a <= b + c
@(a or b or y)
Resume when a, b, or y changes
@(posedge clk)
Resume when clk changes from 0 to 1
101
102
103
104
Most modern simulators use this approach Verilog program compiled into C Each concurrent process (e.g., continuous assignment, always block) becomes one or more C functions Initial and always blocks split into multiple functions, one per segment of code between a delay, a wait, or event control (@) Central, dynamic event queue invokes these functions and advances simulation time
Logic synthesis converts a subset of the Verilog language into an efficient netlist One of the major breakthroughs in designing logic chips in the last 20 years Most chips are designed using at least some logic synthesis
105
106
Logic Synthesis
Takes place in two stages: Translation of Verilog (or VHDL) source to a netlist
Register inference
Logic Optimization
Netlist optimization the critical enabling technology Takes a slow or large netlist and transforms it into one that implements the same function more cheaply Typical operations
Time-consuming operation
Can take hours for large chips
107
108
Continuous assignment
o Expressions turn into little datapaths
Behavioral blocks
Depends on sensitivity list Only when they have reasonable interpretation as combinational logic, edge, or level-sensitive latches Blocks sensitive to both edges of the clock, changes on unrelated signals, changing sensitivity lists, etc. cannot be synthesized
User-defined primitives
Primitives defined with truth tables Some sequential UDPs cant be translated (not latches or flip-flops)
109 110
Register Inference
The main trick
reg does not always equal latch
Used to set up initial state or describe finite testbench stimuli Dont have obvious hardware component
Delays
May be in the Verilog source, but are simply ignored
A variety of other obscure language features In general, things heavily dependent on discrete-event simulation semantics
Certain disable statements Pure events
Rule: Combinational if outputs always depend exclusively on sensitivity list Sequential if outputs may also depend on previous values
111
112
Register Inference
Combinational:
reg y; always @(a or b or sel) if (sel) y = a; else y = b;
Register Inference
A common mistake is not completely specifying a case statement This implies a latch:
always @(a or case ({a, b}) 2b00 : f = 2b01 : f = 2b10 : f = endcase b) 0; 1; 1; f is not assigned when {a,b} = 2b11
Sequential
reg q; always @(d or clk) if (clk) q = d;
113
114
Register Inference
The solution is to always have a default case
always @(a or b) case ({a, b}) 2b00: f = 0; 2b01: f = 1; 2b10: f = 1; default: f = 0; endcase
f is always assigned
115
116
Simulation-synthesis Mismatches
Many possible sources of conflict
Outline
Introduction Basics of the Verilog Language Operators Hierarchy/Modules Procedures and Assignments Timing Controls and Delay Control Statement Logic-Gate Modeling Modeling Delay Other Verilog Features Summary
118
Synthesis ignores delays (e.g., #10), but simulation behavior can be affected by them Simulator models X explicitly, synthesis doesnt Behaviors resulting from shared-variable-like behavior of regs is not synthesized
always @(posedge clk) a = 1; New value of a may be seen by other @(posedge clk) statements in simulation, never in synthesis
117
Summary
Summary
Summary of Verilog
Systems described hierarchically
Modules with interfaces Modules contain instances of primitives, other modules Modules contain initial and always blocks
Modeling Tools
Switch-level primitives
CMOS transistors as switches that move around charge
Gate-level primitives
Boolean logic gates
User-defined primitives
Gates and sequential elements defined with truth tables
Continuous assignment
Modeling combinational logic with expressions
119
120
Summary
Summary
Language Features
Nets (wires) for modeling interconnection
Non state-holding Values set continuously
Language Uses
Event-driven simulation
Event queue containing things to do at particular simulated times Evaluate and update events Compiled-code event-driven simulation for speed
Logic synthesis
Translating Verilog (structural and behavioral) into netlists Register inference: whether output is always updated Logic optimization for cleaning up the result
122
Summary
Summary
Compared to VHDL
Verilog and VHDL are comparable languages VHDL has a slightly wider scope
System-level modeling Exposes even more discrete-event machinery
VHDL is better-behaved
Fewer sources of nondeterminism (e.g., no shared variables ???)
Delays
Simulating circuits with delays does not improve confidence enough Hard to get timing models accurate enough Never sure youve simulated the worst case Static timing analysis has taken its place
123
VHDL is harder to simulate quickly VHDL has fewer built-in facilities for hardware modeling VHDL is a much more verbose language
Most examples dont fit on slides
124