prod2u_p4_axis_executor.v 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. `timescale 1ns/1ps
  2. module prod2u_p4_axis_executor #(
  3. parameter integer DIR_SETUP_CYCLES = 25000,
  4. parameter integer STEP_HALF_CYCLES = 12500
  5. ) (
  6. input wire clk,
  7. input wire rst_n,
  8. input wire command_start,
  9. input wire command_dir,
  10. input wire [15:0] command_steps,
  11. output wire step_out,
  12. output wire dir_out,
  13. output reg started_pulse
  14. `ifdef FORMAL
  15. , output wire [1:0] formal_motion_phase
  16. , output wire [31:0] formal_phase_counter
  17. , output wire [15:0] formal_remaining_steps
  18. `endif
  19. );
  20. localparam [1:0] PHASE_IDLE = 2'd0;
  21. localparam [1:0] PHASE_DIR_SETUP = 2'd1;
  22. localparam [1:0] PHASE_STEP_HIGH = 2'd2;
  23. localparam [1:0] PHASE_STEP_LOW = 2'd3;
  24. localparam integer MAX_PHASE_CYCLES =
  25. (DIR_SETUP_CYCLES > STEP_HALF_CYCLES) ?
  26. DIR_SETUP_CYCLES : STEP_HALF_CYCLES;
  27. localparam integer PHASE_COUNTER_BITS =
  28. (MAX_PHASE_CYCLES <= 1) ? 1 : $clog2(MAX_PHASE_CYCLES);
  29. localparam [31:0] DIR_SETUP_LAST_32 = DIR_SETUP_CYCLES - 1;
  30. localparam [31:0] STEP_HALF_LAST_32 = STEP_HALF_CYCLES - 1;
  31. localparam [PHASE_COUNTER_BITS-1:0] DIR_SETUP_LAST =
  32. DIR_SETUP_LAST_32[PHASE_COUNTER_BITS-1:0];
  33. localparam [PHASE_COUNTER_BITS-1:0] STEP_HALF_LAST =
  34. STEP_HALF_LAST_32[PHASE_COUNTER_BITS-1:0];
  35. (* keep = "true", syn_encoding = "sequential", fsm_encoding = "none" *)
  36. reg [1:0] motion_phase;
  37. reg dir_reg;
  38. reg [15:0] remaining_steps;
  39. reg [PHASE_COUNTER_BITS-1:0] phase_counter;
  40. assign step_out = (motion_phase == PHASE_STEP_HIGH);
  41. assign dir_out = dir_reg;
  42. `ifdef FORMAL
  43. assign formal_motion_phase = motion_phase;
  44. assign formal_phase_counter = {{(32-PHASE_COUNTER_BITS){1'b0}}, phase_counter};
  45. assign formal_remaining_steps = remaining_steps;
  46. `endif
  47. generate
  48. if ((DIR_SETUP_CYCLES < 1) || (STEP_HALF_CYCLES < 1)) begin : g_invalid_motion_period
  49. prod2u_p4_axis_motion_period_must_be_positive u_motion_period_error();
  50. end
  51. endgenerate
  52. always @(posedge clk or negedge rst_n) begin
  53. if (!rst_n) begin
  54. motion_phase <= PHASE_IDLE;
  55. dir_reg <= 1'b0;
  56. remaining_steps <= 16'd0;
  57. phase_counter <= {PHASE_COUNTER_BITS{1'b0}};
  58. started_pulse <= 1'b0;
  59. end else begin
  60. started_pulse <= 1'b0;
  61. // A new command always wins. This intentionally lowers STEP on
  62. // the acceptance edge, discards the old distance, and restarts
  63. // the complete DIR setup interval.
  64. if (command_start) begin
  65. dir_reg <= command_dir;
  66. remaining_steps <= command_steps;
  67. motion_phase <= PHASE_DIR_SETUP;
  68. phase_counter <= DIR_SETUP_LAST;
  69. end else begin
  70. case (motion_phase)
  71. PHASE_IDLE: begin
  72. remaining_steps <= 16'd0;
  73. phase_counter <= {PHASE_COUNTER_BITS{1'b0}};
  74. end
  75. PHASE_DIR_SETUP: begin
  76. if (phase_counter != {PHASE_COUNTER_BITS{1'b0}}) begin
  77. phase_counter <= phase_counter - 1'b1;
  78. end else begin
  79. motion_phase <= PHASE_STEP_HIGH;
  80. phase_counter <= STEP_HALF_LAST;
  81. started_pulse <= 1'b1;
  82. end
  83. end
  84. PHASE_STEP_HIGH: begin
  85. if (phase_counter != {PHASE_COUNTER_BITS{1'b0}}) begin
  86. phase_counter <= phase_counter - 1'b1;
  87. end else begin
  88. motion_phase <= PHASE_STEP_LOW;
  89. phase_counter <= STEP_HALF_LAST;
  90. end
  91. end
  92. PHASE_STEP_LOW: begin
  93. if (phase_counter != {PHASE_COUNTER_BITS{1'b0}}) begin
  94. phase_counter <= phase_counter - 1'b1;
  95. end else if (remaining_steps == 16'd1) begin
  96. motion_phase <= PHASE_IDLE;
  97. remaining_steps <= 16'd0;
  98. phase_counter <= {PHASE_COUNTER_BITS{1'b0}};
  99. end else begin
  100. motion_phase <= PHASE_STEP_HIGH;
  101. remaining_steps <= remaining_steps - 16'd1;
  102. phase_counter <= STEP_HALF_LAST;
  103. end
  104. end
  105. default: begin
  106. motion_phase <= PHASE_IDLE;
  107. remaining_steps <= 16'd0;
  108. phase_counter <= {PHASE_COUNTER_BITS{1'b0}};
  109. end
  110. endcase
  111. end
  112. end
  113. end
  114. endmodule