Sunday 25 August 2013

Cache Memory

Cache is a small, high speed memory which holds the most recently accessd pieces of main memory. SRAM (Static Ram) is used as cache sice they are faster.

Why is cache necessary???
Processors are getting better and their performances are very high. Today the time taken by a processor to execute an instruction is lesser than the time taken to fetch an instruction from the main memory.ypical access time for a DRAM access is around 60ns. A 100MHz processor just takes 10ns to execute an instruction. Thus here there is a bottleneck created and the efficiency of the processor would be wasted if cache and instruction pipelining are not used. Typical access time for an SRAM is 15ns.

If this is the case, why not replace the entire main memory with SRAM??. We cannot use SRAM as the main memory due to the following reasons.
  • SRAM is many times costlier compared to DRAM.
  • SRAM consumes more power.
  • SRAM is much denser than DRAM i.e the space occupied by SRAM is greater than the space that would be occupied by DRAM of the same capacity. A 1MB SRAM would be physically biggeroccupies more space) than a 1MB DRAM.
Cache terminologies
  1. Cache Hit: If cache contains the information required by the CPU, it is a cache hit. The access cycle ends with a cache hit and the cache responds to the CPU request.
  2. Cache miss: When cache does not contain the information required by the CPU, it is a cache miss. The information has to be fetched from the main memory and the cache would update itself during the main memory access.
  3. Cache Consistency: Since cache is a local copy of a small portion of the main memory, it is important that it reflects the actuall contents of the memory. Cache consistency is maintained by two processes,
    • Snooping: Cache is watching the address lines for transactions. This helps in seeing if the information requested by the CPU is available with cache or not.
    • Snarfing: Cacshe is watching the data lines and takes the information on it. Cache is kept updated this way.
    4.  Dirty Data: Data is written in the cache but not updated in the main memory.
    5.  Stale Data: Data is modified in the main memory and not updated in the cache.

Cache Architecture:
Cache architecture has 2 aspects,
1. Read Architecture: The Read Architecture may either be look aside or look through.
2. Write Policy: The Write Policy might be Write Back or Write Through.
Both Read Architectures can have any of the two types of Write Policies.

Saturday 17 August 2013

System Verilog Tasks and Functions


System Verilog Tasks and Functions enable us to operate  the class variables. Tasks and functions are declared inside classes in other words, the tasks and functions associated with a class must be declared inside it. However the task/function defination may be outside the class( Extern Tasks/Functions).

System Verilog tasks and functions are very much similar to their verilog counterparts. Further, System Verilog makes them more user friendly. The enhancements provided by SV are,

  • System verilog tasks and functions don't need a begin- end.


                Ex:           task display( Argument List)
                                 ----
                                 ----
                                 Body of the task
                                 ----
                                endtask
  • SV functions can have output and inout arguments.
  • Pass by reference.

Let us now look at the differences between tasks and functions.
  • Tasks might have time consuming statements but functions cannot. Functions have to end at zero time.
  • A Task can have any number of tasks and functions called in its body. A function can have any number of functions called in its body and not tasks.
  • Tasks cannot have a return value. However it might have an output argument. Functions may return any value or void (Nothing returned).
The syntax/structure of tasks and functions are as follows.
                             class example;
                             int x,y,z;

                                 task sum(input int a,b , output int c);
                                      c = a+b;
                                 endtask
   
                                 function int diff(input int a,b); // the word "int"refers to return type
                                      return(a - b);
                                 endfunction
                             endclass

The variables inside the task sum and function diff are local to their respective methods.

Both Tasks and Functions might have input, output and inout arguements. Default arguement type when nothing is specified is "input".

Ex:                      function int diff(int a,b);
                               return(a - b);
                          endfunction
Though no direction is specified beside the arguments, they will be considered inputs.

The variable passed as the output argument during the function call will be updated at the end of the function/task call.

If you have observed, we had specified the datatype of the argument along with its direction. This type can be any of the SV datatypes.The same applies for the return type as well.

                           function bit[7:0] diff (bit[7:0] a,b);  
                               return(a - b);
                           endfunction

                          function logic [15:0] my_funcn (logic [15:0] a,b);
                               return (b-a+1);
                          endfunction
Further, the default argument type when nothing is speified is "logic" i.e if there is an input argument or a return value specified with no datatype, they default to "logic" type.

Ex:                    function [15:0] [3:0] my_function (input [3:0] [7:0] a , b[3:0]);
                           ---
                           task body
                           ---
                        endfunction

In the above example, the return type is logic[15:0][3:0] and input arguments default to logic as well.

Moving on to the ways in which arguments can be passed to the functions and tasks, there are 4 ways,
1. Pass by Value: Value is passed to the function/task. When arguments are passed this way, a copy of the argument reaches the function/task. Any modification done to the value of the argument done by the caller or the method is local to the entity making the modification and doesn;t reflect at the other.

                             function int calc_crc( bit[7:0] packet [1000:1] );
                                 for( int i = 1; i <= 1000; i++ )
                                     crc ^= packet[j];
                             endfunction

2. Pass by Reference: Reference to the argument is passed to the function/task. Modifications done to the arguments by caller reflects in the function/task and vice-versa.
      
                             function int calc_crc( ref bit[7:0] packet [1000:1] );
                                 for( int i = 1; i <= 1000; i++ )
                                     crc ^= packet[j];
                             endfunction
3. Default Arguments: Default values can be assigned to to the arguments during the function declaration and definition. These values are taken to be the argument values when no arguments are passed during task/function call.

Ex:               task ex_task( int a = 10, b = 20, c = 30);
                         task_implementation
                   endtask

The above task can be called in any of the following ways,
1)  ex_task(11,22,33); // This call passes 11,22,33 as the arguments a,b,c respectively
2) ex_task(11,,33)// This call passes a = 11 , b = 20 (Default Argument Value) and c = 33
3) ex_task(11)// a = 11, b and c have default values
4) ex_task( , , 33)// c has the value 33 and a,b have default values.

Return Values of functions: Functions can return values. They can return values in the following ways,
1. By assigning values to the function name.
Ex:                    function int sum(int a,b);
                            sum = a+b;
                         endfunction
2. Use "return" keyword.
Ex:                   function int sum(int a,b);
                          return(a+b);
                        endfunction

If a function doesn't return any valuethe return type must be specified to be "void".
Ex:                   function void no_ret_ex(input x,y,z);
                             //function body
                        endfunction
Using functions in place of non-time consuming tasks is always recommended.

Going ahead, let us look at extern tasks and functions. System Verilog allows us to declare tasks/functions inside classes as extern tasks/functions and define the tasks outside (may as well be in a different file). Scope resolution operator is to be used while defining the extern tasks and functions. Further, if the tasks/functions are being defined in a different file, include the file in which the class has been defined in the file in which tasks/functions are being defined.
Ex: 
                                        class driver;
                                          int a,b;
                                          bit[31:0] data, address;
      
                                          extern task drive_to_dut(trans tr);
                                          extern function void ex_func( int a,b);
                                       endclass

                                       task driver::drive_to_dut(trans tr);
                                         ...
                                         ...
                                         //task body
                                       endtask

                                       function void driver::ex_funcn(int a,b);
                                         ...
                                         ...
                                         //function body
                                       endfunction