网上关于systemverilog的教材不是很多,也没代码说明。
自己学的时候,把它的一些跟verilog不太同的特殊地方写了点小代码,仿仿真以便以后学习。
枚举:
module test_typedef();
typedef enum {red,green,blue,yellow,white,black} colors;
colors my_colors;
initial
begin
$display("my_color's default value is %s",my_colors);
my_colors = green;
$display("my_color is %s",my_colors. name);
my_colors = colors'(2);
$display("my_color is %s",my_colors. name);
end
endmodule
module test_associate_array();
bit [7:0]age[string];
string tom = "tom";
initial begin
age[tom] = 21;
age["joe"] = 32;
$display("%s is %d years of age ",tom, age[tom]);
$display("%s is %d years of age ","joe", age["joe"]);
end
endmodule
<IMG style="PADDING-BOTTOM: 0px; BORDER-RIGHT-WIDTH: 0px; LIST-STYLE-TYPE: none; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; PADDING-TOP: 0px; border-image: initial" title="systemverilog 小代码" name=image_operate_69451382874440892 alt="systemverilog 小代码" src="http://s8.sinaimg.cn/mw690/0026ZEdigy6DKUwfuVFf7&690" width=690 height=444 real_src="http://s8.sinaimg.cn/mw690/0026ZEdigy6DKUwfuVFf7&690">
结构:
module struct_example();
int array[2:0];
struct {
//非压缩结构体
bit[7:0]a0;
bit a1;
bit a2;
}my_struct;
initial begin
my_struct = {10,1,1};
array[2] = my_struct.a0;
array[1] = my_struct.a1;
array[0] = my_struct.a2;
end
endmodule
module struct_example();
bit[9:0] a;
struct
packed{
//压缩结构体
bit[7:0]a0;
bit a1;
bit a2;
}my_struct;
initial begin
my_struct = {10,1,1};
a = {my_struct.a0,my_struct.a1,my_struct.a2};
end
endmodule
信号量。一般用于互斥:
module semaphore_example();
semaphore s1=new(1);
task t1();
for(int i=0;i<3;i++)begin
s1.get(1);
#5;
$display("t1 owns semaphore");
s1.put(1);
#5;
end
endtask
task t2();
for(int i=0;i<3;i++)begin
s1.get(1);
#5;
$display("t2 owns semaphore");
s1.put(1);
#5;
end
endtask
initial begin
fork
t1();
t2();
join
end
endmodule
<IMG style="PADDING-BOTTOM: 0px; BORDER-RIGHT-WIDTH: 0px; LIST-STYLE-TYPE: none; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; PADDING-TOP: 0px; border-image: initial" title="systemverilog 小代码" name=image_operate_88411383137983260 alt="systemverilog 小代码" src="http://s16.sinaimg.cn/mw690/0026ZEdigy6DPMZ9AyXcf&690" width=197 height=143 real_src="http://s16.sinaimg.cn/mw690/0026ZEdigy6DPMZ9AyXcf&690">
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |