题解 | #4位数值比较器电路#
4位数值比较器电路
https://www.nowcoder.com/practice/e02fde10f1914527b6b6871b97aef86d
用的移位操作和拼接符,定义个无符号数,把输入通过移位和拼接转为无符号的数后再比较。
`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 [4:0]x,y;
reg r_Y2;
reg r_Y1;
reg r_Y0;
assign x = {A[3],3'b000} + {A[2],2'b00} + {A[1],1'b0} + A[0];
assign y = {B[3],3'b000} + {B[2],2'b00} + {B[1],1'b0} + A[0];
always@(*)begin
if(x>y)begin
r_Y2 = 1;
r_Y1 = 0;
r_Y0 = 0;
end
else if(x==y)begin
r_Y2 = 0;
r_Y1 = 1;
r_Y0 = 0;
end
else begin
r_Y2 = 0;
r_Y1 = 0;
r_Y0 = 1;
end
end
assign Y2=r_Y2;
assign Y1=r_Y1;
assign Y0=r_Y0;
endmodule
查看3道真题和解析