1、sv_struct.sv文件
module tb;
typedef struct packed{//结构体默认是非合并数组,加packed为合并行数组,直接赋值不用加',同时要求logic或者比特向量,不能用byte
logic[7:0] r;
logic[7:0] g;
logic[7:0] b;
} pixel_t;
pixel_t pixel_a, pixel_b, pixel_c;
initial begin
pixel_a = {8'h12, 8'h24, 8'h36};
print_pixel(pixel_a);
pixel_b = pixel_a;
print_pixel(pixel_b);
pixel_b = {g:8'haa, b:8'hbb, r:8'hcc};
print_pixel(pixel_b);
end
function void print_pixel(pixel_t pix);//要传类型+形参,现在的结构体是匿名的,单独pixel_a既不是变量也不是类型,此处的pixel_a和结构体声明的pixel_a没有半毛钱关系
$display("r = %0x, g=%0x, b=%0x", pix.r, pix.g, pix.b);
//不typedef的话就只能像下面这么麻烦
//function void print_pixel(logic [2:0] [7:0] pix_arr);//[2:0]是高维,合并数组格式
//$display("r = %0x, g=%0x, b=%0x", pixel_arr[2], pixel_arr[1], pixel_arr[0]);//2:0的
endfunction
endmodule
2、sv_type_convert.sv
module tb;
logic [7:0] vec1;
bit [7:0] vec2;
byte b1;
integer i1;
int unsigned i2;
initial begin
b1 = -1;$display("i2 = %0d", b1);//值是8'hff,要反码后补码
i1 = b1;$display("i2 = %0d", i1);//值是32'hffffffff,反码后补码的结果
i2 = b1; $display("i2 = %0d", i2);//值是32'hffffffff,打印结果显示他对应的十进制结果4294967295
vec1 = b1;
vec2 = b1;
i1 = 32'h00XXZZFF;
i2 = i1;//全0,因为四值给二值逻辑,隐性转化了
b1 = -1;
i2 = int'(b1);
$finish();
end
endmodule
3、sv_enum.sv
module tb;
typedef enum bit[7:0] {
ADD = 8'b0000_0001//b0000_0001相当于索引
, OR = 8'b0000_0010
, AND = 8'b0000_0100
, MUL = 8'b0000_1000
, DEC = 8'b0001_0000
, CNT = 8'b0010_0000
, LOAD = 8'b0100_0000
, IDLE = 8'b1000_0000
} alu_op_t;
alu_op_t op;
bit[7:0] vec;
initial begin
$display("op current value is %s", op);
std::randomize(vec);
std::randomize(op);
case(op)
AND : op = alu_op_t'(op << 1);
OR : op = alu_op_t'(op << 3);
MUL : op = alu_op_t'(op << 5);//溢出,op结果0
//如果op是DEC,DEC左移两位打印op结果为LOAD
DEC : op = alu_op_t'(op << 2);//(op << 2);先是枚举自动转为整形才能左位移操纵,然后再转为alu_op_t类型赋给op。整形是不能直接赋值为枚举类型
default : $display("op %s keeps current value", op);
endcase
$display("op current value is %s", op);
end
endmodule
4、sv_assco_array.sv
module tb;
int asc_arr_idx_int [int];
string asc_arr_idx_str [string];
int idx_int;
string idx_str;
initial begin
asc_arr_idx_int[100] = 200;//在100的地方放200之歌值
asc_arr_idx_int[101] = 210;
asc_arr_idx_int[102] = 220;
asc_arr_idx_int[103] = 230;
asc_arr_idx_int[104] = 240;
asc_arr_idx_int[105] = 250;
asc_arr_idx_int[106] = 260;
asc_arr_idx_int[107] = 270;
asc_arr_idx_int[108] = 280;
asc_arr_idx_int[109] = 290;
foreach(asc_arr_idx_int[i])
$display("asc_arr_idx_int[%0d] = %0d", i, asc_arr_idx_int[i]);
asc_arr_idx_int.first(idx_int);//这句返回0或1,0表示没有找到,1表示找到第一个的序号了,此时idx_int里面才装着100
$display("asc_arr_idx_int first index is %0d", idx_int);
asc_arr_idx_int[1000] = 2000;
asc_arr_idx_str["100"] = "num_200";
asc_arr_idx_str["110"] = "num_210";
asc_arr_idx_str["120"] = "num_220";
asc_arr_idx_str["130"] = "num_230";
asc_arr_idx_str["140"] = "num_240";
asc_arr_idx_str["150"] = "num_250";
asc_arr_idx_str["160"] = "num_260";
asc_arr_idx_str["170"] = "num_270";
asc_arr_idx_str["180"] = "num_280";
asc_arr_idx_str["190"] = "num_290";
asc_arr_idx_str["1000"] = "num_2000";
foreach(asc_arr_idx_str[i])
$display("asc_arr_idx_str[%s] = %s", i, asc_arr_idx_str[i]);
asc_arr_idx_str.first(idx_str);
$display("asc_arr_idx_str first index is %s", idx_str);
$finish();
end
endmodule