Board logo

标题: systemverilog 小代码 [打印本页]

作者: 苹果也疯狂    时间: 2014-6-16 13:09     标题: systemverilog 小代码

网上关于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


<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_38431382873892458 alt="systemverilog 小代码" src="http://s4.sinaimg.cn/mw690/0026ZEdigy6DKUwcBd963&690" width=690 height=460 action-type="show-slide" action-data="http%3A%2F%2Fs4.sinaimg.cn%2Fmw690%2F0026ZEdigy6DKUwcBd963%26690" real_src="http://s4.sinaimg.cn/mw690/0026ZEdigy6DKUwcBd963&690">


关联数组:



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




<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_15511382874439311 alt="systemverilog 小代码" src="http://s6.sinaimg.cn/mw690/0026ZEdigy6DKVl3oTre5&690" width=690 height=292 real_src="http://s6.sinaimg.cn/mw690/0026ZEdigy6DKVl3oTre5&690">




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


<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_16221382874513517 alt="systemverilog 小代码" src="http://s3.sinaimg.cn/mw690/0026ZEdigy6DKUVKyMW82&690" width=690 height=247 real_src="http://s3.sinaimg.cn/mw690/0026ZEdigy6DKUVKyMW82&690">


<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_93151382874512662 alt="systemverilog 小代码" src="http://s8.sinaimg.cn/bmiddle/0026ZEdigy6DKVoS9JJe7&690" real_src="http://s8.sinaimg.cn/bmiddle/0026ZEdigy6DKVoS9JJe7&690">



信号量。一般用于互斥:
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