/******************************************************
*                                                     
*  Description:  Vorlage Ampelsteurerung                                
*         Date:  05.01.2018                          
*    File Name:  ampel_wo_src.v                              
*      Version:  1.2                                  
*       Target:  Simulation and Synthesis             
*   Technology:                                       
*                                                     
* Rev   Author   Date          Changes                
* -----------------------------------------------------
* 1.0   RHK      10.10.2011    Initial Release        
* 1.1   JZ       10.03.2016    several bugfixes
* 1.2   JZ       05.01.2018    angepasst auf neues Ampelboard
* 1.3   MW       07.03.2019    STATE Konstanten angepasst
*******************************************************/

`timescale 1ns / 1ps

module ampel (
  input              CLOCK_50,
  input        [1:0] KEY,
  output       [2:0] LEDH_L,
  output       [2:0] LEDH_R,
  output       [2:0] LEDN_L,
  output       [2:0] LEDN_R,
  output       [1:0] LEDF_L,
  output       [1:0] LEDF_R, 
  output reg			DBG  
);

reg [9:0] CLKDIV1;
reg [9:0] CLKDIV2;
reg [9:0] CLKDIV3;

reg MELDER;
reg MELDER_Q;
reg MELDER_QQ;
reg MELDER_ACK;
reg CLOCK_ENABLE;

reg START_WARTEN;
reg [3:0] STATE;
reg [2:0] HAUPTSTR;
reg [2:0] NEBENSTR;
reg [1:0] FUSSGAENGER;

reg [3:0] WARTEZAEHLER;
reg [3:0] WARTEWERT;

wire en_div_1;
wire en_div_2;
wire en_div_3;

<<<<<<< HEAD
wire tc_div_1;
wire tc_div_2;
wire tc_div_3;

=======
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
wire [9:0] q_div_1;
wire [9:0] q_div_2;
wire [9:0] q_div_3;

<<<<<<< HEAD
=======
wire tc_div_1;
wire tc_div_2;
wire tc_div_3;

>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
//                       R Gr
parameter FUSS_AUS   = 2'b00;
parameter FUSS_ROT   = 2'b10;
parameter FUSS_GRUEN = 2'b01;
//                      R Ge Gr
parameter AUTO_AUS   = 3'b000;
parameter AUTO_ROT   = 3'b100;
parameter AUTO_GELB  = 3'b010;
parameter AUTO_GRUEN = 3'b001;

assign RESET                     = ~KEY[1];

assign LEDH_L  = HAUPTSTR;
assign LEDH_R  = HAUPTSTR;
assign LEDN_L = NEBENSTR;
assign LEDN_R = NEBENSTR;
assign LEDF_L = FUSSGAENGER;
assign LEDF_R = FUSSGAENGER;

`ifdef SIMULATION
`define DIVVAL1 10-1
`define DIVVAL3 5-1
`else
`define DIVVAL1 1000-1
`define DIVVAL3 50-1
`endif

`define FUSS_WARTEN       4  //Wartezeit
`define GELB_DAUER        4  //Wartezeit 
`define FUSS_GRUEN_DAUER  4  //Wartezeit
`define ALLE_ROT_DAUER    4  //Wartezeit

`define HAUPT_GRUEN_DAUER 4  //Wartezeit
`define NEBEN_GRUEN_DAUER 4  //Wartezeit


