Friday, 3 April 2015

Electronics Voting Machine Verilog Code


Hello people,

Following code may be useful for students or enthusiasts who are trying to design a voting machine. It has been successfully simulated as well as implemented on Spartan 3 xc3s400 FPGA. If you are having any doubts regarding following code then post a comment. I will try my best to answer the questions.
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Engineer: Chaitannya Supe
// Create Date:    09:31:19 03/08/2015 
// Design Name: Electronic Voting Machine
// Module Name: evm 
// Target Devices: Xilinx Spartan 3 XCS400-PQ208
// Tool versions: Xilinx ISE 12.2, 14.7
// Description: This is simple Voting machine code in which total 3 partys were considered.
//   A voter can vote to any of the parties only one at a time. If a voter votes two parties 
//   at a time then that vote will be invalid. This code has been successfully simulated as well as
//   implemented on Spartan 3 xc3s400-4pq208 device.
//      
// Additional Comments: To register a vote, operate a voter_switch and then press push button to register 
//   a vote. This operation was chosen to simplify a coding and get better understanding
//   of a voting machine on FPGA.
//////////////////////////////////////////////////////////////////////////////////
module evm(clk,voter_switch,PB,voting_en,opled,invalid,dout);

input voting_en,PB,clk;//voting process will start when vote_en is on
input [2:0]voter_switch;
output [6:0]dout;//Max no. of votes = 127
output reg [2:0]opled;//opled[0]=party1 led, opled[1]=party2 led, opled[2]=party3 led
output reg invalid;//invalid vote indicator led

//counters to count each party votes
reg [6:0]cnt_reg1=0;//party1
reg [6:0]cnt_nxt1=0;//party1
reg [6:0]cnt_reg2=0;//party2
reg [6:0]cnt_nxt2=0;//party2
reg [6:0]cnt_reg3=0;//party3
reg [6:0]cnt_nxt3=0;//party3

reg PB_reg1;  
reg PB_reg2;  
reg [15:0] PB_cnt;
reg PB_state;

//debounce circuit to detect only one rising edge of push button PB(Refer http://www.fpga4fun.com/Debouncer2.html for more information on debounce circuit)
always @(posedge clk)
PB_reg1 <= PB; 
always @(posedge clk)
PB_reg2 <= PB_reg1;

wire PB_idle = (PB_state==PB_reg2);
wire PB_cnt_max = &PB_cnt; // true when all bits of PB_cnt are 1's

always @(posedge clk)
if(PB_idle)
    PB_cnt <= 0;  // idle state i.e. PB_cnt will not increment 
else
begin
    PB_cnt <= PB_cnt + 16'd1;
    if(PB_cnt_max) 
  PB_state <= ~PB_state;  // if the counter is maximum, PB changes
end
assign PB_down = ~PB_idle & PB_cnt_max & ~PB_state;

//counter for party1 votes
always@(posedge PB_down)
 if(voter_switch == 3'b001 && voting_en == 1'b1)
 begin
 cnt_reg1 <= cnt_nxt1;
 end
always@(*)
 begin
 cnt_nxt1 = cnt_reg1 + 1;
 end

//Counter for party2 votes
always@(posedge PB_down)
 if(voter_switch == 3'b010 && voting_en == 1'b1)
 begin
 cnt_reg2 <= cnt_nxt2;
 end
always@(*)
 begin
 cnt_nxt2 = cnt_reg2 + 1;
 end

//Counter for party3 votes
always@(posedge PB_down)
 if(voter_switch == 3'b100 && voting_en == 1'b1)
 begin
 cnt_reg3 <= cnt_nxt3;
 end
always@(*)
 begin
 cnt_nxt3 = cnt_reg3 + 1;
 end

//Final count i.e. total number of votes
assign dout = cnt_reg1 + cnt_reg2 + cnt_reg3;
//relation of "voter_switch" with "opled" & "invalid"
always@(*)
if(voting_en)  
 case(voter_switch)
  3'b100 : begin
     opled = 3'b100;
     invalid = 1'b0;
     end
  3'b010 : begin
     opled = 3'b010;
     invalid = 1'b0;
     end
  3'b001 : begin
     opled = 3'b001; 
     invalid = 1'b0;
     end
  3'b011 : begin
     opled = 3'b000;
     invalid = 1'b1;
     end
  3'b110 : begin
     opled = 3'b000;
     invalid = 1'b1;
     end
  3'b101 : begin
     opled = 3'b000;
     invalid = 1'b1;
     end
  3'b111 : begin
     opled = 3'b000;
     invalid = 1'b1;
     end
  3'b000 : begin
     opled = 3'b000;
     invalid = 1'b0;
     end
  default : begin
      opled = 3'b000;
      invalid = 1'b0;
      end
 endcase
endmodule

