|
| 1 | +/* |
| 2 | +Alex Yazdani |
| 3 | +18 November 2024 |
| 4 | +Elevator Controller Module |
| 5 | +
|
| 6 | +Inputs: |
| 7 | + clk |
| 8 | + reset |
| 9 | + req_floor (3 bits: floor request) |
| 10 | +
|
| 11 | +Outputs: |
| 12 | + current_floor (3 bits: current floor) |
| 13 | + moving (1 bit: elevator moving indicator) |
| 14 | +
|
| 15 | +States: |
| 16 | + IDLE |
| 17 | + MOVING_UP |
| 18 | + MOVING_DOWN |
| 19 | + ARRIVED |
| 20 | +*/ |
| 21 | + |
| 22 | +module elevator_controller( |
| 23 | + input clk, reset, |
| 24 | + input [2:0] req_floor, |
| 25 | + output reg [2:0] current_floor, |
| 26 | + output reg moving |
| 27 | +); |
| 28 | + |
| 29 | + // State Encoding |
| 30 | + parameter IDLE = 2'b00; |
| 31 | + parameter MOVING_UP = 2'b01; |
| 32 | + parameter MOVING_DOWN = 2'b10; |
| 33 | + parameter ARRIVED = 2'b11; |
| 34 | + |
| 35 | + reg [1:0] state, next_state; |
| 36 | + |
| 37 | + // State Memory |
| 38 | + always @(posedge clk or posedge reset) begin |
| 39 | + if (reset) |
| 40 | + state <= IDLE; |
| 41 | + else |
| 42 | + state <= next_state; |
| 43 | + end |
| 44 | + |
| 45 | + // Next State Logic (NSL) |
| 46 | + always @(*) begin |
| 47 | + next_state = state; // Default to prevent latches |
| 48 | + case (state) |
| 49 | + IDLE: begin |
| 50 | + if (req_floor > current_floor) |
| 51 | + next_state = MOVING_UP; |
| 52 | + else if (req_floor < current_floor) |
| 53 | + next_state = MOVING_DOWN; |
| 54 | + end |
| 55 | + MOVING_UP: begin |
| 56 | + if (current_floor == req_floor) |
| 57 | + next_state = ARRIVED; |
| 58 | + end |
| 59 | + MOVING_DOWN: begin |
| 60 | + if (current_floor == req_floor) |
| 61 | + next_state = ARRIVED; |
| 62 | + end |
| 63 | + ARRIVED: next_state = IDLE; |
| 64 | + default: next_state = IDLE; |
| 65 | + endcase |
| 66 | + end |
| 67 | + |
| 68 | + // Output and Floor Logic |
| 69 | + always @(posedge clk or posedge reset) begin |
| 70 | + if (reset) begin |
| 71 | + current_floor <= 3'd0; // Reset to ground floor |
| 72 | + moving <= 1'b0; |
| 73 | + end else begin |
| 74 | + case (state) |
| 75 | + MOVING_UP: begin |
| 76 | + current_floor <= current_floor + 1; |
| 77 | + moving <= 1'b1; |
| 78 | + end |
| 79 | + MOVING_DOWN: begin |
| 80 | + current_floor <= current_floor - 1; |
| 81 | + moving <= 1'b1; |
| 82 | + end |
| 83 | + ARRIVED: moving <= 1'b0; |
| 84 | + default: moving <= 1'b0; |
| 85 | + endcase |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | +endmodule |
0 commit comments