verilog 设计与综合实验报告(2)

题目2设计一个表决器

1、设计方案

输入peolpe_cnt为参与表决的人数,每一位bit表示一人,低位不赞成,高位赞成,输出is_or_not表示表决是否通过,低位不通过,高位通过。使用两个寄存器,cnt_is和cnt_not表示赞成和不赞成的人的个数,如果cnt_is>cnt_not则表示通过,反之则表示不通过。

2、程序代码

`timescale 1ns/1ns
module decision(
	input i_clk,
	input i_rst,
	input [14:0]peolpe_cnt,
	
	output  reg is_or_not
	
);
reg [3:0]cnt_is;
reg [3:0]cnt_not;
integer i;
always@(*)begin
	if(i_rst==1'b1)begin
		cnt_is = 'd0;
		cnt_not = 'd0;
		end
	else begin
		for(i=0;i<15;i=i+1)begin
			if(peolpe_cnt[i])begin
				cnt_is = cnt_is+1;
				cnt_not = cnt_not;end
			else begin
				cnt_not=cnt_not+1;
				cnt_is = cnt_is;end
		end
		if(cnt_is>cnt_not)
			is_or_not = 1'b1;
		else
			is_or_not=1'b0;
	end
end
endmodule

3、仿真测试

`timescale 1ns/1ns
module decision_tb();
reg i_clk;
reg i_rst;
reg [14:0]peolpe_cnt;
wire is_or_not;

decision decision(
	.i_clk(i_clk),
	.i_rst(i_rst),
	.peolpe_cnt(peolpe_cnt),
	
	.is_or_not(is_or_not)

);
always #50 i_clk = ~i_clk;
initial begin
	i_clk = 0;
	i_rst = 1;
	#200 i_rst = 0;
	
	peolpe_cnt = 15'b111101000000000;
	#100;
	peolpe_cnt = 15'b000101010011111;
	end
endmodule

4、结果分析

输入peolpe_cnt = 15'b111101000000000; 1的个数为5个,表示5个人赞成,10个人不赞成,则最终不通过,输出is_or_not为0,

输入peolpe_cnt = 15'b000101010011111; 1的个数为8个,表示8个人赞成,7个人不赞成,则最终通过,输出is_or_not为1,

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

积极向@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值