`define PHASEVAL 8

//clock divider to generate 1 Hz
<<<<<<< HEAD
//

=======
//Clock Enable Generation
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
assign en_div_1 = 1'b1;
assign en_div_2 = tc_div_1;
assign en_div_3 = tc_div_1 & tc_div_2;

<<<<<<< HEAD
always  begin
      CLOCK_ENABLE = tc_div_3;
end

// synchronisze KEY to CLOCK_50
always @(posedge CLOCK_50) begin
=======
always @(tc_div_3) begin
      CLOCK_ENABLE <= tc_div_3;
end

// synchronisze KEY to CLOCK_50
always @(posedge CLOCK_50 or posedge ~KEY[0]) begin
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
      MELDER_Q = ~KEY[0];
      MELDER_QQ = MELDER_Q;
end

// Melder sofort setzen 
// Melder zuruecksetzen wenn die Anforderung von der Statemachine verarbeitet
// wurde. Die Ampelanlage gibt den FUSSGAENGERn nur dann gruen wenn
// ein Melder anstehst...			
<<<<<<< HEAD
always @(posedge CLOCK_50 or negedge RESET) begin
  if (RESET)
    MELDER <= 1'b0;
  else begin
      case ({MELDER_QQ, MELDER_ACK})
            2'b01: MELDER <= 1'b0;
            2'b10: MELDER <= 1'b1;
            2'b11: MELDER <= ~MELDER;
            default: ;
      endcase
  end 	
end   

assign WARTEN_FERTIG = (WARTEZAEHLER == 4'b0000);

//Wartezaehler
always @(posedge CLOCK_50 or RESET) begin
      if(RESET) begin
            WARTEZAEHLER <= WARTEWERT;
      end
      else if(CLOCK_ENABLE)  begin
            if (START_WARTEN)
                  WARTEZAEHLER <= WARTEWERT;
                  START_WARTEN <= 1'b0;
            if (~WARTEN_FERTIG)
                  WARTEZAEHLER <= WARTEZAEHLER - 1'b1;
      end
end

always @(posedge CLOCK_50 or negedge RESET)
=======
always @(posedge CLOCK_50 or posedge RESET) begin
  if (RESET)
    MELDER <= 1'b0;
  else begin
      case ({MELDER_QQ,MELDER_ACK})
            2'b00: ;
            2'b01: MELDER <= 1'b0;
            2'b10: MELDER <= 1'b1;
            2'b11: MELDER <= ~MELDER;
      endcase
  end 	
end            

//Wartezaehler

assign WARTEN_FERTIG = (WARTEZAEHLER == 1'b0);

always @(posedge CLOCK_50 or posedge RESET) begin
      if (RESET) begin
            WARTEZAEHLER <= WARTEWERT;
      end
      else if (CLOCK_ENABLE) begin
            if (~WARTEN_FERTIG) begin
                  WARTEZAEHLER <= WARTEZAEHLER - 1'b1;
            end
            else if (START_WARTEN) begin
                  START_WARTEN <= ~START_WARTEN;
                  WARTEZAEHLER <= WARTEWERT;
            end
      end
end


always @(posedge CLOCK_50 or posedge RESET)
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
  if (RESET)
    begin
    HAUPTSTR     <= AUTO_GRUEN;
    NEBENSTR     <= AUTO_ROT;
    FUSSGAENGER  <= FUSS_ROT;
    START_WARTEN <= 1'b0;
    STATE        <= 4'd0;
    MELDER_ACK   <= 1'b0;
    end
  else
  if (CLOCK_ENABLE)
  case (STATE)
    4'd0: begin
<<<<<<< HEAD
            if(WARTEN_FERTIG) begin
=======
            if (~WARTEN_FERTIG) begin
                  START_WARTEN <= 1'b0;
                  STATE <= 4'd0;
            end
            else begin
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
                  HAUPTSTR <= AUTO_GRUEN;
                  NEBENSTR <= AUTO_ROT;
                  FUSSGAENGER <= FUSS_ROT;
                  START_WARTEN <= 1'b1;
                  WARTEWERT <= `HAUPT_GRUEN_DAUER;
<<<<<<< HEAD
            end else begin
                  START_WARTEN <= 1'b0;
            end
          end        
    4'd1: begin
            if(WARTEN_FERTIG) begin
                  HAUPTSTR = AUTO_GELB;
