当前位置:网站首页>数字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项),然后将它们或起来。
边栏推荐
- Vscade set multi line display of tab
- (practice C language every day) the sum of the nearest three numbers
- Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function
- Exploration and practice of integration of streaming and wholesale in jd.com
- La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
- Analysis of how to prevent virus in industrial computer
- 618深度复盘:海尔智家的制胜方法论
- LeetCode 5. Longest Palindromic Substring
- SSM integration exception handler and project exception handling scheme
- ROW_NUMBER()、RANK()、DENSE_RANK区别
猜你喜欢

Some problems about MySQL installation

Kubernetes family container housekeeper pod online Q & A?

PyC file decompile

Typescript array out of order output

Bone conduction non ear Bluetooth headset brand, bone conduction Bluetooth headset brand recommendation

头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》

Exploration and practice of integration of streaming and wholesale in jd.com
![[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](/img/51/f9c1eed37794db8c8d0eefd60b9e3d.jpg)
[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

七一献礼:易鲸捷 “百日会战”完美收官 贵阳银行数据库提前封板

Recalling the college entrance examination and becoming a programmer, do you regret it?
随机推荐
Aujourd'hui dans l'histoire: Alipay lance le paiement par code à barres; La naissance du père du système de partage du temps; La première publicité télévisée au monde...
What if the win11 app store cannot load the page? Win11 store cannot load page
Multi task prompt learning: how to train a large language model?
曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...
False summer vacation
Some problems about MySQL installation
PCL 点云镜像变换
渗透工具-内网权限维持-Cobalt strike
总结|机器视觉中三大坐标系及其相互关系
618深度複盤:海爾智家的制勝方法論
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
Global and Chinese market of switching valves 2022-2028: Research Report on technology, participants, trends, market size and share
[fluent] dart data type list set type (define set | initialize | generic usage | add elements after initialization | set generation function | set traversal)
Pandora IOT development board learning (RT thread) - Experiment 2 RGB LED experiment (learning notes)
2022 the latest and most detailed will successfully set the background image in vscade and solve unsupported problems at the same time
Download blender on Alibaba cloud image station
Yyds dry goods inventory student attendance system based on QT design
Privacy computing technology innovation and industry practice seminar: Learning
Classifier visual interpretation stylex: Google, MIT, etc. have found the key attributes that affect image classification
[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