当前位置:网站首页>数字IC手撕代码--投票表决器
数字IC手撕代码--投票表决器
2022-07-02 13:24:00 【FPGA硅农】
题目
设计一个投票表决器,输入为一个5bit的向量,为1表示赞同,为0表示不赞同,当赞同人数大于非赞同人数时,表决器输出为1,否则为0。
思路:一种方法是统计向量中1的个数,若大于等于3,则输出1,否则输出0。另一种方法是考虑所有可能的情况,事实上,只要有三个位的值为1,则表决器就输出1,因此可据此进行枚举( C 5 3 = 10 C_5^3=10 C53=10),得到最终的结果。
代码
`timescale 1ns / 1ps
//
// Company:
// Engineer:
//
// Create Date: 2022/07/01 15:19:05
// Design Name:
// Module Name: top
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//
module top(
input logic clk,
input logic rst,
input [4:0] vote,
output logic r1,
output logic r2
);
//1
[email protected](posedge clk,posedge rst)
if(rst)
r1<=0;
else
r1<=(vote[0]&vote[1]&vote[2])
|(vote[0]&vote[1]&vote[3])
|(vote[0]&vote[1]&vote[4])
|(vote[0]&vote[2]&vote[3])
|(vote[0]&vote[2]&vote[4])
|(vote[0]&vote[3]&vote[4])
|(vote[1]&vote[2]&vote[3])
|(vote[1]&vote[2]&vote[4])
|(vote[1]&vote[3]&vote[4])
|(vote[2]&vote[3]&vote[4]);
//2
logic s1;
logic c1;
logic s2;
logic c2;
logic [2:0] sum;
assign {
c1,s1}=vote[0]+vote[1]+vote[2];
assign {
c2,s2}=vote[3]+vote[4];
assign sum={
c1,s1}+{
c2,s2};
[email protected](posedge clk,posedge rst)
if(rst)
r2<=0;
else if(sum>=3)
r2<=1;
else
r2<=0;
endmodule
测试平台
module test;
logic clk;
logic rst;
logic [4:0] vote;
logic r1;
logic r2;
logic error;
//clk
initial
begin
clk=0;
forever
#5 clk=~clk;
end
//rst
initial
begin
rst=1;
#100
rst=0;
end
//
[email protected](posedge clk,posedge rst)
if(rst)
vote<=0;
else
vote<=$urandom%32;
//
assign error=(r1!=r2)?1:0;
top U(.*
/* input logic clk, input logic rst, input [4:0] vote, output logic r1, output logic r2 */
);
endmodule
结果
总结
对于N输入的表决器(N为奇数),我们同样可以采用类似的方法:
1.通过加法器求1的个数,若和大于(N-1)/2,则输出为1
2.枚举这N个输入的(N+1)/2组合(共 C N ( N + 1 ) / 2 C_N^{(N+1)/2} CN(N+1)/2项),然后将它们或起来。
边栏推荐
- 618深度复盘:海尔智家的制胜方法论
- Sqlserver queries which indexes are underutilized
- Summary | three coordinate systems in machine vision and their relationships
- 七一献礼:易鲸捷 “百日会战”完美收官 贵阳银行数据库提前封板
- sim2real环境配置教程
- Yyds dry inventory company stipulates that all interfaces use post requests. Why?
- [North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array
- Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)
- False summer vacation
- Download blender on Alibaba cloud image station
猜你喜欢
sql解决连续登录问题变形-节假日过滤
Interview summary of large factories
Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)
MySQL min() finds the minimum value under certain conditions, and there are multiple results
Win11应用商店无法加载页面怎么办?Win11商店无法加载页面
Foreign enterprise executives, continuous entrepreneurs, yoga and skiing masters, and a program life of continuous iteration and reconstruction
Kubernetes three open interfaces first sight
July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
Penetration tool - intranet permission maintenance -cobalt strike
Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)
随机推荐
Data security industry series Salon (III) | data security industry standard system construction theme Salon
云原生的 CICD 框架:Tekton
Global and Chinese market of desktop hot melt equipment 2022-2028: Research Report on technology, participants, trends, market size and share
618深度複盤:海爾智家的制勝方法論
[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array
Trigger: MySQL implements adding or deleting a piece of data in one table and adding another table at the same time
What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?
Kubernetes family container housekeeper pod online Q & A?
Understand the key technology of AGV -- the difference between laser slam and visual slam
数学分析_笔记_第5章:一元微分学
Machine learning perceptron model
电脑管理员权限在哪里可以打开
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
机器学习-感知机模型
mysql min() 求某条件下最小的值出现多个结果
618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
分析超700万个研发需求发现,这8门编程语言才是行业最需要的!
做机器视觉哪个软件好?
What is normal distribution? What is the 28 law?
Effectively use keywords to increase Amazon sales