door_speed_profile_builder.v 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. `timescale 1ns/1ps
  2. // Post-calibration: latch shared phase_threshold = max_travel * THRESHOLD_FACTOR
  3. // via shift-add. Runtime rate quanta computed in shared DDA.
  4. module door_speed_profile_builder #(
  5. parameter integer TRAVEL_WIDTH = 13,
  6. parameter integer THRESHOLD_FACTOR = 800000
  7. ) (
  8. input wire clk,
  9. input wire rst_n,
  10. input wire start,
  11. input wire [TRAVEL_WIDTH-1:0] travel_x,
  12. input wire [TRAVEL_WIDTH-1:0] travel_y,
  13. input wire [TRAVEL_WIDTH-1:0] travel_z,
  14. input wire cal_ok_x,
  15. input wire cal_ok_y,
  16. input wire cal_ok_z,
  17. output reg busy,
  18. output reg done,
  19. output reg [31:0] phase_threshold
  20. );
  21. reg [TRAVEL_WIDTH-1:0] mul_a;
  22. reg [31:0] mul_b;
  23. reg [31:0] mul_acc;
  24. reg [3:0] mul_bit;
  25. function automatic [TRAVEL_WIDTH-1:0] f_max3;
  26. input [TRAVEL_WIDTH-1:0] a, b, c;
  27. reg [TRAVEL_WIDTH-1:0] m;
  28. begin
  29. m = a;
  30. if (b > m) m = b;
  31. if (c > m) m = c;
  32. f_max3 = m;
  33. end
  34. endfunction
  35. always @(posedge clk or negedge rst_n) begin
  36. if (!rst_n) begin
  37. busy <= 1'b0;
  38. done <= 1'b0;
  39. phase_threshold <= 32'd0;
  40. mul_a <= {TRAVEL_WIDTH{1'b0}};
  41. mul_b <= 32'd0;
  42. mul_acc <= 32'd0;
  43. mul_bit <= 4'd0;
  44. end else begin
  45. done <= 1'b0;
  46. if (start && !busy) begin
  47. busy <= 1'b1;
  48. mul_a <= f_max3(
  49. cal_ok_x ? travel_x : {TRAVEL_WIDTH{1'b0}},
  50. cal_ok_y ? travel_y : {TRAVEL_WIDTH{1'b0}},
  51. cal_ok_z ? travel_z : {TRAVEL_WIDTH{1'b0}}
  52. );
  53. mul_b <= THRESHOLD_FACTOR[31:0];
  54. mul_acc <= 32'd0;
  55. mul_bit <= 4'd0;
  56. end else if (busy) begin
  57. if (mul_bit == 4'd13) begin
  58. phase_threshold <= mul_acc;
  59. busy <= 1'b0;
  60. done <= 1'b1;
  61. end else begin
  62. if (mul_a[0])
  63. mul_acc <= mul_acc + mul_b;
  64. mul_a <= {1'b0, mul_a[TRAVEL_WIDTH-1:1]};
  65. mul_b <= {mul_b[30:0], 1'b0};
  66. mul_bit <= mul_bit + 1'b1;
  67. end
  68. end
  69. end
  70. end
  71. endmodule