练习七 在Verilog HDL中使用task任务

源代码

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date:    11:17:58 07/27/2019 
// Design Name: 
// Module Name:    sort4_test 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//
module sort4_test(
						ra,
						rb,
						rc,
						rd,
						a,
						b,
						c,
						d
    );
	output [3:0]ra,rb,rc,rd;
	input [3:0]a,b,c,d;
	reg [3:0]ra,rb,rc,rd;
	reg [3:0]va,vb,vc,vd;

	always @(a or b or c or d)
		begin
			{va,vb,vc,vd} = {a,b,c,d};
			//sort2(va,vb);
			//sort2(vb,vc);
			//sort2(vc,vd);
			//sort2(va,vb);
			//sort2(vb,vc);
			//sort2(va,vb);
			//两两比较,是否有更好的方法,上面的冒泡不如下面的
			sort2(va,vc);
			sort2(vb,vd);
			sort2(va,vb);
			sort2(vc,vd);
			sort2(vb,vc);## 标题
			{ra,rb,rc,rd} = {va,vb,vc,vd};
		end
	
	task sort2;
		inout [3:0]x,y;
		reg [3:0]tmp;
		if(x > y)
			begin
				tmp = x;			//使用阻塞性赋值,顺序执行
				x = y;
				y = tmp;
			end
	endtask

endmodule

测试代码

`timescale 1ns / 100ps


// Company: 
// Engineer:
//
// Create Date:   11:42:36 07/27/2019
// Design Name:   sort4_test
// Module Name:   D:/FPGA/project/sort4_test/testbench/vtf_sort4_test.v
// Project Name:  sort4_test
// Target Device:  
// Tool versions:  
// Description: 
//
// Verilog Test Fixture created by ISE for module: sort4_test
//
// Dependencies:
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 


module vtf_sort4_test;

	// Inputs
	reg [3:0] a;
	reg [3:0] b;
	reg [3:0] c;
	reg [3:0] d;

	// Outputs
	wire [3:0] ra;
	wire [3:0] rb;
	wire [3:0] rc;
	wire [3:0] rd;

	// Instantiate the Unit Under Test (UUT)
	sort4_test uut (
		.ra(ra), 
		.rb(rb), 
		.rc(rc), 
		.rd(rd), 
		.a(a), 
		.b(b), 
		.c(c), 
		.d(d)
	);

	initial begin
		// Initialize Inputs
		a = 0;
		b = 0;
		c = 0;
		d = 0;
		repeat(50)
			begin
				#100	a = {$random}%15;
						b = {$random}%15;
						c = {$random}%15;
						d = {$random}%15;
			end
		#100	$stop;
	end
	
      
endmodule


仿真波形

波形

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值