=======
                  STATE <= 4'd1;
            end
          end        

    4'd1: begin
            if(~WARTEN_FERTIG) begin
                  START_WARTEN <= 1'b0;
                  STATE <= 4'd1;
            end
            else begin
                  HAUPTSTR <= AUTO_GELB;
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
                  NEBENSTR <= AUTO_ROT;
                  FUSSGAENGER <= FUSS_ROT;
                  START_WARTEN <= 1'b1;
                  WARTEWERT <= `GELB_DAUER;
<<<<<<< HEAD
            end else begin
                  START_WARTEN <= 1'b0;
            end
          end
    4'd2: begin
            if(WARTEN_FERTIG && MELDER) begin
=======
                  STATE <= 4'd2;
            end

          end
    4'd2: begin
            if (~WARTEN_FERTIG) begin
                  START_WARTEN <= 1'b0;
                  STATE <= 4'd2;
            end
            else if (WARTEN_FERTIG && MELDER) begin
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
                  HAUPTSTR <= AUTO_ROT;
                  NEBENSTR <= AUTO_ROT;
                  FUSSGAENGER <= FUSS_ROT;
                  START_WARTEN <= 1'b1;
                  WARTEWERT <= `FUSS_WARTEN;
<<<<<<< HEAD
                  MELDER_ACK <= 1'b1; 

            end else if (WARTEN_FERTIG && ~MELDER) begin
=======
                  MELDER_ACK <= 1'b1;
                  STATE <= 4'd3;
            end
            else if (WARTEN_FERTIG && ~MELDER) begin
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
                  HAUPTSTR <= AUTO_ROT;
                  NEBENSTR <= AUTO_ROT;
                  FUSSGAENGER <= FUSS_ROT;
                  START_WARTEN <= 1'b1;
                  WARTEWERT <= `ALLE_ROT_DAUER;
<<<<<<< HEAD

            end else if(~WARTEN_FERTIG) begin
                  START_WARTEN <= 1'b0;
            end
          end
    4'd3: begin
            if(WARTEN_FERTIG) begin
=======
                  STATE <= 4'd4;
            end

          end
    4'd3: begin
            if (~WARTEN_FERTIG) begin
                  START_WARTEN <= 1'b0;
                  STATE <= 4'd3;
            end
            else begin
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
                  HAUPTSTR <= AUTO_ROT;
                  NEBENSTR <= AUTO_ROT;
                  FUSSGAENGER <= FUSS_GRUEN;
                  START_WARTEN <= 1'b1;
                  WARTEWERT <= `FUSS_GRUEN_DAUER;
                  MELDER_ACK <= 1'b0;
<<<<<<< HEAD

            end else begin
                  START_WARTEN <= 1'b0;
            end
          end 
    4'd4: begin
            if(WARTEN_FERTIG) begin
                  HAUPTSTR <= AUTO_ROT;
                  NEBENSTR <= AUTO_GELB|AUTO_ROT;
                  FUSSGAENGER <= FUSS_ROT;
                  START_WARTEN <= 1'b1;
                  WARTEWERT <= `GELB_DAUER;

            end else begin
                  START_WARTEN <= 1'b0;
            end
          end
    4'd5: begin
            if(WARTEN_FERTIG) begin
=======
                  STATE <= 4'd2;
            end
          end 
    4'd4: begin
            if (~WARTEN_FERTIG) begin
                  START_WARTEN <= 1'b0;
                  STATE <= 4'd4;
            end
            else begin
                  HAUPTSTR <= AUTO_ROT;
                  NEBENSTR <= AUTO_GELB | AUTO_ROT;
                  FUSSGAENGER <= FUSS_ROT;
                  START_WARTEN <= 1'b1;
                  WARTEWERT <= `GELB_DAUER;
                  STATE <= 4'd5;
            end
          end
    4'd5: begin
            if (~WARTEN_FERTIG) begin
                  START_WARTEN <= 1'b0;
                  STATE <= 4'd5;
            end
            else begin
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
                  HAUPTSTR <= AUTO_ROT;
                  NEBENSTR <= AUTO_GRUEN;
                  FUSSGAENGER <= FUSS_ROT;
                  START_WARTEN <= 1'b1;
                  WARTEWERT <= `NEBEN_GRUEN_DAUER;
<<<<<<< HEAD

            end else begin
                  START_WARTEN <= 1'b0;
            end
          end
    4'd6: begin
            if(WARTEN_FERTIG) begin
=======
                  STATE <= 4'd6;
            end
          end
    4'd6: begin
            if (~WARTEN_FERTIG) begin
                  START_WARTEN <= 1'b0;
                  STATE <= 4'd6;
            end
            else begin
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
                  HAUPTSTR <= AUTO_ROT;
                  NEBENSTR <= AUTO_GELB;
                  FUSSGAENGER <= FUSS_ROT;
                  START_WARTEN <= 1'b1;
                  WARTEWERT <= `GELB_DAUER;
