FPGA 上的奇数检测器

问题描述 投票:0回答:1

实现一个电路,当 8 个滑动开关中的奇数个设置为“1”时,该电路会点亮 LED。

这是我正在尝试的项目。在不使用库的情况下,我想到的只有一种解决方案(我还不知道库)。它将 LED 分配给所有开关组合。

知道如何使用/不使用 IEEE 库来优化代码吗?

`timescale 1ns / 1ps

module challenge1(

    input [7:0] sw,
    
    output [1:0] led

    );
    
    assign led[0] = ( ~sw[7] & ~sw[6] & ~sw[5] & ~sw[4] & ~sw[3] & ~sw[2] & ~sw[1] &  sw[0] ) |
                    ( ~sw[7] & ~sw[6] & ~sw[5] & ~sw[4] & ~sw[3] & ~sw[2] &  sw[1] & ~sw[0] ) |
                    ( ~sw[7] & ~sw[6] & ~sw[5] & ~sw[4] & ~sw[3] &  sw[2] & ~sw[1] & ~sw[0] ) |
                    ( ~sw[7] & ~sw[6] & ~sw[5] & ~sw[4] & ~sw[3] &  sw[2] &  sw[1] &  sw[0] ) |
                    ( ~sw[7] & ~sw[6] & ~sw[5] & ~sw[4] &  sw[3] & ~sw[2] & ~sw[1] & ~sw[0] ) |
                    ( ~sw[7] & ~sw[6] & ~sw[5] & ~sw[4] &  sw[3] & ~sw[2] &  sw[1] &  sw[0] ) |
                    ( ~sw[7] & ~sw[6] & ~sw[5] & ~sw[4] &  sw[3] &  sw[2] & ~sw[1] &  sw[0] ) |
                    ( ~sw[7] & ~sw[6] & ~sw[5] & ~sw[4] &  sw[3] &  sw[2] &  sw[1] & ~sw[0] ) |
                    ( ~sw[7] & ~sw[6] & ~sw[5] &  sw[4] & ~sw[3] & ~sw[2] & ~sw[1] & ~sw[0] ) |
                    ( ~sw[7] & ~sw[6] & ~sw[5] &  sw[4] & ~sw[3] & ~sw[2] &  sw[1] &  sw[0] ) |
                    ( ~sw[7] & ~sw[6] & ~sw[5] &  sw[4] & ~sw[3] &  sw[2] & ~sw[1] &  sw[0] ) |
                    ( ~sw[7] & ~sw[6] & ~sw[5] &  sw[4] & ~sw[3] &  sw[2] &  sw[1] & ~sw[0] ) |
                    ( ~sw[7] & ~sw[6] & ~sw[5] &  sw[4] &  sw[3] & ~sw[2] & ~sw[1] &  sw[0] ) |
                    ( ~sw[7] & ~sw[6] & ~sw[5] &  sw[4] &  sw[3] & ~sw[2] &  sw[1] & ~sw[0] ) |
                    ( ~sw[7] & ~sw[6] & ~sw[5] &  sw[4] &  sw[3] &  sw[2] & ~sw[1] & ~sw[0] ) |
                    ( ~sw[7] & ~sw[6] & ~sw[5] &  sw[4] &  sw[3] &  sw[2] &  sw[1] &  sw[0] ) |
                    ( ~sw[7] & ~sw[6] &  sw[5] & ~sw[4] & ~sw[3] & ~sw[2] & ~sw[1] & ~sw[0] ) |
                    ( ~sw[7] & ~sw[6] &  sw[5] & ~sw[4] & ~sw[3] & ~sw[2] &  sw[1] &  sw[0] ) |
                    ( ~sw[7] & ~sw[6] &  sw[5] & ~sw[4] & ~sw[3] &  sw[2] & ~sw[1] &  sw[0] ) |
                    ( ~sw[7] & ~sw[6] &  sw[5] & ~sw[4] & ~sw[3] &  sw[2] &  sw[1] & ~sw[0] ) |
                    ( ~sw[7] & ~sw[6] &  sw[5] & ~sw[4] &  sw[3] & ~sw[2] & ~sw[1] &  sw[0] ) |
                    ( ~sw[7] & ~sw[6] &  sw[5] & ~sw[4] &  sw[3] & ~sw[2] &  sw[1] & ~sw[0] ) |
                    ( ~sw[7] & ~sw[6] &  sw[5] & ~sw[4] &  sw[3] &  sw[2] & ~sw[1] & ~sw[0] ) |
                    ( ~sw[7] & ~sw[6] &  sw[5] & ~sw[4] &  sw[3] &  sw[2] &  sw[1] &  sw[0] ) |
                    ( ~sw[7] & ~sw[6] &  sw[5] &  sw[4] & ~sw[3] & ~sw[2] & ~sw[1] &  sw[0] ) |
                    ( ~sw[7] & ~sw[6] &  sw[5] &  sw[4] & ~sw[3] & ~sw[2] &  sw[1] & ~sw[0] ) |
                    ( ~sw[7] & ~sw[6] &  sw[5] &  sw[4] & ~sw[3] &  sw[2] & ~sw[1] & ~sw[0] ) |
                    ( ~sw[7] & ~sw[6] &  sw[5] &  sw[4] & ~sw[3] &  sw[2] &  sw[1] &  sw[0] ) |
                    ( ~sw[7] & ~sw[6] &  sw[5] &  sw[4] &  sw[3] & ~sw[2] & ~sw[1] & ~sw[0] ) |
                    ( ~sw[7] & ~sw[6] &  sw[5] &  sw[4] &  sw[3] & ~sw[2] &  sw[1] &  sw[0] ) |
                    ( ~sw[7] & ~sw[6] &  sw[5] &  sw[4] &  sw[3] &  sw[2] & ~sw[1] &  sw[0] ) |
                    ( ~sw[7] & ~sw[6] &  sw[5] &  sw[4] &  sw[3] &  sw[2] &  sw[1] & ~sw[0] ) |
                    ( ~sw[7] &  sw[6] & ~sw[5] & ~sw[4] & ~sw[3] & ~sw[2] & ~sw[1] & ~sw[0] ) |
                    ( ~sw[7] &  sw[6] & ~sw[5] & ~sw[4] & ~sw[3] & ~sw[2] &  sw[1] &  sw[0] ) |
                    ( ~sw[7] &  sw[6] & ~sw[5] & ~sw[4] & ~sw[3] &  sw[2] & ~sw[1] &  sw[0] ) |
                    ( ~sw[7] &  sw[6] & ~sw[5] & ~sw[4] & ~sw[3] &  sw[2] &  sw[1] & ~sw[0] ) |
                    ( ~sw[7] &  sw[6] & ~sw[5] & ~sw[4] &  sw[3] & ~sw[2] & ~sw[1] &  sw[0] ) |
                    ( ~sw[7] &  sw[6] & ~sw[5] & ~sw[4] &  sw[3] & ~sw[2] &  sw[1] & ~sw[0] ) |
                    ( ~sw[7] &  sw[6] & ~sw[5] & ~sw[4] &  sw[3] &  sw[2] & ~sw[1] & ~sw[0] ) |
                    ( ~sw[7] &  sw[6] & ~sw[5] & ~sw[4] &  sw[3] &  sw[2] &  sw[1] &  sw[0] ) |
                    ( ~sw[7] &  sw[6] & ~sw[5] &  sw[4] & ~sw[3] & ~sw[2] & ~sw[1] &  sw[0] ) |
                    ( ~sw[7] &  sw[6] & ~sw[5] &  sw[4] & ~sw[3] & ~sw[2] &  sw[1] & ~sw[0] ) |
                    ( ~sw[7] &  sw[6] & ~sw[5] &  sw[4] & ~sw[3] &  sw[2] & ~sw[1] & ~sw[0] ) |
                    ( ~sw[7] &  sw[6] & ~sw[5] &  sw[4] & ~sw[3] &  sw[2] &  sw[1] &  sw[0] ) |
                    ( ~sw[7] &  sw[6] & ~sw[5] &  sw[4] &  sw[3] & ~sw[2] & ~sw[1] & ~sw[0] ) |
                    ( ~sw[7] &  sw[6] & ~sw[5] &  sw[4] &  sw[3] & ~sw[2] &  sw[1] &  sw[0] ) |
                    ( ~sw[7] &  sw[6] & ~sw[5] &  sw[4] &  sw[3] &  sw[2] & ~sw[1] &  sw[0] ) |
                    ( ~sw[7] &  sw[6] & ~sw[5] &  sw[4] &  sw[3] &  sw[2] &  sw[1] & ~sw[0] ) |
                    ( ~sw[7] &  sw[6] &  sw[5] & ~sw[4] & ~sw[3] & ~sw[2] & ~sw[1] &  sw[0] ) |
                    ( ~sw[7] &  sw[6] &  sw[5] & ~sw[4] & ~sw[3] & ~sw[2] &  sw[1] & ~sw[0] ) |
                    ( ~sw[7] &  sw[6] &  sw[5] & ~sw[4] & ~sw[3] &  sw[2] & ~sw[1] & ~sw[0] ) |
                    ( ~sw[7] &  sw[6] &  sw[5] & ~sw[4] & ~sw[3] &  sw[2] &  sw[1] &  sw[0] ) |
                    ( ~sw[7] &  sw[6] &  sw[5] & ~sw[4] &  sw[3] & ~sw[2] & ~sw[1] & ~sw[0] ) |
                    ( ~sw[7] &  sw[6] &  sw[5] & ~sw[4] &  sw[3] & ~sw[2] &  sw[1] &  sw[0] ) |
                    ( ~sw[7] &  sw[6] &  sw[5] & ~sw[4] &  sw[3] &  sw[2] & ~sw[1] &  sw[0] ) |
                    ( ~sw[7] &  sw[6] &  sw[5] & ~sw[4] &  sw[3] &  sw[2] &  sw[1] & ~sw[0] ) |
                    ( ~sw[7] &  sw[6] &  sw[5] &  sw[4] & ~sw[3] & ~sw[2] & ~sw[1] & ~sw[0] ) |
                    ( ~sw[7] &  sw[6] &  sw[5] &  sw[4] & ~sw[3] & ~sw[2] &  sw[1] &  sw[0] ) |
                    ( ~sw[7] &  sw[6] &  sw[5] &  sw[4] & ~sw[3] &  sw[2] & ~sw[1] &  sw[0] ) |
                    ( ~sw[7] &  sw[6] &  sw[5] &  sw[4] & ~sw[3] &  sw[2] &  sw[1] & ~sw[0] ) |
                    ( ~sw[7] &  sw[6] &  sw[5] &  sw[4] &  sw[3] & ~sw[2] & ~sw[1] &  sw[0] ) |
                    ( ~sw[7] &  sw[6] &  sw[5] &  sw[4] &  sw[3] & ~sw[2] &  sw[1] & ~sw[0] ) |
                    ( ~sw[7] &  sw[6] &  sw[5] &  sw[4] &  sw[3] &  sw[2] & ~sw[1] & ~sw[0] ) |
                    ( ~sw[7] &  sw[6] &  sw[5] &  sw[4] &  sw[3] &  sw[2] &  sw[1] &  sw[0] ) |
                    (  sw[7] & ~sw[6] & ~sw[5] & ~sw[4] & ~sw[3] & ~sw[2] & ~sw[1] & ~sw[0] ) |
                    (  sw[7] & ~sw[6] & ~sw[5] & ~sw[4] & ~sw[3] & ~sw[2] &  sw[1] &  sw[0] ) | 
                    (  sw[7] & ~sw[6] & ~sw[5] & ~sw[4] & ~sw[3] &  sw[2] & ~sw[1] &  sw[0] ) |
                    (  sw[7] & ~sw[6] & ~sw[5] & ~sw[4] & ~sw[3] &  sw[2] &  sw[1] & ~sw[0] ) |
                    (  sw[7] & ~sw[6] & ~sw[5] & ~sw[4] &  sw[3] & ~sw[2] & ~sw[1] &  sw[0] ) |
                    (  sw[7] & ~sw[6] & ~sw[5] & ~sw[4] &  sw[3] & ~sw[2] &  sw[1] & ~sw[0] ) |
                    (  sw[7] & ~sw[6] & ~sw[5] & ~sw[4] &  sw[3] &  sw[2] & ~sw[1] & ~sw[0] ) |
                    (  sw[7] & ~sw[6] & ~sw[5] & ~sw[4] &  sw[3] &  sw[2] &  sw[1] &  sw[0] ) |
                    (  sw[7] & ~sw[6] & ~sw[5] &  sw[4] & ~sw[3] & ~sw[2] & ~sw[1] &  sw[0] ) |
                    (  sw[7] & ~sw[6] & ~sw[5] &  sw[4] & ~sw[3] & ~sw[2] &  sw[1] & ~sw[0] ) |
                    (  sw[7] & ~sw[6] & ~sw[5] &  sw[4] & ~sw[3] &  sw[2] & ~sw[1] & ~sw[0] ) |
                    (  sw[7] & ~sw[6] & ~sw[5] &  sw[4] & ~sw[3] &  sw[2] &  sw[1] &  sw[0] ) | 
                    (  sw[7] & ~sw[6] & ~sw[5] &  sw[4] &  sw[3] & ~sw[2] & ~sw[1] & ~sw[0] ) |
                    (  sw[7] & ~sw[6] & ~sw[5] &  sw[4] &  sw[3] & ~sw[2] &  sw[1] &  sw[0] ) |
                    (  sw[7] & ~sw[6] & ~sw[5] &  sw[4] &  sw[3] &  sw[2] & ~sw[1] &  sw[0] ) |
                    (  sw[7] & ~sw[6] & ~sw[5] &  sw[4] &  sw[3] &  sw[2] &  sw[1] & ~sw[0] ) |
                    (  sw[7] & ~sw[6] &  sw[5] & ~sw[4] & ~sw[3] & ~sw[2] & ~sw[1] &  sw[0] ) |
                    (  sw[7] & ~sw[6] &  sw[5] & ~sw[4] & ~sw[3] & ~sw[2] &  sw[1] & ~sw[0] ) |
                    (  sw[7] & ~sw[6] &  sw[5] & ~sw[4] & ~sw[3] &  sw[2] & ~sw[1] & ~sw[0] ) |
                    (  sw[7] & ~sw[6] &  sw[5] & ~sw[4] & ~sw[3] &  sw[2] &  sw[1] &  sw[0] ) |
                    (  sw[7] & ~sw[6] &  sw[5] & ~sw[4] &  sw[3] & ~sw[2] & ~sw[1] & ~sw[0] ) |
                    (  sw[7] & ~sw[6] &  sw[5] & ~sw[4] &  sw[3] & ~sw[2] &  sw[1] &  sw[0] ) |
                    (  sw[7] & ~sw[6] &  sw[5] & ~sw[4] &  sw[3] &  sw[2] & ~sw[1] &  sw[0] ) |
                    (  sw[7] & ~sw[6] &  sw[5] & ~sw[4] &  sw[3] &  sw[2] &  sw[1] & ~sw[0] ) |
                    (  sw[7] & ~sw[6] &  sw[5] &  sw[4] & ~sw[3] & ~sw[2] & ~sw[1] & ~sw[0] ) |
                    (  sw[7] & ~sw[6] &  sw[5] &  sw[4] & ~sw[3] & ~sw[2] &  sw[1] &  sw[0] ) |
                    (  sw[7] & ~sw[6] &  sw[5] &  sw[4] & ~sw[3] &  sw[2] & ~sw[1] &  sw[0] ) |
                    (  sw[7] & ~sw[6] &  sw[5] &  sw[4] & ~sw[3] &  sw[2] &  sw[1] & ~sw[0] ) |
                    (  sw[7] & ~sw[6] &  sw[5] &  sw[4] &  sw[3] & ~sw[2] & ~sw[1] &  sw[0] ) |
                    (  sw[7] & ~sw[6] &  sw[5] &  sw[4] &  sw[3] & ~sw[2] &  sw[1] & ~sw[0] ) |
                    (  sw[7] & ~sw[6] &  sw[5] &  sw[4] &  sw[3] &  sw[2] & ~sw[1] & ~sw[0] ) |
                    (  sw[7] & ~sw[6] &  sw[5] &  sw[4] &  sw[3] &  sw[2] &  sw[1] &  sw[0] ) |
                    (  sw[7] &  sw[6] & ~sw[5] & ~sw[4] & ~sw[3] & ~sw[2] & ~sw[1] &  sw[0] ) |
                    (  sw[7] &  sw[6] & ~sw[5] & ~sw[4] & ~sw[3] & ~sw[2] &  sw[1] & ~sw[0] ) |
                    (  sw[7] &  sw[6] & ~sw[5] & ~sw[4] & ~sw[3] &  sw[2] & ~sw[1] & ~sw[0] ) |
                    (  sw[7] &  sw[6] & ~sw[5] & ~sw[4] & ~sw[3] &  sw[2] &  sw[1] &  sw[0] ) |
                    (  sw[7] &  sw[6] & ~sw[5] & ~sw[4] &  sw[3] & ~sw[2] & ~sw[1] & ~sw[0] ) |
                    (  sw[7] &  sw[6] & ~sw[5] & ~sw[4] &  sw[3] & ~sw[2] &  sw[1] &  sw[0] ) |
                    (  sw[7] &  sw[6] & ~sw[5] & ~sw[4] &  sw[3] &  sw[2] & ~sw[1] &  sw[0] ) |
                    (  sw[7] &  sw[6] & ~sw[5] & ~sw[4] &  sw[3] &  sw[2] &  sw[1] & ~sw[0] ) |
                    (  sw[7] &  sw[6] & ~sw[5] &  sw[4] & ~sw[3] & ~sw[2] & ~sw[1] & ~sw[0] ) |
                    (  sw[7] &  sw[6] & ~sw[5] &  sw[4] & ~sw[3] & ~sw[2] &  sw[1] &  sw[0] ) |
                    (  sw[7] &  sw[6] & ~sw[5] &  sw[4] & ~sw[3] &  sw[2] & ~sw[1] &  sw[0] ) |
                    (  sw[7] &  sw[6] & ~sw[5] &  sw[4] & ~sw[3] &  sw[2] &  sw[1] & ~sw[0] ) |
                    (  sw[7] &  sw[6] & ~sw[5] &  sw[4] &  sw[3] & ~sw[2] & ~sw[1] &  sw[0] ) |
                    (  sw[7] &  sw[6] & ~sw[5] &  sw[4] &  sw[3] & ~sw[2] &  sw[1] & ~sw[0] ) |
                    (  sw[7] &  sw[6] & ~sw[5] &  sw[4] &  sw[3] &  sw[2] & ~sw[1] & ~sw[0] ) |
                    (  sw[7] &  sw[6] & ~sw[5] &  sw[4] &  sw[3] &  sw[2] &  sw[1] &  sw[0] ) |
                    (  sw[7] &  sw[6] &  sw[5] & ~sw[4] & ~sw[3] & ~sw[2] & ~sw[1] & ~sw[0] ) |
                    (  sw[7] &  sw[6] &  sw[5] & ~sw[4] & ~sw[3] & ~sw[2] &  sw[1] &  sw[0] ) |
                    (  sw[7] &  sw[6] &  sw[5] & ~sw[4] & ~sw[3] &  sw[2] & ~sw[1] &  sw[0] ) |
                    (  sw[7] &  sw[6] &  sw[5] & ~sw[4] & ~sw[3] &  sw[2] &  sw[1] & ~sw[0] ) |
                    (  sw[7] &  sw[6] &  sw[5] & ~sw[4] &  sw[3] & ~sw[2] & ~sw[1] &  sw[0] ) |
                    (  sw[7] &  sw[6] &  sw[5] & ~sw[4] &  sw[3] & ~sw[2] &  sw[1] & ~sw[0] ) |
                    (  sw[7] &  sw[6] &  sw[5] & ~sw[4] &  sw[3] &  sw[2] & ~sw[1] & ~sw[0] ) |
                    (  sw[7] &  sw[6] &  sw[5] & ~sw[4] &  sw[3] &  sw[2] &  sw[1] &  sw[0] ) |
                    (  sw[7] &  sw[6] &  sw[5] &  sw[4] & ~sw[3] & ~sw[2] & ~sw[1] &  sw[0] ) |
                    (  sw[7] &  sw[6] &  sw[5] &  sw[4] & ~sw[3] & ~sw[2] &  sw[1] & ~sw[0] ) |
                    (  sw[7] &  sw[6] &  sw[5] &  sw[4] & ~sw[3] &  sw[2] & ~sw[1] & ~sw[0] ) |
                    (  sw[7] &  sw[6] &  sw[5] &  sw[4] & ~sw[3] &  sw[2] &  sw[1] &  sw[0] ) |
                    (  sw[7] &  sw[6] &  sw[5] &  sw[4] &  sw[3] & ~sw[2] & ~sw[1] & ~sw[0] ) |
                    (  sw[7] &  sw[6] &  sw[5] &  sw[4] &  sw[3] & ~sw[2] &  sw[1] &  sw[0] ) |
                    (  sw[7] &  sw[6] &  sw[5] &  sw[4] &  sw[3] &  sw[2] & ~sw[1] &  sw[0] ) |
                    (  sw[7] &  sw[6] &  sw[5] &  sw[4] &  sw[3] &  sw[2] &  sw[1] & ~sw[0] ) ;
                    
endmodule

verilog fpga
1个回答
0
投票

这里给出两种方法,方法一是mkrieger1在评论中提供的。

module challenge1(
    input [7:0]  sw,
    output [1:0] led
);
  
  reg [3:0] ones_count = 0;
  wire led_0;
  
  // Method 1, xor reduction
  assign led_0 = ^sw;  
  
  // method 2, loop over the vector, count the ones
  always @(*) begin
        ones_count = 0;
    for(integer i = 0; i < 7 ; i++)
      if(sw[i] == 1'b1)
        ones_count = ones_count + 1;
      else
        ones_count = ones_count;
  end
  //
  assign led[0] = ones_count[0];
  assign led[1] = 1'b0;
  
endmodule

测试台

module challenge1_tb();
  
  reg [7:0] sw;
  reg  [1:0] led;
  
  initial begin
    $monitor("sw = %b, dut.ones_count = %0d, led[0] = %b, dut.led_0 =%b",sw,dut.ones_count,led[0],dut.led_0);
    for(int i = 0; i < 8 ; i++) begin
      sw = i;
      #1;
    end
  end
  
  challenge1 dut(
    .sw(sw),
    .led(led)
  );
  
  initial begin
    $dumpfile("dump.vcd"); 
    $dumpvars;
  end
  
endmodule

产品

sw = 00000000, dut.ones_count = 0, led[0] = 0, dut.led_0 =0
sw = 00000001, dut.ones_count = 1, led[0] = 1, dut.led_0 =1
sw = 00000010, dut.ones_count = 1, led[0] = 1, dut.led_0 =1
sw = 00000011, dut.ones_count = 2, led[0] = 0, dut.led_0 =0
sw = 00000100, dut.ones_count = 1, led[0] = 1, dut.led_0 =1
sw = 00000101, dut.ones_count = 2, led[0] = 0, dut.led_0 =0
sw = 00000110, dut.ones_count = 2, led[0] = 0, dut.led_0 =0
sw = 00000111, dut.ones_count = 3, led[0] = 1, dut.led_0 =1
© www.soinside.com 2019 - 2024. All rights reserved.