1,任务目的
2,RTL代码,交换
// 在verilog中 使用任务 task
module sort4(
ra, rb, rc, rd,
a, b, c, d
);
input [3:0] a, b, c, d;
output 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, vc); // va, vc 互换
sort2(vb, vd); // vb, vd 互换
sort2(va, vb); // va, vb 互换
sort2(vc, vd); // vc, vd 互换
sort2(vb, vc); // 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变量的内容互换,要求顺序执行,则采用阻塞赋值方式
x = y;
y = tmp;
end
endtask
endmodule
3,测试代码
// 测试代码
module sort4_top;
reg [3:0] a, b, c, d;
wire [3:0] ra, rb, rc, rd;
initial begin
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
sort4 u_sort4(
.a (a),
.b (b),
.c (c),
.d (d),
.ra (ra),
.rb (rb),
.rc (rc),
.rd (rd)
);
endmodule
4,波形显示