<<<<<<< HEAD

            end else begin
                  START_WARTEN <= 1'b0;
            end
          end     
    4'd7: begin
            if(WARTEN_FERTIG) begin
=======
                  STATE <= 4'd7;
            end
          end     
    4'd7: begin
            if (~WARTEN_FERTIG) begin
                  START_WARTEN <= 1'b0;
                  STATE <= 4'd7;
            end
            else begin
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
                  HAUPTSTR <= AUTO_ROT;
                  NEBENSTR <= AUTO_ROT;
                  FUSSGAENGER <= FUSS_ROT;
                  START_WARTEN <= 1'b1;
                  WARTEWERT <= `ALLE_ROT_DAUER;
<<<<<<< HEAD

            end else begin
                  START_WARTEN <= 1'b0;
            end
          end     
    4'd8: begin
            if(WARTEN_FERTIG) begin
                  HAUPTSTR <= AUTO_GELB|AUTO_ROT;
=======
                  STATE <= 4'd8;
            end
          end         
    4'd8: begin
            if (~WARTEN_FERTIG) begin
                  START_WARTEN <= 1'b0;
                  STATE <= 4'd8;
            end
            else begin
                  HAUPTSTR <= AUTO_GELB | AUTO_ROT;
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
                  NEBENSTR <= AUTO_ROT;
                  FUSSGAENGER <= FUSS_ROT;
                  START_WARTEN <= 1'b1;
                  WARTEWERT <= `GELB_DAUER;
<<<<<<< HEAD

            end else begin
                  START_WARTEN <= 1'b0;
=======
                  STATE <= 4'd0;
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
            end
          end     
    default:                      
          STATE <= 4'd1;
  endcase

<<<<<<< HEAD
mod_n_counter_10bit #(.N(`DIVVAL1)) clock_divider_1(
=======
mod_n_counter_10bit #(.N(`DIVVAL1)) divider_1(
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
      .CLK(CLOCK_50),
      .RST(RESET),
      .EN(en_div_1),
      .Q(q_div_1),
      .TC(tc_div_1)
);

<<<<<<< HEAD
mod_n_counter_10bit #(.N(`DIVVAL1)) clock_divider_2(
=======
mod_n_counter_10bit #(.N(`DIVVAL1)) divider_2(
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
      .CLK(CLOCK_50),
      .RST(RESET),
      .EN(en_div_2),
      .Q(q_div_2),
      .TC(tc_div_2)
);

<<<<<<< HEAD
mod_n_counter_10bit #(.N(`DIVVAL3)) clock_divider_3(
=======
mod_n_counter_10bit #(.N(`DIVVAL3)) divider_3(
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
      .CLK(CLOCK_50),
      .RST(RESET),
      .EN(en_div_3),
      .Q(q_div_3),
      .TC(tc_div_3)
);
                        
<<<<<<< HEAD
endmodule
=======
endmodule
>>>>>>> 1b9840c27cab5a0b6374c25e1f14bde0476c9b45
