某4位数值比较器的功能表如下。
请用Verilog语言采用门级描述方式,实现此4位数值比较器
参考代码如下:
(CSDN代码块不支持Verilog,代码复制到notepad++编辑器中,语言选择Verilog,看得更清楚)
`timescale 1ns/1ns
module comparator_4(
input [3:0] A ,
input [3:0] B ,
output wire Y2 , //A>B
output wire Y1 , //A=B
output wire Y0 //A<B
);
wire [3:0] y1,y2,y0;
genvar i;
generate
for (i=0;i<4;i=i+1)
compare compare_instance(
.a(A[i]),
.b(B[i]),
.y2(y2[i]),
.y1(y1[i]),
.y0(y0[i])
);
endgenerate
assign Y2 = y2[3]|(y1[3]&y2[2])|(y1[3]&y1[2]&y2[1])|(y1[3]&y1[2]&y1[1]&y2[0]);
assign Y1 = &y1;
assign Y0 = ~(Y2|Y1);
endmodule
module compare(
input wire a,
input wire b,
output wire y2,
output wire y1,
output wire y0
);
assign y2 = a&(~b);
assign y1 = (a&b)|(~a& ~b);
assign y0 = ~(y2|y1);
endmodule