task sort2; //x与y互换任务
inout [t:0] x,y;
reg [t:0] tmp;
if(x > y) begin
tmp = x; //使用临时变量tmp保存x的值
x = y;
y = tmp;
end
endtask
endmodule
例4.9:比较器的设计实例(利用赋值语句设计组合逻辑)。
module compare(equal,a,b); //模块声明
output equal;
input [size-1:0] a,b; //端口声明
parameter size=1; //参数声明
assign equal =(a==b)? 1 : 0; //比较器
endmodule
例4.10:3-8译码器设计实例(利用赋值语句设计组合逻辑)。
module decoder(out,in); //模块声明
output [7:0] out;
input [2:0] in; //端口声明
assign out = 1'b1<<in; //把最低位的1左移 in(根据从in口输入的值)位
//将移位结果赋予out
endmodule
例4.11:3-8编码器的设计实例。
编码器设计方案一。
module encoder1(none_on,out,in); //模块声明
output none_on;
output [2:0] out;
input [7:0] in; //端口声明
reg [2:0] out;
reg none_on; //寄存器声明
always @(in) begin: LOCal //in有变化时,触发
integer i; //变量声明
out = 0;
none_on = 1; //初始化
for( i=0; i<8; i=i+1 ) begin //for循环语句
if( in[i] ) begin //将in中值为1的位编码
out = i;
none_on = 0;
end
end
end
endmodule
编码器设计方案二。
module encoder2 ( none_on,out2,out1,out0,h,g,f,e,d,c,b,a); //模块声明
input h,g,f,e,d,c,b,a;
output none_on,out2,out1,out0; //端口声明
wire [3:0] outvec; //向量声明
assign outvec = //使用assign语句实现输出向量赋值
h ? 4'b0111 : g ? 4'b0110 : f ? 4'b0101:
e ? 4'b0100 : d ? 4'b0011 : c ? 4'b0010 :
b ? 4'b0001 : a ? 4'b0000 : 4'b1000;
assign none_on = outvec[3]; //使用assign语句进行编码
assign out2 = outvec[2];
assign out1 = outvec[1];
assign out0 = outvec[0];
endmodule
编码器设计方案三。
module encoder3 ( none_on,out2,out1,out0,h,g,f,e,d,c,b,a); //模块声明
input h,g,f,e,d,c,b,a;
output none_on,out2,out1,out0; //端口声明
本文关键字:风格 电脑-单片机-自动控制,电子学习 - 基础知识 - 电脑-单片机-自动控制