57 comments:

  1. Good explanation, helpful for beginners to verilog and FPGA

    ReplyDelete
  2. Good explanation, helpful for beginners to verilog and FPGA

    ReplyDelete
  3. code is working perfectly. can u provide testbench for it

    ReplyDelete
  4. Thanks..Currently i am not having testbench code but i will post it sooner with proper explanation.

    ReplyDelete
    Replies
    1. can you provide test bench please

      Delete
    2. Yes please provide testbench sir

      Delete
  5. can you please explain the whole code?

    ReplyDelete
    Replies
    1. Hi, its already explained in the above code. However you can tell me which point you didn't understood.

      Delete
  6. can we run this code on any spartan kit or only the one you have mentioned?

    ReplyDelete
    Replies
    1. You can run it on any Xilinx FPGA as Verilog is common language for all. However for virtex and zynq series, behaviour may differ slightly because of different LUT structure they have. Personally i think it will support all other families(Virtex, Zynq etc)

      Delete
  7. PB_cnt <= PB_cnt + 16'd1
    what does this do?

    ReplyDelete
    Replies
    1. It is a part of a debounce circuit to detect only one high to low edge of push button. To know what debounce circuit is visit http://www.fpga4fun.com/Debouncer2.html.

      Delete
  8. cnt_nxt3 = cnt_reg3 + 1
    and this?

    ReplyDelete
  9. hey when you prepared the counters for parties 1,2,3 why did you take 3'b100 for party 3 and not 3'b011

    ReplyDelete
    Replies
    1. 3'b100 corresponds to three push buttons( for 3 parties). Note for rest of the cases votes will not be counted anymore.

      Delete
    2. and can you explain the case statement?

      Delete
    3. assign PB_down = ~PB_idle & PB_cnt_max & ~PB_state
      and this indicates?

      Delete
  10. @JENNY: Even if i explain, you will not understand it unless you do it yourself. I suggest you to go through the basics (Verilog coding, state machines etc) and then post comments. If you need materials for basics i can provide you that. Contact me on email for that.

    ReplyDelete
  11. Hi chaitannaya
    Do u have state diagram for the above code..
    Thank you

    ReplyDelete
    Replies
    1. Unfortunately i don't have it. I prepared this code without any state diagrams just by thinking..

      Delete
  12. i think on line 41 instead of PB_reg1 <= PB; there should be PB_reg1 <= ~PB; can you check it,because on the website that u have mentioned http://www.fpga4fun.com/Debouncer2.html it has PB_reg1 <=~PB; written there.

    ReplyDelete
    Replies
    1. It is because my FPGA board was having push to ON tactile switch rather than push to OFF. Hope it clears your doubt.

      Delete
  13. could you please explain me from line 34 to line 58 i am not able to understand the significance of these lines

    ReplyDelete
    Replies
    1. Those lines of code belong to debounce circuit which is needed when you use tactile switches.

      Refer http://www.fpga4fun.com/Debouncer2.html for more information on debounce circuit.

      Delete
  14. please provide me ur email id

    ReplyDelete
  15. Sir,for testbench dout is not giving an output wave. What we should give to PB as it depends on PB, others all are giving right output.The total debounce program based on dout right it is not working
    Please reply for this if anyone know the solution

    ReplyDelete
    Replies
    1. Hi, for testbench you can follow folling procedure:
      1. Remove debounce circuit code.
      2. Change PB_down to PB.
      3. Now provide pulses to PB as per your convenience.

      Hope it helps. Let me know if this is the solution for your question.

      Delete
  16. Hi GOOD Work

    Can I know
    How to get the votes polled to the individual candidates who are contesting

    ReplyDelete
  17. Replies
    1. Dout gives total number of votes for all 3 parties.

      Delete
  18. Does it run for quartus vlsi

    ReplyDelete
  19. This code will run for quartus tool

    ReplyDelete
  20. Can u provide testbench for this program

    ReplyDelete
  21. Replies
    1. Hi, you can generate the RTL schematic in the synthesis tool of Xilinx ISE directly.

      Delete
  22. Can u provide testbench plzz..

    ReplyDelete
  23. can u provide text bench also

    ReplyDelete
  24. how can we simulate the source code without test bench ?
    if you have test bench for it please provide it.

    ReplyDelete
    Replies
    1. You can simulate the code directly on FPGA board. Please read the description of the code for using it on board.

      Delete
  25. If any one have testbench for this electronic voting machine using Verizon hdl please send to my mail

    ReplyDelete
    Replies
    1. did you get the test bench?
      if yes send it to me please

      Delete
    2. i also need the test bench please

      Delete
    3. I need the flow diagram and testbench of above code.. Can anyone help me with that?

      Delete
  26. Will you please provide fpga design flow for this

    ReplyDelete
  27. canu u provide ucf for this program

    ReplyDelete
  28. Can I use without any boarf

    ReplyDelete
  29. Kindly provide your mail I'd have doubts.

    ReplyDelete
  30. Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading.
    electronics

    ReplyDelete
  31. Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts.
    online electronic world

    ReplyDelete
  32. I want to run this on CPLD board, what modifications do i need to do

    ReplyDelete