Verilog 7人投票表决器

7人投票表决,当票数大于等于4(即半数以上),输出1表示通过,否则输出0表示未通过。

方法一:

module vote_7(
input clk,
input[6:0] in,
output out
    );
    wire[2:0] vote_count;
    assign vote_count = in[0] + in[1] + in[2] + in[3] + in[4] + in[5] + in[6];
    assign out = (vote_count >= 4) ? 1 : 0;
endmodule

方法二:(串行思想,但是好用)

module vote_7_2(
input clk,
input[6:0] in,
output out
    );
    integer i;
    reg[2:0] vote_count;
    always@(posedge clk or negedge clk)
    begin
        vote_count = 0;//记得初始化为0
        for(i=0;i<7;i=i+1)
        begin
            if(in[i] == 1) vote_count = vote_count + 1;
        end
    end
    
    assign out = (vote_count>=4) ? 1 : 0;
endmodule

testbench,注意vote_7_in产生的方法:

`timescale 1ns / 1ps
 module testbench; // declare testbench name
  reg clock;
  //7人投票表决器
  reg[6:0] vote_7_in;
  wire vote_7_out;
  wire vote_7_out2;
  
  initial begin
  clock = 0;
  //7人投票表决器
  vote_7_in = 0;
  repeat(10) #10
      begin
        clock <= ~clock;
        vote_7_in <= (vote_7_in<<1) | 7'b1;
      end
  $stop;
  end
  
  vote_7 vote7_inst(.clk(clock),
                    .in(vote_7_in),
                    .out(vote_7_out));
                    
  vote_7_2 vote72_inst(.clk(clock),
                    .in(vote_7_in),
                    .out(vote_7_out2));
 endmodule

仿真:
Verilog 7人投票表决器_第1张图片

你可能感兴趣的:(Verilog)