Friday, March 02, 2012

Procedural Statements & Control Flow - Examples

Procedural Statements & Control Flow - Examples

Example for Loop Statements

(a) Ethernet

(i) Example using do While

module Ethernet();
byte preamble = 0;
initial
begin
do
begin
$display ("Current value of preamble = %g", preamble);
preamble ++;
end
while (preamble < 8);
#1 $finish;
end
endmodule

Output in VCS

Current value of preamble = 0
Current value of preamble = 1
Current value of preamble = 2
Current value of preamble = 3
Current value of preamble = 4
Current value of preamble = 5
Current value of preamble = 6
Current value of preamble = 7

(ii) Example using foreach

//Example for foreach in Ethernet

module foreach_example ();
byte Source_address_value = 0;
byte Source_address[10];
int counter;
initial
begin
do
begin
$display ("Current value of Source Address index = %g", Source_address_value);
Source_address[counter] <= Source_address_value;
Source_address_value ++;
end
while (Source_address_value < 10);
foreach (Source_address[counter])
begin
$display ("Value of Source Address is %g",counter);
end
end
endmodule

Output in VCS

Current value of Source Address index = 0
Current value of Source Address index = 1
Current value of Source Address index = 2
Current value of Source Address index = 3
Current value of Source Address index = 4
Current value of Source Address index = 5
Current value of Source Address index = 6
Current value of Source Address index = 7
Current value of Source Address index = 8
Current value of Source Address index = 9
Value of Source Address is 0
Value of Source Address is 1
Value of Source Address is 2
Value of Source Address is 3
Value of Source Address is 4
Value of Source Address is 5
Value of Source Address is 6
Value of Source Address is 7
Value of Source Address is 8
Value of Source Address is 9

(iii) Example using Event

//Use of task, fork-join, event, class, program, define-is used here for Ethernet packet
//Within the 10 packets, if 4 errors are found, event will trigger

`define MAX_ERR_CNT 4 //define the Maximum Error Quantity

class Ethernet;
static event event_ex; // static event of class Eth
task waiting();
$display("\n********Before Event trigger, we will see the output...********");
@event_ex; //wait for event_ex triggered
$display("\n********After Event triggers, are we seeing the packet?********");
endtask
endclass

program main();
Ethernet obj_Ethernet; //create object of class
integer count = 0; // initialize the count value
task driver();
repeat(10)
#10 $display("\n@%0t Sending Packet... ",$time);
$display("\n@%0t Sending Packets Finished ",$time);
endtask
task monitor();
repeat(10)
begin
count =count+1;
#18 $display("@%0t, %0dth Error found ",$time,count);
if(count ==`MAX_ERR_CNT) // checking condition
->obj_Ethernet.event_ex;
end
endtask
initial
begin
obj_Ethernet = new();
fork
driver();
monitor();
report();
join
$display("No Active Events So Ending of Simulation ");
$finish();
end
task report();
fork
obj_Ethernet.waiting();
join
$display("\n********Maximum Errors Found : Ending Simulation.....******** \n\n");
$finish();
endtask
endprogram

Output in VCS

********Before Event trigger, we will see the output...********

@10 Sending Packet...
@18, 1th Error found

@20 Sending Packet...

@30 Sending Packet...
@36, 2th Error found

@40 Sending Packet...

@50 Sending Packet...
@54, 3th Error found

@60 Sending Packet...

@70 Sending Packet...
@72, 4th Error found

********After Event triggers, are we seeing the packet?********
********Maximum Errors Found : Ending Simulation.....********

(b) Sonet

(i) Example using do-while

module do_while_loop ();
byte count_sonet_spe_bytes =0;
initial
begin
do
begin
$display ("count sonet spe bytes:%d",count_sonet_spe_bytes);
count_sonet_spe_bytes ++;
end
while (count_sonet_spe_bytes==783);
#50 $finish;
end
endmodule

Output in VCS

count sonet spe bytes: 0

