//15/4/07
module ramzor2 (clock,reset,police,ck_police,red_,yellow,green,sensor);
    
    input   clock,reset,police,ck_police;
    input   [3:0]sensor;
    output     [12:1]green;
    output  [12:1]red_;
    output  [4:1]yellow;
    reg     [12:1]green;
    reg     [12:1]red_;
    reg     [4:1]yellow;
    reg     [5:0]state;    
    reg     [5:0]state1;
    reg     switch;
    reg     [7:0]counter;
    reg     [2:0]count_ck;
reg [4:0]count0,count1,count2,count3,delay;
    
    parameter  st0=0,st1=1,st2=2,st3=3,st4=4,st5=5,st6=6,st7=7,st_start=8,s_delay=3;  
    
    always @(posedge clock or posedge reset)   //will work if there is clock
       begin                       //or the reset was turnud on               
         if (reset)                  //if reset==1 and not the clock
          begin                    
              state=st_start;
              counter=0;
              count_ck=0;
              delay=5;
          end
         else                    //if clock is on and not the reset
          begin
              if (!police)        //if there is police==1?
                 begin
                   //begin    //if it's at 1 of this states and
                        case(state)
                           0: delay=count0+5;
                           2: delay=count1+5;
                           4: delay=count2+5;
                           6: delay=count3+5;
                        endcase

                   if ((state==0 || state==2 || state==4 || state==6) && counter<(delay)) 
                                            counter=counter+1; 
                    //end
                   else   //if it's not at 1 of these states do this:
                    begin
                        if(count_ck >= (s_delay-1))     //count_ck counts 2 clocks
                           begin
                             counter=0;
                             if(state==st_start) state=0;
                             else if (state==st7)   //if state==st23 then go to the
                              state=st0;     //the next state state==st0
                             else
                              state=state+1; //promoting state in 1
                             count_ck=0;  
                           end   
                        else 
                           count_ck=count_ck+1;
                    end
                 end
              else
                 if (state==0 || state==2 || state==4 || state==6)  
                   begin   //if it's at 1 of these state do these things:
                    if (switch)    //if switch==1
                        state=state+1;  //promote the state
                    else
                        state=state;    //don't promote the state
                   end
                 else   //if it's not at 1 of these states do that:
                   begin
                    if(count_ck >= (s_delay-1))
                        begin
                           if(state==st_start) state=0;
                           else if (state==st7)
                             state=st0;
                           else
                             state=state+1;
                           count_ck=0;   
                        end    
                    else count_ck=count_ck+1;
                   end
          end
       end
    always @(ck_police or state)
       begin
         if(ck_police)    //if ck_police was set then switch==1 also
          switch=1;
         else
          if (state==st7 || state==st1 || state==st3 ||state==st5) 
              switch=0;  //when it comes to 1 of these states put 
       end                //switch==0
    
    
    always @(posedge clock or posedge reset)
        begin
       if(reset)
         begin count0=0;count1=0;count2=0;count3=0; end
       else
         begin
         case(state)
                           0: if(!sensor[0])
                              begin if(count0<20) 
                              count0=count0+1; end
                             else
                              begin if(count0>0) 
                              count0=count0-1; end

                           2: if(!sensor[1]) 
                             begin if(count1<20) 
                              count1=count1+1; end
                             else
                              begin if(count1>0) 
                              count1=count1-1; end

                           4: if(!sensor[2]) 
                              begin if(count2<20) 
                              count2=count2+1; end
                             else
                              begin if(count2>0) 
                              count2=count2-1; end

                           6: if(!sensor[3]) 
                             begin if(count3<20) 
                              count3=count3+1; end
                             else
                              begin if(count3>0) 
                              count3=count3-1; end

                        endcase
         end
 
       end



    always @(state)
       begin
         case (state)
          st_start:begin            
                 green=12'b0000_0000_0000;
                 red_=12'b0000_0000_0000;
                 yellow=4'b0000;
              end
          st0:begin             
                 red_=12'b1101_0110_0001;
                 green=12'b0010_1001_1110;
                 yellow=4'b1111;
              end
          
          st1: begin             
                 green=12'b0010_1001_1110;
                 red_=12'b1101_0110_0001;
                 yellow=4'b1110;
              end          
          //****************************************  
          
          st2: begin            
                 green=12'b1010_0100_1101;
                 red_=12'b0101_1011_0010;
                 yellow=4'b1111;
              end                  
          st3: begin             
                 green=12'b1010_0100_1101;
                 red_=12'b0101_1011_0010;
                 yellow=4'b1101;
              end                  
          //****************************************  
          
          st4: begin            
                 green=12'b1001_0010_1011;
                 red_=12'b0110_1101_0100;
                 yellow=4'b1111;
              end            
          
          st5: begin             
                 green=12'b1001_0010_1011;  //for 1 sec
                 red_=12'b0110_1101_0100;
                 yellow=4'b1011;
              end           
          //****************************************  
          
          st6: begin            
                 green=12'b0100_1010_0111;
                 red_=12'b1011_0101_1000;
                 yellow=4'b1111;
              end                   
          
          st7: begin            
                 green=12'b0100_1010_0111;
                 red_=12'b1011_0101_1000;
                 yellow=4'b0111;
              end                   
          //****************************************   
          
         endcase  
       end
endmodule