Monday, March 3, 2014

C++ OOP Knowledge

1. 类(Class)与 对象(Object)的关系 就是具体(Concrete)与(Abstract)的关系

2. 构造函数(constructor)

     构造函数的函数名和相应的类名相同。构造函数没有任何返回值,也不加void; 一个类可以不包含构造函数,此时当类的对象被定义时,编译器会自动定义一个默认构造函数; 构造函数必须要保证一旦一个对象被构造,类中的所有数据成员都有合理的定义好的值。
     Constructors are recognized by their names which is equal to the class name. Constructors do not specify return type, not even void.
     It is possible to define a class lacking any constructors. In that case the compiler defines a default constructor that is called when an object of that class is defined.
     Constructors must ensure that all the class's data members have sensible or as-least well-defined values once the object has been construct.

     1)默认构造函数(default constructor);Default constructor has no argument.
     2)构造函数可以不带任何参数;
     3)构造函数可以带有默认参数,参数的个数可以任意;
     4)构造函数的重载(overload of constructor):一个类有多个不同参数形式及参数类型不同的构造函数;
     Example: Class point;
                        构造函数: point();
                                          point(double x, double y);

     5)拷贝构造函数 (Copy Constructor):作用是用一个已经存在的对象来初始化该类的对象;用户可以根据需要定义拷贝构造函数,也可以由系统生成一个默认拷贝构造函数.

     A copy constructor is a special constructor for a class/struct that is used to make a copy of an existing instance.        According to the C++ standard, the copy constructor for MyClass must have one of the following signatures:
    MyClass( const MyClass& other );
    MyClass( MyClass& other );
    MyClass( volatile const MyClass& other );
  MyClass( volatile MyClass& other );    Myclass p1;
    Myclass p2(p1); //copy constructor is called;
    

    Copy Constructor is called in following three ways:
    1) Using an object of the class to instantiate another object;
    2) The formal parameter of a function is an object of a class. The copy constructor is called when the formal parameter and the actual parameter are combined;
    3) The return value of a function is an object of a class. The copy constructor is called when the function is completed and the return the caller.
     6)默认拷贝构造函数(Default Copy Constructor):当用一个已经存在的对象初始化本类的新对象时,如果没有自定义拷贝构造函数,则系统会自动生成一个默认的拷贝构造函数来完成初始化的工作。
          When using an existing object to instantiate a new object, if there is no self-defined copy constructor, the system will automatically generate a default copy constructor to complete the instantiation. 



3. 析构函数
    也是一种特殊的成员函数,也被声明为共有成员。析构函数的作用是释放分配给对象的内存空间。 The function of the destructor is to release the memory space that is allocated to objects.
    Destructor has no parameters, no return value, can not be reloaded. There can only be one destructor.


4.  友元(Friend)
    1)友元函数(Friend Function)
             友元函数不是当前类的成员函数,而是独立于当前类的外部函数;它可以是普通函数或其他类的成员函数。            友元函数使用前必须要在类定义时声明,声明是在其函数名前加上关键字friend。友元函数的定义一般在外部进行,不需要在其名字前加class::. 友元函数不是类的成员,不能直接引用对象成员的名字,也不能通过this指针引用对象成员,必须通过作为入口参数传递进来的对象名或对象指针来引用该对象的成员。       

 class point{                friend double dist(point &p1, point &p2);}
 double dist(point &p1, point &p2){}
        当一个函数需要访问多个类时,应该把这个函数同时定义为这些类的友元函数。    2)友元成员 (friend member function)
            当一个类的成员函数是另一个类的友元函数,则称这个成员函数为友元成员。通过友元成员函数,不仅可以访问自己所在类对象中的私有和公有成员,还可以访问由关键字friend 声明语句所在的类对象中的私有和公有成员。    3)友元类(Friend Class)
            当一个类作为另一个类的友元时,称这个类为友元类。这个类的所有成员函数都成为另一个类的友元函数。 因此,友元类中的所有成员函数都可以通过对象名直接访问另一个类中的私有成员。友元类的声明可以放在类声明中的任何位置。



5. 继承与派生(Inheritance and Derivation)
Base\继承方式      Public                  Private                  Protected    

