Saturday, April 28, 2012

Digital Clock on Spartan3E FPGA


Digital Clock on Spartan3E FPGA

//Top module
module Digital_clock(clk1, segA, segB, segC, segD, segE, segF, segG, segDP,anode,hour4,hour3,hour2,hour1,hour0);
input clk1;
output segA, segB, segC, segD, segE, segF, segG, segDP,hour4,hour3,hour2,hour1,hour0;
output [3:0] anode;
wire click;

reg [25:0] count;
always @(posedge clk1)
if(count==26'd50000000)
count<=25'd0;
else
count<=count+26'h1;
wire countovf = (count==26'd50000000);

// anodes of 7segs
wire [1:0] digit_refresh= count[13:12];
assign anode[0] = ~(digit_refresh==2'h0);
assign anode[1] = ~(digit_refresh==2'h1);
assign anode[2] = ~(digit_refresh==2'h2);
assign anode[3] = ~(digit_refresh==2'h3);


//Instantiation from BCD4 module
wire [15:0] BCD_digits;
BCD_display BCD(.clk(clk1), .ena(countovf), .BCD_digits(BCD_digits),.carryout_h(click));  // Instantiation from BCD4 module

//Multiplexing for 7 segments
reg [3:0] BCDdigit;
always @(posedge clk1)
case(digit_refresh)
    2'd0: BCDdigit = BCD_digits[3:0];
    2'd1: BCDdigit = BCD_digits[7:4];
    2'd2: BCDdigit = BCD_digits[11:8];
    2'd3: BCDdigit = BCD_digits[15:12];
endcase

// 7-segments decoder
reg [7:0] SevenSeg;
always @(posedge clk1)
case(BCDdigit)
   4'h0: SevenSeg = 8'b00000011; //0
   4'h1: SevenSeg = 8'b10011111; //1
   4'h2: SevenSeg = 8'b00100101; //2
   4'h3: SevenSeg = 8'b00001101; //3
   4'h4: SevenSeg = 8'b10011001; //4
   4'h5: SevenSeg = 8'b01001001; //5
   4'h6: SevenSeg = 8'b01000001; //6
   4'h7: SevenSeg = 8'b00011111; //7
   4'h8: SevenSeg = 8'b00000001; //8
   4'h9: SevenSeg = 8'b00001001; //9
   default: SevenSeg = 8'b11111110; //default state
endcase

assign {segA, segB, segC, segD, segE, segF, segG, segDP} = SevenSeg;  //giving SevenSeg to individual 7 segment pins

//for Hour LEDs
reg [4:0] hour;
wire rollover=(hour==5'd23);
always @(posedge clk1)
if(click)
begin
if(rollover)
hour<=5'd0;
else
hour<=hour+5'd1;
end

assign {hour4,hour3,hour2,hour1,hour0}=hour; //assigning hour to individual hour pins;
endmodule


//BCD_display module
module BCD_display(clk, ena, BCD_digits,carryout_h);
input clk, ena;
output [15:0] BCD_digits;
wire  [4:0] carryout;
output  carryout_h;

assign carryout[0] = ena;

BCD1 digit0(.clk(clk), .ena(carryout[0]), .BCD_digit(BCD_digits[3:0]),.BCD_carryout1(carryout[1]));
BCD2 digit1(.clk(clk), .ena(carryout[1]), .BCD_digit(BCD_digits[7:4]),.BCD_carryout2(carryout[2]));
BCD3 digit2(.clk(clk), .ena(carryout[2]), .BCD_digit(BCD_digits[11:8]),.BCD_carryout3(carryout[3]));
BCD4 digit3(.clk(clk), .ena(carryout[3]), .BCD_digit(BCD_digits[15:12]),.BCD_carryout4(carryout[4]));

assign carryout_h=carryout[4];

endmodule

// BCD1 module for MSB 7seg , with carryout logic.
module BCD1(clk,ena, BCD_digit,BCD_carryout1);
input clk, ena;
output BCD_digit,BCD_carryout1;
reg [3:0] BCD_digit;
wire BCD_rollover = (BCD_digit==4'd9);
always @(posedge clk)
if(ena)
begin
    if(BCD_rollover)
   BCD_digit<=4'd0;
else BCD_digit<=BCD_digit+4'd1;
end
assign BCD_carryout1 = ena & BCD_rollover;
endmodule

// BCD2 module for 2nd 7seg , with carryout logic.
module BCD2(clk, ena, BCD_digit,BCD_carryout2);
input clk, ena;
output BCD_digit,BCD_carryout2;
reg [3:0] BCD_digit;
wire BCD_rollover = (BCD_digit==4'd5);
always @(posedge clk)
if(ena)
begin
     if(BCD_rollover)
   BCD_digit<=4'd0;
else BCD_digit<=BCD_digit+4'd1;
end
assign BCD_carryout2 = ena & BCD_rollover;
endmodule

// BCD3 for 3rd 7seg , with carryout logic.
module BCD3(clk, ena, BCD_digit,BCD_carryout3);
input clk, ena;
output BCD_digit,BCD_carryout3;
reg [3:0] BCD_digit;
wire BCD_rollover =((BCD_digit==4'd9));
always @(posedge clk)
if(ena)
begin
    if(BCD_rollover)
   BCD_digit<=4'd0;
else BCD_digit<=BCD_digit+4'd1;
end
assign BCD_carryout3 = (ena & BCD_rollover);
endmodule

// BCD4 for 4th 7seg , with carryout logic.
module BCD4(clk, ena, BCD_digit,BCD_carryout4);// when BCD5 will be rollover BCD_carryout4 will be high;
input clk, ena;
output BCD_digit,BCD_carryout4;
reg [3:0] BCD_digit;
wire BCD_rollover = (BCD_digit==4'd5);
always @(posedge clk)
if(ena)
begin
      if(BCD_rollover)
   BCD_digit<=4'd0;
  else BCD_digit<=BCD_digit+4'd1;
end
assign BCD_carryout4 = ena & BCD_rollover;
endmodule

.ucf file for Digital clock-xc3s250e-5cp132


# clock pins for Basys2 Board
NET "clk1" LOC = "B8"; # Bank = 0, Signal name = MCLK
//NET "clk" LOC = "M6"; # Bank = 2, Signal name = UCLK
NET "clk1" CLOCK_DEDICATED_ROUTE = FALSE;

# Connected to Basys2 onBoard 7seg display
NET "segA" LOC = "L14"; # Bank = 1, Signal name = CA
NET "segB" LOC = "H12"; # Bank = 1, Signal name = CB
NET "segC" LOC = "N14"; # Bank = 1, Signal name = CC
NET "segD" LOC = "N11"; # Bank = 2, Signal name = CD
NET "segE" LOC = "P12"; # Bank = 2, Signal name = CE
NET "segF" LOC = "L13"; # Bank = 1, Signal name = CF
NET "segG" LOC = "M12"; # Bank = 1, Signal name = CG
NET "SegDP"LOC = "N13"; # Bank = 1, Signal name = DP


#anode pin  assignments
NET "anode<3>" LOC = "K14"; # Bank = 1, Signal name = AN3
NET "anode<2>" LOC = "M13"; # Bank = 1, Signal name = AN2
NET "anode<1>" LOC = "J12"; # Bank = 1, Signal name = AN1
NET "anode<0>" LOC = "F12"; # Bank = 1, Signal name = AN0

# Pin assignment for LEDs

NET "hour4" LOC = "N5" ;  # Bank = 2, Signal name = LD4
NET "hour3" LOC = "P6" ; # Bank = 2, Signal name = LD3
NET "hour2" LOC = "P7" ; # Bank = 3, Signal name = LD2
NET "hour1" LOC = "M11" ; # Bank = 2, Signal name = LD1
NET "hour0" LOC = "M5" ;  # Bank = 2, Signal name = LD0

Electronic Voting Machine using Spartan3E FPGA


Thursday, April 26, 2012

.ucf file for EVM


# clock pins for Basys2 Board
NET "clk" LOC = "B8"; # Bank = 0, Signal name = MCLK
//NET "clk" LOC = "M6"; # Bank = 2, Signal name = UCLK
NET "clk" CLOCK_DEDICATED_ROUTE = FALSE;



# Connected to Basys2 onBoard 7seg display
NET "segA" LOC = "L14"; # Bank = 1, Signal name = CA
NET "segB" LOC = "H12"; # Bank = 1, Signal name = CB
NET "segC" LOC = "N14"; # Bank = 1, Signal name = CC
NET "segD" LOC = "N11"; # Bank = 2, Signal name = CD
NET "segE" LOC = "P12"; # Bank = 2, Signal name = CE
NET "segF" LOC = "L13"; # Bank = 1, Signal name = CF
NET "segG" LOC = "M12"; # Bank = 1, Signal name = CG
NET "SegDP"LOC = "N13"; # Bank = 1, Signal name = DP



NET "anode<3>" LOC = "K14"; # Bank = 1, Signal name = AN3
NET "anode<2>" LOC = "M13"; # Bank = 1, Signal name = AN2
NET "anode<1>" LOC = "J12"; # Bank = 1, Signal name = AN1
NET "anode<0>" LOC = "F12"; # Bank = 1, Signal name = AN0

NET "switch_2" LOC = "A7"; # Bank = 0, Signal name = BTN0
NET "switch_1" LOC = "M4";  # Bank = 0, Signal name = BTN2

NET "reset_1" LOC = "K3"; # Bank = 2, Signal name = BTN1
NET "reset_2" LOC = "B4";  # Bank = 1, Signal name = BTN3

NET "cntrl" LOC = "P11";  # Bank = 2, Signal name = SW0
NET "master_reset" LOC = "L3";  # Bank = 2, Signal name = SW1
NET "sw4" LOC = "N3";  # Bank = 2, Signal name = SW0
NET "led1" LOC = "M11" ; # Bank = 2, Signal name = LD1
NET "led2" LOC = "N4" ; # Bank = 2, Signal name = LD1


Electronic Voting Machine on Spartan3E FPGA boad


`timescale 1ns / 1ps


//top module
module voting_machine(input clk,switch_1,switch_2,reset_1,reset_2,cntrl,master_reset,sw4,output segA, segB, segC, segD, segE, segF, segG, segDP,led1,led2, output [3:0] anode);
wire q,and1,and2;


LED_SevenSeg LED_instance(.clk(clk),.switch_1(and1),.switch_2(and2),.reset_1(reset_1),.reset_2(reset_2),.cntrl(cntrl),.master_reset(master_reset)
,.segA(segA),.segB(segB),.segC(segC),.segD(segD),.segE(segE),.segF(segF),.segG(segG),.segDP(segDP),.anode(anode),.sw_44(sw4)); //Instance of Led-seven seg module

assign led1=q;
assign led2=q;

cntrl control(.clk1(clk),.switch1(switch_1),.switch2(switch_2),.reset(sw4),.and11(and1),.and22(and2),.Q(q));// Instance of cntrl module

endmodule



//ctrl module
module cntrl(input clk1, switch1,switch2,reset,output and11,and22,Q);

PushButton_Debouncer p1(.clk2(clk1),.PB(~switch1),.PB_down(swt1));
PushButton_Debouncer p2(.clk2(clk1),.PB(~switch2),.PB_down(swt2));
wire Q_bar;
xor x1(xor_out,swt2,swt1);
                                                   
JK_ff flipflop(.clk(xor_out),.reset(reset),.J(Q_bar),.K(Q_bar),.q1(Q),.q_bar(Q_bar));

//for and1
and a1(and1,switch1,Q_bar);
not n1(out_switch2,switch2);
and a11(and11,and1,out_switch2);

//for and2
and a2(and2,switch2,Q_bar);
not n2(out_switch1,switch1);
and a22(and22,and2,out_switch1);

endmodule



//JK_ff module
module JK_ff(input clk,reset,J,K,output q1, output q_bar);
reg q;

always @(posedge clk or posedge reset)
if(reset)
q<=0;
else                                                                                                                                        
q<=(J & ~q)|(~K & q);
assign q_bar=~q;
assign q1=q;

endmodule




//debounce module
module PushButton_Debouncer(clk2, PB, PB_down);
input clk2;
input PB;
output PB_down;  // 1 when the push-button goes down.

// use two flipflops to synchronize the PB signal with the clock domain
reg PB_sync_0;  always @(posedge clk2) PB_sync_0 <= ~PB;  // invert PB to make PB_sync_0 active high
reg PB_sync_1;  always @(posedge clk2) PB_sync_1 <= PB_sync_0;

reg [23:0] PB_cnt;
reg PB_state;   // When the push-button,we increment the counter

initial
PB_state=0;     // state of the push-button (0 when up, 1 when down)
wire PB_idle = (PB_state==PB_sync_1);

wire PB_cnt_max = &PB_cnt;   // The counter has to reach at maximum count before we decide that the push-button state has changed.
always @(posedge clk2)
if(PB_idle)
    PB_cnt <= 0;
else
begin
    PB_cnt <= PB_cnt + 1;  // increment the counter
    if(PB_cnt_max) PB_state <= ~PB_state;  // if the counter is maxed out, state should be changed
end

wire PB_down = ~PB_state & ~PB_idle & PB_cnt_max;  // true for one clock cycle when we detect that PB went down

endmodule



//seven segment display module
module LED_SevenSeg(clk,switch_1,switch_2,reset_1,reset_2,cntrl,master_reset,segA, segB, segC, segD, segE, segF, segG, segDP,anode,sw_44);
input clk,switch_1,switch_2,reset_1,reset_2,cntrl,master_reset,sw_44;

output segA, segB, segC, segD, segE, segF, segG, segDP;
output [3:0] anode;


reg [23:0] count;
always @(posedge clk) count<=count+24'h1;

wire countovf = &count;

// anodes of 7segs
wire [1:0] digit_refresh= count[17:16];
assign anode[0] = ~(digit_refresh==2'h0);
assign anode[1] = ~(digit_refresh==2'h1);
assign anode[2] = ~(digit_refresh==2'h2);
assign anode[3] = ~(digit_refresh==2'h3);


// 4 BCD digits
wire [15:0] BCD_digits;

BCD4 BCD(.clk(clk),.switch1(switch_1),.switch2(switch_2),.reset1(reset_1),.reset2(reset_2),.ena(countovf),.con(cntrl),.BCD_digits(BCD_digits),.m_rst(master_reset),.sw_4(sw_44)); //Instance of BCD 4 module

// multiplexer
reg [3:0] BCDdigit;
always @(posedge clk)
case(digit_refresh)
    2'd0: BCDdigit = BCD_digits[ 3: 0];
    2'd1: BCDdigit = BCD_digits[ 7: 4];
    2'd2: BCDdigit = BCD_digits[11: 8];
    2'd3: BCDdigit = BCD_digits[15:12];
endcase

// 7-segments decoder
reg [7:0] SevenSeg;
always @(posedge clk)
case(BCDdigit)
   4'h0: SevenSeg = 8'b00000011;
   4'h1: SevenSeg = 8'b10011111;
   4'h2: SevenSeg = 8'b00100101;
   4'h3: SevenSeg = 8'b00001101;
   4'h4: SevenSeg = 8'b10011001;
   4'h5: SevenSeg = 8'b01001001;
   4'h6: SevenSeg = 8'b01000001;
   4'h7: SevenSeg = 8'b00011111;
   4'h8: SevenSeg = 8'b00000001;
   4'h9: SevenSeg = 8'b00001001;
   default: SevenSeg = 8'b11111110;
   
endcase
assign {segA, segB, segC, segD, segE, segF, segG, segDP} = SevenSeg;
endmodule

// BCD4 module
module BCD4(clk,switch1,switch2,reset1,reset2,con, ena, BCD_digits,m_rst,sw_4);
input clk,switch1,switch2,reset1,reset2,con,ena,m_rst,sw_4;
output [15:0] BCD_digits;

wire [2:0] carryout;
assign carryout[0] =ena ;
BCD1 digit0(.clk(clk),.switch(switch1),.reset(reset1),.control(con), .ena(carryout[0]),.BCD_digit(BCD_digits[3:0]),.BCD_carryout(carryout[1]),.m_reset(m_rst),.sw(sw_4));
BCD1 digit1(.clk(clk),.switch(switch1),.reset(reset1),.control(con), .ena(carryout[1]),.BCD_digit(BCD_digits[7:4]),.m_reset(m_rst),.sw(sw_4));
BCD1 digit2(.clk(clk),.switch(switch2),.reset(reset2),.control(con),.ena(carryout[0]),.BCD_digit(BCD_digits[11:8]), .BCD_carryout(carryout[2]),.m_reset(m_rst),.sw(sw_4));
BCD1 digit3(.clk(clk),.switch(switch2),.reset(reset2),.control(con),.ena(carryout[2]),.BCD_digit(BCD_digits[15:12]),.m_reset(m_rst),.sw(sw_4));
endmodule


// BCD1 for 1 7seg , with carryout logic. // This is the beauty of the code..!!
module BCD1(clk,switch,reset,control,ena,BCD_digit, BCD_carryout,m_reset,sw);
input clk,switch,reset,control,ena,m_reset,sw;
output [3:0] BCD_digit;
output BCD_carryout;
reg [3:0] BCD_digit;
wire BCD_rollover = (BCD_digit==4'd9);
assign BCD_carryout = ena & BCD_rollover;
reg con;

always @(posedge clk)

begin

if(control ==1'b0)
BCD_digit<=BCD_digit;

if(sw==0)
   con<=1;

if(sw==1)
con<=0;

if((reset && control)||(control && m_reset)==1'b1)
BCD_digit<=4'b0;

if(switch && ena && control && con )
begin
           
             if(BCD_rollover)
          BCD_digit<=4'd0;
         else
          BCD_digit<=BCD_digit+4'd1;

con<=~con;
end    

end
endmodule