Verilog Prep Material
Verilog Prep Material
Syntax:
NB: data_change, reference and reference1 must be declared wires.
$setup(data_change, reference, time_limit);
data_change: signal that is checked against the reference
reference: signal used as reference
time_limit: minimum time required between the two events.
Violation if: Treference - Tdata_change < time_limit.
$width(reference1, time_limit);
reference1: first transition of signal
time_limit: minimum time required between transition1 and transition2.
Violation if: Treference2 - Treference1 < time
Example:
module d_type(q, clk, d);
output q;
input clk, d;
reg
q;
%b
specify
$setup(d2, posedge clk2, 2);
$hold(posedge clk2, d2, 2);
$width(negedge d2, 2);
endspecify
endmodule // stimulus
Output:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
clock
x
0
0
1
1
1
0
0
0
1
1
1
0
0
0
1
d
x
1
1
1
1
1
1
0
0
0
0
0
0
0
1
1
q
x
x
x
x
1
1
1
1
1
1
0
0
0
0
0
0
1
1
0
0
0
1
1
1
0
0
0
0
0
1
1
1
1
1
1
1
0
1
0
0
1 0
0 0
1 0
Priority Encoder:
In this device each input has a priority level associated with it. The encoder outputs indicate the active input that has
the highest priority. When an input with a high priority is asserted, the other inputs with lower priority are ignored.
Truth table:
W3
0
0
0
0
1
W2
0
0
0
1
X
W1
0
0
1
X
X
W0
0
1
X
X
X
Y1
X
0
0
1
1
Y0
X
0
1
0
1
Z
0
1
1
1
1
W3 to W0 are the inputs and Y1, Y0 are the outputs, Z is set to 1 when at least one of the inputs is set to 1.
For the first case Y1, Y0 assumes X values.
Verilog code:
Module priority(W, Y, Z);
Input [3:0]W;
Output reg[1:0]Y;
Output reg Z;
always @(W)
begin
Z=1;
Casex(W)
4b1xxx: Y=3;
4b01xx: Y=2;
4b001x: Y=1;
4b0001: Y=0;
Default: begin
Z=0; Y=2bx;
end
endcase
end
endmodule
Verilog Function:
A function is declared by the keyword function and it comprises a block of statements that ends with the keyword
endfunction. The function must have atleast one input and it returns a single value that is placed where the function
is invoked.
A Verilog function can invoke another function but it cannot call a Verilog task.
A task may call another task and it may invoke a function.
Example:
16:1 Mux:module mux16to1(w, s16, f);
input [0:15]w;
input [3:0]s16;
output reg f;
function mux4to1;
input [0:3]x;
input [1:0]s4;
case (s4)
0: mux4to1= x[0];
1: mux4to1= x[1];
2: mux4to1= x[2];
3: mux4to1= x[3];
endcase
endfunction
always @(w, s16)
case (s16[3:2])
0: f=mux4to1(w[0:3], s16[1:0]);
1: f=mux4to1(w[4:7], s16[1:0]);
2: f=mux4to1(w[8:11], s16[1:0]);
3: f=mux4to1(w[12:15], s16[1:0]);
endcase
endmodule