`timescale 1ns/1ps // Post-calibration: latch shared phase_threshold = max_travel * THRESHOLD_FACTOR // via shift-add. Runtime rate quanta computed in shared DDA. module door_speed_profile_builder #( parameter integer TRAVEL_WIDTH = 13, parameter integer THRESHOLD_FACTOR = 800000 ) ( input wire clk, input wire rst_n, input wire start, input wire [TRAVEL_WIDTH-1:0] travel_x, input wire [TRAVEL_WIDTH-1:0] travel_y, input wire [TRAVEL_WIDTH-1:0] travel_z, input wire cal_ok_x, input wire cal_ok_y, input wire cal_ok_z, output reg busy, output reg done, output reg [31:0] phase_threshold ); reg [TRAVEL_WIDTH-1:0] mul_a; reg [31:0] mul_b; reg [31:0] mul_acc; reg [3:0] mul_bit; function automatic [TRAVEL_WIDTH-1:0] f_max3; input [TRAVEL_WIDTH-1:0] a, b, c; reg [TRAVEL_WIDTH-1:0] m; begin m = a; if (b > m) m = b; if (c > m) m = c; f_max3 = m; end endfunction always @(posedge clk or negedge rst_n) begin if (!rst_n) begin busy <= 1'b0; done <= 1'b0; phase_threshold <= 32'd0; mul_a <= {TRAVEL_WIDTH{1'b0}}; mul_b <= 32'd0; mul_acc <= 32'd0; mul_bit <= 4'd0; end else begin done <= 1'b0; if (start && !busy) begin busy <= 1'b1; mul_a <= f_max3( cal_ok_x ? travel_x : {TRAVEL_WIDTH{1'b0}}, cal_ok_y ? travel_y : {TRAVEL_WIDTH{1'b0}}, cal_ok_z ? travel_z : {TRAVEL_WIDTH{1'b0}} ); mul_b <= THRESHOLD_FACTOR[31:0]; mul_acc <= 32'd0; mul_bit <= 4'd0; end else if (busy) begin if (mul_bit == 4'd13) begin phase_threshold <= mul_acc; busy <= 1'b0; done <= 1'b1; end else begin if (mul_a[0]) mul_acc <= mul_acc + mul_b; mul_a <= {1'b0, mul_a[TRAVEL_WIDTH-1:1]}; mul_b <= {mul_b[30:0], 1'b0}; mul_bit <= mul_bit + 1'b1; end end end end endmodule