Verilog
Verilog
Structure (Plumbing)
• Verilog program build from modules with I/O interfaces
• Modules may contain instances of other modules
• Modules contain local signals, etc.
• Module configuration is static and all run concurrently
Two Main Data Types
Nets represent connections between things
• Do not hold their value
• Take their value from a driver such as a gate or other
module
• Cannot be assigned in an initial or always block
0, 1
• Obvious
Z
• Output of an undriven tri-state driver
• Models case where nothing is setting a wire’s value
X
• Models when the simulator can’t decide the value
• Initial state of registers
• When a wire is being driven to 0 and 1 simultaneously
• Output of a gate with Z inputs
Four-valued Logic
Logical operators work on three-valued logic
0 1 X Z
look like
initial always
begin begin
… imperative statements … … imperative statements …
end end
initial begin
#10 a = 1; b = 0;
#10 a = 0; b = 1;
end
sum = a + b + cin;
case (op)
2’b00: y = a + b;
2’b01: y = a – b;
2’b10: y = a ^ b;
default: y = ‘hxxxx;
endcase
For Loops
A increasing sequence of values on an output
i = 0;
while (I <= 15) begin
output = i;
#10 i = i + 1;
end
Modeling A Flip-Flop With Always
Very basic: an edge-sensitive flip-flop
reg q;
Fundamental problem:
• In a synchronous system, all flip-flops sample
simultaneously
• In Verilog, always @(posedge clk) blocks run in some
undefined sequence
A Flawed Shift Register
This doesn’t work as you’d expect:
a = 1; a <= 1;
b = a; b <= a;
c = b; c <= b;
a = 1;
b = a;
“ 1
a b c ”
c = b;
1 a
a <= 1;
b <= a; “ b ”
c <= b;
c
Building Behavioral Models
Modeling FSMs Behaviorally
There are many ways to do it:
Stimulus
#42
• Schedule process to resume 42 time units from now
wait(cf & of)
• Resume when expression “cf & of” becomes true
@(a or b or y)
• Resume when a, b, or y changes
@(posedge clk)
• Resume when clk changes from 0 to 1
Simulation Behavior
Infinite loops are possible and the simulator does not
check for them
This runs forever: no context switch allowed, so
ready can never change
while (~ready)
count = count + 1;
Instead, use
wait(ready);
Simulation Behavior
Race conditions abound in Verilog
Sequential:
reg q;
always @(d or clk) q only assigned when
clk is 1
if (clk) q = d;
Register Inference
A common mistake is not completely specifying a
case statement
This implies a latch:
always @(a or b)
case ({a, b})
2’b00: f = 0; f is always assigned
2’b01: f = 1;
2’b10: f = 1;
default: f = 0;
endcase
Inferring Latches with Reset
Latches and Flip-flops often have reset inputs
Can be synchronous or asynchronous