door_percent_planner.v 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. `timescale 1ns/1ps
  2. // Shared iterative half-up percent → steps planner (no DSP).
  3. // result = clamp(round_half_up(travel * percent / 100), 1, travel-1)
  4. module door_percent_planner (
  5. input wire clk,
  6. input wire rst_n,
  7. input wire start,
  8. input wire [1:0] axis_id,
  9. input wire [7:0] percent,
  10. input wire [12:0] travel,
  11. output reg busy,
  12. output reg done,
  13. output reg [12:0] result,
  14. output reg [1:0] done_axis_id,
  15. output reg [7:0] done_percent
  16. );
  17. reg [7:0] acc;
  18. reg [12:0] remaining;
  19. reg [12:0] accum_result;
  20. reg [12:0] travel_latched;
  21. reg [7:0] percent_latched;
  22. reg [1:0] axis_latched;
  23. always @(posedge clk or negedge rst_n) begin
  24. if (!rst_n) begin
  25. busy <= 1'b0;
  26. done <= 1'b0;
  27. result <= 13'd0;
  28. done_axis_id <= 2'd0;
  29. done_percent <= 8'd0;
  30. acc <= 8'd0;
  31. remaining <= 13'd0;
  32. accum_result <= 13'd0;
  33. travel_latched <= 13'd0;
  34. percent_latched <= 8'd0;
  35. axis_latched <= 2'd0;
  36. end else begin
  37. done <= 1'b0;
  38. if (start && !busy) begin
  39. busy <= 1'b1;
  40. accum_result <= 13'd0;
  41. acc <= 8'd50;
  42. remaining <= travel;
  43. travel_latched <= travel;
  44. percent_latched <= percent;
  45. axis_latched <= axis_id;
  46. // Degenerate travel: finish immediately with clamped mid (1 if travel>=2 else 0).
  47. if (travel == 13'd0) begin
  48. busy <= 1'b0;
  49. done <= 1'b1;
  50. result <= 13'd0;
  51. done_axis_id <= axis_id;
  52. done_percent <= percent;
  53. end
  54. end else if (busy) begin
  55. if (remaining != 13'd0) begin
  56. if ((acc + percent_latched) >= 8'd100) begin
  57. acc <= acc + percent_latched - 8'd100;
  58. accum_result <= accum_result + 13'd1;
  59. end else begin
  60. acc <= acc + percent_latched;
  61. end
  62. remaining <= remaining - 13'd1;
  63. end else begin
  64. // Clamp mid targets to 1 .. travel-1
  65. if (travel_latched <= 13'd1) begin
  66. result <= 13'd0;
  67. end else if (accum_result < 13'd1) begin
  68. result <= 13'd1;
  69. end else if (accum_result >= travel_latched) begin
  70. result <= travel_latched - 13'd1;
  71. end else begin
  72. result <= accum_result;
  73. end
  74. done_axis_id <= axis_latched;
  75. done_percent <= percent_latched;
  76. busy <= 1'b0;
  77. done <= 1'b1;
  78. end
  79. end
  80. end
  81. end
  82. endmodule