본문 바로가기
Digital Hardware Design/System verilog

Data type(3)

by 꿀벌이목표 2022. 10. 9.
반응형

Ⅰ. Structure

- C언어에서 의 구조체와 동일하다고 생각하면 됨

    + 같은 type 혹은 다른 type의 여러 변수들을 하나로 묶는 개념

(제 생각이지만, System verilog는 verilog 이외에 C의 장점도 잘 살린 언어인 것 같습니다.)

// Structures 모양
struct {
	byte    data1;
	int     data2;
	string  data3;
	logic	data4;
	bit 	data5;
	bit 	[5:0]	bit_array;
} struct_name;

- 보통 C언어 공부를 할 때, Structure를 설명하면서 typedef를 이어서 설명하게 되는데, system verilog에서도 typedef가 있습니다! 그래서 이어서 설명할게요.

    + struct 키워드 이전에 "typedef"만 적어주시면 됩니다.(c언어와 같죠?)

// Structures 모양
struct {
	byte    data1;
	int     data2;
	string  data3;
	logic	data4;
	bit 	data5;
	bit 	[5:0]	bit_array;
} struct_name;

//typedef를 사용하면
// Structures 모양
typedef  struct {
	byte    data1;
	int     data2;
	string  data3;
	logic	data4;
	bit 	data5;
	bit 	[5:0]	bit_array;
} struct_name;

ⅰ. Simulation

- 예제를 참고하여 간단한 simulation 진행

struct {
  	string 	name;
  	int    	num	;
  	byte 	key	;
} example;

typedef struct {
	string	fruit;
	int		count;
	byte	expiry;
} example_typedef;

typedef struct packed {
  bit [3:0] bit_4;
  bit [2:0] bit_3;
  bit       bit_1;
} example_packed;

module sverilog_struct();
	example_packed    packed_temp;
	
	
	initial begin
	// example_typedef is a data type, so we need to declare a variable of this data type
	example_typedef type_temp1 = '{"apple", 4, 15};
	example_typedef type_temp2;
	
	// example is a structure variable, so let's initialize it
	example = '{"apple", 4, 15};
	
	
	// Display the structure variable
	$display ("example = %p", example);
	
	// Change name to pineapple, and key to 7
	example.name = "exam_temp";
	example.key = 7;
	$display ("example = %p", example);
	
	$display ("=============================");
	$display ("copy test====================");
	
	// Display the structure variable
	$display ("type_temp1 = %p type_temp2 = %p", type_temp1, type_temp2);
	
	// Assign one structure variable to another and print
	// Note that contents of this variable is copied into the other
	type_temp2 = type_temp1;
	$display ("type_temp1 = %p type_temp2 = %p", type_temp1, type_temp2);
	
	// Change fruit1 to see if type_temp2 is affected
	type_temp1.fruit = "temptemp";
	$display ("type_temp1 = %p type_temp2 = %p", type_temp1, type_temp2);
	$display ("=============================");
	$display ("packed struct================");
	// Initialize packed structure variable
	packed_temp = '{4'ha, 3'h5, 1};
	$display ("packed_temp = %p", packed_temp);
	
	// Change packed structure member to something else
	packed_temp.bit_4 = 4'h3;
	$display ("packed_temp = %p", packed_temp);
	
	// Assign a packed value to the structure variable
	packed_temp = 8'hfa;
	$display ("packed_temp = %p", packed_temp);
	end
	
endmodule

- 결과는 아래 참고하세요!

더보기

# run 1000ns
example = '{name:"apple",num:4,key:15}
example = '{name:"exam_temp",num:4,key:7}
=============================
copy test====================
type_temp1 = '{fruit:"apple",count:4,expiry:15} type_temp2 = '{fruit:"",count:0,expiry:0}
type_temp1 = '{fruit:"apple",count:4,expiry:15} type_temp2 = '{fruit:"apple",count:4,expiry:15}
type_temp1 = '{fruit:"temptemp",count:4,expiry:15} type_temp2 = '{fruit:"apple",count:4,expiry:15}
=============================
packed struct================
packed_temp = '{bit_4:10,bit_3:5,bit_1:1'b1}
packed_temp = '{bit_4:3,bit_3:5,bit_1:1'b1}
packed_temp = '{bit_4:15,bit_3:5,bit_1:1'b0}

Ⅱ. User-defined data type

- 유저가 data type을 define 한다는 것인데, 간단히 정리하면 typedef를 적절하게 이용하는 것이다.

//보통 아래와 같이 변수를 생성한다.
unsigned shortint			my_data;
enum {RED, YELLOW, GREEN}	color;
bit [7:0]					my_byte;

// 하지만, typedef를 이용하여 아래와 같이 변수 생성이 가능 
typedef unsigned shortint			u_shorti;
typedef enum {RED, YELLOW, GREEN}	color;
typedef bit [7:0]					ubyte;

u_shorti	my_data;
color		light1;
ubyte		my_byte;
반응형

'Digital Hardware Design > System verilog' 카테고리의 다른 글

Control-Case  (0) 2022.10.24
Control - condition  (0) 2022.10.11
Control - loop  (0) 2022.10.09
data type(2)  (0) 2022.10.03
Data type  (0) 2022.09.21

댓글