Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //explicacion
- //mas explicacion
- module stop_light(north, south, east, west, clk, reset);
- input clk, reset;
- output reg [3:0] north, east, south, west;
- reg [3:0] clk_count = 4'b0000;
- // N/S
- parameter NS_LAR = 3'b001; // state 001 => north/south light left arrow/red AND east/west light red
- parameter NS_G = 3'b010; // state 010 => north/south light green AND east/west light red
- parameter NS_Y = 3'b011; // state 011 => north/south light yellow AND east/west light red
- // E/W
- parameter EW_LAR = 3'b100; // state 100 => north/south light red AND east/west light left arrow/red
- parameter EW_G = 3'b101; // state 101 => north/south light red AND east/west light green
- parameter EW_Y = 3'b110; // state 110 => north/south light red AND east/west light yellow
- // Reset / All lights to red
- parameter NSEW_red = 3'b111; // state 111 => north/south light red AND east/west light red (RESET STATE)
- // Our current state value (intitally set to turn north/south left arrow/red light on and east/west light red)
- reg [3:0] state = NS_LAR;
- always @(posedge clk)
- begin
- // check to see if the reset switch has been set
- if (reset == 1)
- begin
- state = NSEW_red;
- clk_count = 4b'0000;
- end //end if
- else
- begin
- if (state == NS_LAR)
- begin
- if (clk_count = 4b'1010) //once the clock has reached 10 seconds
- begin
- state = NS_G;
- clk_count = 4'b0000;
- end //end if
- else
- begin
- state = NS_LAR;
- clk_count = clk_count + 1;
- end //end else
- end //end NS_LAR case
- if (state == NS_G)
- begin
- if (clk_count = 4b'1111) //once the clock has reached 15 seconds
- begin
- state = NS_Y;
- clk_count = 4'b0000;
- end //end if
- else
- begin
- state = NS_G;
- clk_count = clk_count + 1;
- end //end else
- end //end NS_G case
- if (state == NS_Y)
- begin
- if (clk_count = 4b'0101) //once the clock has reached 5 seconds
- begin
- state = EW_LAR;
- clk_count = 4'b0000;
- end //end if
- else
- begin
- state = NS_Y;
- clk_count = clk_count + 1;
- end //end else
- end //end NS_Y case
- if (state == EW_LAR)
- begin
- if (clk_count = 4b'1010) //once the clock has reached 10 seconds
- begin
- state = EW_G;
- clk_count = 4'b0000;
- end //end if
- else
- begin
- state = EW_LAR;
- clk_count = clk_count + 1;
- end //end else
- end //end EW_LAR case
- if (state == EW_G)
- begin
- if (clk_count = 4b'1111) //once the clock has reached 15 seconds
- begin
- state = EW_Y;
- clk_count = 4'b0000;
- end //end if
- else
- begin
- state = EW_G;
- clk_count = clk_count + 1;
- end //end else
- end //end EW_G case
- if (state == EW_Y)
- begin
- if (clk_count = 4b'0101) //once the clock has reached 5 seconds
- begin
- state = NS_LAR;
- clk_count = 4'b0000;
- end //end if
- else
- begin
- state = EW_Y;
- clk_count = clk_count + 1;
- end //end else
- end //end EW_Y case
- end //end else
- end //end always posedge clk
- always @(state)
- begin
- if (state == NS_LAR)
- north = 4'b1001;
- south = 4'b1001;
- east = 4b'0001;
- west = 4b'0001;
- if (state == NS_G)
- north = 4b'0100;
- south = 4b'0100;
- east = 4b'0001;
- west = 4b'0001;
- if (state == NS_Y)
- north = 4b'0010;
- south = 4b'0010;;
- east = 4b'0001;
- west = 4b'0001;
- if (state == EW_LAR)
- north = 4b'0001;
- south = 4b'0001;
- east = 4'b1001;
- west = 4'b1001;
- if (state == EW_G)
- north = 4b'0001;
- south = 4b'0001;
- east = 4b'0100;
- west = 4b'0100;
- if (state == EW_Y)
- north = 4b'0001;
- south = 4b'0001;
- east = 4b'0010;;
- west = 4b'0010;;
- if (state == NSEW_red)
- north = 4b'0001;
- south = 4b'0001;
- east = 4b'0001;
- west = 4b'0001;
- end //end always state
- endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement