当前位置:网站首页>数字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项),然后将它们或起来。
边栏推荐
- Win11应用商店无法加载页面怎么办?Win11商店无法加载页面
- 大厂面试总结大全
- Set the background picture in the idea (ultra detailed)
- mysql数据库mysqldump为啥没有创建数据库的语句
- unity Hub 登錄框變得很窄 無法登錄
- Download blender on Alibaba cloud image station
- Does bone conduction earphone have external sound? Advantages of bone conduction earphones
- Classic quotations
- 电脑设备打印机驱动安装失败如何解决
- ⌈ 2022 ⌋ how to use webp gracefully in projects
猜你喜欢

Mathematical analysis_ Notes_ Chapter 5: univariate differential calculus

Vscade set multi line display of tab

How to choose the right kubernetes storage plug-in? (09)

PCL point cloud image transformation

How to use stustr function in Oracle view

TCP拥塞控制详解 | 2. 背景

Some problems about MySQL installation

机器学习-感知机模型

Route service grid traffic through two-level gateway design

Routing mode: hash and history mode
随机推荐
LeetCode 6. Z 字形变换 (N字形变换)
Kubernetes three open interfaces first sight
TCP congestion control details | 2 background
Remove the underline in router link
Global and Chinese markets of stainless steel surgical suture 2022-2028: Research Report on technology, participants, trends, market size and share
Cloud native cicd framework: Tekton
618 deep resumption: Haier Zhijia's winning methodology
Summary | three coordinate systems in machine vision and their relationships
Original God 2.6 server download and installation tutorial
Some problems about MySQL installation
ROW_ NUMBER()、RANK()、DENSE_ Rank difference
LeetCode 1. 两数之和
Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)
Pandora IOT development board learning (RT thread) - Experiment 2 RGB LED experiment (learning notes)
Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!
Yyds dry inventory company stipulates that all interfaces use post requests. Why?
Set the background picture in the idea (ultra detailed)
云原生的 CICD 框架:Tekton
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
LeetCode 4. Find the median (hard) of two positive arrays