从100个数中找到最大的两个数

这篇博客讨论了如何使用Verilog硬件描述语言来设计一个找出一百个输入数据中最大两个值的模块。代码中包含了三个always块,分别用于计数、更新中间变量out1和out2以及最终更新最大值max1和max2。在每次时钟上升沿,模块会比较当前输入data与已存储的最大值,并更新max1和max2。
摘要由CSDN通过智能技术生成

Verilog有点难顶,用always块完成,要是用java的话,将一百个数放在一个数组里面,排序就行了

下面代码中的data是一个数,应该是给一百次data才行,不知道要怎么给这个data

用out1和out2作为中间变量存储结果,用cnt进行计数,100次的时候更新max1,max2

module top(
input clk,rst,
input [7:0] data,
output reg [7:0] max1,
output reg [7:0] max2
);
reg [6:0] cnt;
reg [7:0] out1;
reg [7:0] out2;

always@(posedge clk or negedge rst)begin
if(!rst) cnt<=0;
else begin
if(cnt==7’d99) cnt<=0;
else cnt<=cnt+1;
end
end

always@(posedge clk or negedge rst)begin
if(!rst) begin
out1<=0;out2<=0;
end
else begin
if(cnt==0)begin
out1<=data;out2<=data;
end
else begin
	if(data>=out1)begin
		if(data>=out2)begin
			out2<=data;
			out1<=out2;
		end
		else begin
			out2<=out2;
			out1<=data;
		end
end
else begin
	out1<=out1;out2<=out2;
end
	  end
	end
end

always@(posedge clk or negedge rst)begin
if(!rst)begin
max1<=0;max2<=0;
	end
	else begin
		if(cnt==0)begin
			max1<=out1;
			max2<=out2;
		end
	end
end

endmodule

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值