Private                  private                 不可访问               不可访问    
Public                   public                     private                  protected     
Protected              protected                 private                  protected      
      1) 派生函数的构造函数            派生类的数据成员由 所有基类的数据成员,派生类新增的数据成员共同组成,如果派生类新增成员中还有对象成员,那派生类的数据成员中还间接含有这些对象的数据成员。因为对派生类对象初始化,就要对基类数据成员,新增数据成员和对象成员的数据进行初始化。           

 <派生类名>::<派生类名> (参数总表):基类名(参数表),对象成员名1(参数表1),..., 对象成员名n(参数表n){                        //派生类新增成员的初始化语句            
      2)派生函数的析构函数
            析构函数不能被继承,如果需要,则在派生类中重新定义。      

      3)构造和析构的顺序
            构造:先base再derivation;            析构:先derivation再base;



6. 多重继承(Multiple Inheritance)
      1)二义性问题(ambiguity)
            当多重继承(有多个基类)以及多个基类中有同名成员时,会产生二义性问题            解决方法:                 

 1)使用作用域运算符“::” , 指定相应的基类                  
2)使用同名覆盖额原则在派生类中重新定义与基类中同名的成员(如果是成员函数,则参数表也要相同,参数不同的情况为重载)以隐蔽掉基类的同名成员,在引用这些同名的成员时,就是使用派生类中的函数。                  
3) 使用虚函数,具体方法见“7.虚基类”的解释。


7. 虚基类(Virtual Base Class)
      虚基类引入的作用:解决基类中由于同名成员的问题而产生的二义性 
      1)base1继承了base,base2继承了base,那就可以把base定义为虚基类,继承时应该这样写      

class base1: virtual public base{
      }      

class base2: virtual public base{
      }



8. 多态(Polymorphism)
      多态就是同一个函数名称,作用在不同的对象上将产生不同的操作。就是“一个接口,多种实现”,就是同一种事物表现出的多种形态。
      多态包括:重载多态,强制多态,包含多态,参数多态
      包含多态:定义于不同类中的同名成员函数的多态行为,主要通过虚函数来实现;


      多态的实现方式: 1)编译时的多态;2)运行时的多态
       1)编译时的多态/静态多态性, 由静态联编支持的,用函数重载和运算符重载来实现;
       2)运行时的多态/动态多态性,由动态联编支持的, 用继承和虚函数来实现;

       3) 虚函数的定义在基类中进行 在原先定义的函数名和返回type前加 virtual
 
              重载虚函数: 要求函数名、返回类型、参数个数、参数的类型和参数的顺序必须与基类中的虚函数的原型完全相同;
              析构函数也可以声明成虚函数,只要在“~”前加上virtual;
              一个虚函数无论被继承多少次,仍保持其虚函数的特性,与继承次数无关

9. 抽象类(Abstract Class)多态的一种
       抽象类为类提供统一的操作界面,简历抽象类的原因就是为了通过抽象类多态使用其中的成员函数,抽象类是带有纯虚函数的类。
     1)纯虚函数(Pure Virtual Function)
            在积累中不能为虚函数给出一个有意义的实现时,可以将其声明为纯虚函数。 纯虚函数的具体实现可以留给派生类完成。纯虚函数的作用是为派生类提供一个一致的接口(A consistent interface)。
            纯虚函数: virtual void set()= 0
      2) 抽象类
            带有纯虚函数的类称为抽象类,抽象类不能定义对象。
            实际中,为了强调一个类是抽象类,可以将该类的构造函数说明为保护的访问控制权限

Thursday, February 27, 2014

NAND, Gates, Mux

Mux related:

                       Using Mux to design gates






















Using NAND gates to design other gates






Setup time, Hold time, clk skew, clk jitter, Metastability (Updating)

One method of clk generation


1) Setup time

2) Hold time

Device Switching is fastest at low temperatures.
The timing analysis are done at extreme corners to ensure that they meet the timing specs.
a. Fastest case: Fast N and Fast P transistors, High Vdd, Low Temperature
b. Slowest Case: Slow N and Slow P transistors, low Vdd, High Temperature

This isn't necessarily true in all cases. In some process technologies there is a phenomenon called temperature inversion.
3) Clock Skew
Skew is the difference in arrival of clock at two consecutive pins of a sequential element is called skew. Clock skew is the variation at arrival time of clock at destination points in the clock network. The difference in the arrival of clock signal at the clock pin of different flops.

If capture clock comes late than launch clock then it is called positive skew. When data and clock are routed in same direction then it is Positive skew.


Positive skew can lead to hold violation.
Positive skew improves setup time.

4) Clock Jitter
Introduction
Jitter is the timing variations of a set of signal edges from their ideal values. Jitters in clock
signals are typically caused by noise or other disturbances in the system. Contributing factors
include thermal noise, power supply variations, loading conditions, device noise, and
interference coupled from nearby circuits.

