Showing posts with label task. Show all posts
Showing posts with label task. Show all posts

Sunday, July 29, 2018

Semaphore

Requirement:
While developing testbench, it is required to process the data between to separate entity. In Verilog provides the option of "event" but, that is a static object. Verilog language does not have the ability to create the dynamic event. Semaphore is the built-in class to use for synchronization.

Application:
Consider two threads are ongoing parallel. Now need to control this two thread. While the First thread is ongoing second thread need to wait to complete the operation of the Fist thread. Similarly, when the second thread is ongoing, First thread need to wait to complete the process for the Second thread.

Analogy:

Consider in the cafeteria where a limited number of chairs are available. Need to manage the arrangement of the cafeteria. The only Premium customer can use the cafeteria. So, every time when the customer enters the cafeteria key is provided to it. Similarly, while leaving the customer drop the key in the box.When there is no keys are available means all the chairs are occupied.
No place to sit in the cafeteria.












Similarly, the semaphore is like a cafeteria, the process is like the customer and key is like the chair. While constructing the semaphore user will decide the size of the semaphore means the number of chairs. When process start simulation semaphore provides the key to it. Once all the keys are occupied means semaphore is full. Once process will complete it drop the key indicate space is again created in semaphore.







USER WANT CODE:

program semaphore_example();
semaphore cafeteria;

task provide_chair(int addr, int data);

  $display("Taking FOOD addr %d", addr);
  #10;// do some operation
endtask

int done = 0, i = 0;

initial
 begin
  // here we initialize the semaphore with one token
  cafeteria = new(1);
  fork
  begin
   // here is one parallel thread competing
    while(!done)
    begin
      // wait for some random time
      $display("First customer is waiting to get the chair");
      #($random() % 100);
      // attempt to get the one semaphore token
      cafeteria.get(1);
      // got it!, lets use the bus
      $display("First customer got the chair");
      provide_chair(200, $random());
      i++;
      // we must return the semaphore token
      cafeteria.put(1); 
      $display("First customer drop the chair"); 
    end
  end
  begin
    // here is second parallel thread competing
    while(!done)
    begin
      // wait for some random time
      $display("Second customer is waiting to get the chair");
      #($random() % 100);
      // attempt to get the one semaphore token
      cafeteria.get(1);
      $display("Second customer got the chair");   
      provide_chair(200, $random());
      i++;
      // we must return the semaphore token
      cafeteria.put(1);
      $display("Second customer drop the chair");         
    end
  end

  // here is a another parallel thread

  // this is to decide close the cafeteria
  begin
    while(!done)
    begin
      @(i);
      if (i > 10)
      done = 1;
    end
  end
  join
  $display ("done -- out\n");
 end
endprogram

To run simulation click on below link.
Link: https://www.edaplayground.com/x/5nkS

Saturday, June 23, 2018

Major advantage of function over task

The function can return the value and task cannot return the value.

What is the return value?
The user called the method, it will execute lines of code.
On completion of method definition method return some value.

How can the function return value?
The return type should be mention in the function definition. Need to use of return keyword.

What is advantage/application to return value?
Many times a user needs to use the same condition multiple times.

Can function return 'x' or 'z'?
- Yes, function can return 'x' or 'z', need to use logic return type in function.

What is preferable task or function?
- Depends on the application user will decide to use task or function.


USER WANT CODE:

function logic hello(bit x);
$display("This is Hello function");
if(x == 1)
begin
  return 1'hz; // return 'z' or 'x' state
end
endfunction


task Hi();
//##1;
int a;
a = 1;
$display("This is Hi task");
hello(a);
endtask


module top();
logic a;
initial
begin
Hi();
a = 1;
if(hello(a) === 1'hz) // '===' require for z or x comparison
begin
  $display("I am good");
end
else
begin
  $display("Good bye");
end
end

endmodule




Saturday, May 19, 2018

Difference between task and function

To reuse the same lines of code again and again methods are used. System Verilog supports two types of methods.
(1) Function
(2) Task

Function:
- Delayed can NOT use in the function.
- Task WITHOUT delay can call in the function. It shouts only WARNING, not an ERROR.
- The function calls another function.
- The function can return value.

Task:
- Delayed can be inserted in the task.
- The task with DELAY can be called in the task.
- The function also can call in the task.
- The task can NOT return value.

The user can NOT call a task in function. Is it true? Is it shouts an ERROR?
NOT ALWAYS. The user can call a task in function if the definition of a task should NOT contain delay.

WITHOUT delay Warning: "warning-[teif] Task enabled inside a function"

WITH delay Error: "Function "function_name" has illegal use of delay or synchronization"


USER WANT CODE:


function hello();
  $display("This is Hello function");
  Hi(); // CALL TASK(without delay) INSIDE FUNCTION
endfunction

task Hi();
  //#1;  // without delay
  $display("This is Hi task");
  hello();
endtask

module top();
initial
   begin
     hello(); // Call function
     //Hi(); // call task
   end
endmodule

To run simulation click on below link:
Link: https://www.edaplayground.com/x/2uZ8