题解 | #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
全部评论
点赞 回复 分享
发布于 2023-04-24 17:19 四川

相关推荐

仁者伍敌:牛子这些人还会点一个自动回复,boss都不带回复的
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务