Saturday, June 5, 2010

SystemVerilog Interview Questions

1.   How many array types in SystemVerilog? How do you use them?

      array_name[ ]  dynamic array

      e.g .   dyna_arr_1 = new[100] (dyna_arr_1);
               dyna_arr_2 = new[4]('{4,5,6}); // elements are {4,5,6,0}
     
      array [5]    fixed array
      e.g.   register1 [6][7:0] = `1;
 
      array[$]   queue
     e.g.  int q[$] = { 2, 4, 8 };
             q = {};                // clear the queue (delete all items)
             e = q[0];              //  read the first (leftmost) item
             e = q[$];              //  read the last (rightmost) item 

      array[string] or array[%] associate array
     e.g.  //associative array of 4-state integers indexed by strings, default is '1.
      integer tab [string] = '{"Peter":20, "Paul":22, "Mary":23, default:-1 };
  
2)  What is the Polymorphism?

    Polymorphism allows an entity to take a variety of representations. Polymorphism means the ability to request that the same Operations  be performed by a wide range of different types of things. Effectively, this means that you can ask many different objects to perform the same action.  Override polymorphism is an override of existing code. Subclasses of existing classes are given a "replacement method" for methods in the superclass. Superclass objects may also use the replacement methods when dealing with objects of the subtype. The replacement method that a subclass provides has exactly the same signature as the original method in the superclass. 

EXAMPLE: with virtual
      class A ;
      virtual  task disp ();
                $display(" This is class A ");
           endtask
      endclass
     
      class EA extends A ;
            task disp ();
                $display(" This is Extended class A ");
            endtask
      endclass
     
      program main ;
           EA my_ea;
           A my_a;
          
           initial
           begin
                my_a = new();
                my_a.disp();
               
                my_ea = new();
                my_a = my_ea;
                my_a.disp();
           end
      endprogram

RESULTS

 This is class A
 This is Extended class A


3)  how the copy works?


Answers:  
    There are 2 types of copy. Show copy or deep copy


    For example:


    class B; 
        int 
    endclass


   

     program main;
         initial
         begin
             B b1;
             B b2;
             b1 = new();
             b1.i = 123;
             b2 = b1;                   // b1 and b2 point to the same memory. The properties did not get copied.
             $display( b2.i );
         end
     endprogram
RESULTS:

123


 A shallow copy of an object copies all of the member field values.
  program main;
          initial
          begin
              B b1;
              B b2;
              b1 = new();
              b1.i = 123;
              b2 = new b1;    // shallow copy of b1
              b2.i = 321;
              $display( b1.i );
              $display( b2.i );
          end
     endprogram
    
RESULTS:

        123
        321
 
If the value of b1 change, it will also change the value of b1. It's because it's pointing to the same memory.


To avoid this, we need to use the deep copy.


Deep Copy

A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. To make a deep copy, you must write a copy constructor and overload the assignment operator, otherwise the copy will point to the original, with disasterous consequences.


EXAMPLE:
    class A;
        int i;
    endclass
   
    class B;
        A a;

        task copy(A a);
            this.a = new a;
        endtask

    endclass
   
    program main;
        initial
        begin
            B b1;
            B b2;
            b1 = new();
            b1.a = new();
            b1.a.i = 123;
            b2 = new b1;
            b2.copy(b1.a);
            $display( b1.a.i );
            $display( b2.a.i );
            b1.a.i = 321;
            $display( b1.a.i );
            $display( b2.a.i );
           
        end
    endprogram

RESULTS:

        123
        123
        321
        123



Wednesday, June 2, 2010

New interview questions

1) There is a waveform

    in _____|====|________
    out_____|=|___|=|______

    The output is "high" when the input change the value.

    Verilog code
   
    always@(posedge clk or reset)
   begin
      if(!reset)
      begin
        in_reg <= 1'b0;    // initial value
        out      <= 1'b0;
     end
    else
    begin
             if(in != in_reg)
             begin
                      out <= 1'b1;
                      in_reg <= in;
             end
            else
                     out <= 1'b0;
    end
   end

   After synthesis, what will it be look like?

   It's like a D-FF and a XOR

   in -----D_FF------in_reg---|XOR|  ___   out
         |__________________|        |

2)   How to write a C or C++ code for Strlen

Answer:
     
    int strlen (char *s)
     begin
          for (int len =0; *s='\0'; s++)
               len++;
          return (len);
     end
  
    Use recurve way

    int strlen_r (char *s)
    begin
             if(*s='\0')  return 0;
             else  return (1 + strlen_r(s+1));   
     end
   

   
  


    

Search This Blog