Types of Jitter
Jitter can be measured in a number of ways; the following are the major types of jitter:
• Period Jitter
• Cycle to Cycle Period Jitter
• Long Term Jitter
• Phase Jitter
• Time Interval Error (TIE)

5) Comparison of Clock skew and Clock jitter

Clock skewThe deterministic (knowable) difference in clock arrival times at each flip-flop
Caused mainly  by imperfect balancing of clock tree/mesh
Can be deliberately introduced using delay blocks in order  to time-borrow
Accounted for in STA by calculating the clock arrival times at each flip-flop
Clock jitterThe random (unknowable, except distribution s) difference in clock arrival times at each flip-flop Caused by on-die process, Vdd, temperature variation, PLL jitter, crosstalk, Static timing analysis (STA) accuracy, layout parameter extraction (LPE) accuracyAccounted for in STA by subtracting (~3 s) from the cycle time in long path analysis, and adding to receiving clock arrival time in race analysis
Jitter is always bad, skew can be helpful or harmful.
Clock uncertainty D º skew ± jitter

6) Clock gating
Gated clocks
Clock signals that are passed through some gate other than buffer and inverters are called gated clocks. These clock signals will be under the control of gated logic. Clock gating is used to turn off clock to some sections of design to save power. Click here to read more about clock gating.
Click here for more details

7) Hazard

Wednesday, February 26, 2014

Some VLSI questions and anwsers

1)Why NMOS technology is preferred more than PMOS technology?
N- channel transistors has greater switching speed when compared to PMOS transistors.

2)What is Channel-length modulation?
The current between drain and source terminals is constant and independent of the applied voltage over the terminals. This is not entirely correct. The effective length of the conductive channel is actually modulated by the applied VDS, increasing VDS causes the depletion region at the drain junction to grow, reducing the length of the effective channel. Therefore I_D' = I_D *(1+LAMBDA*V_DS); Lambda is the channel length modulation and is generally proportional to the inverse of the channel length. Channel length modulation is more pronounced in short channel devices. Also, Short channel devices are prone to velocity saturation.Velocity saturation occurs when the horizontal component of the E-field (along the channel) reaches a critical value. At this point the carriers collide.
3)What is Latch – up?
Latch up is a condition in which the parasitic components give rise to the establishment of low resistance conducting paths between VDD and VSS with disastrous results. Careful control during fabrication is necessary to avoid this problem.
4) How do you prevent latch up problem?
Latch up problem can be reduced by reducing the gain of parasitic transistors and resistors. It can be prevented in 2 ways
• Latch up resistant CMOS program
• Layout technique
The various lay out techniques are
Internal latch up prevention technique
I/O latch up prevention technique.
5)What is Stick Diagram? It is used to convey information through the use of color code. Also it is the cartoon of a chip layout.
What are the uses of Stick diagram? It can be drawn much easier and faster than a complex layout. These are especially important tools for layout built from large cells.
Give the various color coding used in stick diagram? Green – n-diffusion;Red- polysilicon;Blue –metal;Yellow- implant;Black-contact areas.
6)Define Threshold voltage in CMOS?
The Threshold voltage, VT for a MOS transistor can be defined as the voltage applied between the gate and the source of the MOS transistor below which the drain to source current, IDS effectively drops to zero.
7) What is Body effect?
The threshold volatge VT is not a constant w. r. to the voltage difference between the substrate and the source of MOS transistor. This effect is called substrate-bias effect or body effect.
8) Compare nMOS and pMOS devices
                                   nMOS                                                                       pMOS
A section of p-type separating 2 n-type silicon          A section of n-type separating 2 p type silicon
Turns ON with the gate at logic 1                               Turns ON with the gate at logic 0
Majority carriers are electrons                                    Majority carriers are holes
Good transmission of logic 0                                       Good transmission of logic 1
9) What is meant by a transmission gate?
A transmission gate consists of an n-channel transistor and p-channel transistor with separate gates and common source and drain. 
10) Define Rise time and fall time
Rise time, tr is the time taken for a waveform to rise from 10% to 90% of its steady-state value.
Fall time, tf is the time taken for a waveform to fall from 90% to 10% of its steady-state value.
11) Define Delay time
Delay time, td is the time difference between input transition (50%) and the 50% output
level. This is the time taken for a logic transition to pass from input to output.
12) Define Noise Margin
The parameter which gives the quantitative measure of how stable the inputs are with respect to coupled electromagnetic signal interference.
NML = Vil-Vol
NMH = Voh-Vih
13) What is a task in verilog, AND difference between task and funtion?
A task is like a procedure, it provides the ability to execute common pieces of code from several different places in a description;
Functions always return a single value. with in a function, no event, delay or timing control statements are permitted, at least one input argument and they cannot have output or inout arguments. Functions is unable to enable a task however it can enable other functions. Tasks don’t return a value, but can pass multiple values through output and inout arguments.Tasks can enable a function as well as other versions of tasks.

