클래스를 먼저 만들어줍니다.
class myPacket;
bit [2:0] header;
bit encode;
bit [2:0] mode;
bit [7:0] data;
bit stop;
function new (bit [2:0] header = 3'h1, bit [2:0] mode = 5);
this.header = header;
this.encode = 0;
this.mode = mode;
this.stop = 1;
endfunction
function display ();
$display ("Header = 0x%0h, Encode = %0b, Mode = 0x%0h, Stop = %0b",
this.header, this.encode, this.mode, this.stop);
endfunction
endclass
function new는 생성자입니다.
this를 사용하여 현재 클래스를 참조하는 데 사용됩니다. 메서드를 참조하기 위해 클래스 내에서 사용됩니다
이신호를 사용하기위해서 module test를 만듭니다.
module class_test();
myPacket pkt0 [3];
initial begin
for(int i = 0; i < $size (pkt0); i++) begin
pkt0[i] = new ();
pkt0[i].display ();
end
end
endmodule
이 코드의 예상으로는 pkt[0], pkt[1], pkt[2]가 새로운 생성자가 되어
pkt0에서 display됩니다. 이 display 함수는
$display ("Header = 0x%0h, Encode = %0b, Mode = 0x%0h, Stop = %0b",
this.header, this.encode, this.mode, this.stop); 에 의해 나타나게됩니다.

새로운 Packet의 클래스를 만들어줍니다.
class Packet;
int count;
endclass
module class_handle_test;
Packet pkt;
initial begin
if(pkt == null)
$display("Packet handle pke is null");
pkt = new();
if(pkt == null)
$display("wrong");
else
$display("pkt is now pointing to an object, not null");
$display("count = %0d", pkt.count);
end
endmodule
예상되는 시나리오는 pkt가 처음 생성자를 만들지 않았기 때문에 packet hanle pke is null 이라는것이 출력되고 그다음 생성자를 만들었기때문에 pkt ==null이 아니라 pkt is now pointing to an object, not null이 나오고 count가 됩니다.

예상대로 진행되었습니다.
위에 사용한 클래스를 그대로 사용합니다. 이것은 클래스를 사용하는 이점이라 볼수있습니다.
class Packet;
int count;
endclass
module class_handle_test2();
Packet pkt, pkt2;
initial begin
pkt = new();
pkt.count = 16'habcd;
$display ("pkt count = 0x%0h",pkt.count);
$display ("pkt2 count = 0x%0h",pkt2.count);
pkt2 = pkt;
$display ("pkt2 count = 0x%0h", pkt2.count);
end
endmodule
이 코드의 예상시나리오는 pkt를 생성자를 만들고 handle pkt와 pkt2는 동일한 인스턴스를 가르키게 만듭니다.
이로서 예상되는 출력결과는
pkt count = 0xabcd
첫번째 pkt2 count 디스플레이는 handle에 값을 넣지 않았기 때문에 나오지않습니다.
pkt2 count = 0xabcd
입니다.

다음내용은 다음에
'나의 전자 공부방 > System Verilog 공부' 카테고리의 다른 글
| 1. systemverilog Hello world, eum사용, string 사용 (0) | 2023.06.12 |
|---|