ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [SystemVerilog] 클래스 2
    카테고리 없음 2022. 1. 23. 22:38

    클래스 내부에 static 변수를 선언할 수 있다.

    static 변수는 같은 클래스끼리 공유되는 변수이다.

    class frame;
        
        static int framecount = 0;
        int tag;
        logic [4:0] addr;
        logic [7:0] payload;
        bit parity;
        
        function new(input int address, data);
        	payload = data;
            addr = address;
            parity = ^{addr, payload};
            tag = ++framcount;
        endfunction
        
    endclass

    frame 클래스를 인스턴스 할 때 마다 framecount 값이 증가하고, tag에는 몇 번째 오브젝트인지 저장된다.

     

     

     

    메서드또한 static으로 선언할 수 있다.

    static 메서드는 오직 static 속성만 접근할 수 있다.

    class frame;
        
        static int framecount = 0;
        int tag;
        logic [4:0] addr;
        logic [7:0] payload;
        bit parity;
        
        function new(input int address, data);
        	payload = data;
            addr = address;
            parity = ^{addr, payload};
            tag = ++framcount;
        endfunction
        
        static function int getcount();
        	return framecount;
        endcount
        
    endclass
    
    
    
    
    frame f1 = new(10, 10);
    int frmcnt;
    initial begin
        // using dot operator
        frmcnt = f1.getount();
     
        // using scope operator
        frmcnt = frame::getcount();
    end

    static메서드를 사용하려면

    .연산자로 클래스 인스턴스를 통해 접근하거나, :: 연산자로 접근해야한다.

    :: 연산자를 쓰면 클래스 인스턴스가 아니라 클래스 타입이름으로 접근이 가능하다.

     

     

     

    클래스 내부에서 메서드를 정의하는데 메서드 내부에서 선언한 변수 이름이 클래스 속성과 이름이 같을 수 있다.

    그럴 땐 this 키워드로 클래스속성을 직접 지시해야한다.

    this는 현재 클래스 오브젝트를 가르키는 키워드이다.

    class frame;
        
        static int framecount = 0;
        int tag;
        logic [4:0] addr;
        logic [7:0] payload;
        bit parity;
        
        function new(input int addr, payload);
        	this.payload = payload;
            this.addr = addr;
            parity = ^{addr, payload};
            tag = ++framcount;
        endfunction
        
    
    endclass

    변수 addr, payload 는 클래스 속성이면서 new메서드 내부에도 선언되있다.

    이때 클래스 속성을 가르키기 위해서 this를 사용한다. this를 쓰지않는 변수는 메서드에서 선언된 변수를 참조한다.

    클래스 속성과 메서드의 변수명이 같은게 없다면 굳이 this를 쓸 필요는 없다.

    static 속성에는 쓸 수 없다.

     

     

     

     

Designed by Tistory.