14) Mention few data types in Verilog
Nets, registers, vectors, numbers and arrays
15) Mention the four key words used for looping in verilog
while, for, repeat, forever
16) Specify the operator which have highest and lowest precedence.
Unary operator – highest precedence
Conditional operator – lowest precedence
17) Give the examples  for Procedural statement and control flow.
Loop statement , Wait statement, Conditional statement, Case statement
18) Blocking and non-blocking statements differ in executing the statements . How?
They are two types of procedural assignments. Blocking statements are executed in the order in which they are specified in a sequential block. “= “is the operator used to specify blocking assignments. Blocking assignment executes sequentially and usually use Blocking statement for combinational logic. evaluation and assignment are immediately.

Non blocking statements allow scheduling of assignments without blocking execution of the statement that follow in a sequential block. “<=” is the operator used to specify non blocking assignment. For non-blocking assignment, the right hand side statement is evaluated and stored first then been assigned to the left hand side. assignment is postponed until rhs evaluations are done.
Using two ways to swap the content of two registers:
always@(posedge clk) begin                                     always@(posedge clk) begin
    temp=b;                                                                       a<=b;                  
    b = a;                                                                            b<=a;    
    a = temp;                                                              end
end
19) What are the various modeling used in Verilog?
1. Gate-level modeling
2. Data-flow modeling
3. Switch-level modeling
4. Behavioral modeling
20) What is the structural gate-level modeling?
Structural modeling describes a digital logic networks in terms of the components that make up the system. Gate-level modeling is based on using primitive logic gates and specifying how they are wired together.
21) What is Switch-level modeling?
Verilog allows switch-level modeling that is based on the behavior of MOSFETs.Digital circuits at the MOS-transistor level are described using the MOSFET switches.
22) Give the two blocks in behavioral modeling.
1. An initial block executes once in the simulation and is used to set up
initial conditions and step-by-step data flow
2. An always block executes in a loop and repeats during the simulation.
23) Value levels Condition in hardware circuits
Verilog supports four levels for the values needed to describe hardware referred to as value sets.
0 Logic zero, false condition
1 Logic one, true condition
X Unknown logic value
Z High impedance, floating state
24) Different bitwise operators. Operator symbol Operation performed Number of operands
~ Bitwise negation One
& Bitwise and Two
| Bitwise or Two
^ Bitwise xor Two
^~ or ~^ Bitwise xnor Two
~& Bitwise nand Two
~| Bitwise nor Two
24) Different arithmetic operators. Operator symbol Operation performed Number of operands
* Multiply Two
/ Divide Two
+ Add Two
- Subtract Two
% Modulus Two
** Power (exponent) Two
25) What are the types of conditional statements?
1. No else statement
Syntax : if ( [expression] ) true – statement;
2. One else statement
Syntax : if ( [expression] ) true – statement;
else false-statement;
3. Nested if-else-if
Syntax : if ( [expression1] ) true statement 1;
else if ( [expression2] ) true-statement 2;
else if ( [expression3] ) true-statement 3;
else default-statement;
The [expression] is evaluated. If it is true (1 or a non-zero value) true-statement is
executed. If it is false (zero) or ambiguous (x), the false-statement is executed.
26) What is meant by continuous assignment statement in verilog HDL?

Tuesday, February 25, 2014

[LEETCODE] Sort Colors

[LEETCODE] Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

思路:
定义一个red指针在最左,一个blue指针在最右。
一个变量从0开始向blue指针扫描:
遇到red(0)  就跟red 指针对换,red 指向下一个,扫描变量指向下一个;
遇到blue(2)就跟blue指针对换,blue指向前一个,扫描变量不动,因为需要判断被换了的值。

代码:
An one-pass algorithm using only constant space

    void sortColors(int A[], int n) {
        int red  =0  ;
        int blue =n-1;
        int i=0;
        while(i<blue+1){
            if(A[i] == 0){
                std::swap(A[red], A[i]);
                red++;
                i++;
                continue;
            }
            if(A[i] == 2){
                std::swap(A[blue],A[i]);
                blue--;
                continue;
            }
            i++;
        }
    }

Linux Learning

Basic commands

Basic command

Go back to previous directory: cd ../  or cd../../  and so on

TCP/IP Learning (Updating)

1. TCP 3-way Handshake (SYC, SYC-ACK, ACK)
Link to the detailed analysis