door_action_dispatcher.v 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. `timescale 1ns/1ps
  2. // Multi-axis jog dispatcher. DIR=0 open/up, DIR=1 close/down.
  3. // Commands: 00 stop, 01 DIR0, 02 DIR1. Latest door_action_valid overrides any
  4. // in-flight move (re-pulses motor_start with the new mask/dir, including stop).
  5. // AXIS_COUNT=2: exhaust X/Y only (production; intake is solenoid-owned elsewhere).
  6. // AXIS_COUNT=3: bit0=X, bit1=Y, bit2=intake (motor-compat; byte8 -> intake).
  7. module door_action_dispatcher #(
  8. parameter integer OPEN_DIR = 0,
  9. parameter integer CLOSE_DIR = 1,
  10. parameter integer AXIS_COUNT = 2
  11. ) (
  12. input wire clk,
  13. input wire rst_n,
  14. input wire door_action_valid,
  15. input wire [1:0] intake_door_cmd,
  16. input wire [1:0] exhaust_x_cmd,
  17. input wire [1:0] exhaust_y_cmd,
  18. input wire watchdog_abort,
  19. output reg motor_start,
  20. output reg [AXIS_COUNT-1:0] motor_active_mask,
  21. output reg [AXIS_COUNT-1:0] motor_dir_mask,
  22. input wire motor_busy
  23. );
  24. localparam [AXIS_COUNT-1:0] AXIS_ZERO = {AXIS_COUNT{1'b0}};
  25. wire x_active = (exhaust_x_cmd != 2'd0);
  26. wire y_active = (exhaust_y_cmd != 2'd0);
  27. wire intake_active = (intake_door_cmd != 2'd0);
  28. wire x_dir_bit = (exhaust_x_cmd == 2'd2) ? CLOSE_DIR[0] : OPEN_DIR[0];
  29. wire y_dir_bit = (exhaust_y_cmd == 2'd2) ? CLOSE_DIR[0] : OPEN_DIR[0];
  30. wire intake_dir_bit = (intake_door_cmd == 2'd2) ? CLOSE_DIR[0] : OPEN_DIR[0];
  31. // Bit packing: {intake?, y, x} so production X/Y bit positions stay stable.
  32. wire [AXIS_COUNT-1:0] next_active =
  33. (AXIS_COUNT == 3) ? {intake_active, y_active, x_active} :
  34. {y_active, x_active};
  35. wire [AXIS_COUNT-1:0] next_dir =
  36. (AXIS_COUNT == 3) ? {intake_dir_bit, y_dir_bit, x_dir_bit} :
  37. {y_dir_bit, x_dir_bit};
  38. wire needs_motor_event = door_action_valid &&
  39. ((next_active != AXIS_ZERO) || motor_busy);
  40. always @(posedge clk or negedge rst_n) begin
  41. if (!rst_n) begin
  42. motor_start <= 1'b0;
  43. motor_active_mask <= AXIS_ZERO;
  44. motor_dir_mask <= AXIS_ZERO;
  45. end else begin
  46. motor_start <= 1'b0;
  47. if (watchdog_abort) begin
  48. motor_active_mask <= AXIS_ZERO;
  49. end else if (needs_motor_event) begin
  50. motor_active_mask <= next_active;
  51. motor_dir_mask <= next_dir;
  52. motor_start <= 1'b1;
  53. end
  54. end
  55. end
  56. endmodule