栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

统计输入[7:0]data

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

统计输入[7:0]data

如题,Verilog实现,奉上拙见

方法1:各位做加法

module count_one_add(
    input clk,
    input rst_n,
    input [7:0] d_in,
    output [3:0] d_out
    );
    
    assign d_out=d_in[0]+d_in[1]+d_in[2]+d_in[3]+d_in[4]+d_in[5]+d_in[6]+d_in[7];
endmodule

方法1原理图

方法2:4输入查找表

module count_one_2LUT(
    input clk,
    input rst_n,
    input [7:0] d_in,
    output [3:0] d_out
);
    wire [2:0] d_out_1,d_out_2;
    count_one_LUT count_one_LUT_l(clk,rst_n,d_in[3:0],d_out_1);
    count_one_LUT count_one_LUT_h(clk,rst_n,d_in[7:4],d_out_2);
    assign d_out=d_out_1+d_out_2;
endmodule

module count_one_LUT(
    input clk,
    input rst_n,
    input [3:0] d_in,
    output reg [2:0] d_out
);
    always @(posedge clk or negedge rst_n)
        if(!rst_n)
            d_out<=3'b0;
        else
            case(d_in)
                4'b0000:     										d_out<=3'b0;
                4'b0001,4'b0010,4'b0100,4'b1000:     				d_out<=3'b1;
                4'b0011,4'b0101,4'b1001,4'b0110,4'b1010,4'b1100:    d_out<=3'd2;
                4'b1110,4'b1101,4'b1011,4'b0111:     				d_out<=3'd3;
                4'b1111:     										d_out<=3'd4;
            endcase      
endmodule

方法2原理图

方法3[未实现]:快速法(参考C语言算法)
试写了没有成功,不知道是否是循环次数不定的原因,下附C语言算法,欢迎大家一起用Verilog实现

int BitCount2(unsigned int n)
{
    unsigned int c =0 ;
    for (c =0; n; ++c)
    {
        n &= (n -1) ; // 清除最低位的1
    }
    return c ;
}

最后附test bench:

module sim_count1( );
	
	reg clk;
	reg rst_n;
	reg [7:0] data_in;
	wire [3:0] data_out;
	
	initial begin
		clk = 0;
		forever
		#10 clk = ~ clk;
	end
		
	initial begin
		rst_n = 0;    
		data_in = 8'b0;
		
		#10
		rst_n = 1;
		
		#51
		data_in = 8'b1000_0000;
		#51
		data_in = 8'b1100_0000;
		#51
        data_in = 8'b1110_0000;
        #51
        data_in = 8'b1111_0000;
	end
	
	count_one_kuai count_one_2LUT(
	.clk(clk),
	.rst_n(rst_n),
	.d_in(data_in),
	.d_out(data_out)
	
	);	
endmodule

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/312340.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号