Unit 4 - Features of Verilog HDL
Unit 4 - Features of Verilog HDL
PROGRAMMING IN HDL(SECA1605)
MENTORS
Dr.T.VINO
Mr.Muthiah
Department of ECE
Sathyabama Institute of Science and Technology
SYLLABUS
Course Outcome
After the completion of course student will be able to
Function Rules
Functions are more limited than tasks.
The following five rules govern their usage:
• A function definition cannot contain any time controlled statements—that is,
any statements introduced with #, @, or wait.
• Functions cannot enable tasks.
• A function definition must contain at least one input argument.
• A function definition must include an assignment of the function result value to
the internal variable that has the same name as the function.
• A function definition can’t contain an inout declaration or an output declaration
FEATURES IN VERILOG HDL
Function Declaration and Invocation
Declaration syntax:
function <range_or_type> <func_name>;
<input declaration(s)>
<variable_declaration(s)>
begin
<statements>
end
endfunction
Invocation syntax:
<func_name> (<argument(s)>);
Example
module simple_function();
function myfunction;
input a, b, c, d;
begin
myfunction = ((a+b) + (c-d));
end
endfunction
endmodule
FEATURES IN VERILOG HDL
SYSTEM TASKS AND FUNCTIONS
Verilog contains the pre-defined system tasks and functions, including tasks for
creating output from a simulation. All system tasks appear in the
form $.Operations such as displaying the screen, monitoring values of nets,
stopping and finishing are done by system tasks.
DISPLAY TASKS
$display
$monitor
$write
DUT Instantiation
<dut_module> <instancename>(.<dut_signal>(test_module_signal),…)
and_gate dut(.a(A), .b(B), .c(C));
module and_tb;
reg A,B;
wire C;
and_gate dut(.a(A), .b(B), .c(C));
initial
begin
#5 A =0; B=0;
#5 A =0; B=1;
#5 A =1; B=0;
#5 A =1; B=1;
end
end module
FEATURES IN VERILOG HDL
Testbench for D-flip flop
Let’s test the Verilog code for D-flip flop. Here’s the DUT:
module dff_behave(clk,rst,d,q,qbar);
input clk,rst,d;
output reg q,qbar;
always@(posedge clk)
begin
if (rst == 1)
q = 0;qbar = 1;
else
q = d;
qbar = ~d;
end
endmodule
FEATURES IN VERILOG HDL
Testbench for D-flip flop
module dff_tb;
reg CLK = 0;
reg D,RST;
wire Q,QBAR;
dff_behave dut(.clk(CLK), .rst(RST), .d(D), .q(Q), .qbar(QBAR));
always #10 CLK = ~CLK;
initial
begin
RST = 1;
#10 RST = 0;
#10 D = 0;
#20 D = 1
end
endmodule
FEATURES IN VERILOG HDL
Test Bench for Half Adder
module half_adder_verilog_tb;
reg a, b;
wire s, c;
halfadder8 dut (.a(a), .b(b), .s(s), .c(c));
initial
begin
a = 0;b = 0;
#50;a = 0;b = 1;
#50;a = 1;b = 0;
#50;a = 1;b = 1;
end
endmodule
FEATURES IN VERILOG HDL
Timing and Delays in Verilog
The concepts of timing and delays within circuit simulations are very important because
they allow a degree of realism to be incorporated into the modelling process
Delays
Delays can be modelled in a variety of ways, depending on the overall design approach
that has been adopted. namely gate-level modelling, dataflow modelling and behavioural
modelling.
Turn-off delay
The turn-off delay is associated with a gate output transition to the high impedance value
(z) from another value.
Example Program
module multiplexor_2_to_1(out, cnt, a, b);
output out;
input cnt, a, b;
wire not_cnt, a0_out, a1_out;
not # 2 n0(not_cnt, cnt); /* Rise=2, Fall=2, Turn-Off=2 */
and #(2,3) a0(a0_out, a, not_cnt); /* Rise=2, Fall=3, Turn-Off=2 */
and #(2,3) a1(a1_out, b, cnt);
or #(3,2) o0(out, a0_out, a1_out); /* Rise=3, Fall=2, Turn-Off=2 */
endmodule
FEATURES IN VERILOG HDL
Dataflow modelling
Net Declaration Delay
The delay to be attributed to a net can be associated when the net is declared.
e.g.
wire #10 out;
assign out = in1 & in2;
Timing controls
Timing controls provide a way to specify the simulation time at which procedural
statements will execute.
There are three methods of timing control:
Delay based timing control
Event based timing control
Level-sensitive timing control
FEATURES IN VERILOG HDL
Delay-based timing control
There are three types of delay control for procedural assignments
Regular delay control
Intra-assignment delay control
Zero delay control
Example:
module clk_gen;
reg clk, reset;
clk = 0;
reset = 0;
#2 reset = 1;
#5 reset = 0;
#10 $finish;
endmodule
FEATURES IN VERILOG HDL
Intra-assignment delay control
Instead of specifying delay control to the left of the assignment, it is possible
to assign a delay to the right of the assignment operator
Example:
module intra_assign;
reg a, b;
a = 1;
b = 0;
a = #10 0;
b = a;
endmodule
Example:
module edge_wait_example();
reg enable, clk, trigger;
always @ (posedge enable)
begin
trigger = 0;
// Wait for 5 clock cycles
repeat (5)
begin
@ (posedge clk) ;
end
trigger = 1;
end
endmodule
FEATURES IN VERILOG HDL
Named event control
Verilog provides the capability to declare an event and then trigger and recognize the
occurrence of that event. A named event is declared by the keyword event. The
triggering of the event is recognized by the symbol @
Event OR control
The list of events or signals expressed as an OR is also known as a sensitivity list. The
keyword or is used to specify multiple triggers
Example:
always @(reset or clock or d)
At the switch level, transistors behave as on-off switches - Verilog uses a 4 value logic
value system, so Verilog switch input and output signals can take any of the four
0, 1, Z, and X logic values.
MOS switches
FEATURES IN VERILOG HDL
NMOS and PMOS Switches
Since switches are Verilog primitives, like logic gates, the name of the instance is
optional. Therefore, it is acceptable to instantiate a switch without assigning an instance
name.
nmos (out, data , control); // instantiate nmos switch ; no instance name
pmos (out, data, control); // instantiate pmos switch; no instance name
FEATURES IN VERILOG HDL
CMOS Switches
Since a cmos switch is derived from nmos and pmos switches, it is possible derive the
output value from Table, given values of data, ncontrol, and pcontrol signals.
FEATURES IN VERILOG HDL
Bidirectional Switches
Three keywords are used to define bidirectional switches: tran, tranif0, and tranifl.
The tran switch acts as a buffer between the two signals inoutl1and inout2.
Either inout1 or inout2 can be the driver signal.
The tranif0 switch connects the two signals inoutl1 and inout2 only if the control signal is
logical 0.
If the control signal is a logical 1, the non driver signal gets a high impedance value z.
The driver signal retains value from its driver.
ROM RAM
ROM
module rom(addr, data_out);
input [2:0] addr;
output [7:0] data_out;
reg [7:0] data_out;
reg [7:0]mem [0:7];
initial
begin
mem[0]=8'b00000000;
mem[1]=8'b00000010;
mem[2]=8'b00000100;
mem[3]=8'b00001000;
mem[4]=8'b00010000;
mem[5]=8'b00100000;
mem[6]=8'b01000000;
mem[7]=8'b10000000;
end
always@(addr) begin
data_out=mem[addr];
end
endmodule
RAM
module ram(clk, wr_en, data_in, addr, data_out);
input clk;
input wr_en;
input [7:0] data_in;
input [3:0] addr;
output [7:0] data_out;
reg [7:0] data_out;
reg [7:0]mem [0:15];
always@(posedge(clk),wr_en,data_in,addr)
if(clk)
begin
if(wr_en)
mem[addr]=data_in;
else
data_out=mem[addr];
end
endmodule