(ii) Example using For Loop

module Ethernet();
bit data_in ;
byte data;
bit frame;
int n;
//typedef integer 3C;
initial
begin
for (n=1;n<=3;n++)
begin
$display("No fixed data = %d",n);
end
if (n ==1)
begin
#5 $display ("2 fixed data are obtained = %g",n);
#5 $display ("Two packets are obtained = %d",n);
end
else if (n==4)
begin
#5$display("One packet in 3C is obtained = %d",n);
#7 $finish;
end
end
endmodule

Output in VCS

No fixed data =1
No fixed data = 2
No fixed data = 3
One packet in 3C is obtained = 4

(iii) Example using Foreach

//This example explains the foreach loop for SOH(Section Overhead) frame.

module for_each_example();
integer sonet_payload[10] = {0,2,3,7,8,11,56,32,63,22};
initial
begin
foreach (sonet_payload[i])
begin
$display ("Value of payload is %g.\n",sonet_payload[i]);
end
#1 $finish;
end
endmodule

Output in VCS

Value of payload is 0.
Value of payload is 2.
Value of payload is 3.
Value of payload is 7.
Value of payload is 8.
Value of payload is 11.
Value of payload is 56.
Value of payload is 32.
Value of payload is 63.
Value of payload is 22.

(iv) Example using Event

module event_emissiom;
event frame_start,pointer_arrival;
initial
begin
#16 ->frame_start;
#28 ->pointer_arrival;
#100 $finish;
end
always @ (frame_start)
$display("Event frame_start is triggered");
always@ (pointer_arrival)
$display("Event pointer_arrival is triggered");
endmodule

Output in VCS

Event frame_start is triggered
Event pointer_arrival is triggered

(v) Example using Sequence Event

//This example explains the sequence event by SPE- Synchronous Payload Envelope example.

module sequence_event ();
reg J0_sectiontrace, J1_pathtrace, B1_parity;
reg clk = 0;
sequence sonet_sequence;
@(posedge clk) J0_sectiontrace ##1 J1_pathtrace ##0 B1_parity ;
endsequence
initial
always @(posedge clk)
begin
@(J0_sectiontrace, J1_pathtrace, B1_parity)
$display ("\n@%g Check the asserted values..", $time);
end
// Testbench code
initial
begin
$monitor( "Time @%g, Clock = %b, J0 = %b, J1 = %b, B1 = %b", $time, clk, J0_sectiontrace,J1_pathtrace,B1_parity);
begin
#4 J0_sectiontrace = 1;
#2 J1_pathtrace = 1;
#2 B1_parity = 1;
#2 J0_sectiontrace = 0;
J1_pathtrace = 0;
B1_parity = 0;
end
#2 $finish;
end
always#1 clk = ~clk;
endmodule

Output in VCS

Time @0, Clock = 0, J0 = x, J1 = x, B1 = x
Time @1, Clock = 1, J0 = x, J1 = x, B1 = x
Time @2, Clock = 0, J0 = x, J1 = x, B1 = x
Time @3, Clock = 1, J0 = x, J1 = x, B1 = x

@4 Check the asserted values..
Time @4, Clock = 0, J0 = 1, J1 = x, B1 = x
Time @5, Clock = 1, J0 = 1, J1 = x, B1 = x

@6 Check the asserted values..
Time @6, Clock = 0, J0 = 1, J1 = 1, B1 = x
Time @7, Clock = 1, J0 = 1, J1 = 1, B1 = x

@8 Check the asserted values..
Time @8, Clock = 0, J0 = 1, J1 = 1, B1 = 1
Time @9, Clock = 1, J0 = 1, J1 = 1, B1 = 1

@10 Check the asserted values..
Time @10, Clock = 0, J0 = 0, J1 = 0, B1 = 0
Time @11, Clock = 1, J0 = 0, J1 = 0, B1 = 0

No comments:

Post a Comment

Popular Posts