// iCEstick demo
// Al Williams - Hackaday


`default_nettype none   // Don't allow undeclared nets
module demo(
    input      clk,
    output     LED1,
    output     LED2,
    output     LED3,
    output     LED4,
    output     LED5,
    input      PMOD1, // input A
    input      PMOD2, // input B
    input      PMOD3, // run/stop
    input      PMOD4  // reset
    );


// Manage 12MHz clock
   reg [15:0] cnt1;
   reg [6:0]  cnt2;
   reg [1:0]  dec_cntr;
   reg 	      half_sec_pulse;



// Alias inputs
   wire   inA;
   wire   inB;
   wire   runstop;
   wire   reset;

   assign inA=PMOD1;
   assign inB=PMOD2;
   assign runstop=PMOD3;
   assign reset=PMOD4;
   

// Carry from addition
   wire   carry;
   
// remember carry result
   reg 	  carrylatch;
   
// One way to do the sum/carry
//   assign LED1=inA^inB; 
//   assign carry=inA&inB;  // we use carry in more than one place
  // Another (better) way
  wire [1:0] fullsum;
  assign fullsum={1'b0, inA} + {1'b0, inB};
  
  assign LED1=fullsum[0];
  assign carry=fullsum[1];

   assign LED2=carry; 

   assign LED3 = carrylatch;
   
// latch carry result
   always @(posedge clk)
     begin
	if (reset==1'b1) carrylatch<=1'b0;
	else begin
	   if (carry) carrylatch<=1'b1;
	end
     end

   
	

// The 12MHz clock is too fast
// The first counter will / 2^16 (about 5.46ms)
// Then the second counter goes to 91 which is 0.497s
// close enough to 1/2 second for our purpose  
	always@(posedge clk)
	  begin
        if (reset==1'b1)
         begin
           cnt1<=0;
           cnt2<=0;
           half_sec_pulse<=0;
           dec_cntr<=0;
         end
	     else if (runstop==1'b0)  // don't do anything unless enabled
	       begin
// use this line in real life
//		  cnt1 <= cnt1 + 1;
// but too slow for simulation so use this one instead
          cnt1 <= cnt1 + 16'h8000;
		  if (cnt1 == 0) 
// Use 91 for real life
//		    if (cnt2 == 91) 
// But again, simulation is painful with 91 so use 10 when simulating
            if (cnt2 == 10)
		      begin
			 cnt2 <= 0;
			 half_sec_pulse <= 1;  
		      end
			else
			  cnt2 <= cnt2 + 1;
		  else
		    half_sec_pulse <= 0;
		  
		  if (half_sec_pulse == 1)	
		    dec_cntr <= dec_cntr + 1; // count half seconds
// note: dec_cntr>>1 is a seconds timer, but dec_cntr is only 2 bits so 0-3...
	       end
	  end	

  // Make the lights blink
   assign LED4 = (dec_cntr == 2) ;
   assign LED5 = dec_cntr[0];
endmodule


